-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkdir_creat.c
More file actions
225 lines (207 loc) · 4.95 KB
/
mkdir_creat.c
File metadata and controls
225 lines (207 loc) · 4.95 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
//-----------------functions in this file-----------------
// mkdir,kmkdir,tokenize_dir,enter_name,creat
//Look over and test all the new functions...
char gpaths[128];
char *names[64];
int numbers;
int tokenize_dir(char* path)
{
int i;
char *s;
strcpy(gpaths, path);
numbers = 0;
s = strtok(gpaths, "/");
while(s){
names[numbers] = s;
numbers++;
s = strtok(0, "/");
}
names[numbers] = 0;
}
//split pathname and directory to be created...
int mkdirs(char* pathname)
{
if(strcmp(pathname,"") == 0)
{
printf("Please provide a name\n");
return;
}
tokenize_dir(pathname);
pathname[strlen(pathname)-strlen(names[numbers-1])-1] = 0;//look over
// a/b/c
int pino = getino(pathname);//will where the directory a/b is not c because of the -2
MINODE* pmip;
if(numbers == 1)//only the one
{
pmip = running->cwd;
}
else if (pino != 0)
{
pmip = iget(dev,pino);//pathname
}
else
{
printf("Not found\n");
return;
}
//Not a directory
if(!S_ISDIR(pmip->INODE.i_mode))
{
printf("Not a dir\n");
return;
}
if(search(pmip,names[numbers-1]))
{
printf("Already created\n");
return;
}
kmkdir(pmip,names[numbers-1]);
pmip->INODE.i_links_count++;
pmip->dirty = 1;
pmip->INODE.i_atime = pmip->INODE.i_ctime = pmip->INODE.i_mtime = time(0L);//reset the
if(pmip != running->cwd)
{
iput(pmip);
}
}
int kmkdir(MINODE* pmip,char* base)
{
int ino = ialloc(dev);
int blk = balloc(dev);
MINODE* mip = iget(dev,ino);//get the new inode
INODE* ip = &mip->INODE;//pointer to the inode in the block
ip->i_mode = 0x41ed;
ip->i_uid = running->uid;
ip->i_gid = running->gid;
ip->i_size = BLKSIZE;
ip->i_links_count = 2;
time_t seconds = time(0L);
ip->i_atime = ip->i_ctime = ip->i_mtime = seconds;
//printf("%d \n\n",ip->i_mtime);
ip->i_blocks = 2;
ip->i_block[0] = blk;
for(int t = 1; t < 15; t++){
ip->i_block[t] = 0;
}
mip->dirty = 1;
iput(mip);//write back to disk
//now for the dir to contain the .. and . values
char buf[BLKSIZE];
bzero(buf,BLKSIZE);
DIR* dp = (DIR *)buf;
//. entry
dp->inode = ino;//the current directory
dp->rec_len = 12;
dp->name_len = 1;
dp->name[0] = '.';
dp = (char *)dp + 12;
dp->inode = pmip->ino;//parent
dp->rec_len = BLKSIZE -12;
dp->name_len = 2;
dp->name[0] = dp->name[1] = '.';
put_block(dev,blk,buf);//write the block to the data
enter_name(pmip, ino,base);//enter the name into the parent directory
}
int enter_name(MINODE* pip, int ino, char* name)
{
char buf[BLKSIZE];
bzero(buf,BLKSIZE);
get_block(pip->dev,pip->INODE.i_block[0],buf);
//got the block of the parent
DIR* dp = (DIR *) buf;
char* cp = buf;
while(cp + dp->rec_len < buf + BLKSIZE)
{
cp += dp->rec_len;
dp = (DIR *) cp;
printf("%d \n",dp->rec_len);
}
//dp points at the last record
int ideal_length = 0;
int need_length = 0;
ideal_length = (8 + dp->name_len + 3)/4;//ideal length of last entry
need_length = (8 + strlen(name) + 3)/4;//what needs to be added
ideal_length *= 4;
need_length *=4;
// printf("I:%d N:%d",ideal_length,need_length);
int remain = dp->rec_len - ideal_length;
//printf("R:%d ",remain);
dp->rec_len = ideal_length;//last one needs to be this value for ideal
//printf("Length now: %d ",dp->rec_len);
if(remain >= need_length)//needed to place the new last
{
dp = (char *) dp + ideal_length;
dp->inode = ino;//child
dp->rec_len = remain;//remains
dp->name_len = strlen(name);//length of string
strcpy(dp->name,name);
}
//printf("HERERE");
put_block(pip->dev,pip->INODE.i_block[0],buf);
}
int creats(char *pathname)
{
if(strcmp(pathname,"") == 0)
{
printf("Please provide a name\n");
return;
}
tokenize_dir(pathname);
pathname[strlen(pathname)-strlen(names[numbers-1])-1] = 0;//look over
// a/b/c
int pino = getino(pathname);//will where the directory a/b is not c because of the -2
MINODE* pmip;
if(numbers == 1)//only the one
{
pmip = running->cwd;
}
else if (pino != 0)
{
pmip = iget(dev,pino);//pathname
}
else
{
printf("Not found\n");
return;
}
//Not a directory
if(!S_ISDIR(pmip->INODE.i_mode))
{
printf("Not a dir\n");
return;
}
if(search(pmip,names[numbers-1]))
{
printf("Already created\n");
return;
}
kcreat(pmip,names[numbers-1]);
pmip->dirty = 1;
pmip->INODE.i_atime = pmip->INODE.i_ctime = pmip->INODE.i_mtime = time(0L);//reset the
if(pmip != running->cwd)
{
iput(pmip);
}
}
int kcreat(MINODE* pmip, char* base)
{
int ino = ialloc(dev);
MINODE* mip = iget(dev,ino);//get the new inode
INODE* ip = &mip->INODE;//pointer to the inode in the block
ip->i_mode = 0644 + 040000 + 040000;
ip->i_uid = running->uid;
for(int blocks = 0; blocks < 15; blocks++)
{
ip->i_block[blocks] = 0;
}
ip->i_block[0] = 0;
ip->i_gid = running->gid;
ip->i_size = 0;
ip->i_links_count = 1;
time_t seconds = time(0L);
ip->i_atime = ip->i_ctime = ip->i_mtime = seconds;
mip->dirty = 1;
iput(mip);//write back to disk
//now for the dir to contain the .. and . values
enter_name(pmip, ino,base);//enter the name into the parent directory
}