2014-12-16 05:33:41 +00:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-12-16 05:33:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-06-21 14:44:11 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
2016-04-05 12:29:55 +00:00
|
|
|
#include <memory>
|
2014-12-16 05:33:41 +00:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/file_util.h"
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/logging/log.h"
|
2016-09-21 06:52:38 +00:00
|
|
|
#include "core/file_sys/disk_archive.h"
|
2017-05-21 04:41:11 +00:00
|
|
|
#include "core/file_sys/errors.h"
|
2014-12-16 05:33:41 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FileSys namespace
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
2018-09-06 20:03:28 +00:00
|
|
|
ResultVal<std::size_t> DiskFile::Read(const u64 offset, const std::size_t length,
|
|
|
|
u8* buffer) const {
|
2016-10-20 03:45:01 +00:00
|
|
|
if (!mode.read_flag)
|
2017-05-21 04:41:11 +00:00
|
|
|
return ERROR_INVALID_OPEN_FLAGS;
|
2015-12-28 15:03:09 +00:00
|
|
|
|
2014-12-16 05:33:41 +00:00
|
|
|
file->Seek(offset, SEEK_SET);
|
2018-09-06 20:03:28 +00:00
|
|
|
return MakeResult<std::size_t>(file->ReadBytes(buffer, length));
|
2014-12-16 05:33:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 20:03:28 +00:00
|
|
|
ResultVal<std::size_t> DiskFile::Write(const u64 offset, const std::size_t length, const bool flush,
|
|
|
|
const u8* buffer) {
|
2015-12-28 15:03:09 +00:00
|
|
|
if (!mode.write_flag)
|
2017-05-21 04:41:11 +00:00
|
|
|
return ERROR_INVALID_OPEN_FLAGS;
|
2015-12-28 15:03:09 +00:00
|
|
|
|
2014-12-16 05:33:41 +00:00
|
|
|
file->Seek(offset, SEEK_SET);
|
2018-09-06 20:03:28 +00:00
|
|
|
std::size_t written = file->WriteBytes(buffer, length);
|
2014-12-16 05:33:41 +00:00
|
|
|
if (flush)
|
|
|
|
file->Flush();
|
2018-09-06 20:03:28 +00:00
|
|
|
return MakeResult<std::size_t>(written);
|
2014-12-16 05:33:41 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 23:43:34 +00:00
|
|
|
u64 DiskFile::GetSize() const {
|
|
|
|
return file->GetSize();
|
2014-12-16 05:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskFile::SetSize(const u64 size) const {
|
|
|
|
file->Resize(size);
|
|
|
|
file->Flush();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskFile::Close() const {
|
|
|
|
return file->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-03-28 23:40:30 +00:00
|
|
|
DiskDirectory::DiskDirectory(const std::string& path) {
|
2015-12-25 22:03:59 +00:00
|
|
|
unsigned size = FileUtil::ScanDirectoryTree(path, directory);
|
|
|
|
directory.size = size;
|
|
|
|
directory.isDirectory = true;
|
2014-12-16 05:33:41 +00:00
|
|
|
children_iterator = directory.children.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 DiskDirectory::Read(const u32 count, Entry* entries) {
|
|
|
|
u32 entries_read = 0;
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_TRACE(Service_FS, "File {}: size={} dir={}", filename, file.size, file.isDirectory);
|
2014-12-16 05:33:41 +00:00
|
|
|
|
|
|
|
// TODO(Link Mauve): use a proper conversion to UTF-16.
|
2018-09-06 20:03:28 +00:00
|
|
|
for (std::size_t j = 0; j < FILENAME_LENGTH; ++j) {
|
2014-12-16 05:33:41 +00:00
|
|
|
entry.filename[j] = filename[j];
|
|
|
|
if (!filename[j])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileUtil::SplitFilename83(filename, entry.short_name, entry.extension);
|
|
|
|
|
|
|
|
entry.is_directory = file.isDirectory;
|
|
|
|
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
|
|
|
|
// most user SD cards.
|
|
|
|
// Some homebrews (blargSNES for instance) are known to mistakenly use the archive bit as a
|
|
|
|
// file bit.
|
|
|
|
entry.is_archive = !file.isDirectory;
|
|
|
|
|
|
|
|
++entries_read;
|
|
|
|
++children_iterator;
|
|
|
|
}
|
|
|
|
return entries_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FileSys
|