[ox/fs] Add recursive check on directory remove

This commit is contained in:
Gary Talent 2018-08-18 23:49:29 -05:00
parent 40f8af85a8
commit a1b8de0cb0
2 changed files with 22 additions and 4 deletions

View File

@ -23,6 +23,17 @@ enum FileType {
FileType_Directory = 2
};
constexpr const char *toString(FileType t) {
switch (t) {
case FileType_NormalFile:
return "Normal File";
case FileType_Directory:
return "Directory";
default:
return "";
}
}
struct FileStat {
uint64_t inode = 0;
uint64_t links = 0;

View File

@ -163,10 +163,17 @@ Error FileSystemTemplate<InodeId_t>::remove(const char *path, bool recursive) {
auto rootDir = ox_malloca(sizeof(ox::fs::Directory<InodeId_t>), ox::fs::Directory<InodeId_t>, m_fs, fd.value.rootDirInode);
auto inode = rootDir->find(path);
oxReturnError(inode.error);
if (auto err = rootDir->remove(path)) {
// removal failed, try putting the index back
oxLogError(rootDir->write(path, inode));
return err;
auto st = stat(inode);
oxReturnError(st.error);
if (st.value.fileType == FileType_NormalFile || recursive) {
if (auto err = rootDir->remove(path)) {
// removal failed, try putting the index back
oxLogError(rootDir->write(path, inode));
return err;
}
} else {
oxTrace("FileSystemTemplate::remove::fail") << "Tried to remove directory without recursive setting.";
return OxError(1);
}
return OxError(0);
}