Skip to content

Commit

Permalink
IOS/FS: Implement Delete properly
Browse files Browse the repository at this point in the history
  • Loading branch information
leoetlino committed Jan 25, 2020
1 parent 8517528 commit 53ceb6c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
48 changes: 40 additions & 8 deletions Source/Core/Core/IOS/FS/HostBackend/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,18 +401,50 @@ ResultCode HostFileSystem::CreateDirectory(Uid uid, Gid gid, const std::string&
return CreateFileOrDirectory(uid, gid, path, attr, modes, false);
}

ResultCode HostFileSystem::Delete(Uid, Gid, const std::string& path)
bool HostFileSystem::IsFileOpened(const std::string& path) const
{
if (!IsValidPath(path))
return std::any_of(m_handles.begin(), m_handles.end(), [&path](const Handle& handle) {
return handle.opened && handle.wii_path == path;
});
}

bool HostFileSystem::IsDirectoryInUse(const std::string& path) const
{
return std::any_of(m_handles.begin(), m_handles.end(), [&path](const Handle& handle) {
return handle.opened && StringBeginsWith(handle.wii_path, path);
});
}

ResultCode HostFileSystem::Delete(Uid uid, Gid gid, const std::string& path)
{
if (!IsValidNonRootPath(path))
return ResultCode::Invalid;

const std::string file_name = BuildFilename(path);
if (File::Delete(file_name))
INFO_LOG(IOS_FS, "DeleteFile %s", file_name.c_str());
else if (File::DeleteDirRecursively(file_name))
INFO_LOG(IOS_FS, "DeleteDir %s", file_name.c_str());
const std::string host_path = BuildFilename(path);
const auto split_path = SplitPathAndBasename(path);

FstEntry* parent = GetFstEntryForPath(split_path.parent);
if (!parent)
return ResultCode::NotFound;

if (!parent->CheckPermission(uid, gid, Mode::Write))
return ResultCode::AccessDenied;

if (!File::Exists(host_path))
return ResultCode::NotFound;

if (File::IsFile(host_path) && !IsFileOpened(path))
File::Delete(host_path);
else if (File::IsDirectory(host_path) && !IsDirectoryInUse(path))
File::DeleteDirRecursively(host_path);
else
WARN_LOG(IOS_FS, "DeleteFile %s - failed!!!", file_name.c_str());
return ResultCode::InUse;

const auto it = std::find_if(parent->children.begin(), parent->children.end(),
GetNamePredicate(split_path.file_name));
if (it != parent->children.end())
parent->children.erase(it);
SaveFst();

return ResultCode::Success;
}
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Core/IOS/FS/HostBackend/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class HostFileSystem final : public FileSystem

ResultCode CreateFileOrDirectory(Uid uid, Gid gid, const std::string& path,
FileAttribute attribute, Modes modes, bool is_file);
bool IsFileOpened(const std::string& path) const;
bool IsDirectoryInUse(const std::string& path) const;

std::string GetFstFilePath() const;
void ResetFst();
Expand Down

0 comments on commit 53ceb6c

Please sign in to comment.