file_util: Migrate remaining file handling functions over to std::filesystem

Converts creation and deletion functions over to std::filesystem,
simplifying our file-handling code.

Notably with this, CopyDir will now function on Windows.
This commit is contained in:
Lioncash
2022-10-31 19:49:16 +02:00
committed by GPUCode
parent 0675bd946c
commit 9d6aa2db81
5 changed files with 119 additions and 298 deletions

View File

@ -70,13 +70,6 @@
#include <algorithm> #include <algorithm>
#include <sys/stat.h> #include <sys/stat.h>
#ifndef S_ISDIR
#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
#endif
// This namespace has various generic functions related to files and paths.
// The code still needs a ton of cleanup.
// REMEMBER: strdup considered harmful!
namespace Common::FS { namespace Common::FS {
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -90,224 +83,89 @@ bool IsDirectory(const fs::path& path) {
return fs::is_directory(path, ec); return fs::is_directory(path, ec);
} }
bool Delete(const std::string& filename) { bool Delete(const fs::path& path) {
LOG_TRACE(Common_Filesystem, "file {}", filename); LOG_TRACE(Common_Filesystem, "file {}", path.string());
// Return true because we care about the file no // Return true because we care about the file no
// being there, not the actual delete. // being there, not the actual delete.
if (!Exists(filename)) { if (!Exists(path)) {
LOG_DEBUG(Common_Filesystem, "{} does not exist", filename); LOG_DEBUG(Common_Filesystem, "{} does not exist", path.string());
return true; return true;
} }
// We can't delete a directory std::error_code ec;
if (IsDirectory(filename)) { return fs::remove(path, ec);
LOG_ERROR(Common_Filesystem, "Failed: {} is a directory", filename); }
return false;
}
#ifdef _WIN32 bool CreateDir(const fs::path& path) {
if (!DeleteFileW(Common::UTF8ToUTF16W(filename).c_str())) { LOG_TRACE(Common_Filesystem, "directory {}", path.string());
LOG_ERROR(Common_Filesystem, "DeleteFile failed on {}: {}", filename, GetLastErrorMsg());
std::error_code ec;
const bool success = fs::create_directory(path, ec);
if (!success) {
LOG_ERROR(Common_Filesystem, "Unable to create directory: {}", ec.message());
return false; return false;
} }
#else
if (unlink(filename.c_str()) == -1) {
LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filename, GetLastErrorMsg());
return false;
}
#endif
return true; return true;
} }
bool CreateDir(const std::string& path) { bool CreateFullPath(const fs::path& path) {
LOG_TRACE(Common_Filesystem, "directory {}", path); LOG_TRACE(Common_Filesystem, "path {}", path.string());
#ifdef _WIN32
if (::CreateDirectoryW(Common::UTF8ToUTF16W(path).c_str(), nullptr))
return true;
DWORD error = GetLastError();
if (error == ERROR_ALREADY_EXISTS) {
LOG_DEBUG(Common_Filesystem, "CreateDirectory failed on {}: already exists", path);
return true;
}
LOG_ERROR(Common_Filesystem, "CreateDirectory failed on {}: {}", path, error);
return false;
#else
if (mkdir(path.c_str(), 0755) == 0)
return true;
int err = errno; std::error_code ec;
const bool success = fs::create_directories(path, ec);
if (err == EEXIST) { if (!success) {
LOG_DEBUG(Common_Filesystem, "mkdir failed on {}: already exists", path); LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message());
return true;
}
LOG_ERROR(Common_Filesystem, "mkdir failed on {}: {}", path, strerror(err));
return false;
#endif
}
bool CreateFullPath(const std::string& fullPath) {
int panicCounter = 100;
LOG_TRACE(Common_Filesystem, "path {}", fullPath);
if (Common::FS::Exists(fullPath)) {
LOG_DEBUG(Common_Filesystem, "path exists {}", fullPath);
return true;
}
std::size_t position = 0;
while (true) {
// Find next sub path
position = fullPath.find(DIR_SEP_CHR, position);
// we're done, yay!
if (position == fullPath.npos)
return true;
// Include the '/' so the first call is CreateDir("/") rather than CreateDir("")
std::string const subPath(fullPath.substr(0, position + 1));
if (!Common::FS::IsDirectory(subPath) && !Common::FS::CreateDir(subPath)) {
LOG_ERROR(Common, "CreateFullPath: directory creation failed");
return false;
}
// A safety check
panicCounter--;
if (panicCounter <= 0) {
LOG_ERROR(Common, "CreateFullPath: directory structure is too deep");
return false;
}
position++;
}
}
bool DeleteDir(const std::string& filename) {
LOG_TRACE(Common_Filesystem, "directory {}", filename);
// check if a directory
if (!Common::FS::IsDirectory(filename)) {
LOG_ERROR(Common_Filesystem, "Not a directory {}", filename);
return false; return false;
} }
#ifdef _WIN32
if (::RemoveDirectoryW(Common::UTF8ToUTF16W(filename).c_str()))
return true;
#else
if (rmdir(filename.c_str()) == 0)
return true;
#endif
LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg());
return false;
}
bool Rename(const std::string& srcFilename, const std::string& destFilename) {
LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename);
#ifdef _WIN32
if (_wrename(Common::UTF8ToUTF16W(srcFilename).c_str(),
Common::UTF8ToUTF16W(destFilename).c_str()) == 0)
return true;
#else
if (rename(srcFilename.c_str(), destFilename.c_str()) == 0)
return true;
#endif
LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename,
GetLastErrorMsg());
return false;
}
bool Copy(const std::string& srcFilename, const std::string& destFilename) {
LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename);
#ifdef _WIN32
if (CopyFileW(Common::UTF8ToUTF16W(srcFilename).c_str(),
Common::UTF8ToUTF16W(destFilename).c_str(), FALSE))
return true;
LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename,
GetLastErrorMsg());
return false;
#else
using CFilePointer = std::unique_ptr<FILE, decltype(&std::fclose)>;
// Open input file
CFilePointer input{fopen(srcFilename.c_str(), "rb"), std::fclose};
if (!input) {
LOG_ERROR(Common_Filesystem, "opening input failed {} --> {}: {}", srcFilename,
destFilename, GetLastErrorMsg());
return false;
}
// open output file
CFilePointer output{fopen(destFilename.c_str(), "wb"), std::fclose};
if (!output) {
LOG_ERROR(Common_Filesystem, "opening output failed {} --> {}: {}", srcFilename,
destFilename, GetLastErrorMsg());
return false;
}
// copy loop
std::array<char, 1024> buffer;
while (!feof(input.get())) {
// read input
std::size_t rnum = fread(buffer.data(), sizeof(char), buffer.size(), input.get());
if (rnum != buffer.size()) {
if (ferror(input.get()) != 0) {
LOG_ERROR(Common_Filesystem, "failed reading from source, {} --> {}: {}",
srcFilename, destFilename, GetLastErrorMsg());
return false;
}
}
// write output
std::size_t wnum = fwrite(buffer.data(), sizeof(char), rnum, output.get());
if (wnum != rnum) {
LOG_ERROR(Common_Filesystem, "failed writing to output, {} --> {}: {}", srcFilename,
destFilename, GetLastErrorMsg());
return false;
}
}
return true; return true;
#endif
} }
u64 GetSize(const std::string& filename) { bool Rename(const fs::path& src, const fs::path& dst) {
if (!Exists(filename)) { LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string());
LOG_ERROR(Common_Filesystem, "failed {}: No such file", filename);
return 0; std::error_code ec;
fs::rename(src, dst, ec);
if (ec) {
LOG_ERROR(Common_Filesystem, "Unable to rename file from {} to {}: {}", src.string(),
dst.string(), ec.message());
return false;
} }
if (IsDirectory(filename)) { return true;
LOG_ERROR(Common_Filesystem, "failed {}: is a directory", filename);
return 0;
}
struct stat buf;
#ifdef _WIN32
if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0)
#else
if (stat(filename.c_str(), &buf) == 0)
#endif
{
LOG_TRACE(Common_Filesystem, "{}: {}", filename, buf.st_size);
return buf.st_size;
}
LOG_ERROR(Common_Filesystem, "Stat failed {}: {}", filename, GetLastErrorMsg());
return 0;
} }
u64 GetSize(const int fd) { bool Copy(const fs::path& src, const fs::path& dst) {
struct stat buf; LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string());
if (fstat(fd, &buf) != 0) {
LOG_ERROR(Common_Filesystem, "GetSize: stat failed {}: {}", fd, GetLastErrorMsg()); std::error_code ec;
const bool success = fs::copy_file(src, dst, fs::copy_options::overwrite_existing, ec);
if (!success) {
LOG_ERROR(Common_Filesystem, "Unable to copy file {} to {}: {}", src.string(), dst.string(),
ec.message());
return false;
}
return true;
}
u64 GetSize(const fs::path& path) {
std::error_code ec;
const auto size = fs::file_size(path, ec);
if (ec) {
LOG_ERROR(Common_Filesystem, "Unable to retrieve file size ({}): {}", path.string(),
ec.message());
return 0; return 0;
} }
return buf.st_size;
return size;
} }
u64 GetSize(FILE* f) { u64 GetSize(FILE* f) {
@ -328,7 +186,7 @@ u64 GetSize(FILE* f) {
bool CreateEmptyFile(const std::string& filename) { bool CreateEmptyFile(const std::string& filename) {
LOG_TRACE(Common_Filesystem, "{}", filename); LOG_TRACE(Common_Filesystem, "{}", filename);
if (!Common::FS::IOFile(filename, "wb").IsOpen()) { if (!IOFile(filename, "wb").IsOpen()) {
LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg());
return false; return false;
} }
@ -439,90 +297,58 @@ void GetAllFilesFromNestedEntries(FSTEntry& directory, std::vector<FSTEntry>& ou
} }
} }
bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) { bool DeleteDirRecursively(const fs::path& path) {
const auto callback = [recursion](u64* num_entries_out, const std::string& directory, std::error_code ec;
const std::string& virtual_name) -> bool { fs::remove_all(path, ec);
std::string new_path = directory + DIR_SEP_CHR + virtual_name;
if (IsDirectory(new_path)) { if (ec) {
if (recursion == 0) LOG_ERROR(Common_Filesystem, "Unable to completely delete directory {}: {}", path.string(),
return false; ec.message());
return DeleteDirRecursively(new_path, recursion - 1);
}
return Delete(new_path);
};
if (!ForeachDirectoryEntry(nullptr, directory, callback))
return false; return false;
}
// Delete the outermost directory
Common::FS::DeleteDir(directory);
return true; return true;
} }
void CopyDir(const std::string& source_path, const std::string& dest_path) { void CopyDir(const fs::path& src, const fs::path& dst) {
#ifndef _WIN32 constexpr auto copy_flags = fs::copy_options::skip_existing | fs::copy_options::recursive;
if (source_path == dest_path)
return;
if (!Common::FS::Exists(source_path))
return;
if (!Common::FS::Exists(dest_path))
Common::FS::CreateFullPath(dest_path);
DIR* dirp = opendir(source_path.c_str()); std::error_code ec;
if (!dirp) fs::copy(src, dst, copy_flags, ec);
if (ec) {
LOG_ERROR(Common_Filesystem, "Error copying directory {} to {}: {}", src.string(),
dst.string(), ec.message());
return; return;
while (struct dirent* result = readdir(dirp)) {
const std::string virtualName(result->d_name);
// check for "." and ".."
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0')))
continue;
std::string source, dest;
source = source_path + virtualName;
dest = dest_path + virtualName;
if (IsDirectory(source)) {
source += '/';
dest += '/';
if (!Common::FS::Exists(dest))
Common::FS::CreateFullPath(dest);
CopyDir(source, dest);
} else if (!Common::FS::Exists(dest))
Common::FS::Copy(source, dest);
} }
closedir(dirp);
#endif LOG_TRACE(Common_Filesystem, "Successfully copied directory.");
} }
std::optional<std::string> GetCurrentDir() { std::optional<fs::path> GetCurrentDir() {
// Get the current working directory (getcwd uses malloc) std::error_code ec;
#ifdef _WIN32 auto path = fs::current_path(ec);
wchar_t* dir = _wgetcwd(nullptr, 0);
if (!dir) {
#else
char* dir = getcwd(nullptr, 0);
if (!dir) {
#endif
LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg());
return {};
}
#ifdef _WIN32
std::string strDir = Common::UTF16ToUTF8(dir);
#else
std::string strDir = dir;
#endif
free(dir);
return strDir;
} // namespace Common::FS
bool SetCurrentDir(const std::string& directory) { if (ec) {
#ifdef _WIN32 LOG_ERROR(Common_Filesystem, "Unable to retrieve current working directory: {}",
return _wchdir(Common::UTF8ToUTF16W(directory).c_str()) == 0; ec.message());
#else return std::nullopt;
return chdir(directory.c_str()) == 0; }
#endif
return {std::move(path)};
}
bool SetCurrentDir(const fs::path& path) {
std::error_code ec;
fs::current_path(path, ec);
if (ec) {
LOG_ERROR(Common_Filesystem, "Unable to set {} as working directory: {}", path.string(),
ec.message());
return false;
}
return true;
} }
#if defined(__APPLE__) #if defined(__APPLE__)

