FileSys: make Archive interfaces return error code

and make the mode parameter a reference since it is a BitField union
This commit is contained in:
wwylele 2016-10-14 15:29:09 +08:00
parent 958e81404b
commit 4dd8a831bd
6 changed files with 91 additions and 87 deletions

View file

@ -87,7 +87,7 @@ public:
* @return Opened file, or error code
*/
virtual ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
const Mode mode) const = 0;
const Mode& mode) const = 0;
/**
* Delete a file specified by its path
@ -100,53 +100,53 @@ public:
* Rename a File specified by its path
* @param src_path Source path relative to the archive
* @param dest_path Destination path relative to the archive
* @return Whether rename succeeded
* @return Result of the operation
*/
virtual bool RenameFile(const Path& src_path, const Path& dest_path) const = 0;
virtual ResultCode RenameFile(const Path& src_path, const Path& dest_path) const = 0;
/**
* Delete a directory specified by its path
* @param path Path relative to the archive
* @return Whether the directory could be deleted
* @return Result of the operation
*/
virtual bool DeleteDirectory(const Path& path) const = 0;
virtual ResultCode DeleteDirectory(const Path& path) const = 0;
/**
* Delete a directory specified by its path and anything under it
* @param path Path relative to the archive
* @return Whether the directory could be deleted
* @return Result of the operation
*/
virtual bool DeleteDirectoryRecursively(const Path& path) const = 0;
virtual ResultCode DeleteDirectoryRecursively(const Path& path) const = 0;
/**
* Create a file specified by its path
* @param path Path relative to the Archive
* @param size The size of the new file, filled with zeroes
* @return File creation result code
* @return Result of the operation
*/
virtual ResultCode CreateFile(const Path& path, u64 size) const = 0;
/**
* Create a directory specified by its path
* @param path Path relative to the archive
* @return Whether the directory could be created
* @return Result of the operation
*/
virtual bool CreateDirectory(const Path& path) const = 0;
virtual ResultCode CreateDirectory(const Path& path) const = 0;
/**
* Rename a Directory specified by its path
* @param src_path Source path relative to the archive
* @param dest_path Destination path relative to the archive
* @return Whether rename succeeded
* @return Result of the operation
*/
virtual bool RenameDirectory(const Path& src_path, const Path& dest_path) const = 0;
virtual ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const = 0;
/**
* Open a directory specified by its path
* @param path Path relative to the archive
* @return Opened directory, or nullptr
* @return Opened directory, or error code
*/
virtual std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const = 0;
virtual ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const = 0;
/**
* Get the free space

View file

@ -16,7 +16,7 @@
namespace FileSys {
ResultVal<std::unique_ptr<FileBackend>> DiskArchive::OpenFile(const Path& path,
const Mode mode) const {
const Mode& mode) const {
LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex);
auto file = std::make_unique<DiskFile>(*this, path, mode);
ResultCode result = file->Open();
@ -43,16 +43,28 @@ ResultCode DiskArchive::DeleteFile(const Path& path) const {
ErrorLevel::Status);
}
bool DiskArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
return FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString());
ResultCode DiskArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString()))
return RESULT_SUCCESS;
// TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
// exist or similar. Verify.
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::NothingHappened, ErrorLevel::Status);
}
bool DiskArchive::DeleteDirectory(const Path& path) const {
return FileUtil::DeleteDir(mount_point + path.AsString());
ResultCode DiskArchive::DeleteDirectory(const Path& path) const {
if (FileUtil::DeleteDir(mount_point + path.AsString()))
return RESULT_SUCCESS;
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::Canceled, ErrorLevel::Status);
}
bool DiskArchive::DeleteDirectoryRecursively(const Path& path) const {
return FileUtil::DeleteDirRecursively(mount_point + path.AsString());
ResultCode DiskArchive::DeleteDirectoryRecursively(const Path& path) const {
if (FileUtil::DeleteDirRecursively(mount_point + path.AsString()))
return RESULT_SUCCESS;
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::Canceled, ErrorLevel::Status);
}
ResultCode DiskArchive::CreateFile(const FileSys::Path& path, u64 size) const {
@ -81,20 +93,30 @@ ResultCode DiskArchive::CreateFile(const FileSys::Path& path, u64 size) const {
ErrorLevel::Info);
}
bool DiskArchive::CreateDirectory(const Path& path) const {
return FileUtil::CreateDir(mount_point + path.AsString());
ResultCode DiskArchive::CreateDirectory(const Path& path) const {
if (FileUtil::CreateDir(mount_point + path.AsString()))
return RESULT_SUCCESS;
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::Canceled, ErrorLevel::Status);
}
bool DiskArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
return FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString());
ResultCode DiskArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
if (FileUtil::Rename(mount_point + src_path.AsString(), mount_point + dest_path.AsString()))
return RESULT_SUCCESS;
// TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
// exist or similar. Verify.
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::NothingHappened, ErrorLevel::Status);
}
std::unique_ptr<DirectoryBackend> DiskArchive::OpenDirectory(const Path& path) const {
ResultVal<std::unique_ptr<DirectoryBackend>> DiskArchive::OpenDirectory(const Path& path) const {
LOG_DEBUG(Service_FS, "called path=%s", path.DebugStr().c_str());
auto directory = std::make_unique<DiskDirectory>(*this, path);
if (!directory->Open())
return nullptr;
return std::move(directory);
return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound,
ErrorLevel::Permanent);
return MakeResult<std::unique_ptr<DirectoryBackend>>(std::move(directory));
}
u64 DiskArchive::GetFreeBytes() const {

View file

@ -34,15 +34,15 @@ public:
}
ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
const Mode mode) const override;
const Mode& mode) const override;
ResultCode DeleteFile(const Path& path) const override;
bool RenameFile(const Path& src_path, const Path& dest_path) const override;
bool DeleteDirectory(const Path& path) const override;
bool DeleteDirectoryRecursively(const Path& path) const override;
ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override;
ResultCode DeleteDirectory(const Path& path) const override;
ResultCode DeleteDirectoryRecursively(const Path& path) const override;
ResultCode CreateFile(const Path& path, u64 size) const override;
bool CreateDirectory(const Path& path) const override;
bool RenameDirectory(const Path& src_path, const Path& dest_path) const override;
std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override;
ResultCode CreateDirectory(const Path& path) const override;
ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override;
ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const override;
u64 GetFreeBytes() const override;
protected:

View file

@ -18,7 +18,7 @@ std::string IVFCArchive::GetName() const {
}
ResultVal<std::unique_ptr<FileBackend>> IVFCArchive::OpenFile(const Path& path,
const Mode mode) const {
const Mode& mode) const {
return MakeResult<std::unique_ptr<FileBackend>>(
std::make_unique<IVFCFile>(romfs_file, data_offset, data_size));
}
@ -31,22 +31,25 @@ ResultCode IVFCArchive::DeleteFile(const Path& path) const {
ErrorLevel::Status);
}
bool IVFCArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
ResultCode IVFCArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
LOG_CRITICAL(Service_FS, "Attempted to rename a file within an IVFC archive (%s).",
GetName().c_str());
return false;
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
bool IVFCArchive::DeleteDirectory(const Path& path) const {
ResultCode IVFCArchive::DeleteDirectory(const Path& path) const {
LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an IVFC archive (%s).",
GetName().c_str());
return false;
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
bool IVFCArchive::DeleteDirectoryRecursively(const Path& path) const {
ResultCode IVFCArchive::DeleteDirectoryRecursively(const Path& path) const {
LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an IVFC archive (%s).",
GetName().c_str());
return false;
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
ResultCode IVFCArchive::CreateFile(const Path& path, u64 size) const {
@ -57,20 +60,22 @@ ResultCode IVFCArchive::CreateFile(const Path& path, u64 size) const {
ErrorLevel::Permanent);
}
bool IVFCArchive::CreateDirectory(const Path& path) const {
ResultCode IVFCArchive::CreateDirectory(const Path& path) const {
LOG_CRITICAL(Service_FS, "Attempted to create a directory in an IVFC archive (%s).",
GetName().c_str());
return false;
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
bool IVFCArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
ResultCode IVFCArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
LOG_CRITICAL(Service_FS, "Attempted to rename a file within an IVFC archive (%s).",
GetName().c_str());
return false;
// TODO(wwylele): Use correct error code
return ResultCode(-1);
}
std::unique_ptr<DirectoryBackend> IVFCArchive::OpenDirectory(const Path& path) const {
return std::make_unique<IVFCDirectory>();
ResultVal<std::unique_ptr<DirectoryBackend>> IVFCArchive::OpenDirectory(const Path& path) const {
return MakeResult<std::unique_ptr<DirectoryBackend>>(std::make_unique<IVFCDirectory>());
}
u64 IVFCArchive::GetFreeBytes() const {

View file

@ -33,15 +33,15 @@ public:
std::string GetName() const override;
ResultVal<std::unique_ptr<FileBackend>> OpenFile(const Path& path,
const Mode mode) const override;
const Mode& mode) const override;
ResultCode DeleteFile(const Path& path) const override;
bool RenameFile(const Path& src_path, const Path& dest_path) const override;
bool DeleteDirectory(const Path& path) const override;
bool DeleteDirectoryRecursively(const Path& path) const override;
ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override;
ResultCode DeleteDirectory(const Path& path) const override;
ResultCode DeleteDirectoryRecursively(const Path& path) const override;
ResultCode CreateFile(const Path& path, u64 size) const override;
bool CreateDirectory(const Path& path) const override;
bool RenameDirectory(const Path& src_path, const Path& dest_path) const override;
std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override;
ResultCode CreateDirectory(const Path& path) const override;
ResultCode RenameDirectory(const Path& src_path, const Path& dest_path) const override;
ResultVal<std::unique_ptr<DirectoryBackend>> OpenDirectory(const Path& path) const override;
u64 GetFreeBytes() const override;
protected:

View file

@ -338,17 +338,11 @@ ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle,
return ERR_INVALID_ARCHIVE_HANDLE;
if (src_archive == dest_archive) {
if (src_archive->RenameFile(src_path, dest_path))
return RESULT_SUCCESS;
return src_archive->RenameFile(src_path, dest_path);
} else {
// TODO: Implement renaming across archives
return UnimplementedFunction(ErrorModule::FS);
}
// TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
// exist or similar. Verify.
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::NothingHappened, ErrorLevel::Status);
}
ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
@ -356,10 +350,7 @@ ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSy
if (archive == nullptr)
return ERR_INVALID_ARCHIVE_HANDLE;
if (archive->DeleteDirectory(path))
return RESULT_SUCCESS;
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::Canceled, ErrorLevel::Status);
return archive->DeleteDirectory(path);
}
ResultCode DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
@ -368,10 +359,7 @@ ResultCode DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
if (archive == nullptr)
return ERR_INVALID_ARCHIVE_HANDLE;
if (archive->DeleteDirectoryRecursively(path))
return RESULT_SUCCESS;
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::Canceled, ErrorLevel::Status);
return archive->DeleteDirectoryRecursively(path);
}
ResultCode CreateFileInArchive(ArchiveHandle archive_handle, const FileSys::Path& path,
@ -388,10 +376,7 @@ ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSy
if (archive == nullptr)
return ERR_INVALID_ARCHIVE_HANDLE;
if (archive->CreateDirectory(path))
return RESULT_SUCCESS;
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::Canceled, ErrorLevel::Status);
return archive->CreateDirectory(path);
}
ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
@ -404,17 +389,11 @@ ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
return ERR_INVALID_ARCHIVE_HANDLE;
if (src_archive == dest_archive) {
if (src_archive->RenameDirectory(src_path, dest_path))
return RESULT_SUCCESS;
return src_archive->RenameDirectory(src_path, dest_path);
} else {
// TODO: Implement renaming across archives
return UnimplementedFunction(ErrorModule::FS);
}
// TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
// exist or similar. Verify.
return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
ErrorSummary::NothingHappened, ErrorLevel::Status);
}
ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle archive_handle,
@ -423,13 +402,11 @@ ResultVal<Kernel::SharedPtr<Directory>> OpenDirectoryFromArchive(ArchiveHandle a
if (archive == nullptr)
return ERR_INVALID_ARCHIVE_HANDLE;
std::unique_ptr<FileSys::DirectoryBackend> backend = archive->OpenDirectory(path);
if (backend == nullptr) {
return ResultCode(ErrorDescription::FS_NotFound, ErrorModule::FS, ErrorSummary::NotFound,
ErrorLevel::Permanent);
}
auto backend = archive->OpenDirectory(path);
if (backend.Failed())
return backend.Code();
auto directory = Kernel::SharedPtr<Directory>(new Directory(std::move(backend), path));
auto directory = Kernel::SharedPtr<Directory>(new Directory(backend.MoveFrom(), path));
return MakeResult<Kernel::SharedPtr<Directory>>(std::move(directory));
}