2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstddef>
|
2022-08-05 18:54:56 -06:00
|
|
|
#include <filesystem>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <fstream>
|
2008-07-16 14:50:16 -06:00
|
|
|
#include <string>
|
2019-05-28 15:47:26 -06:00
|
|
|
#include <string_view>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <vector>
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2017-06-29 03:20:38 -06:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-19 20:11:52 -07:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-19 20:11:52 -07:00
|
|
|
#endif
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2020-11-04 12:59:39 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "jni/AndroidCommon/AndroidCommon.h"
|
|
|
|
#endif
|
|
|
|
|
2010-02-02 14:56:29 -07:00
|
|
|
// User directory indices for GetUserPath
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
D_USER_IDX,
|
|
|
|
D_GCUSER_IDX,
|
2014-11-17 18:59:14 -07:00
|
|
|
D_WIIROOT_IDX, // always points to User/Wii or global user-configured directory
|
|
|
|
D_SESSION_WIIROOT_IDX, // may point to minimal temporary directory for determinism
|
2013-09-07 15:02:49 -06:00
|
|
|
D_CONFIG_IDX, // global settings
|
|
|
|
D_GAMESETTINGS_IDX, // user-specified settings which override both the global and the default
|
|
|
|
// settings (per game)
|
2010-02-02 14:56:29 -07:00
|
|
|
D_MAPS_IDX,
|
|
|
|
D_CACHE_IDX,
|
2018-07-29 19:16:37 -06:00
|
|
|
D_COVERCACHE_IDX,
|
2019-08-23 05:20:09 -06:00
|
|
|
D_REDUMPCACHE_IDX,
|
2010-02-02 14:56:29 -07:00
|
|
|
D_SHADERCACHE_IDX,
|
|
|
|
D_SHADERS_IDX,
|
|
|
|
D_STATESAVES_IDX,
|
|
|
|
D_SCREENSHOTS_IDX,
|
|
|
|
D_HIRESTEXTURES_IDX,
|
2021-09-25 22:16:55 -06:00
|
|
|
D_RIIVOLUTION_IDX,
|
2010-02-02 14:56:29 -07:00
|
|
|
D_DUMP_IDX,
|
|
|
|
D_DUMPFRAMES_IDX,
|
2018-06-06 06:42:41 -06:00
|
|
|
D_DUMPOBJECTS_IDX,
|
2011-02-11 11:59:42 -07:00
|
|
|
D_DUMPAUDIO_IDX,
|
2010-02-02 14:56:29 -07:00
|
|
|
D_DUMPTEXTURES_IDX,
|
|
|
|
D_DUMPDSP_IDX,
|
2015-07-11 02:31:03 -06:00
|
|
|
D_DUMPSSL_IDX,
|
2014-10-22 15:25:00 -06:00
|
|
|
D_LOAD_IDX,
|
2010-02-02 14:56:29 -07:00
|
|
|
D_LOGS_IDX,
|
|
|
|
D_MAILLOGS_IDX,
|
2013-01-13 13:06:15 -07:00
|
|
|
D_THEMES_IDX,
|
2018-05-06 10:25:37 -06:00
|
|
|
D_STYLES_IDX,
|
2015-10-24 21:20:03 -06:00
|
|
|
D_PIPES_IDX,
|
2019-05-03 13:46:37 -06:00
|
|
|
D_MEMORYWATCHER_IDX,
|
2017-01-02 19:31:45 -07:00
|
|
|
D_WFSROOT_IDX,
|
2016-12-23 18:37:23 -07:00
|
|
|
D_BACKUP_IDX,
|
2018-11-17 08:36:28 -07:00
|
|
|
D_RESOURCEPACK_IDX,
|
2019-08-17 13:40:58 -06:00
|
|
|
D_DYNAMICINPUT_IDX,
|
2022-03-01 23:34:54 -07:00
|
|
|
D_GRAPHICSMOD_IDX,
|
2021-07-04 04:51:36 -06:00
|
|
|
D_GBAUSER_IDX,
|
|
|
|
D_GBASAVES_IDX,
|
2022-04-16 20:10:36 -06:00
|
|
|
D_WIISDCARDSYNCFOLDER_IDX,
|
2021-11-21 19:00:15 -07:00
|
|
|
FIRST_FILE_USER_PATH_IDX,
|
|
|
|
F_DOLPHINCONFIG_IDX = FIRST_FILE_USER_PATH_IDX,
|
2016-01-14 15:37:33 -07:00
|
|
|
F_GCPADCONFIG_IDX,
|
|
|
|
F_WIIPADCONFIG_IDX,
|
|
|
|
F_GCKEYBOARDCONFIG_IDX,
|
|
|
|
F_GFXCONFIG_IDX,
|
2010-02-02 14:56:29 -07:00
|
|
|
F_DEBUGGERCONFIG_IDX,
|
|
|
|
F_LOGGERCONFIG_IDX,
|
|
|
|
F_MAINLOG_IDX,
|
2019-04-10 23:50:52 -06:00
|
|
|
F_MEM1DUMP_IDX,
|
|
|
|
F_MEM2DUMP_IDX,
|
2010-02-02 14:56:29 -07:00
|
|
|
F_ARAMDUMP_IDX,
|
2012-06-19 05:51:29 -06:00
|
|
|
F_FAKEVMEMDUMP_IDX,
|
2010-02-02 14:56:29 -07:00
|
|
|
F_GCSRAM_IDX,
|
2019-05-03 13:46:37 -06:00
|
|
|
F_MEMORYWATCHERLOCATIONS_IDX,
|
|
|
|
F_MEMORYWATCHERSOCKET_IDX,
|
2022-04-16 19:28:34 -06:00
|
|
|
F_WIISDCARDIMAGE_IDX,
|
2019-10-26 10:05:16 -06:00
|
|
|
F_DUALSHOCKUDPCLIENTCONFIG_IDX,
|
2020-07-22 18:19:35 -06:00
|
|
|
F_FREELOOKCONFIG_IDX,
|
2021-07-04 04:51:36 -06:00
|
|
|
F_GBABIOS_IDX,
|
2023-03-16 07:06:24 -06:00
|
|
|
F_RETROACHIEVEMENTSCONFIG_IDX,
|
2011-02-28 20:06:14 -07:00
|
|
|
NUM_PATH_INDICES
|
2010-02-02 14:56:29 -07:00
|
|
|
};
|
|
|
|
|
2008-09-23 16:23:38 -06:00
|
|
|
namespace File
|
2008-07-16 14:50:16 -06:00
|
|
|
{
|
2021-09-03 22:43:19 -06:00
|
|
|
// FileSystem tree node
|
2008-09-23 17:27:38 -06:00
|
|
|
struct FSTEntry
|
|
|
|
{
|
2021-09-03 22:43:19 -06:00
|
|
|
bool isDirectory = false;
|
|
|
|
u64 size = 0; // File length, or for directories, recursive count of children
|
2008-09-23 17:27:38 -06:00
|
|
|
std::string physicalName; // Name on disk
|
|
|
|
std::string virtualName; // Name in FST names table
|
|
|
|
std::vector<FSTEntry> children;
|
|
|
|
};
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2017-06-29 03:20:38 -06:00
|
|
|
// The functions in this class are functionally identical to the standalone functions
|
|
|
|
// below, but if you are going to be calling more than one of the functions using the
|
|
|
|
// same path, creating a single FileInfo object and calling its functions multiple
|
|
|
|
// times is faster than calling standalone functions multiple times.
|
|
|
|
class FileInfo final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit FileInfo(const std::string& path);
|
|
|
|
explicit FileInfo(const char* path);
|
|
|
|
|
|
|
|
// Returns true if the path exists
|
|
|
|
bool Exists() const;
|
|
|
|
// Returns true if the path exists and is a directory
|
|
|
|
bool IsDirectory() const;
|
|
|
|
// Returns true if the path exists and is a file
|
|
|
|
bool IsFile() const;
|
|
|
|
// Returns the size of a file (or returns 0 if the path doesn't refer to a file)
|
|
|
|
u64 GetSize() const;
|
|
|
|
|
|
|
|
private:
|
2022-08-05 18:54:56 -06:00
|
|
|
std::filesystem::file_status m_status;
|
2023-01-10 06:13:18 -07:00
|
|
|
std::uintmax_t m_size;
|
2017-06-29 03:20:38 -06:00
|
|
|
bool m_exists;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns true if the path exists
|
|
|
|
bool Exists(const std::string& path);
|
|
|
|
|
|
|
|
// Returns true if the path exists and is a directory
|
|
|
|
bool IsDirectory(const std::string& path);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2017-06-29 03:20:38 -06:00
|
|
|
// Returns true if the path exists and is a file
|
|
|
|
bool IsFile(const std::string& path);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2017-06-29 03:20:38 -06:00
|
|
|
// Returns the size of a file (or returns 0 if the path isn't a file that exists)
|
|
|
|
u64 GetSize(const std::string& path);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2010-12-03 05:42:01 -07:00
|
|
|
// Overloaded GetSize, accepts FILE*
|
2016-01-21 13:16:51 -07:00
|
|
|
u64 GetSize(FILE* f);
|
2010-12-03 05:42:01 -07:00
|
|
|
|
2023-02-21 18:31:06 -07:00
|
|
|
// Creates a single directory. Returns true if successful or if the path already exists.
|
2016-01-21 13:27:56 -07:00
|
|
|
bool CreateDir(const std::string& filename);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2023-02-21 18:31:06 -07:00
|
|
|
// Creates directories recursively. Returns true if successful or if the path already exists.
|
|
|
|
bool CreateDirs(std::string_view filename);
|
|
|
|
|
2023-02-14 18:13:47 -07:00
|
|
|
// Creates the full path to the file given in fullPath.
|
|
|
|
// That is, for path '/a/b/c.bin', creates folders '/a' and '/a/b'.
|
|
|
|
// Returns true if creation is successful or if the path already exists.
|
|
|
|
bool CreateFullPath(std::string_view fullPath);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2020-12-03 14:13:16 -07:00
|
|
|
enum class IfAbsentBehavior
|
|
|
|
{
|
|
|
|
ConsoleWarning,
|
|
|
|
NoConsoleWarning
|
|
|
|
};
|
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Deletes a given filename, return true on success
|
|
|
|
// Doesn't supports deleting a directory
|
2020-12-03 14:13:16 -07:00
|
|
|
bool Delete(const std::string& filename,
|
|
|
|
IfAbsentBehavior behavior = IfAbsentBehavior::ConsoleWarning);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
// Deletes a directory filename, returns true on success
|
2020-12-03 14:13:16 -07:00
|
|
|
bool DeleteDir(const std::string& filename,
|
|
|
|
IfAbsentBehavior behavior = IfAbsentBehavior::ConsoleWarning);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
// renames file srcFilename to destFilename, returns true on success
|
2016-01-21 13:27:56 -07:00
|
|
|
bool Rename(const std::string& srcFilename, const std::string& destFilename);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2013-10-15 00:52:06 -06:00
|
|
|
// ditto, but syncs the source file and, on Unix, syncs the directories after rename
|
2016-01-21 13:27:56 -07:00
|
|
|
bool RenameSync(const std::string& srcFilename, const std::string& destFilename);
|
2013-10-15 00:52:06 -06:00
|
|
|
|
2023-02-21 17:54:41 -07:00
|
|
|
// Copies a file at source_path to destination_path, as if by std::filesystem::copy_file().
|
|
|
|
// If a file already exists at destination_path it is overwritten. Returns true on success.
|
|
|
|
bool CopyRegularFile(std::string_view source_path, std::string_view destination_path);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// creates an empty file filename, returns true on success
|
2016-01-21 13:27:56 -07:00
|
|
|
bool CreateEmptyFile(const std::string& filename);
|
2009-06-09 13:40:47 -06:00
|
|
|
|
2016-12-25 13:57:33 -07:00
|
|
|
// Recursive or non-recursive list of files and directories under directory.
|
2021-09-13 22:51:34 -06:00
|
|
|
FSTEntry ScanDirectoryTree(std::string directory, bool recursive);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// deletes the given directory and anything under it. Returns true on success.
|
2016-01-21 13:27:56 -07:00
|
|
|
bool DeleteDirRecursively(const std::string& directory);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
|
|
|
// Returns the current directory
|
|
|
|
std::string GetCurrentDir();
|
|
|
|
|
2023-02-21 18:17:11 -07:00
|
|
|
// Copies source_path to dest_path, as if by std::filesystem::copy(). Returns true on success or if
|
|
|
|
// the source and destination are already the same (as determined by std::filesystem::equivalent()).
|
|
|
|
bool Copy(std::string_view source_path, std::string_view dest_path,
|
|
|
|
bool overwrite_existing = false);
|
|
|
|
|
2023-02-24 13:39:02 -07:00
|
|
|
// Moves source_path to dest_path. On success, the source_path will no longer exist, and the
|
|
|
|
// dest_path will contain the data previously in source_path. Files in dest_path will be overwritten
|
|
|
|
// if they match files in source_path, but files that only exist in dest_path will be kept. No
|
|
|
|
// guarantee on the state is given on failure; the move may have completely failed or partially
|
|
|
|
// completed.
|
|
|
|
bool MoveWithOverwrite(std::string_view source_path, std::string_view dest_path);
|
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Set the current directory to given directory
|
2016-01-21 13:27:56 -07:00
|
|
|
bool SetCurrentDir(const std::string& directory);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2014-11-17 18:59:14 -07:00
|
|
|
// Creates and returns the path to a new temporary directory.
|
|
|
|
std::string CreateTempDir();
|
|
|
|
|
2013-10-15 00:52:06 -06:00
|
|
|
// Get a filename that can hopefully be atomically renamed to the given path.
|
2019-10-06 13:37:51 -06:00
|
|
|
std::string GetTempFilenameForAtomicWrite(std::string path);
|
2013-10-15 00:52:06 -06:00
|
|
|
|
2015-03-15 07:51:13 -06:00
|
|
|
// Gets a set user directory path
|
|
|
|
// Don't call prior to setting the base user directory
|
|
|
|
const std::string& GetUserPath(unsigned int dir_index);
|
|
|
|
|
|
|
|
// Sets a user directory path
|
|
|
|
// Rebuilds internal directory structure to compensate for the new directory
|
2021-11-21 19:00:15 -07:00
|
|
|
void SetUserPath(unsigned int dir_index, std::string path);
|
2013-04-01 22:17:15 -06:00
|
|
|
|
|
|
|
// probably doesn't belong here
|
2013-04-02 12:04:40 -06:00
|
|
|
std::string GetThemeDir(const std::string& theme_name);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2009-02-28 16:21:51 -07:00
|
|
|
// Returns the path to where the sys file are
|
2022-05-27 15:25:43 -06:00
|
|
|
const std::string& GetSysDirectory();
|
2009-02-28 16:21:51 -07:00
|
|
|
|
2017-09-09 03:27:24 -06:00
|
|
|
#ifdef ANDROID
|
|
|
|
void SetSysDirectory(const std::string& path);
|
|
|
|
#endif
|
|
|
|
|
2009-02-28 16:21:51 -07:00
|
|
|
#ifdef __APPLE__
|
|
|
|
std::string GetBundleDirectory();
|
|
|
|
#endif
|
|
|
|
|
2018-03-22 17:29:03 -06:00
|
|
|
std::string GetExePath();
|
|
|
|
std::string GetExeDirectory();
|
2011-06-11 14:45:09 -06:00
|
|
|
|
2019-05-28 15:47:26 -06:00
|
|
|
bool WriteStringToFile(const std::string& filename, std::string_view str);
|
2014-03-12 13:33:41 -06:00
|
|
|
bool ReadFileToString(const std::string& filename, std::string& str);
|
2009-04-12 04:21:40 -06:00
|
|
|
|
2020-11-04 12:59:39 -07:00
|
|
|
// To deal with Windows not fully supporting UTF-8 and Android not fully supporting paths.
|
2013-02-28 18:33:39 -07:00
|
|
|
template <typename T>
|
|
|
|
void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
fstream.open(UTF8ToTStr(filename).c_str(), openmode);
|
|
|
|
#else
|
2020-11-04 12:59:39 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
// Unfortunately it seems like the non-standard __open is the only way to use a file descriptor
|
|
|
|
if (IsPathAndroidContent(filename))
|
|
|
|
fstream.__open(OpenAndroidContent(filename, OpenModeToAndroid(openmode)), openmode);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
fstream.open(filename.c_str(), openmode);
|
2013-02-28 18:33:39 -07:00
|
|
|
#endif
|
|
|
|
}
|
2017-01-15 14:46:43 -07:00
|
|
|
|
2018-11-17 08:36:28 -07:00
|
|
|
} // namespace File
|