-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyscalls.c
More file actions
95 lines (78 loc) · 2.41 KB
/
syscalls.c
File metadata and controls
95 lines (78 loc) · 2.41 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
#include"fs.h"
#include"asm.h"
#include"kernel.h"
#include<stdio.h>
#include<string.h>
int create_file(const char* filename,unsigned short permissions){
// char* result=strconcat(uarea.pwd,filename);
struct ii_node* exist=namei(filename);
struct ii_node* inode=NULL;
int i=0,file=0;
int fd=-1;
if(exist!=NULL){
puts("File already exists\n");
/*
look into namei if it has any error
*/
return fd;
}
else{/*File does not exist already*/
inode=ialloc(DEVICE_NUM);
//create_dir_entry in parent/curr directory
create_entry(filename,inode->i_num);
//dir entry=inode_num+filename
}
//allocate filetable entry for inode, initialize count
if((i=allocateitable(inode))==-1)
panic("INODE TABLE FULL\n");
/*
if file alread exists free blocks()
*/
if((file=allocatefile(i))==INT32_MIN)
panic("FILE TABLE FULL\n");
if((fd=allocatefd(file))==-1)
panic("user file descriptor array FULL\n ");
inode->i_status.i_lock=false;
inode->i_refcount++;
return fd;
}
static int find_ref(int inode){
int i=0;
while(inode_table[i]->i_num!=inode && i<TOTAL_INODES)
i++;
return i;
}
int openfile(const char* filename,int flags,int perm){
int fd=-1,file=0,i=0;
struct ii_node* inode=NULL;
// if(strcmp(filename,)==0){
// }
// char* result=strconcat(uarea.pwd,filename);
int i_index=0;
if(!(inode=namei(filename)))
return -1;
if(inode->filetype!=1)
return -1;
/*
add permissions part later
assume all have permissions
*/
if(inode->i_refcount>0){
i_index=find_ref(inode->i_num);
}
else{
if((i_index=allocateitable(inode))==-1)
panic("INODE TABLE FULL\n");
//allocate filetable entry for inode, initialize count
}
/*
if file alread exists free blocks()
*/
if((file=allocatefile(i_index))==INT32_MIN)
panic("FILE TABLE FULL\n");
if((fd=allocatefd(file))==-1)
panic("user file descriptor array FULL\n ");
inode->i_status.i_lock=false;
inode->i_refcount++;
return fd;
}