-
Notifications
You must be signed in to change notification settings - Fork 0
/
open.cpp
72 lines (58 loc) · 1.34 KB
/
open.cpp
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
#include <stdio.h>
#include "filesys.h"
short open(int user_id, char *filename,char openmode){
unsigned int dinodeid;
struct inode *inode;
int i,j,k;
dinodeid = namei(filename);
if (dinodeid == -1){
printf("\nFile does not exist!!!\n");
return -1;
}
inode = iget(dir.direct[dinodeid].d_ino);
if (!(inode->di_mode &DIFILE)){
printf("\nType of the existent %s is not a file!!!\n",filename);
iput(inode);
return -1;
}
if (!access(user_id, inode, openmode)){
printf("\nNo access to opening the file!!!\n");
iput(inode);
return -1;
}
for (i=1; i<SYSOPENFILE; i++){
if (sys_ofile[i].f_count == 0)
break;
}
if (i == SYSOPENFILE){
printf("\nSystem is opening too many files!!!\n");
iput(inode);
return -1;
}
sys_ofile[i].f_inode = inode;
sys_ofile[i].f_flag = openmode;
sys_ofile[i].f_count = 1;
if (openmode & FAPPEND){
sys_ofile[i].f_off = inode->di_size;
}else{
sys_ofile[i].f_off = 0;
}
for (j=0; j<NOFILE; j++){
if (user[user_id].u_ofile[j] == SYSOPENFILE + 1)
break;
}
if (j == NOFILE){
printf("\nUser is opening too many files!!!\n");
sys_ofile[i].f_count = 0;
iput(inode);
return -1;
}
user[user_id].u_ofile[j] = i;
if(openmode & FWRITE){
k=inode->di_size%BLOCKSIZ?1:0;
for (i=0; i<inode->di_size/BLOCKSIZ+k; i++)
bfree(inode->di_addr[i]);
inode->di_size = 0;
}
return j;
}