From 52f326f96c2eba2909a366459e5837fdbfa35d40 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 25 Apr 2017 22:01:21 -0500 Subject: [PATCH] Add mkdir support --- src/ox/fs/filesystem.hpp | 34 ++++++++++++++++++++-------------- src/ox/fs/test/tests.cpp | 33 +++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/ox/fs/filesystem.hpp b/src/ox/fs/filesystem.hpp index a6b71b43d..5f61cd2c8 100644 --- a/src/ox/fs/filesystem.hpp +++ b/src/ox/fs/filesystem.hpp @@ -35,6 +35,8 @@ class FileSystem { public: virtual ~FileSystem() {}; + virtual int mkdir(const char *path) = 0; + virtual int read(const char *path, void *buffer, size_t buffSize) = 0; virtual int read(uint64_t inode, void *buffer, size_t size) = 0; @@ -107,7 +109,7 @@ class FileSystemTemplate: public FileSystem { struct __attribute__((packed)) Directory { /** - * Number of files in this directory. + * Number of bytes after this Directory struct. */ typename FileStore::FsSize_t size = 0; @@ -126,7 +128,7 @@ class FileSystemTemplate: public FileSystem { explicit FileSystemTemplate(void *buff); - int mkdir(const char *path); + int mkdir(const char *path) override; int read(const char *path, void *buffer, size_t buffSize) override; @@ -174,7 +176,8 @@ typename FileStore::InodeId_t FileSystemTemplate::INODE_ROOT template int FileSystemTemplate::mkdir(const char *path) { - return 0; + Directory dir; + return write(path, &dir, sizeof(dir), FileType::Directory); } template @@ -316,10 +319,12 @@ int FileSystemTemplate::write(const char *path, void *buffer inode = 0; } } - insertDirectoryEntry(dirPath, fileName, inode); + err = insertDirectoryEntry(dirPath, fileName, inode); } - err = write(inode, buffer, size, fileType); + if (!err) { + err = write(inode, buffer, size, fileType); + } return err; } @@ -348,8 +353,8 @@ uint64_t FileSystemTemplate::findInodeOf(const char *path) { char fileName[pathLen]; uint64_t inode = INODE_ROOT_DIR; while (it.hasNext()) { - auto dirStat = m_store->stat(inode); - if (dirStat.size >= sizeof(Directory)) { + 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) { @@ -405,10 +410,10 @@ uint8_t *FileSystemTemplate::buff() { template uint8_t *FileSystemTemplate::format(void *buffer, typename FileStore::FsSize_t size, bool useDirectories) { buffer = FileStore::format((uint8_t*) buffer, size, (uint16_t) FS_TYPE); - FileSystemTemplate fs(buffer); if (buffer && useDirectories) { Directory dir; + FileSystemTemplate fs(buffer); fs.write(INODE_ROOT_DIR, &dir, sizeof(dir), FileType::Directory); } @@ -425,13 +430,13 @@ template int FileSystemTemplate::insertDirectoryEntry(const char *dirPath, const char *fileName, uint64_t inode) { auto s = stat(dirPath); if (s.inode) { - size_t dirBuffSize = s.size + DirectoryEntry::spaceNeeded(fileName) + 100; + size_t dirBuffSize = s.size + DirectoryEntry::spaceNeeded(fileName); uint8_t dirBuff[dirBuffSize]; int err = read(s.inode, dirBuff, dirBuffSize); if (!err) { auto dir = (Directory*) dirBuff; - dir->size += DirectoryEntry::spaceNeeded(fileName); + dir->size++; auto entry = (DirectoryEntry*) &dirBuff[s.size]; entry->inode = inode; entry->setName(fileName); @@ -455,11 +460,12 @@ uint64_t FileSystemTemplate::Directory::getFileInode(const c uint64_t inode = 0; auto current = files(); if (current) { - auto end = (DirectoryEntry*) (((uint8_t*) files()) + buffSize); - while (current && ox_strcmp(current->getName(), name) != 0) { - current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); - if (current >= end) { + for (uint64_t i = 1; ox_strcmp(current->getName(), name) != 0; i++) { + if (i < this->size) { + current = (DirectoryEntry*) (((uint8_t*) current) + current->size()); + } else { current = nullptr; + break; } } if (current) { diff --git a/src/ox/fs/test/tests.cpp b/src/ox/fs/test/tests.cpp index fe1a8d9ae..44f08a79e 100644 --- a/src/ox/fs/test/tests.cpp +++ b/src/ox/fs/test/tests.cpp @@ -6,6 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include #include #include #include @@ -133,21 +134,41 @@ map tests = { [](string) { // this value will likely need to change if anything about the // random number generator changes - const auto targetInode = 58542; + //const auto targetInode = ox_rand(); int retval = 0; - auto path = "/charset.gbag"; + auto path = "/usr/share/test.txt"; auto data = "test"; - const auto size = 1024; - uint8_t buff[size]; + const auto size = 1024 * 1024 * 10; + auto buff = new uint8_t[size]; FileSystem32::format(buff, (FileStore32::FsSize_t) size, true); auto fs = (FileSystem32*) createFileSystem(buff, size); - retval |= fs->write(path, &data, ox_strlen(data)); - retval |= !(fs->findInodeOf(path) == targetInode); + fs->mkdir("/usr"); + fs->mkdir("/usr/share"); + fs->mkdir("/usr/lib"); + cout << fs->mkdir("/usr/src") << endl; + + retval |= fs->write(path, &data, ox_strlen(data) + 1); + + auto inode = fs->findInodeOf("/"); + cout << "/ inode: " << inode << endl; + + inode = fs->findInodeOf("/usr"); + cout << "/usr inode: " << inode << endl; + + inode = fs->findInodeOf("/usr/share"); + cout << "/usr/share inode: " << inode << endl; + + inode = fs->findInodeOf(path); + cout << path << " inode: " << inode << endl; + + //retval |= !(fs->findInodeOf(path) == targetInode); delete fs; + delete []buff; + return retval; } },