| 65 | } |
| 66 | |
| 67 | FsNode* ResolvePath(const char* path, const char* workingDir, bool followSymlinks){ |
| 68 | assert(path); |
| 69 | |
| 70 | char* tempPath; |
| 71 | if(workingDir && path[0] != '/'){ // If the path starts with '/' then treat as an absolute path |
| 72 | tempPath = (char*)kmalloc(strlen(path) + strlen(workingDir) + 2); |
| 73 | strcpy(tempPath, workingDir); |
| 74 | strcpy(tempPath + strlen(tempPath), "/"); |
| 75 | strcpy(tempPath + strlen(tempPath), path); |
| 76 | } else { |
| 77 | tempPath = (char*)kmalloc(strlen(path) + 1); |
| 78 | strcpy(tempPath, path); |
| 79 | } |
| 80 | |
| 81 | FsNode* root = fs::GetRoot(); |
| 82 | FsNode* currentNode = root; |
| 83 | |
| 84 | char* file = strtok(tempPath,"/"); |
| 85 | |
| 86 | while(file != NULL){ // Iterate through the directories to find the file |
| 87 | FsNode* node = fs::FindDir(currentNode,file); |
| 88 | if(!node) { |
| 89 | Log::Warning("%s not found!", file); |
| 90 | kfree(tempPath); |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | size_t amountOfSymlinks = 0; |
| 95 | while(((node->flags & FS_NODE_TYPE) == FS_NODE_SYMLINK)){ // Check for symlinks |
| 96 | if(amountOfSymlinks++ > MAXIMUM_SYMLINK_AMOUNT){ |
| 97 | Log::Warning("ResolvePath: Reached maximum number of symlinks"); |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | node = FollowLink(node, currentNode); |
| 102 | |
| 103 | if(!node){ |
| 104 | Log::Warning("ResolvePath: Unresolved symlink!"); |
| 105 | kfree(tempPath); |
| 106 | return node; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if((node->flags & FS_NODE_TYPE) == FS_NODE_DIRECTORY){ |
| 111 | currentNode = node; |
| 112 | file = strtok(NULL, "/"); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if((file = strtok(NULL, "/"))){ |
| 117 | Log::Warning("%s is not a directory!", file); |
| 118 | kfree(tempPath); |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
| 122 | amountOfSymlinks = 0; |
| 123 | while(followSymlinks && ((currentNode->flags & FS_NODE_TYPE) == FS_NODE_SYMLINK)){ // Check for symlinks |
| 124 | if(amountOfSymlinks++ > MAXIMUM_SYMLINK_AMOUNT){ |
no test coverage detected