Merge pull request #303 from linkmauve/fs-cleanup

FileSys cleanup
This commit is contained in:
Tony Wasserka 2014-12-29 12:14:59 +01:00
commit 13699f05e7
9 changed files with 99 additions and 171 deletions

View File

@ -40,40 +40,37 @@ union Mode {
class Path { class Path {
public: public:
Path(): Path() : type(Invalid) {
type(Invalid)
{
} }
Path(const char* path): Path(const char* path) : type(Char), string(path) {
type(Char), string(path)
{
} }
Path(LowPathType type, u32 size, u32 pointer): Path(LowPathType type, u32 size, u32 pointer) : type(type) {
type(type)
{
switch (type) { switch (type) {
case Binary: case Binary:
{ {
u8* data = Memory::GetPointer(pointer); u8* data = Memory::GetPointer(pointer);
binary = std::vector<u8>(data, data + size); binary = std::vector<u8>(data, data + size);
break; break;
} }
case Char:
{ case Char:
const char* data = reinterpret_cast<const char*>(Memory::GetPointer(pointer)); {
string = std::string(data, size - 1); // Data is always null-terminated. const char* data = reinterpret_cast<const char*>(Memory::GetPointer(pointer));
break; string = std::string(data, size - 1); // Data is always null-terminated.
} break;
case Wchar: }
{
const char16_t* data = reinterpret_cast<const char16_t*>(Memory::GetPointer(pointer)); case Wchar:
u16str = std::u16string(data, size/2 - 1); // Data is always null-terminated. {
break; const char16_t* data = reinterpret_cast<const char16_t*>(Memory::GetPointer(pointer));
} u16str = std::u16string(data, size/2 - 1); // Data is always null-terminated.
default: break;
break; }
default:
break;
} }
} }
@ -104,66 +101,64 @@ public:
return "[Char: " + AsString() + ']'; return "[Char: " + AsString() + ']';
case Wchar: case Wchar:
return "[Wchar: " + AsString() + ']'; return "[Wchar: " + AsString() + ']';
default: }
}
const std::string AsString() const {
switch (GetType()) {
case Char:
return string;
case Wchar:
return Common::UTF16ToUTF8(u16str);
case Empty:
return {};
case Invalid:
case Binary:
// TODO(yuriks): Add assert // TODO(yuriks): Add assert
LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!"); LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
return {}; return {};
} }
} }
const std::string AsString() const {
switch (GetType()) {
case Char:
return string;
case Wchar:
return Common::UTF16ToUTF8(u16str);
case Empty:
return {};
default:
// TODO(yuriks): Add assert
LOG_ERROR(Service_FS, "LowPathType cannot be converted to string!");
return {};
}
}
const std::u16string AsU16Str() const { const std::u16string AsU16Str() const {
switch (GetType()) { switch (GetType()) {
case Char: case Char:
return Common::UTF8ToUTF16(string); return Common::UTF8ToUTF16(string);
case Wchar: case Wchar:
return u16str; return u16str;
case Empty: case Empty:
return {}; return {};
default: case Invalid:
// TODO(yuriks): Add assert case Binary:
LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!"); // TODO(yuriks): Add assert
return {}; LOG_ERROR(Service_FS, "LowPathType cannot be converted to u16string!");
return {};
} }
} }
const std::vector<u8> AsBinary() const { const std::vector<u8> AsBinary() const {
switch (GetType()) { switch (GetType()) {
case Binary: case Binary:
return binary; return binary;
case Char: case Char:
return std::vector<u8>(string.begin(), string.end()); return std::vector<u8>(string.begin(), string.end());
case Wchar: case Wchar:
{ {
// use two u8 for each character of u16str // use two u8 for each character of u16str
std::vector<u8> to_return(u16str.size() * 2); std::vector<u8> to_return(u16str.size() * 2);
for (size_t i = 0; i < u16str.size(); ++i) { for (size_t i = 0; i < u16str.size(); ++i) {
u16 tmp_char = u16str.at(i); u16 tmp_char = u16str.at(i);
to_return[i*2] = (tmp_char & 0xFF00) >> 8; to_return[i*2] = (tmp_char & 0xFF00) >> 8;
to_return[i*2 + 1] = (tmp_char & 0x00FF); to_return[i*2 + 1] = (tmp_char & 0x00FF);
}
return to_return;
} }
case Empty: return to_return;
return {}; }
default: case Empty:
// TODO(yuriks): Add assert return {};
LOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!"); case Invalid:
return {}; // TODO(yuriks): Add assert
LOG_ERROR(Service_FS, "LowPathType cannot be converted to binary!");
return {};
} }
} }
@ -176,7 +171,8 @@ private:
class ArchiveBackend : NonCopyable { class ArchiveBackend : NonCopyable {
public: public:
virtual ~ArchiveBackend() { } virtual ~ArchiveBackend() {
}
/** /**
* Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.) * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.)
@ -196,7 +192,7 @@ public:
* @param path Path relative to the archive * @param path Path relative to the archive
* @return Whether the file could be deleted * @return Whether the file could be deleted
*/ */
virtual bool DeleteFile(const FileSys::Path& path) const = 0; virtual bool DeleteFile(const Path& path) const = 0;
/** /**
* Rename a File specified by its path * Rename a File specified by its path
@ -204,14 +200,14 @@ public:
* @param dest_path Destination path relative to the archive * @param dest_path Destination path relative to the archive
* @return Whether rename succeeded * @return Whether rename succeeded
*/ */
virtual bool RenameFile(const FileSys::Path& src_path, const FileSys::Path& dest_path) const = 0; virtual bool RenameFile(const Path& src_path, const Path& dest_path) const = 0;
/** /**
* Delete a directory specified by its path * Delete a directory specified by its path
* @param path Path relative to the archive * @param path Path relative to the archive
* @return Whether the directory could be deleted * @return Whether the directory could be deleted
*/ */
virtual bool DeleteDirectory(const FileSys::Path& path) const = 0; virtual bool DeleteDirectory(const Path& path) const = 0;
/** /**
* Create a file specified by its path * Create a file specified by its path
@ -234,7 +230,7 @@ public:
* @param dest_path Destination path relative to the archive * @param dest_path Destination path relative to the archive
* @return Whether rename succeeded * @return Whether rename succeeded
*/ */
virtual bool RenameDirectory(const FileSys::Path& src_path, const FileSys::Path& dest_path) const = 0; virtual bool RenameDirectory(const Path& src_path, const Path& dest_path) const = 0;
/** /**
* Open a directory specified by its path * Open a directory specified by its path

View File

@ -23,37 +23,21 @@ Archive_RomFS::Archive_RomFS(const Loader::AppLoader& app_loader) {
} }
} }
/**
* Open a file specified by its path, using the specified mode
* @param path Path relative to the archive
* @param mode Mode to open the file with
* @return Opened file, or nullptr
*/
std::unique_ptr<FileBackend> Archive_RomFS::OpenFile(const Path& path, const Mode mode) const { std::unique_ptr<FileBackend> Archive_RomFS::OpenFile(const Path& path, const Mode mode) const {
return Common::make_unique<File_RomFS>(this); return Common::make_unique<File_RomFS>(this);
} }
/** bool Archive_RomFS::DeleteFile(const Path& path) const {
* Delete a file specified by its path
* @param path Path relative to the archive
* @return Whether the file could be deleted
*/
bool Archive_RomFS::DeleteFile(const FileSys::Path& path) const {
LOG_WARNING(Service_FS, "Attempted to delete a file from ROMFS."); LOG_WARNING(Service_FS, "Attempted to delete a file from ROMFS.");
return false; return false;
} }
bool Archive_RomFS::RenameFile(const FileSys::Path& src_path, const FileSys::Path& dest_path) const { bool Archive_RomFS::RenameFile(const Path& src_path, const Path& dest_path) const {
LOG_WARNING(Service_FS, "Attempted to rename a file within ROMFS."); LOG_WARNING(Service_FS, "Attempted to rename a file within ROMFS.");
return false; return false;
} }
/** bool Archive_RomFS::DeleteDirectory(const Path& path) const {
* Delete a directory specified by its path
* @param path Path relative to the archive
* @return Whether the directory could be deleted
*/
bool Archive_RomFS::DeleteDirectory(const FileSys::Path& path) const {
LOG_WARNING(Service_FS, "Attempted to delete a directory from ROMFS."); LOG_WARNING(Service_FS, "Attempted to delete a directory from ROMFS.");
return false; return false;
} }
@ -64,26 +48,16 @@ ResultCode Archive_RomFS::CreateFile(const Path& path, u32 size) const {
return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::FS, ErrorSummary::NotSupported, ErrorLevel::Permanent); return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::FS, ErrorSummary::NotSupported, ErrorLevel::Permanent);
} }
/**
* Create a directory specified by its path
* @param path Path relative to the archive
* @return Whether the directory could be created
*/
bool Archive_RomFS::CreateDirectory(const Path& path) const { bool Archive_RomFS::CreateDirectory(const Path& path) const {
LOG_WARNING(Service_FS, "Attempted to create a directory in ROMFS."); LOG_WARNING(Service_FS, "Attempted to create a directory in ROMFS.");
return false; return false;
} }
bool Archive_RomFS::RenameDirectory(const FileSys::Path& src_path, const FileSys::Path& dest_path) const { bool Archive_RomFS::RenameDirectory(const Path& src_path, const Path& dest_path) const {
LOG_WARNING(Service_FS, "Attempted to rename a file within ROMFS."); LOG_WARNING(Service_FS, "Attempted to rename a file within ROMFS.");
return false; return false;
} }
/**
* Open a directory specified by its path
* @param path Path relative to the archive
* @return Opened directory, or nullptr
*/
std::unique_ptr<DirectoryBackend> Archive_RomFS::OpenDirectory(const Path& path) const { std::unique_ptr<DirectoryBackend> Archive_RomFS::OpenDirectory(const Path& path) const {
return Common::make_unique<Directory_RomFS>(); return Common::make_unique<Directory_RomFS>();
} }

