-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfile.c
More file actions
232 lines (204 loc) · 5.62 KB
/
file.c
File metadata and controls
232 lines (204 loc) · 5.62 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "simdisk.h"
/* file API implementation */
/* open an exisiting file for reading or writing */
int my_open (const char * path){
int inum;
int ret = -1;
struct inode *this; // remember to free
int i, j, k;
this = find(path);
if (this == NULL)
return -1;
inum = this->num;
for (i = 0; i < MAX_OPEN_FILES; i++) {
if (fd[i] == -1 || fd[i] == 0) {
fd[i] = inum;
ret = i;
break;
}
}
if (ret == -1) printf("No empty fd !\n");
free(this);
return ret;
}
/* open a new file for writing only */
int my_creat(const char * path) {
int i = 0, j, k;
int div;
int newinode;
int empty_dentry;
char dir_name[MAX_FILE_NAME_LENGTH * MAX_LEVEL];
char file_name[MAX_FILE_NAME_LENGTH];
struct inode *cur;
struct inode tmp_inode;
struct dir tmp_direc;
while(path[i] != '\0') {
if(path[i] == '/')
div = i;
i++;
}
strncpy(dir_name, path, div + 1); // including the last '/'
dir_name[div] = '\0';
strncpy(file_name, path + div + 1, i - div - 1);
file_name[i - div - 1] = '\0';
/*printf("Create file %s in the dir: %s\n", file_name, dir_name);*/
cur = find(dir_name);
if(cur == NULL) {
printf("No such directory: %s\n", dir_name);
return -1;
}
if(cur->type == FILE_TYPE) {
printf("%s is a file\n", dir_name);
return -1;
}
/* Judged if exists a file with same name, otherwise find a empty position in the folder */
for(i = 0; i < 10; i++) {
if(cur->blocks[i] == -1) continue;
read_block(cur->blocks[i], &tmp_direc); // load dir block
for(j = 0; j < 32; j ++){
if(tmp_direc.dentry[j].inode != 0 && tmp_direc.dentry[j].type == FILE_TYPE && strcmp(tmp_direc.dentry[j].name, file_name) == 0) {
printf("File already exists\n");
return -1;
}
}
}
for(i = 0; i < 10; i ++) {
if(cur->blocks[i] != -1) {
read_block(cur->blocks[i], &tmp_direc);
for(j = 0; j < 32; j ++){
if(tmp_direc.dentry[j].inode == 0) { // found empty dir entry, mark position
empty_dentry = j;
break;
}
}
if(j < 32) break;
}
else { // current dir block is full, get a new dir block
cur->blocks[i] = get_block();
read_block(cur->blocks[i], &tmp_direc);
empty_dentry = 0;
break;
}
}
/* acquire inode for new file */
newinode = get_inode();
if(newinode == -1)
return -1;
tmp_direc.dentry[empty_dentry].inode = newinode;
tmp_direc.dentry[empty_dentry].type = FILE_TYPE;
if(strlen(file_name) < 32) tmp_direc.dentry[empty_dentry].length = 32;
else tmp_direc.dentry[empty_dentry].length = 32;
strncpy(tmp_direc.dentry[empty_dentry].name, file_name, strlen(file_name));
write_block(cur->blocks[i], &tmp_direc); // update dir block(added a new dirEntry)
// set up new file's inode
read_inode(newinode, &tmp_inode);
tmp_inode.type = FILE_TYPE;
tmp_inode.num = newinode;
strcpy(tmp_inode.mode, "rw-rw-r--"); // default mode is 664
tmp_inode.size = 0; // in bytes
// tmp_inode.uid = 0;
// tmp_inode.gid = 0;
strncpy(tmp_inode.name, file_name, strlen(file_name));
tmp_inode.blocks[0] = get_block();
for(i = 1; i < 10; i ++)
tmp_inode.blocks[i] = -1;
for(i = 0; i < 30; i ++)
tmp_inode.ind_blocks[i] = -1;
write_inode(newinode, &tmp_inode);
/* open the file */
int ret = -1;
for (i = 0; i < MAX_OPEN_FILES; i++) {
if (fd[i] == -1 || fd[i] == 0) {
fd[i] = newinode;
ret = i;
break;
}
}
if (ret == -1) printf("No empty fd !\n");
free(cur);
return ret;
}
/* sequentially read from a file */
int my_read(int fdd, void * buf, int cnt) {
int i, j, k;
int count = cnt; // Bytes remain to be read
int blknum = (count - 1) / 1024 + 1; // Allocated block num for data
int indnum;
// int fpoff = fp % 1024;
struct inode this;
struct inode *node = &this;
struct inode *mi = (struct inode *)malloc(sizeof(struct inode));
int len = 0; // file pointer offset
int temp = 0; // every copy bytes
int flag = 0;
// int tempp = fp;
unsigned char tmp[1024];
read_inode(fd[fdd], &this); // load file's inode
/* only need direct blocks */
if (blknum <= 10) {
if (count > 1024) temp = 1024; // remain Bytes greater than 1024(block space)?
else temp = count;
for (i = 0; i < blknum; i ++) {
read_block(this.blocks[i], tmp);
memcpy(buf + len, tmp, temp); // copy 'temp' Bytes
length += temp; // file offset 'temp' Bytes
}
break;
}
/* need indirect blocks, firstly read from direct blocks */
for (i = 0; i < 10; i++) {
temp = 1024;
if (flag) {
read_block(this.blocks[i], tmp);
memcpy(buf + len - tempp, tmp, temp);
fp += temp;
} else {
if (len + temp >= tempp) {
temp = temp - fpoff;
if (temp > 0) {
read_block(this.blocks[i], tmp);
memcpy(buf, tmp + fpoff, temp);
fp += temp;
len += fpoff;
}
flag = 1;
}
}
len += temp;
}
blknum = blknum - 10;
indnum = blknum / 256 + 1; /* number of indirect blocks needed */
/* read from indirect blocks */
for (i = 0; i < indnum; i++) {
struct ind_block ib;
read_block(this.ind_blocks[i], (char *) &ib);
for (j = 0; j < 256; j++) {
temp = 1024;
if ((temp + len) > count)
temp = count - len;
if (flag) {
read_block(ib.blocks[j], tmp);
memcpy(buf + len - tempp, tmp, temp);
fp += temp;
} else {
if (len + temp >= tempp) {
temp = temp - fpoff;
if (temp > 0) {
read_block(ib.blocks[j], tmp);
memcpy(buf, tmp + fpoff, temp);
fp += temp;
len += fpoff;
}
flag = 1;
}
}
len += temp;
if (blknum == (j + i * 256))
break;
}
}
return len - tempp;
}