2023-12-22 11:57:49 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0+
|
2021-09-21 09:52:13 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include "Pcsx2Defs.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <ctime>
|
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2022-12-28 11:13:26 +00:00
|
|
|
class Error;
|
|
|
|
|
2021-09-21 09:52:13 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define FS_OSPATH_SEPARATOR_CHARACTER '\\'
|
|
|
|
#define FS_OSPATH_SEPARATOR_STR "\\"
|
|
|
|
#else
|
|
|
|
#define FS_OSPATH_SEPARATOR_CHARACTER '/'
|
|
|
|
#define FS_OSPATH_SEPARATOR_STR "/"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum FILESYSTEM_FILE_ATTRIBUTES
|
|
|
|
{
|
|
|
|
FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY = 1,
|
|
|
|
FILESYSTEM_FILE_ATTRIBUTE_READ_ONLY = 2,
|
|
|
|
FILESYSTEM_FILE_ATTRIBUTE_COMPRESSED = 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum FILESYSTEM_FIND_FLAGS
|
|
|
|
{
|
|
|
|
FILESYSTEM_FIND_RECURSIVE = (1 << 0),
|
|
|
|
FILESYSTEM_FIND_RELATIVE_PATHS = (1 << 1),
|
|
|
|
FILESYSTEM_FIND_HIDDEN_FILES = (1 << 2),
|
|
|
|
FILESYSTEM_FIND_FOLDERS = (1 << 3),
|
|
|
|
FILESYSTEM_FIND_FILES = (1 << 4),
|
|
|
|
FILESYSTEM_FIND_KEEP_ARRAY = (1 << 5),
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FILESYSTEM_STAT_DATA
|
|
|
|
{
|
2022-05-19 14:46:33 +00:00
|
|
|
std::time_t CreationTime; // actually inode change time on linux
|
2021-09-21 09:52:13 +00:00
|
|
|
std::time_t ModificationTime;
|
|
|
|
s64 Size;
|
|
|
|
u32 Attributes;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FILESYSTEM_FIND_DATA
|
|
|
|
{
|
2022-05-19 14:46:33 +00:00
|
|
|
std::time_t CreationTime; // actually inode change time on linux
|
2021-09-21 09:52:13 +00:00
|
|
|
std::time_t ModificationTime;
|
|
|
|
std::string FileName;
|
|
|
|
s64 Size;
|
|
|
|
u32 Attributes;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace FileSystem
|
|
|
|
{
|
|
|
|
using FindResultsArray = std::vector<FILESYSTEM_FIND_DATA>;
|
|
|
|
|
|
|
|
/// Returns a list of "root directories" (i.e. root/home directories on Linux, drive letters on Windows).
|
|
|
|
std::vector<std::string> GetRootDirectoryList();
|
|
|
|
|
|
|
|
/// Search for files
|
|
|
|
bool FindFiles(const char* path, const char* pattern, u32 flags, FindResultsArray* results);
|
|
|
|
|
|
|
|
/// Stat file
|
|
|
|
bool StatFile(const char* path, struct stat* st);
|
|
|
|
bool StatFile(std::FILE* fp, struct stat* st);
|
|
|
|
bool StatFile(const char* path, FILESYSTEM_STAT_DATA* pStatData);
|
|
|
|
bool StatFile(std::FILE* fp, FILESYSTEM_STAT_DATA* pStatData);
|
|
|
|
s64 GetPathFileSize(const char* path);
|
|
|
|
|
2023-09-30 15:50:11 +00:00
|
|
|
/// Returns the last modified timestamp for a file.
|
|
|
|
std::optional<std::time_t> GetFileTimestamp(const char* path);
|
|
|
|
|
2021-09-21 09:52:13 +00:00
|
|
|
/// File exists?
|
|
|
|
bool FileExists(const char* path);
|
|
|
|
|
|
|
|
/// Directory exists?
|
|
|
|
bool DirectoryExists(const char* path);
|
|
|
|
|
2022-05-19 14:46:33 +00:00
|
|
|
/// Directory does not contain any files?
|
|
|
|
bool DirectoryIsEmpty(const char* path);
|
|
|
|
|
2021-09-21 09:52:13 +00:00
|
|
|
/// Delete file
|
|
|
|
bool DeleteFilePath(const char* path);
|
|
|
|
|
|
|
|
/// Rename file
|
|
|
|
bool RenamePath(const char* OldPath, const char* NewPath);
|
|
|
|
|
2023-07-18 15:21:28 +00:00
|
|
|
/// Deleter functor for managed file pointers
|
|
|
|
struct FileDeleter
|
|
|
|
{
|
|
|
|
void operator()(std::FILE* fp)
|
|
|
|
{
|
|
|
|
std::fclose(fp);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-21 09:52:13 +00:00
|
|
|
/// open files
|
2023-07-18 15:21:28 +00:00
|
|
|
using ManagedCFilePtr = std::unique_ptr<std::FILE, FileDeleter>;
|
2022-12-28 11:13:26 +00:00
|
|
|
ManagedCFilePtr OpenManagedCFile(const char* filename, const char* mode, Error* error = nullptr);
|
|
|
|
std::FILE* OpenCFile(const char* filename, const char* mode, Error* error = nullptr);
|
2021-09-21 09:52:13 +00:00
|
|
|
int FSeek64(std::FILE* fp, s64 offset, int whence);
|
|
|
|
s64 FTell64(std::FILE* fp);
|
|
|
|
s64 FSize64(std::FILE* fp);
|
|
|
|
|
2022-12-28 11:13:26 +00:00
|
|
|
int OpenFDFile(const char* filename, int flags, int mode, Error* error = nullptr);
|
2021-09-21 09:52:13 +00:00
|
|
|
|
2022-06-06 12:46:39 +00:00
|
|
|
/// Sharing modes for OpenSharedCFile().
|
|
|
|
enum class FileShareMode
|
|
|
|
{
|
|
|
|
DenyReadWrite, /// Exclusive access.
|
|
|
|
DenyWrite, /// Other processes can read from this file.
|
|
|
|
DenyRead, /// Other processes can write to this file.
|
|
|
|
DenyNone, /// Other processes can read and write to this file.
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Opens a file in shareable mode (where other processes can access it concurrently).
|
|
|
|
/// Only has an effect on Windows systems.
|
2022-12-28 11:13:26 +00:00
|
|
|
ManagedCFilePtr OpenManagedSharedCFile(const char* filename, const char* mode, FileShareMode share_mode, Error* error = nullptr);
|
|
|
|
std::FILE* OpenSharedCFile(const char* filename, const char* mode, FileShareMode share_mode, Error* error = nullptr);
|
2022-06-06 12:46:39 +00:00
|
|
|
|
2021-09-21 09:52:13 +00:00
|
|
|
std::optional<std::vector<u8>> ReadBinaryFile(const char* filename);
|
|
|
|
std::optional<std::vector<u8>> ReadBinaryFile(std::FILE* fp);
|
|
|
|
std::optional<std::string> ReadFileToString(const char* filename);
|
|
|
|
std::optional<std::string> ReadFileToString(std::FILE* fp);
|
|
|
|
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length);
|
2022-02-18 04:10:15 +00:00
|
|
|
bool WriteStringToFile(const char* filename, const std::string_view& sv);
|
2021-09-21 09:52:13 +00:00
|
|
|
|
|
|
|
/// creates a directory in the local filesystem
|
|
|
|
/// if the directory already exists, the return value will be true.
|
|
|
|
/// if Recursive is specified, all parent directories will be created
|
|
|
|
/// if they do not exist.
|
|
|
|
bool CreateDirectoryPath(const char* path, bool recursive);
|
|
|
|
|
2022-02-20 06:44:08 +00:00
|
|
|
/// Creates a directory if it doesn't already exist.
|
|
|
|
/// Returns false if it does not exist and creation failed.
|
|
|
|
bool EnsureDirectoryExists(const char* path, bool recursive);
|
|
|
|
|
2022-03-25 09:22:57 +00:00
|
|
|
/// Removes a directory.
|
|
|
|
bool DeleteDirectory(const char* path);
|
|
|
|
|
|
|
|
/// Recursively removes a directory and all subdirectories/files.
|
|
|
|
bool RecursiveDeleteDirectory(const char* path);
|
|
|
|
|
2022-05-19 14:46:33 +00:00
|
|
|
/// Copies one file to another, optionally replacing it if it already exists.
|
|
|
|
bool CopyFilePath(const char* source, const char* destination, bool replace);
|
|
|
|
|
2021-09-21 09:52:13 +00:00
|
|
|
/// Returns the path to the current executable.
|
|
|
|
std::string GetProgramPath();
|
|
|
|
|
|
|
|
/// Retrieves the current working directory.
|
|
|
|
std::string GetWorkingDirectory();
|
|
|
|
|
|
|
|
/// Sets the current working directory. Returns true if successful.
|
|
|
|
bool SetWorkingDirectory(const char* path);
|
|
|
|
|
2022-04-12 12:16:05 +00:00
|
|
|
/// Enables/disables NTFS compression on a file or directory.
|
|
|
|
/// Does not apply the compression flag recursively if called for a directory.
|
|
|
|
/// Does nothing and returns false on non-Windows platforms.
|
|
|
|
bool SetPathCompression(const char* path, bool enable);
|
|
|
|
|
2022-10-22 04:52:01 +00:00
|
|
|
/// Abstracts a POSIX file lock.
|
|
|
|
#ifndef _WIN32
|
|
|
|
class POSIXLock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
POSIXLock(int fd);
|
|
|
|
POSIXLock(std::FILE* fp);
|
|
|
|
~POSIXLock();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_fd;
|
|
|
|
};
|
|
|
|
#endif
|
2021-09-21 09:52:13 +00:00
|
|
|
}; // namespace FileSystem
|