2026-06-12 12:10:57 -07:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
2016-11-23 05:02:58 -08:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
|
* in all copies or substantial portions of the Software.
|
2016-11-23 05:02:58 -08:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2011-05-25 03:39:13 -07:00
|
|
|
/*
|
|
|
|
|
* higher-level interface on top of sysdep/filesystem.h
|
|
|
|
|
*/
|
|
|
|
|
|
2009-04-18 09:14:48 -07:00
|
|
|
#include "precompiled.h"
|
2009-08-04 12:57:53 -07:00
|
|
|
|
2026-06-12 12:10:57 -07:00
|
|
|
#include "file_system.h"
|
|
|
|
|
|
2023-04-19 09:35:00 -07:00
|
|
|
#include "lib/debug.h"
|
2025-07-17 11:14:23 -07:00
|
|
|
#include "lib/posix/posix_filesystem.h"
|
2009-08-04 12:57:53 -07:00
|
|
|
|
2026-06-12 12:10:57 -07:00
|
|
|
#include <chrono>
|
2025-10-20 04:45:11 -07:00
|
|
|
#include <filesystem>
|
2011-05-25 03:39:13 -07:00
|
|
|
|
|
|
|
|
bool DirectoryExists(const OsPath& path)
|
|
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return std::filesystem::is_directory(path.string());
|
|
|
|
|
}
|
|
|
|
|
catch (std::filesystem::filesystem_error& err)
|
2011-05-25 03:39:13 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
debug_printf("DirectoryExists: failed to check if directory '%s' exists, reason: %s\n", path.string8().c_str(), err.what());
|
2011-05-25 03:39:13 -07:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool FileExists(const OsPath& pathname)
|
|
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return std::filesystem::is_regular_file(pathname.string());
|
|
|
|
|
}
|
|
|
|
|
catch (std::filesystem::filesystem_error& err)
|
|
|
|
|
{
|
|
|
|
|
debug_printf("FileExists: failed to check if file '%s' exists, reason: %s\n", pathname.string8().c_str(), err.what());
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2011-05-25 03:39:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u64 FileSize(const OsPath& pathname)
|
|
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return static_cast<u64>(std::filesystem::file_size(pathname.string()));
|
|
|
|
|
}
|
|
|
|
|
catch (std::filesystem::filesystem_error& err)
|
|
|
|
|
{
|
|
|
|
|
debug_printf("FileSize: failed to get filesize for '%s', reason: %s\n", pathname.string8().c_str(), err.what());
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2011-05-25 03:39:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-09-10 07:17:04 -07:00
|
|
|
Status GetFileInfo(const OsPath& pathname, CFileInfo* pPtrInfo)
|
2011-05-25 03:39:13 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
const std::filesystem::path path{pathname.string()};
|
|
|
|
|
*pPtrInfo = CFileInfo(path.filename().wstring(), static_cast<u64>(std::filesystem::file_size(path)),
|
|
|
|
|
static_cast<time_t>(std::chrono::duration_cast<std::chrono::seconds>(std::filesystem::last_write_time(path).time_since_epoch()).count()));
|
|
|
|
|
}
|
|
|
|
|
catch (std::filesystem::filesystem_error& err)
|
2009-08-04 12:57:53 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
debug_printf("GetFileInfo: failed to get file info for '%s', reason: %s\n", pathname.string8().c_str(), err.what());
|
|
|
|
|
return ERR::EXCEPTION;
|
2009-08-04 12:57:53 -07:00
|
|
|
}
|
2026-06-12 12:10:57 -07:00
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
2009-08-04 12:57:53 -07:00
|
|
|
|
2013-09-10 07:17:04 -07:00
|
|
|
Status GetDirectoryEntries(const OsPath& path, CFileInfos* files, DirectoryNames* subdirectoryNames)
|
2009-08-04 12:57:53 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
try
|
2009-08-04 12:57:53 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
for (const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator(path.string()))
|
2023-04-19 09:35:00 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
if (entry.is_directory() && entry.path().filename() != "." && entry.path().filename() != ".." && subdirectoryNames)
|
|
|
|
|
{
|
|
|
|
|
subdirectoryNames->emplace_back(entry.path().filename());
|
|
|
|
|
}
|
|
|
|
|
else if (entry.is_regular_file() && files)
|
2023-04-19 09:35:00 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
files->emplace_back(entry.path().filename().wstring(), static_cast<u64>(entry.file_size()),
|
|
|
|
|
static_cast<time_t>(std::chrono::duration_cast<std::chrono::seconds>(entry.last_write_time().time_since_epoch()).count()));
|
2023-04-19 09:35:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
2009-08-04 12:57:53 -07:00
|
|
|
}
|
2026-06-12 12:10:57 -07:00
|
|
|
catch (std::filesystem::filesystem_error& err)
|
|
|
|
|
{
|
|
|
|
|
debug_printf("GetDirectoryEntries: failed to get directory entries for'%s', reason: %s\n", path.string8().c_str(), err.what());
|
|
|
|
|
return ERR::EXCEPTION;
|
|
|
|
|
}
|
|
|
|
|
return INFO::OK;
|
2009-08-04 12:57:53 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-12 12:10:57 -07:00
|
|
|
namespace
|
|
|
|
|
{
|
2009-08-04 12:57:53 -07:00
|
|
|
|
2026-06-12 12:10:57 -07:00
|
|
|
std::filesystem::perms ModeTToPerms(mode_t mode)
|
2009-08-04 12:57:53 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
using std::filesystem::perms;
|
|
|
|
|
perms perm{perms::none};
|
|
|
|
|
|
|
|
|
|
if (mode | S_IRUSR)
|
|
|
|
|
perm |= perms::owner_read;
|
|
|
|
|
if (mode | S_IWUSR)
|
|
|
|
|
perm |= perms::owner_write;
|
|
|
|
|
if (mode | S_IXUSR)
|
|
|
|
|
perm |= perms::owner_exec;
|
|
|
|
|
|
|
|
|
|
if (mode | S_IRGRP)
|
|
|
|
|
perm |= perms::group_read;
|
|
|
|
|
if (mode | S_IWGRP)
|
|
|
|
|
perm |= perms::group_write;
|
|
|
|
|
if (mode | S_IXGRP)
|
|
|
|
|
perm |= perms::group_exec;
|
|
|
|
|
|
|
|
|
|
if (mode | S_IROTH)
|
|
|
|
|
perm |= perms::others_read;
|
|
|
|
|
if (mode | S_IWOTH)
|
|
|
|
|
perm |= perms::others_write;
|
|
|
|
|
if (mode | S_IXOTH)
|
|
|
|
|
perm |= perms::others_exec;
|
|
|
|
|
|
|
|
|
|
return perm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Status CreateDirectoriesImpl(const std::filesystem::path& path, const std::filesystem::perms& perms)
|
|
|
|
|
{
|
|
|
|
|
if (std::filesystem::exists(path))
|
|
|
|
|
return ERR::FAIL;
|
2011-03-21 10:53:13 -07:00
|
|
|
|
2026-06-12 12:10:57 -07:00
|
|
|
if (!std::filesystem::is_directory(path.parent_path()))
|
2009-08-04 12:57:53 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
const Status status = CreateDirectoriesImpl(path.parent_path(), perms);
|
|
|
|
|
if (status != INFO::OK)
|
|
|
|
|
return status;
|
2009-08-04 12:57:53 -07:00
|
|
|
}
|
|
|
|
|
|
2026-06-12 12:10:57 -07:00
|
|
|
std::filesystem::create_directory(path);
|
|
|
|
|
std::filesystem::permissions(path, perms);
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
2009-11-14 02:57:08 -08:00
|
|
|
|
2026-06-12 12:10:57 -07:00
|
|
|
} // namespace
|
2009-08-04 12:57:53 -07:00
|
|
|
|
2026-06-12 12:10:57 -07:00
|
|
|
Status CreateDirectories(const OsPath& path, mode_t mode, bool breakpoint)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return CreateDirectoriesImpl(std::filesystem::path(path.string()), ModeTToPerms(mode));
|
|
|
|
|
}
|
|
|
|
|
catch (std::filesystem::filesystem_error& err)
|
2012-02-16 05:50:31 -08:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
debug_printf("CreateDirectories: failed to create directories '%s', reason: %s\n", path.string8().c_str(), err.what());
|
2016-02-19 03:22:32 -08:00
|
|
|
if (breakpoint)
|
2026-06-12 12:10:57 -07:00
|
|
|
WARN_RETURN(ERR::EXCEPTION);
|
|
|
|
|
else
|
|
|
|
|
return ERR::EXCEPTION;
|
2012-02-16 05:50:31 -08:00
|
|
|
}
|
2009-08-04 12:57:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status DeleteDirectory(const OsPath& path)
|
2009-08-04 12:57:53 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
try
|
2009-08-04 12:57:53 -07:00
|
|
|
{
|
2026-06-12 12:10:57 -07:00
|
|
|
std::filesystem::remove_all(path.string());
|
|
|
|
|
}
|
|
|
|
|
catch (std::filesystem::filesystem_error& err)
|
|
|
|
|
{
|
|
|
|
|
debug_printf("DeleteDirectory: failed to delete directory '%s', reason: %s\n", path.string8().c_str(), err.what());
|
|
|
|
|
return ERR::EXCEPTION;
|
2009-08-04 12:57:53 -07:00
|
|
|
}
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
2018-04-14 18:46:28 -07:00
|
|
|
|
2021-08-22 04:35:34 -07:00
|
|
|
Status RenameFile(const OsPath& path, const OsPath& newPath)
|
|
|
|
|
{
|
|
|
|
|
if (path.empty())
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-10-20 04:45:11 -07:00
|
|
|
std::filesystem::rename(std::filesystem::path(path.string()), std::filesystem::path(newPath.string()));
|
2021-08-22 04:35:34 -07:00
|
|
|
}
|
2025-10-20 04:45:11 -07:00
|
|
|
catch (std::filesystem::filesystem_error& err)
|
2021-08-22 04:35:34 -07:00
|
|
|
{
|
|
|
|
|
debug_printf("RenameFile: failed to rename %s to %s.\n%s\n", path.string8().c_str(), path.string8().c_str(), err.what());
|
|
|
|
|
return ERR::EXCEPTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
|
|
|
|
|
}
|
2018-04-14 18:46:28 -07:00
|
|
|
|
|
|
|
|
Status CopyFile(const OsPath& path, const OsPath& newPath, bool override_if_exists/* = false*/)
|
|
|
|
|
{
|
|
|
|
|
if(path.empty())
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if(override_if_exists)
|
2025-10-20 04:45:11 -07:00
|
|
|
std::filesystem::copy_file(std::filesystem::path(path.string()), std::filesystem::path(newPath.string()), std::filesystem::copy_options::overwrite_existing);
|
2018-04-14 18:46:28 -07:00
|
|
|
else
|
2025-10-20 04:45:11 -07:00
|
|
|
std::filesystem::copy_file(std::filesystem::path(path.string()), std::filesystem::path(newPath.string()));
|
2018-04-14 18:46:28 -07:00
|
|
|
}
|
2025-10-20 04:45:11 -07:00
|
|
|
catch(std::filesystem::filesystem_error& err)
|
2018-04-14 18:46:28 -07:00
|
|
|
{
|
|
|
|
|
debug_printf("CopyFile: failed to copy %s to %s.\n%s\n", path.string8().c_str(), path.string8().c_str(), err.what());
|
|
|
|
|
return ERR::EXCEPTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|