Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fs: Switch to c++ filesystem #1912

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion vita3k/config/include/config/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ struct Config : YamlLoader {

// Load a function to the node network, and then update the members
void load_new_config(const fs::path &path) override {
yaml_node = YAML::LoadFile(path.generic_path().string());
yaml_node = YAML::LoadFile(path.generic_string());
update_members();
}

Expand Down
4 changes: 2 additions & 2 deletions vita3k/config/include/config/yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct YamlLoader {
}

explicit YamlLoader(const fs::path &path) {
yaml_node = YAML::LoadFile(path.generic_path().string());
yaml_node = YAML::LoadFile(path.generic_string());
}

virtual ~YamlLoader() = default;
Expand All @@ -55,7 +55,7 @@ struct YamlLoader {
}

virtual void load_new_config(const fs::path &path) {
yaml_node = YAML::LoadFile(path.generic_path().string());
yaml_node = YAML::LoadFile(path.generic_string());
}

// Check if a node index exists, and return the current value in the node network
Expand Down
2 changes: 1 addition & 1 deletion vita3k/config/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static std::set<std::string> get_file_set(const fs::path &loc, bool dirs_only =
cur_set.insert(it->path().stem().string());
}

boost::system::error_code err{};
fs::error_code err{};
it.increment(err);
}
return cur_set;
Expand Down
12 changes: 6 additions & 6 deletions vita3k/gui/src/content_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ auto get_recursive_directory_size(const T &path) {
return acc + fs::file_size(app.path());
return acc;
};
return boost::accumulate(path_list, boost::uintmax_t{}, pred);
return std::accumulate(fs::begin(path_list), fs::end(path_list), uintmax_t{}, pred);
}
} // namespace