View File

@ -91,39 +91,34 @@ private:
friend class boost::serialization::access; friend class boost::serialization::access;
}; };
// Returns true if the exists // Returns true if the path exists
[[nodiscard]] bool Exists(const std::filesystem::path& path); [[nodiscard]] bool Exists(const std::filesystem::path& path);
// Returns true if path is a directory // Returns true if path is a directory
[[nodiscard]] bool IsDirectory(const std::filesystem::path& path); [[nodiscard]] bool IsDirectory(const std::filesystem::path& path);
// Returns the size of filename (64bit) // Returns the size of filename (64bit)
[[nodiscard]] u64 GetSize(const std::string& filename); [[nodiscard]] u64 GetSize(const std::filesystem::path& path);
// Overloaded GetSize, accepts file descriptor
[[nodiscard]] u64 GetSize(int fd);
// Overloaded GetSize, accepts FILE* // Overloaded GetSize, accepts FILE*
[[nodiscard]] u64 GetSize(FILE* f); [[nodiscard]] u64 GetSize(FILE* f);
// Returns true if successful, or path already exists. // Returns true if successful, or path already exists.
bool CreateDir(const std::string& filename); bool CreateDir(const std::filesystem::path& path);
// Creates the full path of fullPath returns true on success // Creates the full path of path. Returns true on success
bool CreateFullPath(const std::string& fullPath); bool CreateFullPath(const std::filesystem::path& path);
// Deletes a given filename, return true on success // Deletes a given file at the path.
// Doesn't supports deleting a directory // This will also delete empty directories.
bool Delete(const std::string& filename); // Return true on success
bool Delete(const std::filesystem::path& path);
// Deletes a directory filename, returns true on success // Renames file src to dst, returns true on success
bool DeleteDir(const std::string& filename); bool Rename(const std::filesystem::path& src, const std::filesystem::path& dst);
// renames file srcFilename to destFilename, returns true on success // copies file src to dst, returns true on success
bool Rename(const std::string& srcFilename, const std::string& destFilename); bool Copy(const std::filesystem::path& src, const std::filesystem::path& dst);
// copies file srcFilename to destFilename, returns true on success
bool Copy(const std::string& srcFilename, const std::string& destFilename);
// creates an empty file filename, returns true on success // creates an empty file filename, returns true on success
bool CreateEmptyFile(const std::string& filename); bool CreateEmptyFile(const std::string& filename);
@ -167,17 +162,17 @@ u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry,
*/ */
void GetAllFilesFromNestedEntries(FSTEntry& directory, std::vector<FSTEntry>& output); void GetAllFilesFromNestedEntries(FSTEntry& directory, std::vector<FSTEntry>& output);
// deletes the given directory and anything under it. Returns true on success. // Deletes the given path and anything under it. Returns true on success.
bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256); bool DeleteDirRecursively(const std::filesystem::path& path);
// Returns the current directory // Returns the current directory
[[nodiscard]] std::optional<std::string> GetCurrentDir(); [[nodiscard]] std::optional<std::filesystem::path> GetCurrentDir();
// Create directory and copy contents (does not overwrite existing files) // Create directory and copy contents (does not overwrite existing files)
void CopyDir(const std::string& source_path, const std::string& dest_path); void CopyDir(const std::filesystem::path& src, const std::filesystem::path& dst);
// Set the current directory to given directory // Set the current directory to given path
bool SetCurrentDir(const std::string& directory); bool SetCurrentDir(const std::filesystem::path& path);
void SetUserPath(const std::string& path = ""); void SetUserPath(const std::string& path = "");

