aboutsummaryrefslogtreecommitdiff
path: root/filesystem/tfs.c
blob: 2a44fdf579fb57712eb88ea69ff43acfe807692e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "disk.h"

#define MAX_INODES 10
#define INODE_START (TRACKS * SECTORS) / (8 * 512)


struct block_ptr {
	char track;
	short sector; 
};
struct blockll{
	struct block_ptr data;
	struct blockll *next;
};

struct meta_data{ 
	char name[8];
	int size;
	int read;
	int write;
	int create;
 };

struct inode {
	struct meta_data info;
	struct block_ptr data[20];
};

typedef struct filestuff{
	int fd;
	struct inode file;
};


struct inode_list{
	struct inode node;
	struct inode_list *next;
}


int inode_list_size = 0;
struct inode_list *root;

char bitmap[128][4096/8];

int check_bitmap(t,s)
int t,s;
{
	char tmp;
	tmp = bitmap[t][s/8];
	tmp &= (1 << (s % 8));
	return (int) tmp;
}

void set_bitmap(t,s)
int t,s;
{
	bitmap[t][s/8] |= (1 << (s % 8));
	return;
}

void print_bitmap()
{
	int i,j;
	for(i = 0; i < 128; i++){

		printf("\n%4d  ", i);
		for (j = 0; j < 4096/8; j++) {
			printf("%02x", bitmap[i][j]);
			if (j %31 == 0) {
				printf("\n%4d  ",i);
			}
		}
	}
}


/*  TODO
 *  Implement inode table as binary tree to speedup searches
 */
int search_inodes(name)
char *name;
{
	int i;
	for(i = 0; i < MAX_INODES; i++){
		if(strcmp(name, i_table[i].info.name) == 0) 
			return i;
	}
}

struct blockll* get_blocks(size)
int size;
{
	int i, t, s;
	struct blockll *root, *current = malloc(sizeof(struct blockll));
	root = current;

	for (i = 0; size > 0 && i < (4096 * 128); i++) {
		t = i / 4096;
		s = i % 4096;
		printf("checkedmap%d\t%d: %d\n", t,s,check_bitmap(t,s));
		if (!check_bitmap(t, s)) {
			current->next = malloc(sizeof(struct blockll));
			current = current->next;
			current-> next = NULL;
			current->data.track = (char) t;
			current->data.sector = (short) s;

			set_bitmap(t,s);
			size-= 512;
		}
	}

	return i <(4096 * 128) ? root : NULL;
}

int inode_init()
{
	int n = MAX_INODES / 4;
	int i;
	char *ptr;
	struct inode_list tmp;

	if (MAX_INODES % 4 > 0)
		n++;

	char *buf = malloc(512 * n);

	for (i =0; i < n; i++) {
		rsector(0, i, buf + (512 * i));
	}
	ptr = buf;

	tmp = root;
	for(i=0; i< MAX_INODES; i++) {
		tmp->next = malloc(sizeof(struct inode_list));
		memcpy(&tmp->node, ptr, 64);
		ptr += 64;
		tmp = tmp->next
		inode_list_size++;
	}
}

/*save inodes to first n sectors on disk*/
void inode_save()
{
	int i, j;
	char *buf = malloc(512);
	struct inode_list tmp = root;

	for (i = 0; i < MAX_INODES && tmp->next;i++) {
		for (j = 0; j < 4; j++){
			tmp = tmp->next;
			memcpy(buf + j, tmp->node, sizeof(struct inode));
		}
		wsector(0, INODE_START + i, buf);
	}
}

int open(fname, mode)
char *fname, *mode;
{
	
}

int close(fd)
int fd;
{
	
}

int read(fd, buf)
int fd;
char *buf;
{
	
}

int write(fd, buf)
int fd;
char *buf;
{
	
}

int ulink(fname)
char *fname;
{
	
}

int main() 
{
	inode_init();
	inode_save();
}