IOS/FS: Implement CreateFile and CreateDir properly

This commit is contained in:
Léo Lam 2019-12-28 20:44:16 +01:00
parent 8f74d02659
commit 8517528f8c
2 changed files with 43 additions and 19 deletions

View File

@ -348,36 +348,57 @@ ResultCode HostFileSystem::Format(Uid uid)
return ResultCode::Success; return ResultCode::Success;
} }
ResultCode HostFileSystem::CreateFile(Uid, Gid, const std::string& path, FileAttribute, Modes) ResultCode HostFileSystem::CreateFileOrDirectory(Uid uid, Gid gid, const std::string& path,
FileAttribute attr, Modes modes, bool is_file)
{ {
std::string file_name(BuildFilename(path)); if (!IsValidNonRootPath(path) || !std::all_of(path.begin(), path.end(), IsPrintableCharacter))
// check if the file already exist return ResultCode::Invalid;
if (File::Exists(file_name))
if (!is_file && std::count(path.begin(), path.end(), '/') > int(MaxPathDepth))
return ResultCode::TooManyPathComponents;
const auto split_path = SplitPathAndBasename(path);
const std::string host_path = BuildFilename(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::AlreadyExists; return ResultCode::AlreadyExists;
// create the file const bool ok = is_file ? File::CreateEmptyFile(host_path) : File::CreateDir(host_path);
File::CreateFullPath(file_name); // just to be sure if (!ok)
if (!File::CreateEmptyFile(file_name))
{ {
ERROR_LOG(IOS_FS, "couldn't create new file"); ERROR_LOG(IOS_FS, "Failed to create file or directory: %s", host_path.c_str());
return ResultCode::Invalid; return ResultCode::UnknownError;
} }
FstEntry* child = GetFstEntryForPath(path);
*child = {};
child->name = split_path.file_name;
child->data.is_file = is_file;
child->data.modes = modes;
child->data.uid = uid;
child->data.gid = gid;
child->data.attribute = attr;
SaveFst();
return ResultCode::Success; return ResultCode::Success;
} }
ResultCode HostFileSystem::CreateDirectory(Uid, Gid, const std::string& path, FileAttribute, Modes) ResultCode HostFileSystem::CreateFile(Uid uid, Gid gid, const std::string& path, FileAttribute attr,
Modes modes)
{ {
if (!IsValidPath(path)) return CreateFileOrDirectory(uid, gid, path, attr, modes, true);
return ResultCode::Invalid; }
std::string name(BuildFilename(path)); ResultCode HostFileSystem::CreateDirectory(Uid uid, Gid gid, const std::string& path,
FileAttribute attr, Modes modes)
name += "/"; {
File::CreateFullPath(name); return CreateFileOrDirectory(uid, gid, path, attr, modes, false);
DEBUG_ASSERT_MSG(IOS_FS, File::IsDirectory(name), "CREATE_DIR %s failed", name.c_str());
return ResultCode::Success;
} }
ResultCode HostFileSystem::Delete(Uid, Gid, const std::string& path) ResultCode HostFileSystem::Delete(Uid, Gid, const std::string& path)

View File

@ -87,6 +87,9 @@ private:
std::string BuildFilename(const std::string& wii_path) const; std::string BuildFilename(const std::string& wii_path) const;
std::shared_ptr<File::IOFile> OpenHostFile(const std::string& host_path); std::shared_ptr<File::IOFile> OpenHostFile(const std::string& host_path);
ResultCode CreateFileOrDirectory(Uid uid, Gid gid, const std::string& path,
FileAttribute attribute, Modes modes, bool is_file);
std::string GetFstFilePath() const; std::string GetFstFilePath() const;
void ResetFst(); void ResetFst();
void LoadFst(); void LoadFst();