-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsePath.c
More file actions
137 lines (113 loc) · 3.8 KB
/
parsePath.c
File metadata and controls
137 lines (113 loc) · 3.8 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
/**************************************************************
* Class:: CSC-415-02 Spring 2024
* Name:: Tushin Kulshreshtha, Hayden Coke, Eric Khuong,
* Thanh Duong
* Student IDs:: 922180763, 921741974, 923406338, 922438176
* GitHub-Name:: Dextron04, crowcode17, ekhuong, DanielDoubleDx
* Group-Name:: Something Simple
* Project:: Basic File System
*
* File:: parsePath.c
*
* Description:: This file implements functions for parsing file paths
* and locating directory entries, aiding in directory navigation and file access.
*
**************************************************************/
#include "fileSystem.h"
// Will find the relavent directory entry from the name passed in.
int findInDirectory(char * name, DirectoryEntry * parent){
if(name == NULL || parent == NULL){
return -1;
}
int entryCount = getEntryCount(parent);
for(int i = 0; i < entryCount; i++) {
if(strcmp(parent[i].dirName, name) == 0) {
return i;
}
}
return -1;
}
// check if DE is a directory
// returns 1 if DE is a directory, 0 if not, -1 if the DE is NULL
int isDirectory(DirectoryEntry * DE){
if(DE == NULL){
return -1;
}
return DE->isDirectory;
}
// returns a 0 if the pathname is valid
int parsePath(char * path, ppReturn * ret){
if(strlen(path) > MAX_PATH_SIZE) {
printf("[ERROR] Path given exceeds max path size. Shorten your path.\n");
return -1;
}
char * pathname = strdup(path);
if(pathname == NULL){
printf("[ERROR] No path given.\n");
return -1;
}
DirectoryEntry * startParent;
DirectoryEntry * currentParent;
DirectoryEntry * tempParent;
int deNum;
// check - did the user pass in a relative path or an absolute path?
if(pathname[0] == '/'){
// the path the user gave was an absolute path, so parsing starts from the root
startParent = RootDir;
if(startParent == NULL){
// printf("(PP) startParent = RootDir; returned NULL");
}
}
else {
// the user provided a relative path, so parsing starts from the current working directory
startParent = CurrentWorkingDir;
}
currentParent = startParent;
char * token;
char * token2;
// only way to break out of this loop is by returning from this function
token = strtok(pathname, "/");
while(true){
// printf("(PP) token: %s\n", token);
if(token == NULL){
// If only a '/' was passed in.
if(pathname[0] == '/'){
ret->parent = startParent;
ret->deNum = 0;
return -2;
}
else {
return -1;
}
}
// find the index of the next directory in the parent directory
deNum = findInDirectory(token, currentParent);
token2 = strtok(NULL, "/");
if (token2 != NULL) {
if (deNum == -1) {
// invalid path - directory doesn't exist in parent
return -1;
}
if (!isDirectory(¤tParent[deNum])) {
// invalid path - token is not a directory
return -1;
}
// at this point we know token2 is a valid directory
// bring directory into memory
tempParent = loadDirectory(¤tParent[deNum]);
// compare pointers
if (currentParent != startParent) {
// free old directory
free(currentParent);
}
currentParent = tempParent;
token = token2;
} else {
// token was last element
ret->parent = currentParent;
ret->deNum = deNum;
ret->lastElement = token;
return 0;
}
}
}