View File

@ -36,7 +36,7 @@ public:
* @param path Path relative to the archive * @param path Path relative to the archive
* @return Whether the file could be deleted * @return Whether the file could be deleted
*/ */
bool DeleteFile(const FileSys::Path& path) const override; bool DeleteFile(const Path& path) const override;
/** /**
* Rename a File specified by its path * Rename a File specified by its path
@ -44,14 +44,14 @@ public:
* @param dest_path Destination path relative to the archive * @param dest_path Destination path relative to the archive
* @return Whether rename succeeded * @return Whether rename succeeded
*/ */
bool RenameFile(const FileSys::Path& src_path, const FileSys::Path& dest_path) const override; bool RenameFile(const Path& src_path, const Path& dest_path) const override;
/** /**
* Delete a directory specified by its path * Delete a directory specified by its path
* @param path Path relative to the archive * @param path Path relative to the archive
* @return Whether the directory could be deleted * @return Whether the directory could be deleted
*/ */
bool DeleteDirectory(const FileSys::Path& path) const override; bool DeleteDirectory(const Path& path) const override;
/** /**
* Create a file specified by its path * Create a file specified by its path
@ -74,7 +74,7 @@ public:
* @param dest_path Destination path relative to the archive * @param dest_path Destination path relative to the archive
* @return Whether rename succeeded * @return Whether rename succeeded
*/ */
bool RenameDirectory(const FileSys::Path& src_path, const FileSys::Path& dest_path) const override; bool RenameDirectory(const Path& src_path, const Path& dest_path) const override;
/** /**
* Open a directory specified by its path * Open a directory specified by its path

View File

@ -16,7 +16,7 @@
namespace FileSys { namespace FileSys {
Archive_SaveData::Archive_SaveData(const std::string& mount_point, u64 program_id) Archive_SaveData::Archive_SaveData(const std::string& mount_point, u64 program_id)
: DiskArchive(mount_point + Common::StringFromFormat("%016X", program_id) + DIR_SEP) { : DiskArchive(mount_point + Common::StringFromFormat("%016X", program_id) + DIR_SEP) {
LOG_INFO(Service_FS, "Directory %s set as SaveData.", this->mount_point.c_str()); LOG_INFO(Service_FS, "Directory %s set as SaveData.", this->mount_point.c_str());
} }

View File

@ -15,7 +15,7 @@
namespace FileSys { namespace FileSys {
/// File system interface to the SystemSaveData archive /// File system interface to the SystemSaveData archive
/// TODO(Subv): This archive should point to a location in the NAND, /// TODO(Subv): This archive should point to a location in the NAND,
/// specifically nand:/data/<ID0>/sysdata/<SaveID-Low>/<SaveID-High> /// specifically nand:/data/<ID0>/sysdata/<SaveID-Low>/<SaveID-High>
class Archive_SystemSaveData final : public DiskArchive { class Archive_SystemSaveData final : public DiskArchive {
public: public:

View File

@ -21,20 +21,10 @@ bool Directory_RomFS::Open() {
return false; return false;
} }
/**
* List files contained in the directory
* @param count Number of entries to return at once in entries
* @param entries Buffer to read data into
* @return Number of entries listed
*/
u32 Directory_RomFS::Read(const u32 count, Entry* entries) { u32 Directory_RomFS::Read(const u32 count, Entry* entries) {
return 0; return 0;
} }
/**
* Close the directory
* @return true if the directory closed correctly
*/
bool Directory_RomFS::Close() const { bool Directory_RomFS::Close() const {
return false; return false;
} }

