LCOV - code coverage report
Current view: top level - source/lib/file/vfs - vfs_populate.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 50 72 69.4 %
Date: 2026-12-03 01:19:39 Functions: 6 7 85.7 %

          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             :  * populate VFS directories with files
      25             :  */
      26             : 
      27             : #include "precompiled.h"
      28             : 
      29             : #include "vfs_populate.h"
      30             : 
      31             : #include "lib/code_annotation.h"
      32             : #include "lib/file/archive/archive.h"
      33             : #include "lib/file/archive/archive_zip.h"
      34             : #include "lib/file/file_system.h"
      35             : #include "lib/file/vfs/vfs.h" // error codes
      36             : #include "lib/file/vfs/vfs_lookup.h"
      37             : #include "lib/file/vfs/vfs_path.h"
      38             : #include "lib/file/vfs/vfs_tree.h"
      39             : #include "lib/os_path.h"
      40             : #include "lib/path.h"
      41             : 
      42             : #include <cstddef>
      43             : #include <cstdint>
      44             : 
      45             : struct CompareFileInfoByName
      46             : {
      47             :     bool operator()(const CFileInfo& a, const CFileInfo& b)
      48             :     {
      49             :         return a.Name() < b.Name();
      50             :     }
      51             : };
      52             : 
      53             : // helper class that allows breaking up the logic into sub-functions without
      54             : // always having to pass directory/realDirectory as parameters.
      55        1110 : class PopulateHelper
      56             : {
      57             :     NONCOPYABLE(PopulateHelper);
      58             : public:
      59        1110 :     PopulateHelper(VfsDirectory* directory, const PRealDirectory& realDirectory)
      60        2220 :         : m_directory(directory), m_realDirectory(realDirectory)
      61             :     {
      62             :     }
      63             : 
      64        1110 :     Status AddEntries() const
      65             :     {
      66        1110 :         CFileInfos files; files.reserve(500);
      67        1110 :         DirectoryNames subdirectoryNames; subdirectoryNames.reserve(50);
      68        1110 :         RETURN_STATUS_IF_ERR(GetDirectoryEntries(m_realDirectory->Path(), &files, &subdirectoryNames));
      69             : 
      70             :         // Since .DELETED files only remove files in lower priority mods
      71             :         // loose files and archive files have no conflicts so we do not need
      72             :         // to sort them.
      73             :         // We add directories after they might have been removed by .DELETED
      74             :         // files (as they did not contain any files at that point). The order
      75             :         // of GetDirectoryEntries is undefined, but that does not really matter (TODO really?)
      76             :         // so we do not need to sort its output.
      77        1110 :         RETURN_STATUS_IF_ERR(AddFiles(files));
      78        1110 :         AddSubdirectories(subdirectoryNames);
      79             : 
      80             :         return INFO::OK;
      81        1110 :     }
      82             : 
      83             : private:
      84       11876 :     void AddFile(const CFileInfo& fileInfo) const
      85             :     {
      86       11876 :         const VfsPath name = fileInfo.Name();
      87       11876 :         const VfsFile file(name, (size_t)fileInfo.Size(), fileInfo.MTime(), m_realDirectory->Priority(), m_realDirectory);
      88       23752 :         if(name.Extension() == L".DELETED")
      89             :         {
      90           0 :             m_directory->DeleteSubtree(file);
      91           0 :             if(!(m_realDirectory->Flags() & VFS_MOUNT_KEEP_DELETED))
      92           0 :                 return;
      93             :         }
      94             : 
      95       11876 :         m_directory->AddFile(file);
      96       23752 :     }
      97             : 
      98           0 :     static void AddArchiveFile(const VfsPath& pathname, const CFileInfo& fileInfo, PIArchiveFile archiveFile, uintptr_t cbData)
      99             :     {
     100           0 :         PopulateHelper* this_ = (PopulateHelper*)cbData;
     101             : 
     102             :         // (we have to create missing subdirectoryNames because archivers
     103             :         // don't always place directory entries before their files)
     104           0 :         const size_t flags = VFS_LOOKUP_ADD|VFS_LOOKUP_SKIP_POPULATE;
     105           0 :         VfsDirectory* directory;
     106           0 :         WARN_IF_ERR(vfs_Lookup(pathname, this_->m_directory, directory, 0, flags));
     107             : 
     108           0 :         const VfsPath name = fileInfo.Name();
     109           0 :         const VfsFile file(name, (size_t)fileInfo.Size(), fileInfo.MTime(), this_->m_realDirectory->Priority(), archiveFile);
     110           0 :         if(name.Extension() == L".DELETED")
     111             :         {
     112           0 :             directory->DeleteSubtree(file);
     113           0 :             if(!(this_->m_realDirectory->Flags() & VFS_MOUNT_KEEP_DELETED))
     114           0 :                 return;
     115             :         }
     116             : 
     117           0 :         directory->AddFile(file);
     118             :     }
     119             : 
     120        1110 :     Status AddFiles(const CFileInfos& files) const
     121             :     {
     122        1110 :         const OsPath path(m_realDirectory->Path());
     123             : 
     124       12986 :         for(size_t i = 0; i < files.size(); i++)
     125             :         {
     126       11876 :             const OsPath pathname = path / files[i].Name();
     127       11876 :             if(pathname.Extension() == L".zip")
     128             :             {
     129           0 :                 PIArchiveReader archiveReader = CreateArchiveReader_Zip(pathname);
     130             :                 // archiveReader == nullptr if file could not be opened (e.g. because
     131             :                 // archive is currently open in another program)
     132           0 :                 if(archiveReader)
     133           0 :                     RETURN_STATUS_IF_ERR(archiveReader->ReadEntries(AddArchiveFile, (uintptr_t)this));
     134           0 :             }
     135             :             else    // regular (non-archive) file
     136       11876 :                 AddFile(files[i]);
     137       11876 :         }
     138             : 
     139             :         return INFO::OK;
     140        1110 :     }
     141             : 
     142        1110 :     void AddSubdirectories(const DirectoryNames& subdirectoryNames) const
     143             :     {
     144        2535 :         for(size_t i = 0; i < subdirectoryNames.size(); i++)
     145             :         {
     146             :             // skip version control directories - this avoids cluttering the
     147             :             // VFS with hundreds of irrelevant files.
     148        4275 :             if(subdirectoryNames[i] == L".svn" || subdirectoryNames[i] == L".git")
     149           0 :                 continue;
     150             : 
     151        1425 :             VfsDirectory* subdirectory = m_directory->AddSubdirectory(subdirectoryNames[i]);
     152        1425 :             PRealDirectory realDirectory = CreateRealSubdirectory(m_realDirectory, subdirectoryNames[i]);
     153        1425 :             vfs_Attach(subdirectory, realDirectory);
     154        1425 :         }
     155        1110 :     }
     156             : 
     157             :     VfsDirectory* const m_directory;
     158             :     PRealDirectory m_realDirectory;
     159             : };
     160             : 
     161             : 
     162      347919 : Status vfs_Populate(VfsDirectory* directory)
     163             : {
     164      347919 :     if(!directory->ShouldPopulate())
     165             :         return INFO::OK;
     166             : 
     167        1110 :     const PRealDirectory& realDirectory = directory->AssociatedDirectory();
     168             : 
     169        1110 :     if(realDirectory->Flags() & VFS_MOUNT_WATCH)
     170           0 :         realDirectory->Watch();
     171             : 
     172        1110 :     PopulateHelper helper(directory, realDirectory);
     173        1110 :     RETURN_STATUS_IF_ERR(helper.AddEntries());
     174             : 
     175             :     return INFO::OK;
     176      347919 : }
     177             : 
     178             : 
     179        1924 : Status vfs_Attach(VfsDirectory* directory, const PRealDirectory& realDirectory)
     180             : {
     181        1924 :     PRealDirectory existingRealDir = directory->AssociatedDirectory();
     182             : 
     183             :     // Don't allow replacing the real directory by a lower-priority one.
     184        1924 :     if (!existingRealDir || existingRealDir->Priority() < realDirectory->Priority())
     185             :     {
     186             :         // This ordering is peculiar but useful, as it "defers" the population call.
     187             :         // If there is already a real directory, we will replace it (and lose track of it),
     188             :         // so we'll populate it right away, but the 'new' real directory can wait until we access it.
     189        1850 :         RETURN_STATUS_IF_ERR(vfs_Populate(directory));
     190        1850 :         directory->SetAssociatedDirectory(realDirectory);
     191             :         return INFO::OK;
     192             :     }
     193             :     // We are attaching a lower-priority real directory.
     194             :     // Because of deferred population, we need to immediately populate this new directory.
     195          74 :     bool shouldPop = directory->ShouldPopulate();
     196             :     // This sets "should populate" to true, so the vfs_Populate call below immediately populates.
     197          74 :     directory->SetAssociatedDirectory(realDirectory);
     198          74 :     RETURN_STATUS_IF_ERR(vfs_Populate(directory));
     199             :     // Reset to the higher priority realDirectory, which resets ShouldPopulate to true.
     200          74 :     directory->SetAssociatedDirectory(existingRealDir);
     201             :     // Avoid un-necessary repopulation by clearing the flag.
     202          74 :     if (!shouldPop)
     203           0 :         directory->ShouldPopulate();
     204             : 
     205             :     return INFO::OK;
     206        1924 : }

Generated by: LCOV version 1.16