View File

@ -218,7 +218,7 @@ static ResultCode DeleteDirectoryHelper(const Path& path, const std::string& mou
} }
ResultCode SDMCArchive::DeleteDirectory(const Path& path) const { ResultCode SDMCArchive::DeleteDirectory(const Path& path) const {
return DeleteDirectoryHelper(path, mount_point, Common::FS::DeleteDir); return DeleteDirectoryHelper(path, mount_point, Common::FS::Delete);
} }
ResultCode SDMCArchive::DeleteDirectoryRecursively(const Path& path) const { ResultCode SDMCArchive::DeleteDirectoryRecursively(const Path& path) const {

View File

@ -200,7 +200,7 @@ static ResultCode DeleteDirectoryHelper(const Path& path, const std::string& mou
} }
ResultCode SaveDataArchive::DeleteDirectory(const Path& path) const { ResultCode SaveDataArchive::DeleteDirectory(const Path& path) const {
return DeleteDirectoryHelper(path, mount_point, Common::FS::DeleteDir); return DeleteDirectoryHelper(path, mount_point, Common::FS::Delete);
} }
ResultCode SaveDataArchive::DeleteDirectoryRecursively(const Path& path) const { ResultCode SaveDataArchive::DeleteDirectoryRecursively(const Path& path) const {

View File

@ -285,7 +285,7 @@ bool CIAFile::Close() const {
// Install aborted // Install aborted
if (!complete) { if (!complete) {
LOG_ERROR(Service_AM, "CIAFile closed prematurely, aborting install..."); LOG_ERROR(Service_AM, "CIAFile closed prematurely, aborting install...");
Common::FS::DeleteDir(GetTitlePath(media_type, container.GetTitleMetadata().GetTitleID())); Common::FS::Delete(GetTitlePath(media_type, container.GetTitleMetadata().GetTitleID()));
return true; return true;
} }