|
10 | 10 | #include <vfs.h>
|
11 | 11 | #include <string.h>
|
12 | 12 |
|
| 13 | +/* clean(): helper function to clean up a path |
| 14 | + * params: path - path to be cleaned, the string will be directly modified |
| 15 | + * params: pointer to path |
| 16 | + */ |
| 17 | + |
| 18 | +static char *clean(char *path) { |
| 19 | + if(strlen(path) == 1) return path; // root will never need to be cleaned |
| 20 | + |
| 21 | + // first try to get rid of excessive slashes |
| 22 | + for(int i = 0; i < strlen(path); i++) { |
| 23 | + if((path[i] == '/') && (i < strlen(path)-1) && (path[i+1] == '/')) { |
| 24 | + memmove(&path[i], &path[i+1], strlen(&path[i])+1); |
| 25 | + continue; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // if the last character is a slash, remove it except for the root dir |
| 30 | + if(strlen(path) == 1) return path; |
| 31 | + while(path[strlen(path)-1] == '/') path[strlen(path)-1] = 0; |
| 32 | + |
| 33 | + // parse '../' to reflect the parent directory |
| 34 | + for(int i = 0; i < strlen(path); i++) { |
| 35 | + if((path[i] == '.') && (path[i+1] == '.') && ((path[i+2] == '/') || (!path[i+2]))) { |
| 36 | + // find the parent |
| 37 | + int parent = i-2; |
| 38 | + for(; parent > 0; parent--) { |
| 39 | + if(path[parent] == '/') break; |
| 40 | + } |
| 41 | + |
| 42 | + parent++; |
| 43 | + memmove(&path[parent], &path[i+3], strlen(&path[i+3])+1); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // remove './' because it refers to the self directory |
| 48 | + for(int i = 0; i < strlen(path); i++) { |
| 49 | + if((path[i] == '.') && ((path[i+1] == '/') || (!path[i+1]))) { |
| 50 | + memmove(&path[i], &path[i+2], strlen(&path[i+2])+1); |
| 51 | + continue; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // and finally remove any trailing slashes left while processing the string |
| 56 | + if(strlen(path) == 1) return path; |
| 57 | + while(path[strlen(path)-1] == '/') path[strlen(path)-1] = 0; |
| 58 | + |
| 59 | + return path; |
| 60 | +} |
| 61 | + |
13 | 62 | /* resolve(): resolves a path relative to a mountpoint
|
14 | 63 | * params: buffer - destination to store the resolved path
|
15 | 64 | * params: type - buffer to store file system type
|
16 | 65 | * params: path - source absolute path
|
17 | 66 | * returns: pointer to resolved path on success, NULL on fail
|
18 | 67 | */
|
19 | 68 |
|
20 |
| -char *resolve(char *buffer, char *type, char *source, const char *path) { |
| 69 | +char *resolve(char *buffer, char *type, char *source, char *path) { |
21 | 70 | // iterate over the mountpoints to determine which device this path is
|
22 | 71 | // located on
|
23 | 72 | if(!mpCount) return NULL;
|
24 | 73 |
|
| 74 | + clean(path); |
| 75 | + |
25 | 76 | for(int i = 0; i < mpCount; i++) {
|
26 | 77 | size_t mplen = strlen(mps[i].path);
|
27 | 78 | if(!memcmp(path, mps[i].path, mplen)) {
|
|
0 commit comments