Fix filename error with untruncated / at end of paths

This commit is contained in:
2017-10-11 16:26:26 -05:00
parent cdaf21f415
commit b01cacc2db
2 changed files with 23 additions and 5 deletions
+14 -5
View File
@@ -384,8 +384,18 @@ int FileSystemTemplate<FileStore, FS_TYPE>::stripDirectories() {
} }
template<typename FileStore, FsType FS_TYPE> template<typename FileStore, FsType FS_TYPE>
int FileSystemTemplate<FileStore, FS_TYPE>::mkdir(const char *path) { int FileSystemTemplate<FileStore, FS_TYPE>::mkdir(const char *pathIn) {
if (!stat(path).inode) { 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<typename FileStore::InodeId_t, typename FileStore::FsSize_t> dir; Directory<typename FileStore::InodeId_t, typename FileStore::FsSize_t> dir;
auto err = write(path, &dir, sizeof(dir), FileType::FileType_Directory); auto err = write(path, &dir, sizeof(dir), FileType::FileType_Directory);
if (err) { if (err) {
@@ -401,7 +411,6 @@ int FileSystemTemplate<FileStore, FS_TYPE>::mkdir(const char *path) {
} }
// add .. entry for parent // add .. entry for parent
size_t pathLen = ox_strlen(path);
char dirPath[pathLen]; char dirPath[pathLen];
PathIterator pathReader(path, pathLen); PathIterator pathReader(path, pathLen);
err |= pathReader.dirPath(dirPath, pathLen); err |= pathReader.dirPath(dirPath, pathLen);
@@ -630,13 +639,13 @@ uint64_t FileSystemTemplate<FileStore, FS_TYPE>::findInodeOf(const char *path) {
PathIterator it(path, pathLen); PathIterator it(path, pathLen);
char fileName[pathLen]; char fileName[pathLen];
uint64_t inode = INODE_ROOT_DIR; uint64_t inode = INODE_ROOT_DIR;
while (it.hasNext()) { while (it.hasNext() && it.next(fileName, pathLen) == 0 && ox_strlen(fileName)) {
auto dirStat = stat(inode); auto dirStat = stat(inode);
if (dirStat.inode && dirStat.size >= sizeof(Directory<typename FileStore::InodeId_t, typename FileStore::FsSize_t>)) { if (dirStat.inode && dirStat.size >= sizeof(Directory<typename FileStore::InodeId_t, typename FileStore::FsSize_t>)) {
uint8_t dirBuffer[dirStat.size]; uint8_t dirBuffer[dirStat.size];
auto dir = (Directory<typename FileStore::InodeId_t, typename FileStore::FsSize_t>*) dirBuffer; auto dir = (Directory<typename FileStore::InodeId_t, typename FileStore::FsSize_t>*) dirBuffer;
if (read(inode, dirBuffer, dirStat.size) == 0) { 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); inode = dir->getFileInode(fileName);
} else { } else {
inode = 0; // null out inode and break inode = 0; // null out inode and break
+9
View File
@@ -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) { int PathIterator::next(char *pathOut, size_t pathOutSize) {
size_t size = 0; size_t size = 0;
int retval = 1; int retval = 1;
@@ -69,8 +70,16 @@ int PathIterator::next(char *pathOut, size_t pathOutSize) {
} }
size_t end = substr - m_path; size_t end = substr - m_path;
size = end - start; size = end - start;
// cannot fit the output in the output parameter
if (size >= pathOutSize) {
return -1;
}
ox_memcpy(pathOut, &m_path[start], size); ox_memcpy(pathOut, &m_path[start], size);
} }
// truncate trailing /
if (size && pathOut[size - 1] == '/') {
size--;
}
pathOut[size] = 0; // end with null terminator pathOut[size] = 0; // end with null terminator
m_iterator += size; m_iterator += size;
return retval; return retval;