Repository navigation
Expand file tree
/
Copy pathdirLow.c
More file actions
397 lines (307 loc) · 9.07 KB
/
Copy pathdirLow.c
File metadata and controls
397 lines (307 loc) · 9.07 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**************************************************************
* Class:: CSC-415-02 Summer 2025
* Name::Phillip Davis, Igor Tello, Preet Vithani, Jared Aung
* Student IDs::923980431, 923043807, 923575806, 922772159
* GitHub-Name::R3plug
* Group-Name::Team Kentucky Kernels
* Project:: Basic File System
*
* File:: dirLow.c
*
* Description::Implementation low low level directory functions
* including creating a directory and helper functions
*
**************************************************************/
#include <stdint.h>
#include <time.h>
#include "dirLow.h"
#include "fsInit.h"
#include "mfs.h"
#include <stdio.h>
#include "fsFreeSpace.h"
#define MAX_NAME_LENGTH 255
#define BLOCK_SIZE 512
static DE *g_rootDir = NULL;
static DE *g_cwdDir = NULL;
int findInDir(DE *parent, char *token1);
DE *loadDir(DE *dir);
int setRoot();
DE* getRoot();
void setRootDir(DE *root)
{
g_rootDir = root;
g_cwdDir = root;
}
void setCwdDir(DE *cwd)
{
g_cwdDir = cwd;
}
DE *getRootDir(void)
{
return g_rootDir;
}
DE *getCwd(void)
{
return g_cwdDir;
}
DE *createDir(int numEntries, DE *parent)
{
int memNeeded = numEntries * sizeof(DE);
int blocksNeeded = (memNeeded + BLOCK_SIZE - 1) / BLOCK_SIZE;
//memNeeded = blocksNeeded; // Accounts for allocating memory in blocks
printf("blocksNeeded %d\n", blocksNeeded);
int actualEntries = memNeeded / sizeof(DE); // calculate the number of entries that fit in
// allocated memory
DE *newDir = calloc(actualEntries,sizeof(DE)); // initialize directory array
for (int i = 2; i < actualEntries; i++)
{ // Mark unused entries
newDir[i].name[0] = '\0';
}
uint32_t blocksAllocated = 0;
Extent *dirMem = allocateFreeBlocks(blocksNeeded, &blocksAllocated); // get memory for directory
printf("Dir block:%d, Dir count: %d,Dir used:%d\n",dirMem->block,dirMem->count,dirMem->used);
// printf("\nExtent count: %d\n", dirMem->count);
if (dirMem == NULL)
{
printf("No memory allocated for root dir\n");
return NULL;
}
time_t initTime = time(NULL);
strcpy(newDir[0].name, ".");
newDir[0].size = blocksNeeded*BLOCK_SIZE; //
newDir[0].mem.extents[0].block = dirMem->block; // assign directory memory to extent table to '.' entry
newDir[0].mem.extents[0].count = dirMem->count;
newDir[0].mem.extents[0].used = dirMem->used;
newDir[0].mem.extentCount = 1;
newDir[0].isDir = 1; // Sentinel value of 1 is True
newDir[0].creationTime = initTime;
newDir[0].modificationTime = initTime;
newDir[0].lastAccessTime = initTime;
newDir[0].entryCount = actualEntries;
//if root dir set self to parent
if (parent == NULL)
{
parent = newDir;
}
int extentFileCount = newDir[0].mem.extentCount; // size of extent table
strcpy(newDir[1].name, "..");
newDir[1].size = parent[0].size;
for (int i = 0; i < extentFileCount; i++)
{ // copy file extent of parent Dir to ".."
newDir[1].mem.extents[i].block = parent[0].mem.extents[i].block;
newDir[1].mem.extents[i].count = parent[0].mem.extents[i].count;
newDir[1].mem.extents[i].used = parent[0].mem.extents[i].used;
}
newDir[1].mem.extentCount = parent[0].mem.extentCount;
newDir[1].isDir = parent[0].isDir;
newDir[1].creationTime = parent[0].creationTime;
newDir[1].modificationTime = parent[0].creationTime;
newDir[1].lastAccessTime = parent[0].lastAccessTime;
if (writeDir(newDir) == -1)
{ // returns NULL if the directory fails to write
printf("Dir failed to write\n");
return NULL;
}
return newDir;
}
int writeDir(DE *newDir)
{
if(newDir==NULL){
printf("WriteDir was passed a NULL directory\n");
return -1;
}
int numExtents = newDir[0].mem.extentCount; // Number of extents that will need to be written
// printf("Extents count for root dir %d", numExtents);
// printf("Start lcoation root dir %d", newDir[0].mem.extents[0].block);
// if not a new directory write full extent table of data to disk.
int offset = 0;
for (int i = 0; i < numExtents; i++)
{
char* start = (char*)newDir +offset;
// all LBAWrite data stored in the '.' entry of the directory
uint64_t result = LBAwrite(start, newDir[0].mem.extents[i].count, newDir[0].mem.extents[i].block);
if(result != newDir[0].mem.extents[i].count){
printf("LBA write failed in writeDir\n");
return -1;
}
offset+=newDir[0].mem.extents[i].count*BLOCK_SIZE;
}
return 0;
}
/**
* Takes a file path and an info structure
* returns 0 on success, -1 on doesnt exist
*/
int parsePath(const char *pathname, ppInfo *info)
{
if (pathname == NULL)
{ // empty path invalid
printf("Path invalid\n");
return -1;
}
int pathLength = strlen(pathname);
if (pathLength < 1)
{
printf("Path length too short\n");
return -1;
}
DE *parent;
DE *startParent;
char *savePtr;
char *token1;
char *token2;
char *path = malloc(pathLength + 1);
strcpy(path, pathname);
// Sets starting point of path search
if (path[0] == '/')
{
startParent = getRootDir();
}
else
{
startParent = getCwd();
}
parent = startParent;
token1 = strtok_r(path, "/", &savePtr);
if (token1 == NULL)
{
if (path[0] == '/')
{ // passed the root directory
info->parent = parent;
info->index = -2;
info->lastElementName[0] = '\0';
free(path);
return 0;
}
else
{
free(path);
return -1;
}
}
while (1)
{
int idx = findInDir(parent, token1);
printf("Idx: %d\n", idx);
token2 = strtok_r(NULL, "/", &savePtr);
if (token2 == NULL)
{
info->parent = parent;
info->index = idx;
strcpy(info->lastElementName, token1);
free(path);
return 0;
}
if (idx == -1)
{
free(path);
return -1;
}
if (parent[idx].isDir != 1)
{
free(path);
return -1;
}
DE *tempParent = loadDir(&(parent[idx]));
if (parent != startParent)
{
free(parent);
}
parent = tempParent;
token1 = token2;
}
}
DE *loadDir(DE *dir)
{
printf("Start of load dir ext count %d\n",dir->mem.extentCount);
if (dir == NULL)
{
printf("A NULL directory was passed to function\n");
return NULL; // invalid input
}
if (dir->isDir != 1)
{
printf("passed dir not a directory\n");
return NULL; // is not a directory
}
printf("dir->size: %d\n", dir->size);
int memNeeded = 0;
for(int i = 0; i<dir->mem.extentCount;i++){
memNeeded+=dir->mem.extents[i].count*BLOCK_SIZE;
}
DE *tempDir = malloc(memNeeded);
if(tempDir == NULL){
printf("Malloc failed in loadDir\n");
return NULL;
}
int extInDir = dir->mem.extentCount;
printf("Extents in dir: %d",extInDir);
int byteOffset = 0;
for (int i = 0; i < extInDir; i++)
{
int loc = dir->mem.extents[i].block;
int blocks = dir->mem.extents[i].count;
int bytesToRead = blocks * BLOCK_SIZE;
printf("Bytes to Read: %d\n",bytesToRead);
LBAread(tempDir+byteOffset, blocks, loc);
byteOffset += bytesToRead;
}
printf("ByteOffset %d\n",byteOffset);
printf("DirSize: %d\n",dir->size);
// if(byteOffset!=dir->size){
// printf("error reading file\n");
// return NULL;
// }
printf("End of load dir extents count: %d",tempDir->mem.extentCount);
return tempDir;
}
/**
* Return the index of the token if the token
* is in the Directory
* Returns -1 if the token is not in the directory
*/
int findInDir(DE *parent, char *token1)
{
int numEntries = parent[0].entryCount;
printf("Token1 %s\n", token1);
for (int i = 0; i < numEntries; i++)
{
if(parent[i].name[0] == '\0') continue;
if (strcmp(parent[i].name, token1) == 0)
{
return i;
}
}
return -1;
}
DE *createFile(const char *name, DE *parent)
{
if (!parent || !name || strlen(name) == 0)
{
return NULL;
}
printf("inside create a file, before findFreeDE\n");
int index = findFreeDE(parent);
if (index < 0)
{
printf("No space in parent\n");
return NULL;
}
DE *entry = &(parent[index]);
memset(entry, 0, sizeof(DE));
strncpy(entry->name, name, strlen(name)+1);
//entry->name[MAX_NAME_LENGTH - 1] = '\0';
entry->isDir = 0;
entry->size = 0;
time_t now = time(NULL);
entry->creationTime = now;
entry->modificationTime = now;
entry->lastAccessTime = now;
entry->mem.extentCount = 0;
if (writeDir(parent) != 0)
{
printf("Failed to save parent dir\n");
return NULL;
}
return entry;
}