Expand All @@ -53,7 +53,7 @@ void get_app_info(GuiState &gui, EmuEnvState &emuenv, const std::string &app_pat
auto lang = gui.lang.app_context;
gui.app_selector.app_info.trophy = fs::exists(APP_PATH / "sce_sys/trophy") ? lang["eligible"] : lang["ineligible"];

const auto last_writen = fs::last_write_time(APP_PATH);
const auto last_writen = fs_utils::last_write_time(APP_PATH);
SAFE_LOCALTIME(&last_writen, &gui.app_selector.app_info.updated);
}
}
Expand Down Expand Up @@ -106,7 +106,7 @@ static void get_save_data_list(GuiState &gui, EmuEnvState &emuenv) {
if (fs::is_directory(save.path()) && !fs::is_empty(save.path()) && get_app_index(gui, title_id) != gui.app_selector.user_apps.end()) {
tm updated_tm = {};

const auto last_writen = fs::last_write_time(save);
const auto last_writen = fs_utils::last_write_time(save);
SAFE_LOCALTIME(&last_writen, &updated_tm);

const auto size = get_recursive_directory_size(save);
Expand Down Expand Up @@ -147,9 +147,9 @@ void init_content_manager(GuiState &gui, EmuEnvState &emuenv) {
const auto &directory_list = fs::directory_iterator(THEME_PATH);
const auto pred = [&](const auto acc, const auto &) { return acc + get_recursive_directory_size(THEME_PATH); };
if (fs::exists(THEME_PATH) && !fs::is_empty(THEME_PATH)) {
return boost::accumulate(directory_list, boost::uintmax_t{}, pred);
return std::accumulate(fs::begin(directory_list), fs::end(directory_list), uintmax_t{}, pred);
}
return boost::uintmax_t{};
return uintmax_t{};
};

const auto get_list_size_or_dash = [](const auto query) {
Expand Down Expand Up @@ -208,7 +208,7 @@ static void get_content_info(GuiState &gui, EmuEnvState &emuenv) {

tm updated_tm = {};

const auto last_writen = fs::last_write_time(addcont);
const auto last_writen = fs_utils::last_write_time(addcont);
SAFE_LOCALTIME(&last_writen, &addcont_info[content_id].date);

const auto addcont_size = get_recursive_directory_size(addcont);
Expand Down
2 changes: 1 addition & 1 deletion vita3k/gui/src/firmware_install_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool delete_pup_file;
nfdchar_t *pup_path;

static void get_firmware_version(EmuEnvState &emuenv) {
fs::ifstream versionFile(emuenv.pref_path + L"/PUP_DEC/PUP/version.txt");
fs::ifstream versionFile(fs::path(emuenv.pref_path) / "PUP_DEC/PUP/version.txt");

if (versionFile.is_open()) {
std::getline(versionFile, fw_version);
Expand Down
3 changes: 1 addition & 2 deletions vita3k/gui/src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,7 @@ void get_user_apps_title(GuiState &gui, EmuEnvState &emuenv) {

gui.app_selector.user_apps.clear();
for (const auto &app : fs::directory_iterator(app_path)) {
if (!app.path().empty() && fs::is_directory(app.path())
&& !app.path().filename_is_dot() && !app.path().filename_is_dot_dot()) {
if (!app.path().empty() && fs::is_directory(app.path())) {
const auto app_path = app.path().stem().generic_string();
get_app_param(gui, emuenv, app_path);
}
Expand Down
2 changes: 1 addition & 1 deletion vita3k/gui/src/pkg_install_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void draw_pkg_install_dialog(GuiState &gui, EmuEnvState &emuenv) {
} else if (state == "work") {
result = NFD_OpenDialog("bin", nullptr, &work_path);
if (result == NFD_OKAY) {
fs::ifstream binfile(string_utils::utf_to_wide(std::string(work_path)), std::ios::in | std::ios::binary | std::ios::ate);
fs::ifstream binfile(fs::path(string_utils::utf_to_wide(std::string(work_path))), std::ios::in | std::ios::binary | std::ios::ate);
zRIF = rif2zrif(binfile);
state = "install";
} else
Expand Down
4 changes: 2 additions & 2 deletions vita3k/gui/src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ static void get_themes_list(GuiState &gui, EmuEnvState &emuenv) {
return acc;
};
const auto theme_content_ids = fs::recursive_directory_iterator(theme_path / content_id_wstr);
const auto theme_size = std::accumulate(fs::begin(theme_content_ids), fs::end(theme_content_ids), boost::uintmax_t{}, pred);
const auto theme_size = std::accumulate(fs::begin(theme_content_ids), fs::end(theme_content_ids), uintmax_t{}, pred);

const auto updated = fs::last_write_time(theme_path / content_id_wstr);
const auto updated = fs_utils::last_write_time(theme_path / content_id_wstr);
SAFE_LOCALTIME(&updated, &themes_info[content_id].updated);

themes_info[content_id].size = theme_size / KiB(1);
Expand Down
2 changes: 1 addition & 1 deletion vita3k/gui/src/trophy_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void init_trophy_collection(GuiState &gui, EmuEnvState &emuenv) {
}

// Read Updated time
const auto updated = fs::last_write_time(trophy_data_np_com_id_path / "TROPUSR.DAT");
const auto updated = fs_utils::last_write_time(trophy_data_np_com_id_path / "TROPUSR.DAT");
SAFE_LOCALTIME(&updated, &np_com_id_info[np_com_id].updated);

// Open trophy progress file
Expand Down
2 changes: 1 addition & 1 deletion vita3k/gui/src/user_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void get_users_list(GuiState &gui, EmuEnvState &emuenv) {
if (fs::exists(user_path) && !fs::is_empty(user_path)) {
for (const auto &path : fs::directory_iterator(user_path)) {
pugi::xml_document user_xml;
if (fs::is_directory(path) && user_xml.load_file(((path / "user.xml").c_str()))) {
if (fs::is_directory(path) && user_xml.load_file(((path.path() / "user.xml").c_str()))) {
const auto user_child = user_xml.child("user");

// Load user id
Expand Down
10 changes: 5 additions & 5 deletions vita3k/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ bool install_archive_content(EmuEnvState &emuenv, GuiState *gui, const fs::path
if (!fs::exists(file_output.parent_path()))
fs::create_directories(file_output.parent_path());

LOG_INFO("Extracting {}", file_output.generic_path().string());
mz_zip_reader_extract_to_file(zip.get(), i, file_output.generic_path().string().c_str(), 0);
LOG_INFO("Extracting {}", file_output.generic_string());
mz_zip_reader_extract_to_file(zip.get(), i, file_output.generic_string().c_str(), 0);
}
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ static std::vector<std::string> get_archive_contents_path(const ZipPtr &zip) {

std::vector<ContentInfo> install_archive(EmuEnvState &emuenv, GuiState *gui, const fs::path &archive_path, const std::function<void(ArchiveContents)> &progress_callback) {
if (!fs::exists(archive_path)) {
LOG_CRITICAL("Failed to load archive file in path: {}", archive_path.generic_path().string());
LOG_CRITICAL("Failed to load archive file in path: {}", archive_path.generic_string());
return {};
}
const ZipPtr zip(new mz_zip_archive, delete_zip);
Expand All @@ -282,9 +282,9 @@ std::vector<ContentInfo> install_archive(EmuEnvState &emuenv, GuiState *gui, con
FILE *vpk_fp;

#ifdef WIN32
_wfopen_s(&vpk_fp, archive_path.generic_path().wstring().c_str(), L"rb");
_wfopen_s(&vpk_fp, archive_path.generic_wstring().c_str(), L"rb");
#else
vpk_fp = fopen(archive_path.generic_path().string().c_str(), "rb");
vpk_fp = fopen(archive_path.generic_string().c_str(), "rb");
#endif

if (!mz_zip_reader_init_cfile(zip.get(), vpk_fp, 0, 0)) {
Expand Down
3 changes: 3 additions & 0 deletions vita3k/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <util/exit_code.h>
#include <util/fs.h>

// functional must be included after filesystem in visual studio (https://github.com/microsoft/terminal/issues/435)
#include <functional>

#include <miniz.h>

struct GuiState;
Expand Down
8 changes: 4 additions & 4 deletions vita3k/io/include/io/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ typedef std::shared_ptr<FILE> FilePtr;
const wchar_t *translate_open_mode(const int flags);

inline FilePtr create_shared_file(const fs::path &path, const int open_mode) {
const auto file = _wfopen(path.generic_path().wstring().c_str(), translate_open_mode(open_mode));
const auto file = _wfopen(path.generic_wstring().c_str(), translate_open_mode(open_mode));
return file ? FilePtr(file, std::fclose) : FilePtr();
}

typedef std::shared_ptr<_WDIR> DirPtr;

inline DirPtr create_shared_dir(const fs::path &path) {
return DirPtr(_wopendir(path.generic_path().wstring().c_str()), _wclosedir);
return DirPtr(_wopendir(path.generic_wstring().c_str()), _wclosedir);
}

inline std::string get_file_in_dir(const _wdirent *file) {
Expand All @@ -56,14 +56,14 @@ inline _wdirent *get_system_dir_ptr(const DirPtr &dir) {
const char *translate_open_mode(const int flags);

inline FilePtr create_shared_file(const fs::path &path, const int open_mode) {
const auto file = fopen(path.generic_path().string().c_str(), translate_open_mode(open_mode));
const auto file = fopen(path.generic_string().c_str(), translate_open_mode(open_mode));
return file ? FilePtr(file, std::fclose) : FilePtr();
}

typedef std::shared_ptr<DIR> DirPtr;

inline DirPtr create_shared_dir(const fs::path &path) {
return DirPtr(opendir(path.generic_path().string().c_str()), closedir);
return DirPtr(opendir(path.generic_string().c_str()), closedir);
}

inline std::string get_file_in_dir(dirent *file) {
Expand Down
4 changes: 2 additions & 2 deletions vita3k/io/include/io/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ class VitaStats {
// Overloaded functions for separate systems
#ifdef WIN32
const wchar_t *get_char_path() const {
return file_info.sys_loc.generic_path().wstring().c_str();
return file_info.sys_loc.c_str();
}
#else
const char *get_char_path() const {
return file_info.sys_loc.generic_path().string().c_str();
return file_info.sys_loc.c_str();
}
#endif

Expand Down
18 changes: 9 additions & 9 deletions vita3k/io/src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ constexpr bool log_file_stat = false;
namespace vfs {

bool read_file(const VitaIoDevice device, FileBuffer &buf, const std::wstring &pref_path, const fs::path &vfs_file_path) {
const auto host_file_path = device::construct_emulated_path(device, vfs_file_path, pref_path).generic_path();
const auto host_file_path = device::construct_emulated_path(device, vfs_file_path, pref_path);

fs::ifstream f{ host_file_path, fs::ifstream::binary };
if (!f)
Expand Down Expand Up @@ -522,11 +522,11 @@ int stat_file(IOState &io, const char *file, SceIoStat *statp, const std::wstrin

#ifdef _WIN32
struct _stati64 sb;
if (_wstati64(file_path.generic_path().wstring().c_str(), &sb) < 0)
if (_wstati64(file_path.c_str(), &sb) < 0)
return IO_ERROR_UNK();
#else
struct stat64 sb;
if (stat64(file_path.generic_path().string().c_str(), &sb) < 0)
if (stat64(file_path.c_str(), &sb) < 0)
return IO_ERROR_UNK();
#endif

Expand Down Expand Up @@ -616,7 +616,7 @@ SceUID open_dir(IOState &io, const char *path, const std::wstring &pref_path, co
auto device_for_icase = device;
const auto translated_path = translate_path(path, device, io.device_paths);

auto dir_path = device::construct_emulated_path(device, translated_path, pref_path, io.redirect_stdio) / "/";
auto dir_path = device::construct_emulated_path(device, translated_path, pref_path, io.redirect_stdio);
if (!fs::exists(dir_path)) {
if (io.case_isens_find_enabled) {
// Attempt a case-insensitive file search.
Expand Down Expand Up @@ -675,9 +675,8 @@ SceUID read_dir(IOState &io, const SceUID fd, SceIoDirent *dent, const std::wstr

const auto d_name_utf8 = get_file_in_dir(d);
strncpy(dent->d_name, d_name_utf8.c_str(), sizeof(dent->d_name));

const auto cur_path = dir->second.get_system_location() / d_name_utf8;
if (!(cur_path.filename_is_dot() || cur_path.filename_is_dot_dot())) {
if (d_name_utf8 != "." && d_name_utf8 != "..") {
const auto cur_path = dir->second.get_system_location() / d_name_utf8;
const auto file_path = std::string(dir->second.get_vita_loc()) + '/' + d_name_utf8;

LOG_TRACE_IF(log_file_op, "{}: Reading entry {} of fd: {}", export_name, file_path, log_hex(fd));
Expand All @@ -686,6 +685,7 @@ SceUID read_dir(IOState &io, const SceUID fd, SceIoDirent *dent, const std::wstr
else
return 1; // move to the next file
}

return read_dir(io, fd, dent, pref_path, export_name);
}

Expand All @@ -704,7 +704,7 @@ bool copy_directories(const fs::path &src_path, const fs::path &dst_path) {
LOG_INFO("Copy {}", dst_path.string());

if (fs::is_regular_file(src))
fs::copy_file(src, dst_path, fs::copy_option::overwrite_if_exists);
fs_utils::copy_file_overwrite(src, dst_path);
else if (!fs::exists(dst_path))
fs::create_directory(dst_path);
}
Expand Down Expand Up @@ -744,7 +744,7 @@ int create_dir(IOState &io, const char *dir, int mode, const std::wstring &pref_
if (fs::exists(emulated_path))
return IO_ERROR(SCE_ERROR_ERRNO_EEXIST);

const auto parent_path = fs::path(emulated_path).remove_trailing_separator().parent_path();
const auto parent_path = fs::weakly_canonical(emulated_path).parent_path();
if (!fs::exists(parent_path)) // Vita cannot recursively create directories
return IO_ERROR(SCE_ERROR_ERRNO_ENOENT);

Expand Down
4 changes: 2 additions & 2 deletions vita3k/packages/src/license.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool copy_license(EmuEnvState &emuenv, const fs::path &license_path) {

const auto license_dst_path{ dst_path / fmt::format("{}.rif", emuenv.license_content_id) };
if (license_path != license_dst_path) {
fs::copy_file(license_path, license_dst_path, fs::copy_option::overwrite_if_exists);
fs_utils::copy_file_overwrite(license_path, license_dst_path);
if (fs::exists(license_dst_path)) {
LOG_INFO("Success copy license file to: {}", license_dst_path.string());
return true;
Expand Down Expand Up @@ -118,7 +118,7 @@ bool create_license(EmuEnvState &emuenv, const std::string &zRIF) {

// Create temp license file
const auto temp_license_path = cache_path / "temp_licence.rif";
std::ofstream temp_file(temp_license_path.string(), std::ios::out | std::ios::binary);
fs::ofstream temp_file(temp_license_path, std::ios::out | std::ios::binary);
zrif2rif(zRIF, temp_file);

return copy_license(emuenv, temp_license_path);
Expand Down
8 changes: 4 additions & 4 deletions vita3k/packages/src/pkg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static void aes128_ctr_xor(aes_context *ctx, const uint8_t *iv, uint64_t block,
bool decrypt_install_nonpdrm(EmuEnvState &emuenv, std::string &drmlicpath, const std::string &title_path) {
std::string title_id_src = title_path;
std::string title_id_dst = title_path + "_dec";
fs::ifstream binfile(string_utils::utf_to_wide(drmlicpath), std::ios::in | std::ios::binary | std::ios::ate);
fs::ifstream binfile(fs::path(string_utils::utf_to_wide(drmlicpath)), std::ios::in | std::ios::binary | std::ios::ate);
std::string zRIF = rif2zrif(binfile);
F00DEncryptorTypes f00d_enc_type = F00DEncryptorTypes::native;
std::string f00d_arg = std::string();
Expand Down Expand Up @@ -111,7 +111,7 @@ bool decrypt_install_nonpdrm(EmuEnvState &emuenv, std::string &drmlicpath, const

bool install_pkg(const std::string &pkg, EmuEnvState &emuenv, std::string &p_zRIF, const std::function<void(float)> &progress_callback) {
std::wstring pkg_path = string_utils::utf_to_wide(pkg);
fs::ifstream infile(pkg_path, std::ios::binary);
fs::ifstream infile(fs::path(pkg_path), std::ios::binary);
PkgHeader pkg_header;
PkgExtHeader ext_header;
infile.read(reinterpret_cast<char *>(&pkg_header), sizeof(PkgHeader));
Expand Down Expand Up @@ -272,9 +272,9 @@ bool install_pkg(const std::string &pkg, EmuEnvState &emuenv, std::string &p_zRI
LOG_INFO(string_name);

if ((byte_swap(entry.type) & 0xFF) == 4 || (byte_swap(entry.type) & 0xFF) == 18) { // Directory
fs::create_directories(path.string() + "/" + string_name);
fs::create_directories(path / string_name);
} else { // File
std::ofstream outfile(path.string() + "/" + string_name, std::ios::binary);
fs::ofstream outfile(path / string_name, fs::ofstream::binary);

auto offset = byte_swap(entry.data_offset);
auto data_size = byte_swap(entry.data_size);
Expand Down
10 changes: 5 additions & 5 deletions vita3k/packages/src/pup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static std::string make_filename(unsigned char *hdr, int64_t filetype) {
static void extract_pup_files(const std::wstring &pup, const std::wstring &output) {
constexpr int SCEUF_HEADER_SIZE = 0x80;
constexpr int SCEUF_FILEREC_SIZE = 0x20;
fs::ifstream infile(pup, std::ios::binary);
fs::ifstream infile(fs::path(pup), std::ios::binary);
char header[SCEUF_HEADER_SIZE];
infile.read(header, SCEUF_HEADER_SIZE);

Expand Down Expand Up @@ -155,7 +155,7 @@ static void extract_pup_files(const std::wstring &pup, const std::wstring &outpu
filename = make_filename((unsigned char *)hdr, filetype);
}

fs::ofstream outfile(fmt::format(L"{}/{}", output, string_utils::utf_to_wide(filename)), std::ios::binary);
fs::ofstream outfile(fs::path(output) / string_utils::utf_to_wide(filename), std::ios::binary);
infile.seekg(offset);
std::vector<char> buffer(length);
infile.read(&buffer[0], length);
Expand All @@ -176,7 +176,7 @@ static void decrypt_segments(std::ifstream &infile, const std::wstring &outdir,

const auto scesegs = get_segments(infile, sce_hdr, SCE_KEYS, sysver, selftype);
for (const auto &sceseg : scesegs) {
fs::ofstream outfile(fmt::format(L"{}/{}.seg02", outdir, filename), std::ios::binary);
fs::ofstream outfile(fs::path(outdir) / (filename + L".seg02"), std::ios::binary);
infile.seekg(sceseg.offset);
std::vector<unsigned char> encrypted_data(sceseg.size);
infile.read((char *)&encrypted_data[0], sceseg.size);
Expand Down Expand Up @@ -207,7 +207,7 @@ static void join_files(const std::wstring &path, const std::string &filename, co

std::sort(files.begin(), files.end());

fs::ofstream fileout(output, std::ios::binary);
fs::ofstream fileout(fs::path(output), std::ios::binary);
for (const auto &file : files) {
fs::ifstream filein(file, std::ios::binary);
std::vector<char> buffer(fs::file_size(file));
Expand All @@ -228,7 +228,7 @@ static void decrypt_pup_packages(const std::wstring &src, const std::wstring &de
}

for (const auto &filename : pkgfiles) {
const std::wstring &filepath = fmt::format(L"{}/{}", src, filename);
const fs::path &filepath = fs::path(src) / filename;
fs::ifstream infile(filepath, std::ios::binary);
decrypt_segments(infile, dest, filename, SCE_KEYS);
infile.close();
Expand Down