Merge pull request #129 from linkmauve/master

Fix the filesystem implementation in order to get blargSnes to run
This commit is contained in:
bunnei 2014-10-06 15:25:19 -04:00
commit 0e2d83fa3a
13 changed files with 227 additions and 47 deletions

View file

@ -780,6 +780,48 @@ size_t ReadFileToString(bool text_file, const char *filename, std::string &str)
return file.ReadArray(&str[0], str.size()); return file.ReadArray(&str[0], str.size());
} }
/**
* Splits the filename into 8.3 format
* Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename
* @param filename The normal filename to use
* @param short_name A 9-char array in which the short name will be written
* @param extension A 4-char array in which the extension will be written
*/
void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name,
std::array<char, 4>& extension) {
const std::string forbidden_characters = ".\"/\\[]:;=, ";
// On a FAT32 partition, 8.3 names are stored as a 11 bytes array, filled with spaces.
short_name = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'};
extension = {' ', ' ', ' ', '\0'};
std::string::size_type point = filename.rfind('.');
if (point == filename.size() - 1)
point = filename.rfind('.', point);
// Get short name.
int j = 0;
for (char letter : filename.substr(0, point)) {
if (forbidden_characters.find(letter, 0) != std::string::npos)
continue;
if (j == 8) {
// TODO(Link Mauve): also do that for filenames containing a space.
// TODO(Link Mauve): handle multiple files having the same short name.
short_name[6] = '~';
short_name[7] = '1';
break;
}
short_name[j++] = toupper(letter);
}
// Get extension.
if (point != std::string::npos) {
j = 0;
for (char letter : filename.substr(point + 1, 3))
extension[j++] = toupper(letter);
}
}
IOFile::IOFile() IOFile::IOFile()
: m_file(NULL), m_good(true) : m_file(NULL), m_good(true)
{} {}

View file

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <array>
#include <fstream> #include <fstream>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
@ -131,6 +132,16 @@ std::string &GetExeDirectory();
size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename); size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename);
size_t ReadFileToString(bool text_file, const char *filename, std::string &str); size_t ReadFileToString(bool text_file, const char *filename, std::string &str);
/**
* Splits the filename into 8.3 format
* Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename
* @param filename The normal filename to use
* @param short_name A 9-char array in which the short name will be written
* @param extension A 4-char array in which the extension will be written
*/
void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name,
std::array<char, 4>& extension);
// simple wrapper for cstdlib file functions to // simple wrapper for cstdlib file functions to
// hopefully will make error checking easier // hopefully will make error checking easier
// and make forgetting an fclose() harder // and make forgetting an fclose() harder

View file

