LCOV - code coverage report
Current view: top level - source/lib/file - file_system.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 56 102 54.9 %
Date: 2026-12-03 01:19:39 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : /*
      24             :  * higher-level interface on top of sysdep/filesystem.h
      25             :  */
      26             : 
      27             : #include "precompiled.h"
      28             : 
      29             : #include "file_system.h"
      30             : 
      31             : #include "lib/debug.h"
      32             : #include "lib/posix/posix_filesystem.h"
      33             : 
      34             : #include <chrono>
      35             : #include <filesystem>
      36             : #include <version>
      37             : 
      38             : namespace
      39             : {
      40       11879 : time_t FileTimeToTimeT(const std::filesystem::file_time_type& fileTime)
      41             : {
      42             : #if defined(_MSVC_STL_VERSION)
      43             :     // clock_cast pulls in icu.dll for utc_clock which is only available since
      44             :     // Windows 10 1903, since casting to system_clock goes via utc_clock with
      45             :     // MSVC do the conversion manually for now.
      46             :     const std::uint64_t ticks{static_cast<std::uint64_t>(fileTime.time_since_epoch().count())};
      47             :     const std::int64_t unixTicks{static_cast<std::int64_t>(ticks) - 116444736000000000LL};
      48             :     return static_cast<time_t>(unixTicks / 10000000LL);
      49             : #elif defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L
      50             :     const auto sysTime{std::chrono::clock_cast<std::chrono::system_clock>(fileTime)};
      51             :     return static_cast<time_t>(std::chrono::duration_cast<std::chrono::seconds>(sysTime.time_since_epoch()).count());
      52             : #else
      53             :     // std::chrono::clock_cast missing in clang 14 with libstdc++ 12
      54           3 :     const auto sysTime{std::filesystem::file_time_type::clock::to_sys(fileTime)};
      55       11879 :     return static_cast<time_t>(std::chrono::duration_cast<std::chrono::seconds>(sysTime.time_since_epoch()).count());
      56             : #endif
      57             : }
      58             : } // namespace
      59             : 
      60         311 : bool DirectoryExists(const OsPath& path)
      61             : {
      62         311 :     try
      63             :     {
      64         622 :         return std::filesystem::is_directory(path.string());
      65             :     }
      66           0 :     catch (std::filesystem::filesystem_error& err)
      67             :     {
      68           0 :         debug_printf("DirectoryExists: failed to check if directory '%s' exists, reason: %s\n", path.string8().c_str(), err.what());
      69           0 :     }
      70           0 :     return false;
      71             : }
      72             : 
      73             : 
      74           3 : Status GetFileInfo(const OsPath& pathname, CFileInfo* pPtrInfo)
      75             : {
      76           3 :     try
      77             :     {
      78           3 :         const std::filesystem::path path{pathname.string()};
      79          15 :         *pPtrInfo = CFileInfo(path.filename().wstring(),
      80           3 :             static_cast<std::uint64_t>(std::filesystem::file_size(path)),
      81           6 :             FileTimeToTimeT(std::filesystem::last_write_time(path)));
      82           3 :     }
      83           0 :     catch (std::filesystem::filesystem_error& err)
      84             :     {
      85           0 :         debug_printf("GetFileInfo: failed to get file info for '%s', reason: %s\n", pathname.string8().c_str(), err.what());
      86           0 :         return ERR::EXCEPTION;
      87           0 :     }
      88           3 :     return INFO::OK;
      89             : }
      90             : 
      91        1110 : Status GetDirectoryEntries(const OsPath& path, CFileInfos* files, DirectoryNames* subdirectoryNames)
      92             : {
      93        1110 :     try
      94             :     {
      95       16288 :         for (const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator(path.string()))
      96             :         {
      97       13301 :             if (entry.is_directory() && entry.path().filename() != "." && entry.path().filename() != ".." && subdirectoryNames)
      98             :             {
      99        1425 :                 subdirectoryNames->emplace_back(entry.path().filename());
     100             :             }
     101       11876 :             else if (entry.is_regular_file() && files)
     102             :             {
     103       35628 :                 files->emplace_back(entry.path().filename().wstring(),
     104       11876 :                     static_cast<std::uint64_t>(entry.file_size()),
     105       23752 :                     FileTimeToTimeT(entry.last_write_time()));
     106             :             }
     107        1110 :         }
     108             :     }
     109           0 :     catch (std::filesystem::filesystem_error& err)
     110             :     {
     111           0 :         debug_printf("GetDirectoryEntries: failed to get directory entries for'%s', reason: %s\n", path.string8().c_str(), err.what());
     112           0 :         return ERR::EXCEPTION;
     113           0 :     }
     114        1110 :     return INFO::OK;
     115             : }
     116             : 
     117             : namespace
     118             : {
     119             : 
     120         128 : std::filesystem::perms ModeTToPerms(mode_t mode)
     121             : {
     122         128 :     using std::filesystem::perms;
     123         128 :     perms perm{perms::none};
     124             : 
     125         128 :     if (mode & S_IRUSR)
     126         128 :         perm |= perms::owner_read;
     127         128 :     if (mode & S_IWUSR)
     128         128 :         perm |= perms::owner_write;
     129         128 :     if (mode & S_IXUSR)
     130         128 :         perm |= perms::owner_exec;
     131             : 
     132         128 :     if (mode & S_IRGRP)
     133           0 :         perm |= perms::group_read;
     134         128 :     if (mode & S_IWGRP)
     135           0 :         perm |= perms::group_write;
     136         128 :     if (mode & S_IXGRP)
     137           0 :         perm |= perms::group_exec;
     138             : 
     139         128 :     if (mode & S_IROTH)
     140           0 :         perm |= perms::others_read;
     141         128 :     if (mode & S_IWOTH)
     142           0 :         perm |= perms::others_write;
     143         128 :     if (mode & S_IXOTH)
     144           0 :         perm |= perms::others_exec;
     145             : 
     146         128 :     return perm;
     147             : }
     148             : 
     149         255 : Status CreateDirectoriesImpl(const std::filesystem::path& path, const std::filesystem::perms& perms)
     150             : {
     151         510 :     if (std::filesystem::exists(path))
     152             :         return ERR::FAIL;
     153             : 
     154         510 :     if (!std::filesystem::is_directory(path.parent_path()))
     155             :     {
     156         127 :         const Status status = CreateDirectoriesImpl(path.parent_path(), perms);
     157         127 :         if (status != INFO::OK)
     158             :             return status;
     159             :     }
     160             : 
     161         255 :     std::filesystem::create_directory(path);
     162         255 :     std::filesystem::permissions(path, perms);
     163         255 :     return INFO::OK;
     164             : }
     165             : 
     166             : } // namespace
     167             : 
     168         128 : Status CreateDirectories(const OsPath& path, mode_t mode, bool breakpoint)
     169             : {
     170         128 :     try
     171             :     {
     172         128 :         return CreateDirectoriesImpl(std::filesystem::absolute(path.string()), ModeTToPerms(mode));
     173             :     }
     174           0 :     catch (std::filesystem::filesystem_error& err)
     175             :     {
     176           0 :         debug_printf("CreateDirectories: failed to create directories '%s', reason: %s\n", path.string8().c_str(), err.what());
     177           0 :         if (breakpoint)
     178           0 :             WARN_RETURN(ERR::EXCEPTION);
     179             :         else
     180             :             return ERR::EXCEPTION;
     181           0 :     }
     182             : }
     183             : 
     184             : 
     185         116 : Status DeleteDirectory(const OsPath& path)
     186             : {
     187         116 :     try
     188             :     {
     189         116 :         std::filesystem::remove_all(path.string());
     190             :     }
     191           0 :     catch (std::filesystem::filesystem_error& err)
     192             :     {
     193           0 :         debug_printf("DeleteDirectory: failed to delete directory '%s', reason: %s\n", path.string8().c_str(), err.what());
     194           0 :         return ERR::EXCEPTION;
     195           0 :     }
     196         116 :     return INFO::OK;
     197             : }
     198             : 
     199           0 : Status RenameFile(const OsPath& path, const OsPath& newPath)
     200             : {
     201           0 :     if (path.empty())
     202             :         return INFO::OK;
     203             : 
     204           0 :     try
     205             :     {
     206           0 :         std::filesystem::rename(std::filesystem::path(path.string()), std::filesystem::path(newPath.string()));
     207             :     }
     208           0 :     catch (std::filesystem::filesystem_error& err)
     209             :     {
     210           0 :         debug_printf("RenameFile: failed to rename %s to %s.\n%s\n", path.string8().c_str(), path.string8().c_str(), err.what());
     211           0 :         return ERR::EXCEPTION;
     212           0 :     }
     213             : 
     214           0 :     return INFO::OK;
     215             : 
     216             : }
     217             : 
     218           0 : Status CopyFile(const OsPath& path, const OsPath& newPath, bool override_if_exists/* = false*/)
     219             : {
     220           0 :     if(path.empty())
     221             :         return INFO::OK;
     222             : 
     223           0 :     try
     224             :     {
     225           0 :         if(override_if_exists)
     226           0 :             std::filesystem::copy_file(std::filesystem::path(path.string()), std::filesystem::path(newPath.string()), std::filesystem::copy_options::overwrite_existing);
     227             :         else
     228           0 :             std::filesystem::copy_file(std::filesystem::path(path.string()), std::filesystem::path(newPath.string()));
     229             :     }
     230           0 :     catch(std::filesystem::filesystem_error& err)
     231             :     {
     232           0 :         debug_printf("CopyFile: failed to copy %s to %s.\n%s\n", path.string8().c_str(), path.string8().c_str(), err.what());
     233           0 :         return ERR::EXCEPTION;
     234           0 :     }
     235             : 
     236             :     return INFO::OK;
     237             : }

Generated by: LCOV version 1.16