diff --git a/src/ox/fs/filesystem.hpp b/src/ox/fs/filesystem.hpp index 3fc5cee6c..1092195e6 100644 --- a/src/ox/fs/filesystem.hpp +++ b/src/ox/fs/filesystem.hpp @@ -384,8 +384,18 @@ int FileSystemTemplate::stripDirectories() { } template -int FileSystemTemplate::mkdir(const char *path) { - if (!stat(path).inode) { +int FileSystemTemplate::mkdir(const char *pathIn) { + if (!findInodeOf(pathIn)) { + auto pathLen = ox_strlen(pathIn); + char path[pathLen + 1]; + ox_memcpy(path, pathIn, pathLen + 1); + + // make sure last character does not end with / + if (pathLen >= 1 && path[pathLen - 1] == '/') { + path[pathLen - 1] = 0; + pathLen--; + } + Directory dir; auto err = write(path, &dir, sizeof(dir), FileType::FileType_Directory); if (err) { @@ -401,7 +411,6 @@ int FileSystemTemplate::mkdir(const char *path) { } // add .. entry for parent - size_t pathLen = ox_strlen(path); char dirPath[pathLen]; PathIterator pathReader(path, pathLen); err |= pathReader.dirPath(dirPath, pathLen); @@ -630,13 +639,13 @@ uint64_t FileSystemTemplate::findInodeOf(const char *path) { PathIterator it(path, pathLen); char fileName[pathLen]; uint64_t inode = INODE_ROOT_DIR; - while (it.hasNext()) { + while (it.hasNext() && it.next(fileName, pathLen) == 0 && ox_strlen(fileName)) { auto dirStat = stat(inode); if (dirStat.inode && dirStat.size >= sizeof(Directory)) { uint8_t dirBuffer[dirStat.size]; auto dir = (Directory*) dirBuffer; if (read(inode, dirBuffer, dirStat.size) == 0) { - if (dirStat.fileType == FileType::FileType_Directory && it.next(fileName, pathLen) == 0) { + if (dirStat.fileType == FileType::FileType_Directory) { inode = dir->getFileInode(fileName); } else { inode = 0; // null out inode and break diff --git a/src/ox/fs/pathiterator.cpp b/src/ox/fs/pathiterator.cpp index 4c14a0a3e..dae07a7e9 100644 --- a/src/ox/fs/pathiterator.cpp +++ b/src/ox/fs/pathiterator.cpp @@ -52,6 +52,7 @@ int PathIterator::fileName(char *out, size_t outSize) { } } +// Gets the next item in the path int PathIterator::next(char *pathOut, size_t pathOutSize) { size_t size = 0; int retval = 1; @@ -69,8 +70,16 @@ int PathIterator::next(char *pathOut, size_t pathOutSize) { } size_t end = substr - m_path; size = end - start; + // cannot fit the output in the output parameter + if (size >= pathOutSize) { + return -1; + } ox_memcpy(pathOut, &m_path[start], size); } + // truncate trailing / + if (size && pathOut[size - 1] == '/') { + size--; + } pathOut[size] = 0; // end with null terminator m_iterator += size; return retval;