Files
KytyPS5/src/common/file.h
T
Maayan080 c3cc8454b9 common: remove unused recursive file enumeration (#681)
common: don't follow reparse points in the Windows recursive file walk

SysFileFindFiles recurses into every entry carrying
FILE_ATTRIBUTE_DIRECTORY, which on Windows includes directory symlinks
and junctions. A junction pointing at one of its own ancestors makes the
walk recurse until the stack runs out.

The Linux implementation already guards against this: it uses lstat
rather than stat specifically so a symlink is never followed into a
cycle. Skip reparse-point entries on Windows for the same reason.

Co-authored-by: nmzik <Nmzik@mail.ru>
2026-09-20 18:50:57 +04:00

110 lines
3.2 KiB
C++

#ifndef KYTY_COMMON_FILE_H_
#define KYTY_COMMON_FILE_H_
#include "common/common.h"
#include "common/dateTime.h"
#include <cstddef>
#include <filesystem>
#include <memory>
#include <string>
#include <vector>
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
#ifdef CreateDirectory
#undef CreateDirectory
#endif
#ifdef DeleteFile
#undef DeleteFile
#endif
#ifdef CopyFile
#undef CopyFile
#endif
#endif
namespace Common {
// SUBSYSTEM_DEFINE(File);
class File {
public:
enum class Mode { Read, Write, ReadWrite, WriteRead };
struct DirEntry {
std::string name;
bool is_file;
};
File();
explicit File(const std::filesystem::path& name);
File(const std::filesystem::path& name, Mode mode);
virtual ~File();
bool Create(const std::filesystem::path& name);
bool Open(const std::filesystem::path& name, Mode mode);
bool OpenInMem(void* buf, uint32_t buf_size);
bool CreateInMem();
void Close();
bool Flush();
[[nodiscard]] uint64_t Size() const;
[[nodiscard]] uint64_t Remaining() const;
bool Seek(uint64_t offset);
[[nodiscard]] uint64_t Tell() const;
bool Truncate(uint64_t size);
bool Unlink();
[[nodiscard]] bool IsInvalid() const;
[[nodiscard]] bool IsEOF() const { return Tell() >= Size(); }
void GetLastAccessAndWriteTimeUTC(DateTime* access, DateTime* write);
void Read(void* data, uint32_t size, uint32_t* bytes_read = nullptr);
void Write(const void* data, uint32_t size, uint32_t* bytes_written = nullptr);
void Printf(const char* format, ...) KYTY_FORMAT_PRINTF(2, 3);
std::vector<std::byte> ReadWholeBuffer();
static uint64_t Size(const std::filesystem::path& name);
static bool IsDirectoryExisting(const std::filesystem::path& path);
static bool IsFileExisting(const std::filesystem::path& name);
static bool CreateDirectory(const std::filesystem::path& path);
static bool CreateDirectories(const std::filesystem::path& path);
static bool DeleteDirectory(const std::filesystem::path& path);
static bool DeleteDirectories(const std::filesystem::path& path);
static bool DeleteFile(const std::filesystem::path& name);
static DateTime GetLastAccessTimeUTC(const std::filesystem::path& name);
static DateTime GetLastWriteTimeUTC(const std::filesystem::path& name);
static void GetLastAccessAndWriteTimeUTC(const std::filesystem::path& name, DateTime* access,
DateTime* write);
static bool SetLastAccessTimeUTC(const std::filesystem::path& name, const DateTime& dt);
static bool SetLastWriteTimeUTC(const std::filesystem::path& name, const DateTime& dt);
static bool SetLastAccessAndWriteTimeUTC(const std::filesystem::path& name,
const DateTime& access, const DateTime& write);
static std::vector<DirEntry> GetDirEntries(const std::filesystem::path& path);
static bool CopyFile(const std::filesystem::path& src, const std::filesystem::path& dst);
static bool RenameFile(const std::filesystem::path& src, const std::filesystem::path& dst);
static void RemoveReadonly(const std::filesystem::path& name);
KYTY_CLASS_NO_COPY(File);
private:
struct FilePrivate;
std::filesystem::path m_file_name;
std::unique_ptr<FilePrivate> m_p;
};
} // namespace Common
#endif /* KYTY_COMMON_FILE_H_ */