LCOV - code coverage report
Current view: top level - source/lib/file/vfs - vfs_tree.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 85 113 75.2 %
Date: 2026-12-03 01:19:39 Functions: 15 17 88.2 %

          Line data    Source code
       1             : /* Copyright (C) 2025 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             :  * 'tree' of VFS directories and files
      25             :  */
      26             : 
      27             : #include "precompiled.h"
      28             : 
      29             : #include "vfs_tree.h"
      30             : 
      31             : #include "lib/code_annotation.h"
      32             : #include "lib/debug.h"
      33             : #include "lib/file/common/file_stats.h"
      34             : #include "lib/secure_crt.h"
      35             : #include "lib/status.h"
      36             : 
      37             : #include <ctime>
      38             : #include <cwchar>
      39             : #include <utility>
      40             : 
      41             : 
      42             : //-----------------------------------------------------------------------------
      43             : 
      44       14024 : VfsFile::VfsFile(const VfsPath& name, size_t size, time_t mtime, size_t priority, const PIFileLoader& loader)
      45       14024 :     : m_name(name), m_size(size), m_mtime(mtime), m_priority(priority), m_loader(loader)
      46             : {
      47       14024 : }
      48             : 
      49             : 
      50             : 
      51             : //-----------------------------------------------------------------------------
      52             : 
      53        1852 : VfsDirectory::VfsDirectory()
      54        1852 :     : m_shouldPopulate(0)
      55             : {
      56         145 : }
      57             : 
      58          12 : static bool ShouldDelete(const VfsFile& file, const VfsFile& deletedFile)
      59             : {
      60             :     // We only check priority here, a .DELETED file in a mod should not
      61             :     // delete files in that mod. For the same reason we ignore loose
      62             :     // .DELETED files next to an archive.
      63           9 :     return file.Priority() < deletedFile.Priority();
      64             : }
      65             : 
      66         136 : static bool ShouldReplaceWith(const VfsFile& previousFile, const VfsFile& newFile)
      67             : {
      68             :     // 1) priority (override mods)
      69         136 :     if(newFile.Priority() < previousFile.Priority())
      70             :         return false;
      71         135 :     if(newFile.Priority() > previousFile.Priority())
      72             :         return true;
      73             : 
      74             :     // 2) timestamp
      75         129 :     {
      76         129 :         const double howMuchNewer = difftime(newFile.MTime(), previousFile.MTime());
      77         129 :         const double threshold = 2.0;   // FAT timestamp resolution [seconds]
      78         129 :         if(howMuchNewer > threshold) // newer
      79             :             return true;
      80         126 :         if(howMuchNewer < -threshold)    // older
      81             :             return false;
      82             :         // else: "equal" (tolerating small differences due to FAT's low
      83             :         // mtime resolution)
      84             :     }
      85             : 
      86             :     // 3) precedence (efficiency of file provider)
      87         125 :     if(newFile.Loader()->Precedence() < previousFile.Loader()->Precedence())
      88             :         return false;
      89             : 
      90             :     return true;
      91             : }
      92             : 
      93             : 
      94           9 : void VfsDirectory::DeleteSubtree(const VfsFile& file)
      95             : {
      96          18 :     ENSURE(file.Name().Extension() == L".DELETED");
      97             : 
      98           9 :     const VfsPath basename = file.Name().Basename();
      99           9 :     std::map<VfsPath, VfsFile>::iterator fit = m_files.find(basename);
     100           9 :     if(fit != m_files.end() && ShouldDelete(fit->second, file))
     101           2 :         m_files.erase(basename);
     102             : 
     103           9 :     std::map<VfsPath, VfsDirectory>::iterator dit = m_subdirectories.find(basename);
     104           9 :     if(dit != m_subdirectories.end() && dit->second.DeleteTree(file))
     105           5 :         m_subdirectories.erase(dit);
     106           9 : }
     107             : 
     108           6 : bool VfsDirectory::DeleteTree(const VfsFile& file)
     109             : {
     110           6 :     for(std::map<VfsPath, VfsFile>::iterator it = m_files.begin(); it != m_files.end();)
     111           9 :         if(ShouldDelete(it->second, file))
     112           8 :             it = m_files.erase(it);
     113             :         else
     114          16 :             ++it;
     115             : 
     116           6 :     for(std::map<VfsPath, VfsDirectory>::iterator it = m_subdirectories.begin(); it != m_subdirectories.end();)
     117           0 :         if(it->second.DeleteTree(file))
     118           0 :             it = m_subdirectories.erase(it);
     119             :         else
     120           6 :             ++it;
     121             : 
     122           6 :     return m_files.empty() && m_subdirectories.empty();
     123             : }
     124             : 
     125             : 
     126       14017 : VfsFile* VfsDirectory::AddFile(const VfsFile& file)
     127             : {
     128       14017 :     std::pair<VfsPath, VfsFile> value = std::make_pair(file.Name(), file);
     129       14017 :     std::pair<VfsFiles::iterator, bool> ret = m_files.insert(value);
     130       14017 :     if(!ret.second) // already existed
     131             :     {
     132         136 :         VfsFile& previousFile = ret.first->second;
     133         136 :         const VfsFile& newFile = value.second;
     134         136 :         if(ShouldReplaceWith(previousFile, newFile))
     135         134 :             previousFile = newFile;
     136             :     }
     137             :     else
     138             :     {
     139       14017 :         stats_vfs_file_add(file.Size());
     140             :     }
     141             : 
     142       14017 :     return &(*ret.first).second;
     143       14017 : }
     144             : 
     145             : 
     146             : // rationale: passing in a pre-constructed VfsDirectory and copying that into
     147             : // our map would be slower and less convenient for the caller.
     148        1707 : VfsDirectory* VfsDirectory::AddSubdirectory(const VfsPath& name)
     149             : {
     150        1707 :     std::pair<VfsPath, VfsDirectory> value = std::make_pair(name.string(), VfsDirectory());
     151        1707 :     std::pair<VfsSubdirectories::iterator, bool> ret = m_subdirectories.insert(value);
     152        1707 :     return &(*ret.first).second;
     153        1707 : }
     154             : 
     155             : 
     156          12 : void VfsDirectory::RemoveFile(const VfsPath& name)
     157             : {
     158          36 :     m_files.erase(name.string());
     159          12 : }
     160             : 
     161             : 
     162       74891 : VfsFile* VfsDirectory::GetFile(const VfsPath& name)
     163             : {
     164      149774 :     VfsFiles::iterator it = m_files.find(name.string());
     165       74891 :     if(it == m_files.end())
     166             :         return 0;
     167       52000 :     return &it->second;
     168             : }
     169             : 
     170      268416 : VfsDirectory* VfsDirectory::GetSubdirectory(const VfsPath& name)
     171             : {
     172      534253 :     VfsSubdirectories::iterator it = m_subdirectories.find(name.string());
     173      268416 :     if(it == m_subdirectories.end())
     174             :         return 0;
     175      262796 :     return &it->second;
     176             : }
     177             : 
     178             : 
     179        1998 : void VfsDirectory::SetAssociatedDirectory(const PRealDirectory& realDirectory)
     180             : {
     181        1998 :     if(m_shouldPopulate)
     182           0 :         DEBUG_WARN_ERR(ERR::LOGIC); // caller didn't check ShouldPopulate
     183        1998 :     m_shouldPopulate = true;
     184        1998 :     m_realDirectory = realDirectory;
     185        1998 : }
     186             : 
     187             : 
     188      347993 : bool VfsDirectory::ShouldPopulate()
     189             : {
     190             :     // test and reset
     191      347993 :     if(!m_shouldPopulate)
     192             :         return false;
     193             : 
     194        1184 :     m_shouldPopulate = false;
     195        1184 :     return true;
     196             : }
     197             : 
     198             : 
     199          12 : void VfsDirectory::RequestRepopulate()
     200             : {
     201          12 :     m_shouldPopulate = 1;
     202          12 : }
     203             : 
     204             : 
     205           3 : void VfsDirectory::Clear()
     206             : {
     207           3 :     m_files.clear();
     208           3 :     m_subdirectories.clear();
     209           3 :     m_realDirectory.reset();
     210           3 :     m_shouldPopulate = 0;
     211           3 : }
     212             : 
     213             : 
     214             : //-----------------------------------------------------------------------------
     215             : 
     216           0 : std::wstring FileDescription(const VfsFile& file)
     217             : {
     218           0 :     wchar_t timestamp[25];
     219           0 :     const time_t mtime = file.MTime();
     220           0 :     wcsftime(timestamp, ARRAY_SIZE(timestamp), L"%a %b %d %H:%M:%S %Y", localtime(&mtime));
     221             : 
     222           0 :     wchar_t buf[200];
     223           0 :     swprintf_s(buf, ARRAY_SIZE(buf), L"(%lc; %6lu; %ls) %ls", file.Loader()->LocationCode(), (unsigned long)file.Size(), timestamp, file.Name().string().c_str());
     224           0 :     return buf;
     225             : }
     226             : 
     227             : 
     228           0 : std::wstring FileDescriptions(const VfsDirectory& directory, size_t indentLevel)
     229             : {
     230           0 :     VfsDirectory::VfsFiles files = directory.Files();
     231             : 
     232           0 :     std::wstring descriptions;
     233           0 :     descriptions.reserve(100*files.size());
     234             : 
     235           0 :     const std::wstring indentation(4*indentLevel, ' ');
     236           0 :     for(VfsDirectory::VfsFiles::const_iterator it = files.begin(); it != files.end(); ++it)
     237             :     {
     238           0 :         const VfsFile& file = it->second;
     239           0 :         descriptions += indentation;
     240           0 :         descriptions += FileDescription(file);
     241           0 :         descriptions += L"\n";
     242             :     }
     243             : 
     244           0 :     return descriptions;
     245           0 : }
     246             : 
     247             : 
     248           1 : void DirectoryDescriptionR(std::wstring& descriptions, const VfsDirectory& directory, size_t indentLevel)
     249             : {
     250           1 :     const std::wstring indentation(4*indentLevel, ' ');
     251             : 
     252           1 :     const VfsDirectory::VfsSubdirectories& subdirectories = directory.Subdirectories();
     253           1 :     for(VfsDirectory::VfsSubdirectories::const_iterator it = subdirectories.begin(); it != subdirectories.end(); ++it)
     254             :     {
     255           0 :         const VfsPath& name = it->first;
     256           0 :         const VfsDirectory& subdirectory = it->second;
     257           0 :         descriptions += indentation;
     258           0 :         descriptions += std::wstring(L"[") + name.string() + L"]\n";
     259           0 :         descriptions += FileDescriptions(subdirectory, indentLevel+1);
     260             : 
     261           0 :         DirectoryDescriptionR(descriptions, subdirectory, indentLevel+1);
     262             :     }
     263           1 : }

Generated by: LCOV version 1.16