View File

@ -23,15 +23,15 @@ std::unique_ptr<FileBackend> DiskArchive::OpenFile(const Path& path, const Mode
return std::unique_ptr<FileBackend>(file); return std::unique_ptr<FileBackend>(file);
} }
bool DiskArchive::DeleteFile(const FileSys::Path& path) const { bool DiskArchive::DeleteFile(const Path& path) const {
return FileUtil::Delete(GetMountPoint() + path.AsString()); return FileUtil::Delete(GetMountPoint() + path.AsString());
} }
bool DiskArchive::RenameFile(const FileSys::Path& src_path, const FileSys::Path& dest_path) const { bool DiskArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
return FileUtil::Rename(GetMountPoint() + src_path.AsString(), GetMountPoint() + dest_path.AsString()); return FileUtil::Rename(GetMountPoint() + src_path.AsString(), GetMountPoint() + dest_path.AsString());
} }
bool DiskArchive::DeleteDirectory(const FileSys::Path& path) const { bool DiskArchive::DeleteDirectory(const Path& path) const {
return FileUtil::DeleteDir(GetMountPoint() + path.AsString()); return FileUtil::DeleteDir(GetMountPoint() + path.AsString());
} }
@ -60,7 +60,7 @@ bool DiskArchive::CreateDirectory(const Path& path) const {
return FileUtil::CreateDir(GetMountPoint() + path.AsString()); return FileUtil::CreateDir(GetMountPoint() + path.AsString());
} }
bool DiskArchive::RenameDirectory(const FileSys::Path& src_path, const FileSys::Path& dest_path) const { bool DiskArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
return FileUtil::Rename(GetMountPoint() + src_path.AsString(), GetMountPoint() + dest_path.AsString()); return FileUtil::Rename(GetMountPoint() + src_path.AsString(), GetMountPoint() + dest_path.AsString());
} }
@ -85,7 +85,7 @@ DiskFile::DiskFile(const DiskArchive* archive, const Path& path, const Mode mode
bool DiskFile::Open() { bool DiskFile::Open() {
if (!mode.create_flag && !FileUtil::Exists(path)) { if (!mode.create_flag && !FileUtil::Exists(path)) {
LOG_ERROR(Service_FS, "Non-existing file %s cant be open without mode create.", path.c_str()); LOG_ERROR(Service_FS, "Non-existing file %s can't be open without mode create.", path.c_str());
return false; return false;
} }

View File

@ -16,8 +16,8 @@
namespace FileSys { namespace FileSys {
/** /**
* Helper which implements a backend accessing the host machine's filesystem. * Helper which implements a backend accessing the host machine's filesystem.
* This should be subclassed by concrete archive types, which will provide the * This should be subclassed by concrete archive types, which will provide the
* base directory on the host filesystem and override any required functionality. * base directory on the host filesystem and override any required functionality.
*/ */
class DiskArchive : public ArchiveBackend { class DiskArchive : public ArchiveBackend {
@ -26,12 +26,12 @@ public:
virtual std::string GetName() const = 0; virtual std::string GetName() const = 0;
std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const override; std::unique_ptr<FileBackend> OpenFile(const Path& path, const Mode mode) const override;
bool DeleteFile(const FileSys::Path& path) const override; bool DeleteFile(const Path& path) const override;
bool RenameFile(const FileSys::Path& src_path, const FileSys::Path& dest_path) const override; bool RenameFile(const Path& src_path, const Path& dest_path) const override;
bool DeleteDirectory(const FileSys::Path& path) const override; bool DeleteDirectory(const Path& path) const override;
ResultCode CreateFile(const Path& path, u32 size) const override; ResultCode CreateFile(const Path& path, u32 size) const override;
bool CreateDirectory(const Path& path) const override; bool CreateDirectory(const Path& path) const override;
bool RenameDirectory(const FileSys::Path& src_path, const FileSys::Path& dest_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; std::unique_ptr<DirectoryBackend> OpenDirectory(const Path& path) const override;
/** /**
@ -50,7 +50,7 @@ class DiskFile : public FileBackend {
public: public:
DiskFile(); DiskFile();
DiskFile(const DiskArchive* archive, const Path& path, const Mode mode); DiskFile(const DiskArchive* archive, const Path& path, const Mode mode);
~DiskFile() override { ~DiskFile() override {
Close(); Close();
} }
@ -61,7 +61,7 @@ public:
size_t GetSize() const override; size_t GetSize() const override;
bool SetSize(const u64 size) const override; bool SetSize(const u64 size) const override;
bool Close() const override; bool Close() const override;
void Flush() const override { void Flush() const override {
file->Flush(); file->Flush();
} }

View File

@ -12,62 +12,30 @@
namespace FileSys { namespace FileSys {
/**
* Open the file
* @return true if the file opened correctly
*/
bool File_RomFS::Open() { bool File_RomFS::Open() {
return true; return true;
} }
/**
* Read data from the file
* @param offset Offset in bytes to start reading data from
* @param length Length in bytes of data to read from file
* @param buffer Buffer to read data into
* @return Number of bytes read
*/
size_t File_RomFS::Read(const u64 offset, const u32 length, u8* buffer) const { size_t File_RomFS::Read(const u64 offset, const u32 length, u8* buffer) const {
LOG_TRACE(Service_FS, "called offset=%llu, length=%d", offset, length); LOG_TRACE(Service_FS, "called offset=%llu, length=%d", offset, length);
memcpy(buffer, &archive->raw_data[(u32)offset], length); memcpy(buffer, &archive->raw_data[(u32)offset], length);
return length; return length;
} }
/**
* Write data to the file
* @param offset Offset in bytes to start writing data to
* @param length Length in bytes of data to write to file
* @param flush The flush parameters (0 == do not flush)
* @param buffer Buffer to read data from
* @return Number of bytes written
*/
size_t File_RomFS::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const { size_t File_RomFS::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const {
LOG_WARNING(Service_FS, "Attempted to write to ROMFS."); LOG_WARNING(Service_FS, "Attempted to write to ROMFS.");
return 0; return 0;
} }
/**
* Get the size of the file in bytes
* @return Size of the file in bytes
*/
size_t File_RomFS::GetSize() const { size_t File_RomFS::GetSize() const {
return sizeof(u8) * archive->raw_data.size(); return sizeof(u8) * archive->raw_data.size();
} }
/**
* Set the size of the file in bytes
* @param size New size of the file
* @return true if successful
*/
bool File_RomFS::SetSize(const u64 size) const { bool File_RomFS::SetSize(const u64 size) const {
LOG_WARNING(Service_FS, "Attempted to set the size of ROMFS"); LOG_WARNING(Service_FS, "Attempted to set the size of ROMFS");
return false; return false;
} }
/**
* Close the file
* @return true if the file closed correctly
*/
bool File_RomFS::Close() const { bool File_RomFS::Close() const {
return false; return false;
} }