fs:: const renaming, fs::g_tls_error stub

This commit is contained in:
Nekotekina 2015-08-12 04:52:26 +03:00
parent c2897cddd6
commit 73b108765e
33 changed files with 196 additions and 121 deletions

View file

@ -114,8 +114,12 @@ bool truncate_file(const std::string& file, u64 length)
#endif #endif
thread_local fse fs::g_tls_error = fse::ok;
bool fs::stat(const std::string& path, stat_t& info) bool fs::stat(const std::string& path, stat_t& info)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs; WIN32_FILE_ATTRIBUTE_DATA attrs;
if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs)) if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs))
@ -149,6 +153,8 @@ bool fs::stat(const std::string& path, stat_t& info)
bool fs::exists(const std::string& path) bool fs::exists(const std::string& path)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
return GetFileAttributesW(to_wchar(path).get()) != 0xFFFFFFFF; return GetFileAttributesW(to_wchar(path).get()) != 0xFFFFFFFF;
#else #else
@ -159,6 +165,8 @@ bool fs::exists(const std::string& path)
bool fs::is_file(const std::string& file) bool fs::is_file(const std::string& file)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
DWORD attrs; DWORD attrs;
if ((attrs = GetFileAttributesW(to_wchar(file).get())) == INVALID_FILE_ATTRIBUTES) if ((attrs = GetFileAttributesW(to_wchar(file).get())) == INVALID_FILE_ATTRIBUTES)
@ -180,6 +188,8 @@ bool fs::is_file(const std::string& file)
bool fs::is_dir(const std::string& dir) bool fs::is_dir(const std::string& dir)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
DWORD attrs; DWORD attrs;
if ((attrs = GetFileAttributesW(to_wchar(dir).get())) == INVALID_FILE_ATTRIBUTES) if ((attrs = GetFileAttributesW(to_wchar(dir).get())) == INVALID_FILE_ATTRIBUTES)
@ -201,6 +211,8 @@ bool fs::is_dir(const std::string& dir)
bool fs::create_dir(const std::string& dir) bool fs::create_dir(const std::string& dir)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
if (!CreateDirectoryW(to_wchar(dir).get(), NULL)) if (!CreateDirectoryW(to_wchar(dir).get(), NULL))
#else #else
@ -216,6 +228,8 @@ bool fs::create_dir(const std::string& dir)
bool fs::create_path(const std::string& path) bool fs::create_path(const std::string& path)
{ {
g_tls_error = fse::ok;
size_t start = 0; size_t start = 0;
while (true) while (true)
@ -258,6 +272,8 @@ bool fs::create_path(const std::string& path)
bool fs::remove_dir(const std::string& dir) bool fs::remove_dir(const std::string& dir)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
if (!RemoveDirectoryW(to_wchar(dir).get())) if (!RemoveDirectoryW(to_wchar(dir).get()))
#else #else
@ -273,6 +289,8 @@ bool fs::remove_dir(const std::string& dir)
bool fs::rename(const std::string& from, const std::string& to) bool fs::rename(const std::string& from, const std::string& to)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
if (!MoveFileW(to_wchar(from).get(), to_wchar(to).get())) if (!MoveFileW(to_wchar(from).get(), to_wchar(to).get()))
#else #else
@ -324,6 +342,8 @@ int OSCopyFile(const char* source, const char* destination, bool overwrite)
bool fs::copy_file(const std::string& from, const std::string& to, bool overwrite) bool fs::copy_file(const std::string& from, const std::string& to, bool overwrite)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
if (!CopyFileW(to_wchar(from).get(), to_wchar(to).get(), !overwrite)) if (!CopyFileW(to_wchar(from).get(), to_wchar(to).get(), !overwrite))
#else #else
@ -339,6 +359,8 @@ bool fs::copy_file(const std::string& from, const std::string& to, bool overwrit
bool fs::remove_file(const std::string& file) bool fs::remove_file(const std::string& file)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
if (!DeleteFileW(to_wchar(file).get())) if (!DeleteFileW(to_wchar(file).get()))
#else #else
@ -354,6 +376,8 @@ bool fs::remove_file(const std::string& file)
bool fs::truncate_file(const std::string& file, u64 length) bool fs::truncate_file(const std::string& file, u64 length)
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
if (!::truncate_file(file, length)) if (!::truncate_file(file, length))
#else #else
@ -383,62 +407,64 @@ bool fs::file::open(const std::string& filename, u32 mode)
{ {
this->close(); this->close();
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
DWORD access = 0; DWORD access = 0;
switch (mode & (o_read | o_write | o_append)) switch (mode & (fom::read | fom::write | fom::append))
{ {
case o_read: access |= GENERIC_READ; break; case fom::read: access |= GENERIC_READ; break;
case o_read | o_append: access |= GENERIC_READ; break; case fom::read | fom::append: access |= GENERIC_READ; break;
case o_write: access |= GENERIC_WRITE; break; case fom::write: access |= GENERIC_WRITE; break;
case o_write | o_append: access |= FILE_APPEND_DATA; break; case fom::write | fom::append: access |= FILE_APPEND_DATA; break;
case o_read | o_write: access |= GENERIC_READ | GENERIC_WRITE; break; case fom::read | fom::write: access |= GENERIC_READ | GENERIC_WRITE; break;
case o_read | o_write | o_append: access |= GENERIC_READ | FILE_APPEND_DATA; break; case fom::read | fom::write | fom::append: access |= GENERIC_READ | FILE_APPEND_DATA; break;
default: default:
{ {
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither o_read nor o_write specified (0x%x)", filename, mode); LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither fom::read nor fom::write specified (0x%x)", filename, mode);
return false; return false;
} }
} }
DWORD disp = 0; DWORD disp = 0;
switch (mode & (o_create | o_trunc | o_excl)) switch (mode & (fom::create | fom::trunc | fom::excl))
{ {
case 0: disp = OPEN_EXISTING; break; case 0: disp = OPEN_EXISTING; break;
case o_create: disp = OPEN_ALWAYS; break; case fom::create: disp = OPEN_ALWAYS; break;
case o_trunc: disp = TRUNCATE_EXISTING; break; case fom::trunc: disp = TRUNCATE_EXISTING; break;
case o_create | o_trunc: disp = CREATE_ALWAYS; break; case fom::create | fom::trunc: disp = CREATE_ALWAYS; break;
case o_create | o_excl: disp = CREATE_NEW; break; case fom::create | fom::excl: disp = CREATE_NEW; break;
case o_create | o_excl | o_trunc: disp = CREATE_NEW; break; case fom::create | fom::excl | fom::trunc: disp = CREATE_NEW; break;
} }
if (!disp || (mode & ~(o_read | o_write | o_append | o_create | o_trunc | o_excl))) if (!disp || (mode & ~(fom::read | fom::write | fom::append | fom::create | fom::trunc | fom::excl)))
{ {
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode); LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode);
return false; return false;
} }
m_fd = (intptr_t)CreateFileW(to_wchar(filename).get(), access, FILE_SHARE_READ, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL); m_fd = (intptr_t)CreateFileW(to_wchar(filename).get(), access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
#else #else
int flags = 0; int flags = 0;
switch (mode & (o_read | o_write)) switch (mode & (fom::read | fom::write))
{ {
case o_read: flags |= O_RDONLY; break; case fom::read: flags |= O_RDONLY; break;
case o_write: flags |= O_WRONLY; break; case fom::write: flags |= O_WRONLY; break;
case o_read | o_write: flags |= O_RDWR; break; case fom::read | fom::write: flags |= O_RDWR; break;
default: default:
{ {
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither o_read nor o_write specified (0x%x)", filename, mode); LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither fom::read nor fom::write specified (0x%x)", filename, mode);
return false; return false;
} }
} }
if (mode & o_append) flags |= O_APPEND; if (mode & fom::append) flags |= O_APPEND;
if (mode & o_create) flags |= O_CREAT; if (mode & fom::create) flags |= O_CREAT;
if (mode & o_trunc) flags |= O_TRUNC; if (mode & fom::trunc) flags |= O_TRUNC;
if (mode & o_excl) flags |= O_EXCL; if (mode & fom::excl) flags |= O_EXCL;
if (((mode & o_excl) && !(mode & o_create)) || (mode & ~(o_read | o_write | o_append | o_create | o_trunc | o_excl))) if (((mode & fom::excl) && !(mode & fom::create)) || (mode & ~(fom::read | fom::write | fom::append | fom::create | fom::trunc | fom::excl)))
{ {
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode); LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode);
return false; return false;
@ -458,6 +484,8 @@ bool fs::file::open(const std::string& filename, u32 mode)
bool fs::file::trunc(u64 size) const bool fs::file::trunc(u64 size) const
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
LARGE_INTEGER old, pos; LARGE_INTEGER old, pos;
@ -479,6 +507,8 @@ bool fs::file::trunc(u64 size) const
bool fs::file::stat(stat_t& info) const bool fs::file::stat(stat_t& info) const
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
FILE_BASIC_INFO basic_info; FILE_BASIC_INFO basic_info;
@ -513,6 +543,8 @@ bool fs::file::stat(stat_t& info) const
bool fs::file::close() bool fs::file::close()
{ {
g_tls_error = fse::ok;
if (m_fd == null) if (m_fd == null)
{ {
return false; return false;
@ -530,6 +562,8 @@ bool fs::file::close()
u64 fs::file::read(void* buffer, u64 count) const u64 fs::file::read(void* buffer, u64 count) const
{ {
g_tls_error = fse::ok;
const int size = count <= INT_MAX ? static_cast<int>(count) : throw EXCEPTION("Invalid count (0x%llx)", count); const int size = count <= INT_MAX ? static_cast<int>(count) : throw EXCEPTION("Invalid count (0x%llx)", count);
#ifdef _WIN32 #ifdef _WIN32
@ -547,6 +581,8 @@ u64 fs::file::read(void* buffer, u64 count) const
u64 fs::file::write(const void* buffer, u64 count) const u64 fs::file::write(const void* buffer, u64 count) const
{ {
g_tls_error = fse::ok;
const int size = count <= INT_MAX ? static_cast<int>(count) : throw EXCEPTION("Invalid count (0x%llx)", count); const int size = count <= INT_MAX ? static_cast<int>(count) : throw EXCEPTION("Invalid count (0x%llx)", count);
#ifdef _WIN32 #ifdef _WIN32
@ -562,14 +598,20 @@ u64 fs::file::write(const void* buffer, u64 count) const
#endif #endif
} }
u64 fs::file::seek(u64 offset, u32 mode) const u64 fs::file::seek(s64 offset, fsm seek_mode) const
{ {
assert(mode < 3); g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
LARGE_INTEGER pos; LARGE_INTEGER pos;
pos.QuadPart = offset; pos.QuadPart = offset;
const DWORD mode =
seek_mode == fsm::begin ? FILE_BEGIN :
seek_mode == fsm::cur ? FILE_CURRENT :
seek_mode == fsm::end ? FILE_END :
throw EXCEPTION("Unknown seek_mode (0x%x)", seek_mode);
if (!SetFilePointerEx((HANDLE)m_fd, pos, &pos, mode)) if (!SetFilePointerEx((HANDLE)m_fd, pos, &pos, mode))
{ {
return -1; return -1;
@ -577,12 +619,20 @@ u64 fs::file::seek(u64 offset, u32 mode) const
return pos.QuadPart; return pos.QuadPart;
#else #else
return ::lseek(m_fd, offset, mode); const int whence =
seek_mode == fsm::begin ? SEEK_SET :
seek_mode == fsm::cur ? SEEK_CUR :
seek_mode == fsm::end ? SEEK_END :
throw EXCEPTION("Unknown seek_mode (0x%x)", seek_mode);
return ::lseek(m_fd, offset, whence);
#endif #endif
} }
u64 fs::file::size() const u64 fs::file::size() const
{ {
g_tls_error = fse::ok;
#ifdef _WIN32 #ifdef _WIN32
LARGE_INTEGER size; LARGE_INTEGER size;
if (!GetFileSizeEx((HANDLE)m_fd, &size)) if (!GetFileSizeEx((HANDLE)m_fd, &size))
@ -625,6 +675,8 @@ void fs::dir::import(handle_type dd, const std::string& path)
#endif #endif
} }
g_tls_error = fse::ok;
m_dd = dd; m_dd = dd;
#ifdef _WIN32 #ifdef _WIN32
@ -646,6 +698,8 @@ bool fs::dir::open(const std::string& dirname)
#endif #endif
} }
g_tls_error = fse::ok;
m_dd = null; m_dd = null;
m_path.reset(); m_path.reset();
@ -667,6 +721,8 @@ bool fs::dir::open(const std::string& dirname)
bool fs::dir::close() bool fs::dir::close()
{ {
g_tls_error = fse::ok;
if (m_dd == null) if (m_dd == null)
{ {
if (m_path) if (m_path)
@ -703,6 +759,8 @@ bool fs::dir::get_first(std::string& name, stat_t& info)
#endif #endif
} }
g_tls_error = fse::ok;
m_dd = null; m_dd = null;
if (!m_path) if (!m_path)
@ -739,6 +797,8 @@ bool fs::dir::get_first(std::string& name, stat_t& info)
bool fs::dir::get_next(std::string& name, stat_t& info) bool fs::dir::get_next(std::string& name, stat_t& info)
{ {
g_tls_error = fse::ok;
if (m_dd == null) if (m_dd == null)
{ {
return false; return false;

View file

@ -1,32 +1,53 @@
#pragma once #pragma once
enum file_seek_mode : u32 enum class fsm : u32 // file seek mode
{ {
from_begin, begin,
from_cur, cur,
from_end, end,
}; };
enum file_open_mode : u32 namespace fom // file open mode
{ {
o_read = 1 << 0, enum : u32
o_write = 1 << 1, {
o_append = 1 << 2, read = 1 << 0,
o_create = 1 << 3, write = 1 << 1,
o_trunc = 1 << 4, append = 1 << 2,
o_excl = 1 << 5, create = 1 << 3,
trunc = 1 << 4,
excl = 1 << 5,
};
};
enum class fse : u32 // filesystem (file or dir) error
{
ok, // no error
invalid_arguments,
};
enum : u32 // obsolete flags
{
o_read = fom::read,
o_write = fom::write,
o_append = fom::append,
o_create = fom::create,
o_trunc = fom::trunc,
o_excl = fom::excl,
}; };
namespace fs namespace fs
{ {
thread_local extern fse g_tls_error;
struct stat_t struct stat_t
{ {
bool is_directory; bool is_directory;
bool is_writable; bool is_writable;
u64 size; u64 size;
time_t atime; s64 atime;
time_t mtime; s64 mtime;
time_t ctime; s64 ctime;
}; };
bool stat(const std::string& path, stat_t& info); bool stat(const std::string& path, stat_t& info);
@ -53,7 +74,7 @@ namespace fs
public: public:
file() = default; file() = default;
~file(); ~file();
explicit file(const std::string& filename, u32 mode = o_read) { open(filename, mode); } explicit file(const std::string& filename, u32 mode = fom::read) { open(filename, mode); }
file(const file&) = delete; file(const file&) = delete;
file(file&&) = delete; // possibly TODO file(file&&) = delete; // possibly TODO
@ -65,7 +86,7 @@ namespace fs
void import(handle_type fd) { this->~file(); m_fd = fd; } void import(handle_type fd) { this->~file(); m_fd = fd; }
bool open(const std::string& filename, u32 mode = o_read); bool open(const std::string& filename, u32 mode = fom::read);
bool is_opened() const { return m_fd != null; } bool is_opened() const { return m_fd != null; }
bool trunc(u64 size) const; // change file size (possibly appending zero bytes) bool trunc(u64 size) const; // change file size (possibly appending zero bytes)
bool stat(stat_t& info) const; // get file info bool stat(stat_t& info) const; // get file info
@ -73,7 +94,7 @@ namespace fs
u64 read(void* buffer, u64 count) const; u64 read(void* buffer, u64 count) const;
u64 write(const void* buffer, u64 count) const; u64 write(const void* buffer, u64 count) const;
u64 seek(u64 offset, u32 mode = from_begin) const; u64 seek(s64 offset, fsm seek_mode = fsm::begin) const;
u64 size() const; u64 size() const;
}; };

View file

@ -94,7 +94,7 @@ struct FileListener : LogListener
bool mPrependChannelName; bool mPrependChannelName;
FileListener(const std::string& name = _PRGNAME_, bool prependChannel = true) FileListener(const std::string& name = _PRGNAME_, bool prependChannel = true)
: mFile(rPlatform::getConfigDir() + name + ".log", o_write | o_create | o_trunc) : mFile(rPlatform::getConfigDir() + name + ".log", fom::write | fom::create | fom::trunc)
, mPrependChannelName(prependChannel) , mPrependChannelName(prependChannel)
{ {
if (!mFile) if (!mFile)

View file

@ -815,7 +815,7 @@ int DecryptEDAT(const std::string& input_file_name, const std::string& output_fi
{ {
// Prepare the files. // Prepare the files.
fs::file input(input_file_name); fs::file input(input_file_name);
fs::file output(output_file_name, o_write | o_create | o_trunc); fs::file output(output_file_name, fom::write | fom::create | fom::trunc);
fs::file rap(rap_file_name); fs::file rap(rap_file_name);
// Set keys (RIF and DEVKLIC). // Set keys (RIF and DEVKLIC).

View file

@ -69,7 +69,7 @@ static bool CheckHeader(const fs::file& pkg_f, PKGHeader& header)
bool Unpack(const fs::file& pkg_f, std::string dir) bool Unpack(const fs::file& pkg_f, std::string dir)
{ {
// Save current file offset (probably zero) // Save current file offset (probably zero)
const u64 start_offset = pkg_f.seek(0, from_cur); const u64 start_offset = pkg_f.seek(0, fsm::cur);
// Get basic PKG information // Get basic PKG information
PKGHeader header; PKGHeader header;
@ -186,7 +186,7 @@ bool Unpack(const fs::file& pkg_f, std::string dir)
LOG_WARNING(LOADER, "PKG Loader: '%s' is overwritten", path); LOG_WARNING(LOADER, "PKG Loader: '%s' is overwritten", path);
} }
if (fs::file out{ path, o_write | o_create | o_trunc }) if (fs::file out{ path, fom::write | fom::create | fom::trunc })
{ {
for (u64 pos = 0; pos < entry.file_size; pos += BUF_SIZE) for (u64 pos = 0; pos < entry.file_size; pos += BUF_SIZE)
{ {

View file

@ -1076,7 +1076,7 @@ bool SELFDecrypter::DecryptData()
bool SELFDecrypter::MakeElf(const std::string& elf, bool isElf32) bool SELFDecrypter::MakeElf(const std::string& elf, bool isElf32)
{ {
// Create a new ELF file. // Create a new ELF file.
fs::file e(elf, o_write | o_create | o_trunc); fs::file e(elf, fom::write | fom::create | fom::trunc);
if(!e) if(!e)
{ {
LOG_ERROR(LOADER, "Could not create ELF file! (%s)", elf.c_str()); LOG_ERROR(LOADER, "Could not create ELF file! (%s)", elf.c_str());
@ -1283,7 +1283,7 @@ bool CheckDebugSelf(const std::string& self, const std::string& elf)
s.seek(elf_offset); s.seek(elf_offset);
// Write the real ELF file back. // Write the real ELF file back.
fs::file e(elf, o_write | o_create | o_trunc); fs::file e(elf, fom::write | fom::create | fom::trunc);
if(!e) if(!e)
{ {
LOG_ERROR(LOADER, "Could not create ELF file! (%s)", elf.c_str()); LOG_ERROR(LOADER, "Could not create ELF file! (%s)", elf.c_str());

View file

@ -12,7 +12,7 @@ AudioDumper::~AudioDumper()
bool AudioDumper::Init(u8 ch) bool AudioDumper::Init(u8 ch)
{ {
if ((m_init = m_output.open("audio.wav", o_write | o_create | o_trunc))) if ((m_init = m_output.open("audio.wav", fom::write | fom::create | fom::trunc)))
{ {
m_header = WAVHeader(ch); m_header = WAVHeader(ch);
WriteHeader(); WriteHeader();

View file

@ -160,7 +160,7 @@ void SPURecompilerCore::Compile(u16 pos)
// entry[start].count, excess, stamp1 - stamp0, time0, get_system_time() - stamp1); // entry[start].count, excess, stamp1 - stamp0, time0, get_system_time() - stamp1);
//} //}
//fs::file(fmt::Format("SPUjit_%d.log", this->CPU.GetId()), o_write | o_create | (first ? o_trunc : o_append)).write(log.c_str(), log.size()); //fs::file(fmt::Format("SPUjit_%d.log", this->CPU.GetId()), fom::write | fom::create | (first ? fom::trunc : fom::append)).write(log.c_str(), log.size());
m_enc->compiler = nullptr; m_enc->compiler = nullptr;
first = false; first = false;

View file

@ -48,7 +48,7 @@ u64 vfsFile::Read(void* dst, u64 size)
return m_stream->Read(dst, size); return m_stream->Read(dst, size);
} }
u64 vfsFile::Seek(s64 offset, u32 mode) u64 vfsFile::Seek(s64 offset, fsm mode)
{ {
return m_stream->Seek(offset, mode); return m_stream->Seek(offset, mode);
} }

View file

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "vfsFileBase.h" #include "vfsFileBase.h"
class vfsFile : public vfsFileBase class vfsFile : public vfsFileBase
@ -8,9 +9,9 @@ private:
public: public:
vfsFile(); vfsFile();
vfsFile(const std::string& path, u32 mode = vfsRead); vfsFile(const std::string& path, u32 mode = fom::read);
virtual bool Open(const std::string& path, u32 mode = vfsRead) override; virtual bool Open(const std::string& path, u32 mode = fom::read) override;
virtual bool Close() override; virtual bool Close() override;
virtual u64 GetSize() const override; virtual u64 GetSize() const override;
@ -18,7 +19,7 @@ public:
virtual u64 Write(const void* src, u64 size) override; virtual u64 Write(const void* src, u64 size) override;
virtual u64 Read(void* dst, u64 size) override; virtual u64 Read(void* dst, u64 size) override;
virtual u64 Seek(s64 offset, u32 mode = from_begin) override; virtual u64 Seek(s64 offset, fsm seek_mode = fsm::begin) override;
virtual u64 Tell() const override; virtual u64 Tell() const override;
virtual bool IsOpened() const override; virtual bool IsOpened() const override;

View file

@ -1,13 +1,6 @@
#pragma once #pragma once
#include "vfsStream.h"
enum vfsOpenMode : u32 #include "vfsStream.h"
{
vfsRead = o_read,
vfsReadWrite = o_read | o_write,
vfsWriteNew = o_write | o_create | o_trunc,
vfsWriteExcl = o_write | o_create | o_excl,
};
class vfsDevice; class vfsDevice;

View file

@ -33,14 +33,14 @@ u64 vfsLocalFile::Read(void* dst, u64 size)
return m_file.read(dst, size); return m_file.read(dst, size);
} }
u64 vfsLocalFile::Seek(s64 offset, u32 mode) u64 vfsLocalFile::Seek(s64 offset, fsm mode)
{ {
return m_file.seek(offset, mode); return m_file.seek(offset, mode);
} }
u64 vfsLocalFile::Tell() const u64 vfsLocalFile::Tell() const
{ {
return m_file.seek(0, from_cur); return m_file.seek(0, fsm::cur);
} }
bool vfsLocalFile::IsOpened() const bool vfsLocalFile::IsOpened() const

View file

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "vfsFileBase.h" #include "vfsFileBase.h"
class vfsLocalFile : public vfsFileBase class vfsLocalFile : public vfsFileBase
@ -9,7 +10,7 @@ private:
public: public:
vfsLocalFile(vfsDevice* device); vfsLocalFile(vfsDevice* device);
virtual bool Open(const std::string& path, u32 mode = vfsRead) override; virtual bool Open(const std::string& path, u32 mode = fom::read) override;
virtual bool Close() override; virtual bool Close() override;
virtual u64 GetSize() const override; virtual u64 GetSize() const override;
@ -17,7 +18,7 @@ public:
virtual u64 Write(const void* src, u64 size) override; virtual u64 Write(const void* src, u64 size) override;
virtual u64 Read(void* dst, u64 size) override; virtual u64 Read(void* dst, u64 size) override;
virtual u64 Seek(s64 offset, u32 mode = from_begin) override; virtual u64 Seek(s64 offset, fsm seek_mode = fsm::begin) override;
virtual u64 Tell() const override; virtual u64 Tell() const override;
virtual bool IsOpened() const override; virtual bool IsOpened() const override;

View file

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "Utilities/File.h" #include "Utilities/File.h"
struct vfsStream struct vfsStream
@ -31,7 +32,7 @@ struct vfsStream
return Read(&data, count) == count; return Read(&data, count) == count;
} }
virtual u64 Seek(s64 offset, u32 mode = from_begin) = 0; virtual u64 Seek(s64 offset, fsm seek_mode = fsm::begin) = 0;
virtual u64 Tell() const = 0; virtual u64 Tell() const = 0;

View file

@ -31,18 +31,16 @@ public:
virtual u64 Read(void* dst, u64 count) override; virtual u64 Read(void* dst, u64 count) override;
virtual u64 Seek(s64 offset, u32 mode = from_begin) override virtual u64 Seek(s64 offset, fsm seek_mode = fsm::begin) override
{ {
assert(mode < 3); switch (seek_mode)
switch (mode)
{ {
case from_begin: return m_pos = offset; case fsm::begin: return m_pos = offset;
case from_cur: return m_pos += offset; case fsm::cur: return m_pos += offset;
case from_end: return m_pos = m_size + offset; case fsm::end: return m_pos = m_size + offset;
} }
return m_pos; throw EXCEPTION("Unknown seek_mode (0x%x)", seek_mode);
} }
virtual u64 Tell() const override virtual u64 Tell() const override

View file

@ -15,13 +15,13 @@ void vfsHDDManager::CreateEntry(vfsHDD_Entry& entry)
entry.atime = ctime; entry.atime = ctime;
entry.ctime = ctime; entry.ctime = ctime;
entry.mtime = ctime; entry.mtime = ctime;
entry.access = vfsReadWrite; entry.access = 0666;
CreateBlock(entry); CreateBlock(entry);
} }
void vfsHDDManager::CreateHDD(const std::string& path, u64 size, u64 block_size) void vfsHDDManager::CreateHDD(const std::string& path, u64 size, u64 block_size)
{ {
fs::file f(path, o_write | o_create | o_trunc); fs::file f(path, fom::write | fom::create | fom::trunc);
static const u64 cur_dir_block = 1; static const u64 cur_dir_block = 1;
@ -353,7 +353,7 @@ vfsHDD::vfsHDD(vfsDevice* device, const std::string& hdd_path)
, m_hdd_path(hdd_path) , m_hdd_path(hdd_path)
, vfsFileBase(device) , vfsFileBase(device)
{ {
m_hdd_file.Open(hdd_path, vfsReadWrite); m_hdd_file.Open(hdd_path, fom::read | fom::write);
m_hdd_file.Read(&m_hdd_info, sizeof(vfsHDD_Hdr)); m_hdd_file.Read(&m_hdd_info, sizeof(vfsHDD_Hdr));
m_cur_dir_block = m_hdd_info.next_block; m_cur_dir_block = m_hdd_info.next_block;
if (!m_hdd_info.block_size) if (!m_hdd_info.block_size)
@ -747,16 +747,16 @@ u64 vfsHDD::Read(void* dst, u64 size)
return m_file.Read(dst, size); // ??? return m_file.Read(dst, size); // ???
} }
u64 vfsHDD::Seek(s64 offset, u32 mode) u64 vfsHDD::Seek(s64 offset, fsm seek_mode)
{ {
switch (mode) switch (seek_mode)
{ {
case from_begin: return m_file.Seek(offset); case fsm::begin: return m_file.Seek(offset);
case from_cur: return m_file.Seek(Tell() + offset); case fsm::cur: return m_file.Seek(Tell() + offset);
case from_end: return m_file.Seek(m_file.GetSize() + offset); case fsm::end: return m_file.Seek(m_file.GetSize() + offset);
} }
return m_file.Tell(); // ??? throw EXCEPTION("Unknown seek_mode(0x%x)", seek_mode);
} }
u64 vfsHDD::Tell() const u64 vfsHDD::Tell() const

View file

@ -182,7 +182,7 @@ public:
bool GetNextEntry(u64& block, vfsHDD_Entry& entry, std::string& name); bool GetNextEntry(u64& block, vfsHDD_Entry& entry, std::string& name);
virtual bool Open(const std::string& path, u32 mode = vfsRead) override; virtual bool Open(const std::string& path, u32 mode = fom::read) override;
bool HasEntry(const std::string& name); bool HasEntry(const std::string& name);
@ -196,7 +196,7 @@ public:
virtual u64 Read(void* dst, u64 count) override; virtual u64 Read(void* dst, u64 count) override;
virtual u64 Seek(s64 offset, u32 mode = from_begin) override; virtual u64 Seek(s64 offset, fsm seek_mode = fsm::begin) override;
virtual u64 Tell() const override; virtual u64 Tell() const override;

View file

@ -2,7 +2,7 @@
#define ID_MANAGER_INCLUDED #define ID_MANAGER_INCLUDED
// default specialization for all types // default traits for any arbitrary type
template<typename T> struct id_traits template<typename T> struct id_traits
{ {
// get next mapped id (may return 0 if out of IDs) // get next mapped id (may return 0 if out of IDs)

View file

@ -580,7 +580,7 @@ void GLTexture::Save(RSXTexture& tex, const std::string& name)
return; return;
} }
fs::file(name + ".raw", o_write | o_create | o_trunc).write(alldata, texPixelCount * 4); fs::file(name + ".raw", fom::write | fom::create | fom::trunc).write(alldata, texPixelCount * 4);
u8* data = new u8[texPixelCount * 3]; u8* data = new u8[texPixelCount * 3];
u8* alpha = new u8[texPixelCount]; u8* alpha = new u8[texPixelCount];

View file

@ -19,7 +19,7 @@ struct GLTraits
//checkForGlError("m_fragment_prog.Compile"); //checkForGlError("m_fragment_prog.Compile");
// TODO: This shouldn't use current dir // TODO: This shouldn't use current dir
fs::file("./FragmentProgram.txt", o_write | o_create | o_trunc).write(fragmentProgramData.shader.c_str(), fragmentProgramData.shader.size()); fs::file("./FragmentProgram.txt", fom::write | fom::create | fom::trunc).write(fragmentProgramData.shader.c_str(), fragmentProgramData.shader.size());
} }
static static
@ -30,7 +30,7 @@ struct GLTraits
//checkForGlError("m_vertex_prog.Compile"); //checkForGlError("m_vertex_prog.Compile");
// TODO: This shouldn't use current dir // TODO: This shouldn't use current dir
fs::file("./VertexProgram.txt", o_write | o_create | o_trunc).write(vertexProgramData.shader.c_str(), vertexProgramData.shader.size()); fs::file("./VertexProgram.txt", fom::write | fom::create | fom::trunc).write(vertexProgramData.shader.c_str(), vertexProgramData.shader.size());
} }
static static

View file

@ -752,8 +752,8 @@ bool sdata_check(u32 version, u32 flags, u64 filesizeInput, u64 filesizeTmp)
s32 sdata_unpack(const std::string& packed_file, const std::string& unpacked_file) s32 sdata_unpack(const std::string& packed_file, const std::string& unpacked_file)
{ {
std::shared_ptr<vfsFileBase> packed_stream(Emu.GetVFS().OpenFile(packed_file, vfsRead)); std::shared_ptr<vfsFileBase> packed_stream(Emu.GetVFS().OpenFile(packed_file, fom::read));
std::shared_ptr<vfsFileBase> unpacked_stream(Emu.GetVFS().OpenFile(unpacked_file, vfsWriteNew)); std::shared_ptr<vfsFileBase> unpacked_stream(Emu.GetVFS().OpenFile(unpacked_file, fom::write | fom::create | fom::trunc));
if (!packed_stream || !packed_stream->IsOpened()) if (!packed_stream || !packed_stream->IsOpened())
{ {

View file

@ -64,7 +64,7 @@ s32 cellGifDecOpen(PMainHandle mainHandle, PPSubHandle subHandle, PSrc src, POpe
case CELL_GIFDEC_FILE: case CELL_GIFDEC_FILE:
{ {
// Get file descriptor and size // Get file descriptor and size
std::shared_ptr<vfsStream> file_s(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)); std::shared_ptr<vfsStream> file_s(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), fom::read));
if (!file_s) return CELL_GIFDEC_ERROR_OPEN_FILE; if (!file_s) return CELL_GIFDEC_ERROR_OPEN_FILE;
current_subHandle.fd = idm::make<lv2_file_t>(file_s, 0, 0); current_subHandle.fd = idm::make<lv2_file_t>(file_s, 0, 0);

View file

@ -53,7 +53,7 @@ s32 cellJpgDecOpen(u32 mainHandle, vm::ptr<u32> subHandle, vm::ptr<CellJpgDecSrc
case CELL_JPGDEC_FILE: case CELL_JPGDEC_FILE:
{ {
// Get file descriptor and size // Get file descriptor and size
std::shared_ptr<vfsStream> file_s(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)); std::shared_ptr<vfsStream> file_s(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), fom::read));
if (!file_s) return CELL_JPGDEC_ERROR_OPEN_FILE; if (!file_s) return CELL_JPGDEC_ERROR_OPEN_FILE;
current_subHandle.fd = idm::make<lv2_file_t>(file_s, 0, 0); current_subHandle.fd = idm::make<lv2_file_t>(file_s, 0, 0);

View file

@ -98,7 +98,7 @@ s32 pngDecOpen(PMainHandle dec, PPSubHandle subHandle, PSrc src, POpenInfo openI
case CELL_PNGDEC_FILE: case CELL_PNGDEC_FILE:
{ {
// Get file descriptor and size // Get file descriptor and size
std::shared_ptr<vfsStream> file_s(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), vfsRead)); std::shared_ptr<vfsStream> file_s(Emu.GetVFS().OpenFile(src->fileName.get_ptr(), fom::read));
if (!file_s) return CELL_PNGDEC_ERROR_OPEN_FILE; if (!file_s) return CELL_PNGDEC_ERROR_OPEN_FILE;
stream->fd = idm::make<lv2_file_t>(file_s, 0, 0); stream->fd = idm::make<lv2_file_t>(file_s, 0, 0);

View file

@ -561,7 +561,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
{ {
case CELL_SAVEDATA_FILEOP_READ: case CELL_SAVEDATA_FILEOP_READ:
{ {
fs::file file(local_path, o_read); fs::file file(local_path, fom::read);
file.seek(fileSet->fileOffset); file.seek(fileSet->fileOffset);
fileGet->excSize = static_cast<u32>(file.read(fileSet->fileBuf.get_ptr(), std::min<u32>(fileSet->fileSize, fileSet->fileBufSize))); fileGet->excSize = static_cast<u32>(file.read(fileSet->fileBuf.get_ptr(), std::min<u32>(fileSet->fileSize, fileSet->fileBufSize)));
break; break;
@ -569,10 +569,10 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
case CELL_SAVEDATA_FILEOP_WRITE: case CELL_SAVEDATA_FILEOP_WRITE:
{ {
fs::file file(local_path, o_write | o_create); fs::file file(local_path, fom::write | fom::create);
file.seek(fileSet->fileOffset); file.seek(fileSet->fileOffset);
fileGet->excSize = static_cast<u32>(file.write(fileSet->fileBuf.get_ptr(), std::min<u32>(fileSet->fileSize, fileSet->fileBufSize))); fileGet->excSize = static_cast<u32>(file.write(fileSet->fileBuf.get_ptr(), std::min<u32>(fileSet->fileSize, fileSet->fileBufSize)));
file.trunc(file.seek(0, from_cur)); // truncate file.trunc(file.seek(0, fsm::cur)); // truncate
break; break;
} }
@ -585,7 +585,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
case CELL_SAVEDATA_FILEOP_WRITE_NOTRUNC: case CELL_SAVEDATA_FILEOP_WRITE_NOTRUNC:
{ {
fs::file file(local_path, o_write | o_create); fs::file file(local_path, fom::write | fom::create);
file.seek(fileSet->fileOffset); file.seek(fileSet->fileOffset);
fileGet->excSize = static_cast<u32>(file.write(fileSet->fileBuf.get_ptr(), std::min<u32>(fileSet->fileSize, fileSet->fileBufSize))); fileGet->excSize = static_cast<u32>(file.write(fileSet->fileBuf.get_ptr(), std::min<u32>(fileSet->fileSize, fileSet->fileBufSize)));
break; break;
@ -602,7 +602,7 @@ never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cpt
// Write PARAM.SFO // Write PARAM.SFO
if (psf) if (psf)
{ {
vfsFile f(sfo_path, vfsWriteNew); vfsFile f(sfo_path, fom::write | fom::create | fom::trunc);
psf.Save(f); psf.Save(f);
} }

View file

@ -22,7 +22,7 @@ s32 cellUserInfoGetStat(u32 id, vm::ptr<CellUserInfoUserStat> stat)
return CELL_USERINFO_ERROR_NOUSER; return CELL_USERINFO_ERROR_NOUSER;
sprintf(path, "/dev_hdd0/home/%08d/localusername", id); sprintf(path, "/dev_hdd0/home/%08d/localusername", id);
vfsStream* stream = Emu.GetVFS().OpenFile(path, vfsRead); vfsStream* stream = Emu.GetVFS().OpenFile(path, fom::read);
if (!stream || !(stream->IsOpened())) if (!stream || !(stream->IsOpened()))
return CELL_USERINFO_ERROR_INTERNAL; return CELL_USERINFO_ERROR_INTERNAL;

View file

@ -103,7 +103,7 @@ s32 sceNpTrophyCreateContext(vm::ptr<u32> context, vm::cptr<SceNpCommunicationId
std::string name = fmt::format("%s_%02d", commId->data, commId->num); std::string name = fmt::format("%s_%02d", commId->data, commId->num);
// open trophy pack file // open trophy pack file
std::unique_ptr<vfsStream> stream(Emu.GetVFS().OpenFile("/app_home/../TROPDIR/" + name + "/TROPHY.TRP", vfsRead)); std::unique_ptr<vfsStream> stream(Emu.GetVFS().OpenFile("/app_home/../TROPDIR/" + name + "/TROPHY.TRP", fom::read));
// check if exists and opened // check if exists and opened
if (!stream || !stream->IsOpened()) if (!stream || !stream->IsOpened())

View file

@ -53,31 +53,31 @@ s32 sys_fs_open(vm::cptr<char> path, s32 flags, vm::ptr<u32> fd, s32 mode, vm::c
switch (flags & CELL_FS_O_ACCMODE) switch (flags & CELL_FS_O_ACCMODE)
{ {
case CELL_FS_O_RDONLY: open_mode |= o_read; break; case CELL_FS_O_RDONLY: open_mode |= fom::read; break;
case CELL_FS_O_WRONLY: open_mode |= o_write; break; case CELL_FS_O_WRONLY: open_mode |= fom::write; break;
case CELL_FS_O_RDWR: open_mode |= o_read | o_write; break; case CELL_FS_O_RDWR: open_mode |= fom::read | fom::write; break;
} }
if (flags & CELL_FS_O_CREAT) if (flags & CELL_FS_O_CREAT)
{ {
open_mode |= o_create; open_mode |= fom::create;
} }
if (flags & CELL_FS_O_TRUNC) if (flags & CELL_FS_O_TRUNC)
{ {
open_mode |= o_trunc; open_mode |= fom::trunc;
} }
if (flags & CELL_FS_O_APPEND) if (flags & CELL_FS_O_APPEND)
{ {
open_mode |= o_append; open_mode |= fom::append;
} }
if (flags & CELL_FS_O_EXCL) if (flags & CELL_FS_O_EXCL)
{ {
if (flags & CELL_FS_O_CREAT) if (flags & CELL_FS_O_CREAT)
{ {
open_mode |= o_excl; open_mode |= fom::excl;
} }
else else
{ {
@ -106,7 +106,7 @@ s32 sys_fs_open(vm::cptr<char> path, s32 flags, vm::ptr<u32> fd, s32 mode, vm::c
{ {
sys_fs.Error("sys_fs_open('%s'): failed to open file (flags=%#o, mode=%#o)", path.get_ptr(), flags, mode); sys_fs.Error("sys_fs_open('%s'): failed to open file (flags=%#o, mode=%#o)", path.get_ptr(), flags, mode);
if (open_mode & o_excl) if (open_mode & fom::excl)
{ {
return CELL_FS_EEXIST; // approximation return CELL_FS_EEXIST; // approximation
} }
@ -430,7 +430,7 @@ s32 sys_fs_lseek(u32 fd, s64 offset, s32 whence, vm::ptr<u64> pos)
std::lock_guard<std::mutex> lock(file->mutex); std::lock_guard<std::mutex> lock(file->mutex);
*pos = file->file->Seek(offset, whence); *pos = file->file->Seek(offset, (fsm)whence);
return CELL_OK; return CELL_OK;
} }

View file

@ -96,7 +96,7 @@ void AutoPauseManagerDialog::LoadEntries(void)
//This would always use a 0xFFFFFFFF as end of the pause.bin //This would always use a 0xFFFFFFFF as end of the pause.bin
void AutoPauseManagerDialog::SaveEntries(void) void AutoPauseManagerDialog::SaveEntries(void)
{ {
fs::file list("pause.bin", o_write | o_create | o_trunc); fs::file list("pause.bin", fom::write | fom::create | fom::trunc);
//System calls ID and Function calls ID are all u32 iirc. //System calls ID and Function calls ID are all u32 iirc.
u32 num = 0; u32 num = 0;
list.seek(0); list.seek(0);

View file

@ -245,7 +245,7 @@ void MainFrame::InstallPkg(wxCommandEvent& WXUNUSED(event))
Emu.Stop(); Emu.Stop();
// Open and install PKG file // Open and install PKG file
fs::file pkg_f(ctrl.GetPath().ToStdString(), o_read); fs::file pkg_f(ctrl.GetPath().ToStdString(), fom::read);
if (pkg_f) if (pkg_f)
{ {

View file

@ -122,7 +122,7 @@ void VHDDExplorer::Import(const std::string& path, const std::string& to)
return; return;
} }
if(!m_hdd->Open(to, o_write)) if(!m_hdd->Open(to, fom::write))
{ {
wxMessageBox("IMPORT ERROR: file open error."); wxMessageBox("IMPORT ERROR: file open error.");
return; return;

View file

@ -29,7 +29,7 @@ bool TROPUSRLoader::Load(const std::string& filepath, const std::string& configp
Generate(filepath, configpath); Generate(filepath, configpath);
} }
m_file = Emu.GetVFS().OpenFile(filepath, vfsRead); m_file = Emu.GetVFS().OpenFile(filepath, fom::read);
LoadHeader(); LoadHeader();
LoadTableHeaders(); LoadTableHeaders();
LoadTables(); LoadTables();
@ -124,7 +124,7 @@ bool TROPUSRLoader::Save(const std::string& filepath)
Close(); Close();
} }
m_file = Emu.GetVFS().OpenFile(filepath, vfsWriteNew); m_file = Emu.GetVFS().OpenFile(filepath, fom::write | fom::create | fom::trunc);
m_file->Write(&m_header, sizeof(TROPUSRHeader)); m_file->Write(&m_header, sizeof(TROPUSRHeader));
for (const TROPUSRTableHeader& tableHeader : m_tableHeaders) for (const TROPUSRTableHeader& tableHeader : m_tableHeaders)

View file

@ -36,7 +36,7 @@ bool TRPLoader::Install(std::string dest, bool show)
char* buffer = new char [(u32)entry.size]; char* buffer = new char [(u32)entry.size];
trp_f.Seek(entry.offset); trp_f.Seek(entry.offset);
trp_f.Read(buffer, entry.size); trp_f.Read(buffer, entry.size);
vfsFile(dest + entry.name, vfsWriteNew).Write(buffer, entry.size); vfsFile(dest + entry.name, fom::write | fom::create | fom::trunc).Write(buffer, entry.size);
delete[] buffer; delete[] buffer;
} }