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

          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             : #ifndef INCLUDED_VFS_TREE
      28             : #define INCLUDED_VFS_TREE
      29             : 
      30             : #include "lib/file/common/file_loader.h"  // PIFileLoader
      31             : #include "lib/file/common/real_directory.h"   // PRealDirectory
      32             : #include "lib/file/vfs/vfs_path.h"
      33             : 
      34             : #include <ctime>
      35             : #include <map>
      36             : #include <string>
      37             : 
      38             : class VfsFile
      39             : {
      40             : public:
      41             :     VfsFile(const VfsPath& name, size_t size, time_t mtime, size_t priority, const PIFileLoader& provider);
      42             : 
      43       50233 :     const VfsPath& Name() const
      44             :     {
      45       28126 :         return m_name;
      46             :     }
      47             : 
      48       50283 :     size_t Size() const
      49             :     {
      50       28176 :         return m_size;
      51             :     }
      52             : 
      53       22246 :     time_t MTime() const
      54             :     {
      55       22237 :         return m_mtime;
      56             :     }
      57             : 
      58         155 :     size_t Priority() const
      59             :     {
      60         155 :         return m_priority;
      61             :     }
      62             : 
      63             :     const PIFileLoader& Loader() const
      64             :     {
      65       23090 :         return m_loader;
      66             :     }
      67             : 
      68             : private:
      69             :     VfsPath m_name;
      70             :     size_t m_size;
      71             :     time_t m_mtime;
      72             : 
      73             :     size_t m_priority;
      74             : 
      75             :     PIFileLoader m_loader;
      76             : };
      77             : 
      78             : 
      79             : class VfsDirectory
      80             : {
      81             :     /**
      82             :      * remove all files with a lower priority than @p file
      83             :      * and do the same for all subdirectories recursively.
      84             :      * @return true if the directory is empty afterwards
      85             :      **/
      86             :     bool DeleteTree(const VfsFile& file);
      87             : 
      88             : public:
      89             :     typedef std::map<VfsPath, VfsFile> VfsFiles;
      90             :     typedef std::map<VfsPath, VfsDirectory> VfsSubdirectories;
      91             : 
      92             :     VfsDirectory();
      93             : 
      94             :     /**
      95             :      * remove the given file or subdirectory according to the priority of the
      96             :      * passed .DELETED file.
      97             :      * CAUTION: Invalidates all previously returned pointers of the file or
      98             :      *          subdirectory (and contents) if those have lower priority
      99             :      *          than @p file.
     100             :      **/
     101             :     void DeleteSubtree(const VfsFile& file);
     102             : 
     103             :     /**
     104             :      * @return address of existing or newly inserted file.
     105             :      **/
     106             :     VfsFile* AddFile(const VfsFile& file);
     107             : 
     108             :     /**
     109             :      * @return address of existing or newly inserted subdirectory.
     110             :      **/
     111             :     VfsDirectory* AddSubdirectory(const VfsPath& name);
     112             : 
     113             :     /**
     114             :      * remove the given file from the virtual directory (no effect on
     115             :      * the physical file). no effect if the file does not exist.
     116             :      **/
     117             :     void RemoveFile(const VfsPath& name);
     118             : 
     119             :     /**
     120             :      * @return file with the given name.
     121             :      * (note: non-const to allow changes to the file)
     122             :      **/
     123             :     VfsFile* GetFile(const VfsPath& name);
     124             : 
     125             :     /**
     126             :      * @return subdirectory with the given name.
     127             :      * (note: non-const to allow changes to the subdirectory)
     128             :      **/
     129             :     VfsDirectory* GetSubdirectory(const VfsPath& name);
     130             : 
     131             :     // note: exposing only iterators wouldn't enable callers to reserve space.
     132             : 
     133             :     const VfsFiles& Files() const
     134             :     {
     135         604 :         return m_files;
     136             :     }
     137             : 
     138             :     const VfsSubdirectories& Subdirectories() const
     139             :     {
     140         242 :         return m_subdirectories;
     141             :     }
     142             : 
     143             :     /**
     144             :      * side effect: the next ShouldPopulate() will return true.
     145             :      **/
     146             :     void SetAssociatedDirectory(const PRealDirectory& realDirectory);
     147             : 
     148             :     const PRealDirectory& AssociatedDirectory() const
     149             :     {
     150      277683 :         return m_realDirectory;
     151             :     }
     152             : 
     153             :     /**
     154             :      * @return whether this directory should be populated from its
     155             :      * AssociatedDirectory(). note that calling this is a promise to
     156             :      * do so if true is returned -- the flag is reset immediately.
     157             :      **/
     158             :     bool ShouldPopulate();
     159             : 
     160             :     /**
     161             :      * ensure the next ShouldPopulate returns true.
     162             :      **/
     163             :     void RequestRepopulate();
     164             : 
     165             :     /**
     166             :      * empty file and subdirectory lists (e.g. when rebuilding VFS).
     167             :      * CAUTION: this invalidates all previously returned pointers.
     168             :      **/
     169             :     void Clear();
     170             : 
     171             : private:
     172             :     VfsFiles m_files;
     173             :     VfsSubdirectories m_subdirectories;
     174             : 
     175             :     PRealDirectory m_realDirectory;
     176             :     bool m_shouldPopulate;
     177             : };
     178             : 
     179             : 
     180             : /**
     181             :  * @return a string containing file attributes (location, size, timestamp) and name.
     182             :  **/
     183             : extern std::wstring FileDescription(const VfsFile& file);
     184             : 
     185             : /**
     186             :  * @return a string holding each files' description (one per line).
     187             :  **/
     188             : extern std::wstring FileDescriptions(const VfsDirectory& directory, size_t indentLevel);
     189             : 
     190             : /**
     191             :  * append each directory's files' description to the given string.
     192             :  **/
     193             : void DirectoryDescriptionR(std::wstring& descriptions, const VfsDirectory& directory, size_t indentLevel);
     194             : 
     195             : #endif  // #ifndef INCLUDED_VFS_TREE

Generated by: LCOV version 1.16