-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_close.c
More file actions
390 lines (339 loc) · 8.14 KB
/
Copy pathopen_close.c
File metadata and controls
390 lines (339 loc) · 8.14 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#ifndef OPENCLOSE
#define OPENCLOSE
#include "type.h"
#include "util.c"
#include "alloc_dalloc.c"
int pfd()
{
OFT *oftp;
printf("fd mode ref offset inode\n");
printf("-- ---- --- ------ -----\n");
for (int i = 0; i < NFD; i++)
{
oftp = running->fd[i];
if (oftp)
{
char* mode_string;
switch(oftp->mode)
{
case 0:
mode_string = "READ";
break;
case 1:
mode_string = "WRIT";
break;
case 2:
mode_string = "RDWR";
break;
case 3:
mode_string = "APND";
break;
default:
mode_string = "OOPS";
}
printf("%2d %s %3d %6d [%d, %d]\n", i, mode_string, oftp->refCount, oftp->offset, oftp->minodePtr->dev, oftp->minodePtr->ino);
}
}
return 1;
}
int truncate(MINODE *mip)
{
// (1) release mip's data blocks
INODE *inode = &mip->INODE;
// release direct blocks
for (int i = 0; i < 12; i++)
{
if (inode->i_block[i])
{
bdalloc(mip->dev, inode->i_block[i]);
inode->i_block[i] = 0;
}
}
// release indirect blocks
if (inode->i_block[12])
{
int buf[256];
bzero(buf, BLKSIZE);
get_block(mip->dev, inode->i_block[12], buf);
for (int i = 0; i < 256; i++)
{
if (buf[i])
{
bdalloc(mip->dev, buf[i]);
buf[i] = 0;
}
}
bdalloc(mip->dev, inode->i_block[12]);
inode->i_block[12] = 0;
}
// release double indirect blocks
if (inode->i_block[13])
{
int buf[256];
bzero(buf, BLKSIZE);
get_block(mip->dev, inode->i_block[13], buf);
for (int i = 0; i < 256; i++)
{
if (buf[i])
{
int buf2[256];
bzero(buf2, BLKSIZE);
get_block(mip->dev, buf[i], buf2);
for (int j = 0; j < 256; j++)
{
if (buf2[j])
{
bdalloc(mip->dev, buf2[j]);
buf2[j] = 0;
}
}
bdalloc(mip->dev, buf[i]);
buf[i] = 0;
}
}
bdalloc(mip->dev, inode->i_block[13]);
inode->i_block[13] = 0;
}
// (2) update time field
mip->INODE.i_mtime = time(0L); // update modify time
// (3) update size and mark as dirty
mip->INODE.i_size = 0;
mip->dirty = 1;
}
int open_file(char* pathname, char* mode_string)
{
// (1) extract mode to open as
int mode = atoi(mode_string);
printf("mode: %s %d\n", mode_string, mode);
if (mode < 0 || mode > 3)
{
printf("open> incorrect mode: %d\n", mode);
return -1;
}
// (2) pathname's inumber, minode pointer
int ino = getino(pathname);
if (ino == 0)
{
printf("open> incorrect pathname: %s\n", pathname);
return -1;
}
MINODE *mip = iget(dev, ino);
if (mip == 0)
{
printf("open> no free minodes left\n");
return -1;
}
// (3) check if regular file and permission OK
// check if regular
if (!S_ISREG(mip->INODE.i_mode))
{
printf("open> not a regular file\n");
iput(mip);
return -1;
}
// check if already opened in 1 W | 2 RW | 3 APP
for (int i = 0; i < NFD; i++)
{
if (running->fd[i] == 0) continue; // not an oft
if (running->fd[i]->minodePtr->ino == ino)
{
// found related file, check if open in non-read mode
if (running->fd[i]->mode > 0)
{
iput(mip);
printf("open> file opened in non-read mode already\n");
return -1;
}
}
}
// (4) allocate a free OpenFileTable
OFT *oftp = oget();
if (oftp == 0)
{
iput(mip);
printf("open> no free OpenFileTables\n");
return -1;
}
oftp->mode = mode;
oftp->refCount = 1;
oftp->minodePtr = mip;
// (5) set oft offset
switch(mode)
{
case 0:
oftp->offset = 0;
break;
case 1:
truncate(mip);
oftp->offset = 0;
break;
case 2:
oftp->offset = 0;
break;
case 3:
oftp->offset = mip->INODE.i_size;
break;
default:
printf("open> invalid mode: %d", mode);
iput(mip);
oftp->refCount = 0;
return -1;
}
// (6) set running PROC's lowest open fd to oftp
int i = 0;
int flag = 1;
for (; i < NFD; i++)
{
if (running->fd[i] == 0)
{
running->fd[i] = oftp;
flag = 0;
break;
}
}
if (flag)
{
printf("open> all fd's of running process in use\n");
iput(mip);
oftp->refCount = 0;
return -1;
}
// (7) update INODE's time field
mip->INODE.i_atime = time(0L);
if (oftp->mode > 0)
{
mip->INODE.i_mtime = time(0L);
}
mip->dirty = 1;
// (8) return the file descriptor
pfd();
return i;
}
int close_file(int fd)
{
// (1) verify fd is within range
if (fd < 0 || fd >= NFD)
{
printf("close> not a valid file descriptor\n");
return -1;
}
// (2) verify fd points to an OFT instance
OFT *oftp = running->fd[fd];
if (!oftp)
{
printf("close> file descriptor does not point to an instance\n");
return -1;
}
// (3) remove fd from list and iput if last refCount
running->fd[fd] = 0;
oftp->refCount--;
if (oftp->refCount > 0)
{
return 0;
}
// last user of this OFT entry
MINODE *mip = oftp->minodePtr;
iput(mip);
pfd();
return 0;
}
int close_file_str(char* fd_string)
{
int fd = atoi(fd_string);
return close_file(fd);
}
int my_lseek(int fd, int position)
{
// check boundaries of fd and position
if (fd < 0 || fd >= NFD)
{
printf("lseek> not a valid file descriptor\n");
return -1;
}
OFT *oftp = running->fd[fd];
if (!oftp)
{
printf("lseek> file descriptor not in use\n");
return -1;
}
if (position < 0 || position >= oftp->minodePtr->INODE.i_size)
{
printf("lseek> not a valid position\n");
return -1;
}
// save original position and update new position
int original = oftp->offset;
oftp->offset = position;
// return the original (saved) position
return original;
}
int my_lseek_str(char* fd_string, char* position_string)
{
// convert parameters to their integer version
int fd = atoi(fd_string);
int position = atoi(position_string);
return my_lseek(fd, position);
}
// does not verify mode (can duplicate R, RW, APP)
int dup(int fd)
{
if (fd < 0 || fd >= NFD)
{
printf("dup> fd not valid\n");
return -1;
}
OFT *oftp = running->fd[fd];
if (!oftp)
{
printf("dup> fd does not point to an instance\n");
return -1;
}
int i = 0;
for (; i < NFD; i++)
{
if (!running->fd[i])
{
running->fd[i] = oftp;
}
}
if (i == NFD)
{
printf("dup> no free file descriptors\n");
return -1;
}
oftp->refCount++;
return i;
}
// does not verify modes (can duplicate R, RW, APP)
int dup2(int fd, int gd)
{
if (fd < 0 || fd >= NFD)
{
printf("dup> fd not valid\n");
return -1;
}
if (gd < 0 || gd >= NFD)
{
printf("dup> gd not valid\n");
return -1;
}
OFT *foftp = running->fd[fd];
if (!foftp)
{
printf("dup> fd does not point to an instance\n");
return -1;
}
if (gd == fd)
{
return gd;
}
OFT *goftp = running->fd[gd];
if (goftp)
{
close_file(gd);
}
running->fd[gd] = running->fd[fd];
foftp->refCount++;
return gd;
}
#endif