-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsPath.c
More file actions
149 lines (142 loc) · 3.69 KB
/
fsPath.c
File metadata and controls
149 lines (142 loc) · 3.69 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
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include "fsPath.h"
#include "fsFreeSpace.h"
#include "mfs.h"
// Recursively delete everything in a directory and free the allocated space
int clearDir (directoryEntry* directory)
{
for (int i = 2; i < directory[0].isDirectory; i++)
{
if (directory[i].isDirectory > 0)
{
directoryEntry* recurseDir = loadDir(&directory[i]);
clearDir(recurseDir);
free(recurseDir);
recurseDir = NULL;
}
returnFreeSpace(directory[i].extents);
}
return 0;
}
// Return the first directoryDentry that has a null filename
int findUnusedEntry (directoryEntry* directory)
{
for (int i = 0; i < directory[0].isDirectory; i++)
{
if (strcmp("\0", directory[i].filename) == 0)
{
return i;
}
}
return -1;
}
// Match the caller's string with filename in the directory
int findInDir (directoryEntry* directory, char* target)
{
for (int i = 0; i < directory[0].isDirectory; i++)
{
if (strcmp(target, directory[i].filename) == 0)
{
return i;
}
}
return -1;
}
// Given a single directory entry, load the rest of the directory
directoryEntry* loadDir (directoryEntry* directory)
{
directoryEntry* newDir = malloc(directory->isDirectory * sizeof(directoryEntry));
int offset = 0;
for (int i = 0; directory->extents[i].location != -1; i++)
{
LBAread(newDir+offset, directory->extents[i].count, directory->extents[i].location);
offset += directory->extents[i].count * BLOCK_SIZE;
}
return newDir;
}
// "Middleware" between caller's string paths and system's structs
int parsePath (const char* pathname, pathInfo* info)
{
// Safety error handling
if (pathname == NULL) return -1;
if (info == NULL) return -1;
char* path = strdup(pathname);
directoryEntry* startParent;
if (path[0] == '/') // Absolute Path
{
startParent = getRoot();
}
else // Relative Path
{
startParent = getCwd();
}
directoryEntry* parent = startParent;
char* savePtr = path;
char* token = strtok_r (path, "/", &savePtr);
if (token == NULL)
{
if (path[0] != '/') // Invalid Path
{
free(path);
path = NULL;
return -1;
}
else // Empty Path
{
info->index = -2;
info->name = NULL;
info->parent = parent;
free(path);
path = NULL;
return 0;
}
}
int index;
char* token2;
// Main Loop
while (1)
{
index = findInDir(parent, token);
token2 = strtok_r (NULL, "/", &savePtr);
if (token2 == NULL) // Successfully found final path element
{
info->index = index;
info->name = token;
info->parent = parent;
return 0;
}
if (index == -1) // Directory doesn't exist
{
free(path);
path = NULL;
return -1;
}
if (parent[index].isDirectory == 0) // Entry is not a directory
{
free(path);
path = NULL;
return -1;
}
// Move onto next path element
directoryEntry* tempParent;
if (parent[index].filesize == -1)
{
tempParent = getRoot();
}
else
{
tempParent = loadDir(&parent[index]);
}
if (parent != startParent)
{
free(parent->extents);
free(parent);
}
parent = tempParent;
token = token2;
}
}