Add . and .. directory entries to directores

This commit is contained in:
2017-07-29 02:28:37 -05:00
parent 30c28373d4
commit 3211cc40d5
2 changed files with 45 additions and 13 deletions
+40 -10
View File
@@ -120,7 +120,9 @@ int Directory<InodeId_t, FsSize_t>::getChildrenInodes(InodeId_t *inodes, size_t
auto current = files(); auto current = files();
if (current) { if (current) {
for (uint64_t i = 0; i < this->children; i++) { for (uint64_t i = 0; i < this->children; i++) {
inodes[i] = current->inode; if (ox_strcmp(current->getName(), ".") and ox_strcmp(current->getName(), "..")) {
inodes[i] = current->inode;
}
current = (DirectoryEntry<InodeId_t>*) (((uint8_t*) current) + current->size()); current = (DirectoryEntry<InodeId_t>*) (((uint8_t*) current) + current->size());
} }
return 0; return 0;
@@ -201,6 +203,13 @@ class FileSystem {
virtual int mkdir(const char *path) = 0; virtual int mkdir(const char *path) = 0;
/**
* Moves an entry from one directory to another.
* @param src the path to the file
* @param dest the path of the destination directory
*/
virtual int move(const char *src, const char *dest) = 0;
template<typename List> template<typename List>
int ls(const char *path, List *list); int ls(const char *path, List *list);
@@ -316,13 +325,7 @@ class FileSystemTemplate: public FileSystem {
uint64_t size() override; uint64_t size() override;
uint8_t *buff() override; uint8_t *buff() override;
int move(const char *src, const char *dest) override;
/**
* Moves an entry from one directory to another.
* @param src the path to the file
* @param dest the path of the destination directory
*/
int move(const char *src, const char *dest);
/** /**
* Removes an entry from a directory. This does not delete the referred to file. * Removes an entry from a directory. This does not delete the referred to file.
@@ -372,7 +375,31 @@ 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 *path) {
Directory<typename FileStore::InodeId_t, typename FileStore::FsSize_t> dir; Directory<typename FileStore::InodeId_t, typename FileStore::FsSize_t> dir;
return write(path, &dir, sizeof(dir), FileType::FileType_Directory); auto err = write(path, &dir, sizeof(dir), FileType::FileType_Directory);
if (err) {
return err;
}
// add . entry for self
auto inode = findInodeOf(path);
err = insertDirectoryEntry(path, ".", inode);
if (err) {
remove(inode);
return err;
}
// add .. entry for parent
size_t pathLen = ox_strlen(path);
char dirPath[pathLen];
PathIterator pathReader(path, pathLen);
err |= pathReader.dirPath(dirPath, pathLen);
err = insertDirectoryEntry(path, "..", findInodeOf(dirPath));
if (err) {
remove(inode);
return err;
}
return err;
} }
template<typename FileStore, FsType FS_TYPE> template<typename FileStore, FsType FS_TYPE>
@@ -506,10 +533,13 @@ int FileSystemTemplate<FileStore, FS_TYPE>::remove(uint64_t inode, bool recursiv
} }
typename FileStore::InodeId_t inodes[dir->children]; typename FileStore::InodeId_t inodes[dir->children];
ox_memset(inodes, 0, sizeof(typename FileStore::InodeId_t) * dir->children);
dir->getChildrenInodes(inodes, dir->children); dir->getChildrenInodes(inodes, dir->children);
for (auto i : inodes) { for (auto i : inodes) {
err |= remove(i, true); if (i) {
err |= remove(i, true);
}
} }
if (!err) { if (!err) {
+5 -3
View File
@@ -314,9 +314,11 @@ map<string, int(*)(string)> tests = {
fs->ls("/usr/share/", &files); fs->ls("/usr/share/", &files);
retval |= !(files[0].name == "a.txt"); retval |= !(files[0].name == ".");
retval |= !(files[1].name == "b.txt"); retval |= !(files[1].name == "..");
retval |= !(files[2].name == "c.txt"); retval |= !(files[2].name == "a.txt");
retval |= !(files[3].name == "b.txt");
retval |= !(files[4].name == "c.txt");
delete fs; delete fs;
delete []buff; delete []buff;