@ -24,6 +24,10 @@ Archive_SDMC::Archive_SDMC(const std::string& mount_point) {
Archive_SDMC::~Archive_SDMC() { Archive_SDMC::~Archive_SDMC() {
} }
/**
* Initialize the archive.
* @return true if it initialized successfully
*/
bool Archive_SDMC::Initialize() { bool Archive_SDMC::Initialize() {
if (!FileUtil::IsDirectory(mount_point)) { if (!FileUtil::IsDirectory(mount_point)) {
WARN_LOG(FILESYS, "Directory %s not found, disabling SDMC.", mount_point.c_str()); WARN_LOG(FILESYS, "Directory %s not found, disabling SDMC.", mount_point.c_str());
@ -42,6 +46,8 @@ bool Archive_SDMC::Initialize() {
std::unique_ptr<File> Archive_SDMC::OpenFile(const std::string& path, const Mode mode) const { std::unique_ptr<File> Archive_SDMC::OpenFile(const std::string& path, const Mode mode) const {
DEBUG_LOG(FILESYS, "called path=%s mode=%d", path.c_str(), mode); DEBUG_LOG(FILESYS, "called path=%s mode=%d", path.c_str(), mode);
File_SDMC* file = new File_SDMC(this, path, mode); File_SDMC* file = new File_SDMC(this, path, mode);
if (!file->Open())
return nullptr;
return std::unique_ptr<File>(file); return std::unique_ptr<File>(file);
} }

View file

@ -20,6 +20,10 @@ public:
Archive_SDMC(const std::string& mount_point); Archive_SDMC(const std::string& mount_point);
~Archive_SDMC() override; ~Archive_SDMC() override;
/**
* Initialize the archive.
* @return true if it initialized successfully
*/
bool Initialize(); bool Initialize();
/** /**

View file

@ -4,6 +4,8 @@
#pragma once #pragma once
#include <cstddef>
#include "common/common_types.h" #include "common/common_types.h"
#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/kernel.h"
@ -17,9 +19,9 @@ namespace FileSys {
const size_t FILENAME_LENGTH = 0x20C / 2; const size_t FILENAME_LENGTH = 0x20C / 2;
struct Entry { struct Entry {
char16_t filename[FILENAME_LENGTH]; // Entry name (UTF-16, null-terminated) char16_t filename[FILENAME_LENGTH]; // Entry name (UTF-16, null-terminated)
char short_name[8]; // 8.3 file name ('longfilename' -> 'LONGFI~1') std::array<char, 9> short_name; // 8.3 file name ('longfilename' -> 'LONGFI~1', null-terminated)
char unknown1; // unknown (observed values: 0x0A, 0x70, 0xFD) char unknown1; // unknown (observed values: 0x0A, 0x70, 0xFD)
char extension[3]; // 8.3 file extension (set to spaces for directories) std::array<char, 4> extension; // 8.3 file extension (set to spaces for directories, null-terminated)
char unknown2; // unknown (always 0x01) char unknown2; // unknown (always 0x01)
char unknown3; // unknown (0x00 or 0x08) char unknown3; // unknown (0x00 or 0x08)
char is_directory; // directory flag char is_directory; // directory flag
@ -29,6 +31,10 @@ struct Entry {
u64 file_size; // file size (for files only) u64 file_size; // file size (for files only)
}; };
static_assert(sizeof(Entry) == 0x228, "Directory Entry struct isn't exactly 0x228 bytes long!"); static_assert(sizeof(Entry) == 0x228, "Directory Entry struct isn't exactly 0x228 bytes long!");
static_assert(offsetof(Entry, short_name) == 0x20C, "Wrong offset for short_name in Entry.");
static_assert(offsetof(Entry, extension) == 0x216, "Wrong offset for extension in Entry.");
static_assert(offsetof(Entry, is_archive) == 0x21E, "Wrong offset for is_archive in Entry.");
static_assert(offsetof(Entry, file_size) == 0x220, "Wrong offset for file_size in Entry.");
class Directory : NonCopyable { class Directory : NonCopyable {
public: public:

View file

@ -20,8 +20,8 @@ Directory_SDMC::Directory_SDMC(const Archive_SDMC* archive, const std::string& p
// the root directory we set while opening the archive. // the root directory we set while opening the archive.
// For example, opening /../../usr/bin can give the emulated program your installed programs. // For example, opening /../../usr/bin can give the emulated program your installed programs.
std::string absolute_path = archive->GetMountPoint() + path; std::string absolute_path = archive->GetMountPoint() + path;
entry_count = FileUtil::ScanDirectoryTree(absolute_path, entry); FileUtil::ScanDirectoryTree(absolute_path, directory);
current_entry = 0; children_iterator = directory.children.begin();
} }
Directory_SDMC::~Directory_SDMC() { Directory_SDMC::~Directory_SDMC() {
@ -35,44 +35,39 @@ Directory_SDMC::~Directory_SDMC() {
* @return Number of entries listed * @return Number of entries listed
*/ */
u32 Directory_SDMC::Read(const u32 count, Entry* entries) { u32 Directory_SDMC::Read(const u32 count, Entry* entries) {
u32 i; u32 entries_read = 0;
for (i = 0; i < count && current_entry < entry_count; ++i) {
FileUtil::FSTEntry file = entry.children[current_entry];
std::string filename = file.virtualName;
WARN_LOG(FILESYS, "File %s: size=%d dir=%d", filename.c_str(), file.size, file.isDirectory);
Entry* entry = &entries[i]; while (entries_read < count && children_iterator != directory.children.cend()) {
const FileUtil::FSTEntry& file = *children_iterator;
const std::string& filename = file.virtualName;
Entry& entry = entries[entries_read];
WARN_LOG(FILESYS, "File %s: size=%d dir=%d", filename.c_str(), file.size, file.isDirectory);
// TODO(Link Mauve): use a proper conversion to UTF-16. // TODO(Link Mauve): use a proper conversion to UTF-16.
for (int j = 0; j < FILENAME_LENGTH; ++j) { for (int j = 0; j < FILENAME_LENGTH; ++j) {
entry->filename[j] = filename[j]; entry.filename[j] = filename[j];
if (!filename[j]) if (!filename[j])
break; break;
} }
// Split the filename into 8.3 format. FileUtil::SplitFilename83(filename, entry.short_name, entry.extension);
// TODO(Link Mauve): move that to common, I guess, and make it more robust to long filenames.
std::string::size_type n = filename.rfind('.');
if (n == std::string::npos) {
strncpy(entry->short_name, filename.c_str(), 8);
memset(entry->extension, '\0', 3);
} else {
strncpy(entry->short_name, filename.substr(0, n).c_str(), 8);
strncpy(entry->extension, filename.substr(n + 1).c_str(), 8);
}
entry->is_directory = file.isDirectory; entry.is_directory = file.isDirectory;
entry->file_size = file.size; entry.is_hidden = (filename[0] == '.');
entry.is_read_only = 0;
entry.file_size = file.size;
// We emulate a SD card where the archive bit has never been cleared, as it would be on // We emulate a SD card where the archive bit has never been cleared, as it would be on
// most user SD cards. // most user SD cards.
// Some homebrews (blargSNES for instance) are known to mistakenly use the archive bit as a // Some homebrews (blargSNES for instance) are known to mistakenly use the archive bit as a
// file bit. // file bit.
entry->is_archive = !file.isDirectory; entry.is_archive = !file.isDirectory;
++current_entry; ++entries_read;
++children_iterator;
} }
return i; return entries_read;
} }
/** /**

View file

@ -37,9 +37,12 @@ public:
bool Close() const override; bool Close() const override;
private: private:
u32 entry_count; u32 total_entries_in_directory;
u32 current_entry; FileUtil::FSTEntry directory;
FileUtil::FSTEntry entry;
// We need to remember the last entry we returned, so a subsequent call to Read will continue
// from the next one. This iterator will always point to the next unread entry.
std::vector<FileUtil::FSTEntry>::iterator children_iterator;
}; };
} // namespace FileSys } // namespace FileSys

View file

@ -18,6 +18,12 @@ public:
File() { } File() { }
virtual ~File() { } virtual ~File() { }
/**
* Open the file
* @return true if the file opened correctly
*/
virtual bool Open() = 0;
/** /**
* Read data from the file * Read data from the file
* @param offset Offset in bytes to start reading data from * @param offset Offset in bytes to start reading data from
@ -31,8 +37,8 @@ public:
* Write data to the file * Write data to the file
* @param offset Offset in bytes to start writing data to * @param offset Offset in bytes to start writing data to
* @param length Length in bytes of data to write to file * @param length Length in bytes of data to write to file
* @param buffer Buffer to write data from
* @param flush The flush parameters (0 == do not flush) * @param flush The flush parameters (0 == do not flush)
* @param buffer Buffer to read data from
* @return Number of bytes written * @return Number of bytes written
*/ */
virtual size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const = 0; virtual size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const = 0;
@ -43,6 +49,13 @@ public:
*/ */
virtual size_t GetSize() const = 0; virtual size_t GetSize() const = 0;
/**
* Set the size of the file in bytes
* @param size New size of the file
* @return true if successful
*/
virtual bool SetSize(const u64 size) const = 0;
/** /**
* Close the file * Close the file
* @return true if the file closed correctly * @return true if the file closed correctly

View file

@ -17,6 +17,14 @@ File_RomFS::File_RomFS() {
File_RomFS::~File_RomFS() { File_RomFS::~File_RomFS() {
} }
/**
* Open the file
* @return true if the file opened correctly
*/
bool File_RomFS::Open() {
return false;
}
/** /**
* Read data from the file * Read data from the file
* @param offset Offset in bytes to start reading data from * @param offset Offset in bytes to start reading data from
@ -32,8 +40,8 @@ size_t File_RomFS::Read(const u64 offset, const u32 length, u8* buffer) const {
* Write data to the file * Write data to the file
* @param offset Offset in bytes to start writing data to * @param offset Offset in bytes to start writing data to
* @param length Length in bytes of data to write to file * @param length Length in bytes of data to write to file
* @param buffer Buffer to write data from
* @param flush The flush parameters (0 == do not flush) * @param flush The flush parameters (0 == do not flush)
* @param buffer Buffer to read data from
* @return Number of bytes written * @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 {
@ -48,6 +56,15 @@ size_t File_RomFS::GetSize() const {
return -1; return -1;
} }
/**
* 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 {
return false;
}
/** /**
* Close the file * Close the file
* @return true if the file closed correctly * @return true if the file closed correctly

View file

@ -19,6 +19,12 @@ public:
File_RomFS(); File_RomFS();
~File_RomFS() override; ~File_RomFS() override;
/**
* Open the file
* @return true if the file opened correctly
*/
bool Open() override;
/** /**
* Read data from the file * Read data from the file
* @param offset Offset in bytes to start reading data from * @param offset Offset in bytes to start reading data from
@ -32,8 +38,8 @@ public:
* Write data to the file * Write data to the file
* @param offset Offset in bytes to start writing data to * @param offset Offset in bytes to start writing data to
* @param length Length in bytes of data to write to file * @param length Length in bytes of data to write to file
* @param buffer Buffer to write data from
* @param flush The flush parameters (0 == do not flush) * @param flush The flush parameters (0 == do not flush)
* @param buffer Buffer to read data from
* @return Number of bytes written * @return Number of bytes written
*/ */
size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const override; size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const override;
@ -44,6 +50,13 @@ public:
*/ */
size_t GetSize() const override; size_t GetSize() const override;
/**
* Set the size of the file in bytes
* @param size New size of the file
* @return true if successful
*/
bool SetSize(const u64 size) const override;
/** /**
* Close the file * Close the file
* @return true if the file closed correctly * @return true if the file closed correctly

View file

@ -19,31 +19,56 @@ File_SDMC::File_SDMC(const Archive_SDMC* archive, const std::string& path, const
// TODO(Link Mauve): normalize path into an absolute path without "..", it can currently bypass // TODO(Link Mauve): normalize path into an absolute path without "..", it can currently bypass
// the root directory we set while opening the archive. // the root directory we set while opening the archive.
// For example, opening /../../etc/passwd can give the emulated program your users list. // For example, opening /../../etc/passwd can give the emulated program your users list.
std::string real_path = archive->GetMountPoint() + path; this->path = archive->GetMountPoint() + path;
this->mode.hex = mode.hex;
if (!mode.create_flag && !FileUtil::Exists(real_path)) {
file = nullptr;
return;
}
std::string mode_string;
if (mode.read_flag)
mode_string += "r";
if (mode.write_flag)
mode_string += "w";
file = new FileUtil::IOFile(real_path, mode_string.c_str());
} }
File_SDMC::~File_SDMC() { File_SDMC::~File_SDMC() {
Close(); Close();
} }
/**
* Open the file
* @return true if the file opened correctly
*/
bool File_SDMC::Open() {
if (!mode.create_flag && !FileUtil::Exists(path)) {
ERROR_LOG(FILESYS, "Non-existing file %s cant be open without mode create.", path.c_str());
return false;
}
std::string mode_string;
if (mode.read_flag && mode.write_flag)
mode_string = "w+";
else if (mode.read_flag)
mode_string = "r";
else if (mode.write_flag)
mode_string = "w";
file = new FileUtil::IOFile(path, mode_string.c_str());
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_SDMC::Read(const u64 offset, const u32 length, u8* buffer) const { size_t File_SDMC::Read(const u64 offset, const u32 length, u8* buffer) const {
file->Seek(offset, SEEK_SET); file->Seek(offset, SEEK_SET);
return file->ReadBytes(buffer, length); return file->ReadBytes(buffer, 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_SDMC::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const { size_t File_SDMC::Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const {
file->Seek(offset, SEEK_SET); file->Seek(offset, SEEK_SET);
size_t written = file->WriteBytes(buffer, length); size_t written = file->WriteBytes(buffer, length);
@ -52,10 +77,29 @@ size_t File_SDMC::Write(const u64 offset, const u32 length, const u32 flush, con
return written; return written;
} }
/**
* Get the size of the file in bytes
* @return Size of the file in bytes
*/
size_t File_SDMC::GetSize() const { size_t File_SDMC::GetSize() const {
return static_cast<size_t>(file->GetSize()); return static_cast<size_t>(file->GetSize());
} }
/**
* Set the size of the file in bytes
* @param size New size of the file
* @return true if successful
*/
bool File_SDMC::SetSize(const u64 size) const {
file->Resize(size);
file->Flush();
return true;
}
/**
* Close the file
* @return true if the file closed correctly
*/
bool File_SDMC::Close() const { bool File_SDMC::Close() const {
return file->Close(); return file->Close();
} }

View file

@ -22,6 +22,12 @@ public:
File_SDMC(const Archive_SDMC* archive, const std::string& path, const Mode mode); File_SDMC(const Archive_SDMC* archive, const std::string& path, const Mode mode);
~File_SDMC() override; ~File_SDMC() override;
/**
* Open the file
* @return true if the file opened correctly
*/
bool Open() override;
/** /**
* Read data from the file * Read data from the file
* @param offset Offset in bytes to start reading data from * @param offset Offset in bytes to start reading data from
@ -35,8 +41,8 @@ public:
* Write data to the file * Write data to the file
* @param offset Offset in bytes to start writing data to * @param offset Offset in bytes to start writing data to
* @param length Length in bytes of data to write to file * @param length Length in bytes of data to write to file
* @param buffer Buffer to write data from
* @param flush The flush parameters (0 == do not flush) * @param flush The flush parameters (0 == do not flush)
* @param buffer Buffer to read data from
* @return Number of bytes written * @return Number of bytes written
*/ */
size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const override; size_t Write(const u64 offset, const u32 length, const u32 flush, const u8* buffer) const override;
@ -47,6 +53,13 @@ public:
*/ */
size_t GetSize() const override; size_t GetSize() const override;
/**
* Set the size of the file in bytes
* @param size New size of the file
* @return true if successful
*/
bool SetSize(const u64 size) const override;
/** /**
* Close the file * Close the file
* @return true if the file closed correctly * @return true if the file closed correctly
@ -54,6 +67,8 @@ public:
bool Close() const override; bool Close() const override;
private: private:
std::string path;
Mode mode;
FileUtil::IOFile* file; FileUtil::IOFile* file;
}; };

View file

@ -181,6 +181,14 @@ public:
break; break;
} }
case FileCommand::SetSize:
{
u64 size = cmd_buff[1] | ((u64)cmd_buff[2] << 32);
DEBUG_LOG(KERNEL, "SetSize %s %s size=%d", GetTypeName().c_str(), GetName().c_str(), size);
backend->SetSize(size);
break;
}
case FileCommand::Close: case FileCommand::Close:
{ {
DEBUG_LOG(KERNEL, "Close %s %s", GetTypeName().c_str(), GetName().c_str()); DEBUG_LOG(KERNEL, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
@ -366,6 +374,9 @@ Handle OpenFileFromArchive(Handle archive_handle, const std::string& path, const
file->path = path; file->path = path;
file->backend = archive->backend->OpenFile(path, mode); file->backend = archive->backend->OpenFile(path, mode);
if (!file->backend)
return 0;
return handle; return handle;
} }