-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_close.c
More file actions
296 lines (263 loc) · 5.67 KB
/
open_close.c
File metadata and controls
296 lines (263 loc) · 5.67 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
//Files contained here:
//open_file close_file truncate lseek pfd
int my_open(char* filename,int mode)
{
MINODE* mip;//Pointer
int ino = getino(filename);//Get the inode number
if(!ino)//if not created yet
{
creats(filename);//creat the file if it doesn't exist yet
ino = getino(filename);//get the inode now
}
mip = iget(dev,ino);//actually get the inode
if(!(mip->INODE.i_mode == 0644 + 040000 + 040000))//check type and permissions
{
printf("Not a regular file\n");//not regular
iput(mip);//put the file back
return;//return
}
int check = 0;
for(check = 0; check < 64 && oft[check].refCount != 0; check++)//Make sure not already open and if so make sure for the correct mode
{
if(oft[check].minodePtr == mip)//Same inode
{
if(oft[check].mode != mode)//not the same mode
{
printf("This file is already open for a different mode\n");
iput(mip);//put back
return;//return
}
else
{
break;//found the file
}
}
}
OFT* of;
int index = 0;
int find = 0;
int where = 0;
if(oft[check].minodePtr == mip && oft[check].mode == mode && oft[check].refCount != 0)//Everything checks out
{
of = &oft[check];//have it point to that memory address
of->refCount+=1;//increase refCount
where = check;//this is where the proc will point to
}
else
{
for(find = 0; find < 64; find++)//Need to find a free space
{
if(oft[find].refCount == 0)//Nothing here
{
of = &oft[find];//find address
of->mode = mode;//set mode
of->minodePtr = mip;//set MINODE
of->refCount = 1;//refCount =1
//What type of mode
switch(mode)
{
case 0: of->offset = 0;
break;
case 1: truncate(mip);
of->offset = 0;
break;
case 2: of->offset = 0;
break;
case 3: of->offset = mip->INODE.i_size;
break;
default: printf("Invalid input\n");
return;
}
where = find;//this is where the proc will point to
break;
}
}
}
for(index = 0; index < 10; index++)//find a place for the fd
{
if(running->fd[index] == NULL)
{
running->fd[index] = &oft[where];//have it point to that place
break;
}
}
time_t seconds = time(0L);//get time
if(mode == 0)//set the time either accessed or modified and accessed
{
mip->INODE.i_atime = time(0L);//check this...
}
else
{
mip->INODE.i_atime = mip->INODE.i_mtime = seconds;//upating time
mip->dirty = 1;//only dirty if write or append
}
return index;
}
//Look Over again...
int truncate(MINODE* mip)
{
///need to work on this part
int blk;
char values[BLKSIZE];
int indirect[256];
for(int index = 0; blk != 0 && index < 14; index++)
{
if(index < 12)
{
if(mip->INODE.i_block[index] != 0)
{
blk = mip->INODE.i_block[index];
bdalloc(dev,blk);
mip->INODE.i_block[index] = 0;
}
else
{
blk = 0;
}
}
else if (index == 12)
{
if(mip->INODE.i_block[12] != 0)
{
get_block(dev,mip->INODE.i_block[12],indirect);
for(int values = 0; values < 256 && blk != 0; values++)
{
blk = indirect[values];
if(blk != 0)
{
bdalloc(dev,blk);
}
}
bdalloc(dev,mip->INODE.i_block[12]);
mip->INODE.i_block[12] = 0;
}
else
{
blk = 0;
}
}
else
{
if(mip->INODE.i_block[13] != 0)
{
int doublein[256];
get_block(dev,mip->INODE.i_block[13],indirect);
for(int values = 0; values < 256 && blk != 0; values++)
{
if(indirect[values] != 0)
{
get_block(dev,indirect[values],doublein);
for(int doub = 0; doub < 256 && blk != 0; doub++)
{
blk = doublein[doub];
if(blk != 0)
{
bdalloc(dev,blk);
}
else
{
blk = 0;
}
}
bdalloc(dev,indirect[values]);
}
else
{
blk = 0;
}
}
bdalloc(dev,mip->INODE.i_block[13]);
mip->INODE.i_block[13] = 0;
}
else
{
blk = 0;
}
}
}
mip->INODE.i_size = 0;//Updating the size of the minode size all blocks were deallocated
}
int my_close(int fd)
{
printf("%d \n",fd);
if(fd < -1 || fd > 10)//Only 10 places
{
printf("This is out of range\n");
return;
}
OFT* place = running->fd[fd];//get the place
if(place == 0)//nothing there
{
printf("Nothing to close\n");
return;
}
running->fd[fd] = 0;//now empty
place->refCount--;//decrease refCount
if(place->refCount > 0)//If not zero still in use
{
return;
}
MINODE* mip = place->minodePtr;//no one else using
iput(mip);//put back
return 0;//return
}
//Just print the files descriptors in the process that are currently open
int pfd()
{
OFT *place;
MINODE* mp;
char type[20];
printf("fd \tmode\t offset\t INODE\n");
printf("---------------------------\n");
for(int index = 0; index < 10; index++)
{
if(running->fd[index] == 0)
{
continue;
}
place = running->fd[index];
if(place->mode == 0)
{
strcpy(type,"READ");
}
else if(place->mode == 1)
{
strcpy(type,"WRITE");
}
else if (place->mode == 2)
{
strcpy(type,"READ/WRITE");
}
else
{
strcpy(type,"APPEND");
}
printf("%4d %5s %4d [%d,%d]\n",index,type,place->offset,place->minodePtr->dev,place->minodePtr->ino);
}
printf("---------------------------\n");
}
//Look Over...
//change position of the file being read or written to
int lseeks(int fd, int position)
{
if(fd < -1 || fd > 10)
{
printf("This is out of range\n");
return;
}
OFT* find = running->fd[fd];
if(find == 0)
{
printf("Nothing open at this location\n");
return;
}
MINODE* mip = find->minodePtr;
int file_size = mip->INODE.i_size;
if(position > file_size || position < -1)
{
printf("Not enough items to read with that length\n");
return;
}
find->offset = position;
return;
}