-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
245 lines (204 loc) · 5.87 KB
/
Copy pathutil.c
File metadata and controls
245 lines (204 loc) · 5.87 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
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "common.h"
#include<fcntl.h>
#include "file_handling_functions.h"
#include "common.h"
#include "util.h"
#include "file_handling_functions.h"
DataBlock* getDataBlockFromIndex(int index)
{
char *directoryEntry = "abc 12 xyz 100 def 17";
DataBlock* db = (DataBlock *)malloc(sizeof(DataBlock));
strcpy(db->content, directoryEntry);
//db->content = directoryEntry;
return db;
}
int getNumberOfDataBlockFromSize(int size)
{
return ((int)size/DATA_BLOCK_SIZE + 1);
}
/* char* readDataBlock(WholeFS*fs, FILE *fp, int index)
{
int offset = sizeof(int) + fs->sb.dataBlockOffset + (index * DATA_BLOCK_SIZE);
fseek(fp, offset, SEEK_SET);
char data[128];
fgets(data,DATA_BLOCK_SIZE,fp);
return data;
} */
int readSingleRedirectDataBlock(int index,int n)
{
return 0;
}
char getFileType(int fileType)
{
if(fileType)
return 'd';
return '-';
}
void printDirectoryContent(WholeFS* fs,char *data,int size)
{
int offset = 0;
char name[12], *buffer;
int inodeIndex;
Inode* inode;
while(offset < size)
{
//int currentPosition = offset;
// Working code
/* Start
char c = data[currentPosition++];
while(c !='\0')
{
printf("%c",c);
c = data[currentPosition++];
}
End */
sscanf(data+offset,"%d %s",&inodeIndex,name);
//printf("index: %d, name : %s \n",inodeIndex,name);
if(inodeIndex > 0)
{
buffer = readInodeBlockFromFile(fs,inodeIndex);
//printf("buffer : %s\n",buffer);
inode = strToInode(buffer,sizeof(Inode));
printf("fileMode : %c ,fileSize: %d, linkCount: %d, name: %s \n",getFileType(inode->fileMode),inode->fileSize,inode->linkCount,name);
}
offset += DIRECTORY_ENTRY_LENGTH;
}
}
int getDataBlockIndex(WholeFS* fs, int inodeIndex, int nDataBlock)
{
//printf("In getDataBlockIndex\n");
int nEntryInSingleIndirect = DATA_BLOCK_SIZE/sizeof(int);
int nDirectDataBlock = DIRECT_DATA_BLOCK_NUMBER - 3;
char *buffer = readInodeBlockFromFile(fs,inodeIndex);
Inode *inode = strToInode(buffer,sizeof(Inode));
//printf("Inode index : %d, DB[0] : %d\n",inodeIndex,inode->directDBIndex[0]);
if(nDataBlock < nDirectDataBlock)
return inode->directDBIndex[nDataBlock];
else if((nDataBlock >= nDirectDataBlock) && (nDataBlock < nDirectDataBlock + nEntryInSingleIndirect))
return readSingleRedirectDataBlock(fs->ib[inodeIndex].directDBIndex[nDirectDataBlock],nDataBlock - nDirectDataBlock);
}
int getInodeIndexFromName(WholeFS *fs,char *name,int pdIndex)
{
Inode* inode = strToInode(readInodeBlockFromFile(fs,pdIndex),sizeof(Inode));
//TODO : need to do whole content, only first direct block access,
char *dataBlockContent = readDataBlockFromFile(fs,inode->directDBIndex[0]);
int offset = 0, currentOffset;
int i, index;
char dirName[12];
sscanf(dataBlockContent + offset,"%d %s",&index,dirName);
//printf("Directory name : %s\n",dirName);
while(offset <128)
{
if(strcmp(dirName, name) == 0)
{
return index;
}
offset+= 16;
sscanf(dataBlockContent + offset,"%d %s",&index,dirName);
}
return 0;
}
/* void testReadDataBlock()
{
SUPERBLOCK sb = {}
WholeFS *fs =
FILE* fp = fopen("S3R.fs","r");
} */
/* void testPrintDirectory()
{
char content[128];
strcpy(content+4,"home");
strcpy(content+20,"dev");
int i;
for (i = 8; i < 15; i++)
content[i] ='\0';
for (i = 24; i < 128; i++)
content[i] ='\0';
int size = 32;
printDirectoryContent(content,size);
} */
/* char* makeDirString(char* filename, int len, int inodeNo)
{
int i, j, inodeNumDigit=0;
char* buffer = (char*)malloc((INODE_NO_STRING_SIZE + FILE_NAME_STRING_SIZE)*sizeof(char));
//int len = strlen(filename);
int copyInode = inodeNo;
while (copyInode!=0)
{
inodeNumDigit ++;
copyInode /= 10;
}
assert(inodeNo>=0);
assert(inodeNumDigit<=INODE_NO_STRING_SIZE);
assert(len<=FILE_NAME_STRING_SIZE);
//printf("inode digits: %d\n",inodeNumDigit);
if (inodeNo==0)
{
//printf("This inode is invalid.\n");
return;
}
if (inodeNo==1)
{
printf("This is the root inode.\n");
return;
}
if(inodeNo==1 || inodeNo==0)
{
inodeNumDigit = 1;
buffer[0] = inodeNo+'0';
}
else{
for (i=inodeNumDigit-1; i>=0; i--)
{
buffer[i] = (copyInode%10)+'0';
//printf("%c\n",buffer[i]);
copyInode /= 10;
}
}
// add one space
buffer[inodeNumDigit] = ' ';
// write string
int k=0;
for (i=inodeNumDigit+1; i<INODE_NO_STRING_SIZE + len; i++)
{
buffer[i] = filename[k];
//printf("%c\n",buffer[i]);
k++;
}
// fill the rest with space
for (; i < INODE_NO_STRING_SIZE + FILE_NAME_STRING_SIZE; i++)
{
buffer[i] = ' ';
//printf("%c\n",buffer[i]);
}
printf("%s", buffer);
return buffer;
}
*/
/*
int main(int argc, char const *argv[])
{
char path[] = "/abc/def/ghij/xyz";
char *directoryEntry = "abc 12 xyz 14 def 17";
//printf("String: %s, Length : %ld\n",directoryEntry,strlen(directoryEntry));
char *dir;
int i = 0;
int inodeIndex = 1;
if(path[0] == '/')
{
dir = strtok(path,"/:");
while(dir!=NULL)
{
inodeIndex = getInodeIndexFromName(dir,inodeIndex);
printf("string : %s, index : %d\n",dir,inodeIndex);
dir = strtok(NULL,"/:");
}
return 0;
}
//perror("not a valid path.");
testPrintDirectory();
return 0;
} */