Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vfs_tree.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2013 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 #include "lib/file/vfs/vfs_tree.h"
29 
30 #include <cstdio>
31 
33 #include "lib/sysdep/cpu.h"
34 
35 
36 //-----------------------------------------------------------------------------
37 
38 VfsFile::VfsFile(const VfsPath& name, size_t size, time_t mtime, size_t priority, const PIFileLoader& loader)
39  : m_name(name), m_size(size), m_mtime(mtime), m_priority(priority), m_loader(loader)
40 {
41 }
42 
43 
44 
45 //-----------------------------------------------------------------------------
46 
48  : m_shouldPopulate(0)
49 {
50 }
51 
52 
53 static bool ShouldReplaceWith(const VfsFile& previousFile, const VfsFile& newFile)
54 {
55  // 1) priority (override mods)
56  if(newFile.Priority() < previousFile.Priority())
57  return false;
58  if(newFile.Priority() > previousFile.Priority())
59  return true;
60 
61  // 2) timestamp
62  {
63  const double howMuchNewer = difftime(newFile.MTime(), previousFile.MTime());
64  const double threshold = 2.0; // FAT timestamp resolution [seconds]
65  if(howMuchNewer > threshold) // newer
66  return true;
67  if(howMuchNewer < -threshold) // older
68  return false;
69  // else: "equal" (tolerating small differences due to FAT's low
70  // mtime resolution)
71  }
72 
73  // 3) precedence (efficiency of file provider)
74  if(newFile.Loader()->Precedence() < previousFile.Loader()->Precedence())
75  return false;
76 
77  return true;
78 }
79 
80 
82 {
83  std::pair<VfsPath, VfsFile> value = std::make_pair(file.Name(), file);
84  std::pair<VfsFiles::iterator, bool> ret = m_files.insert(value);
85  if(!ret.second) // already existed
86  {
87  VfsFile& previousFile = ret.first->second;
88  const VfsFile& newFile = value.second;
89  if(ShouldReplaceWith(previousFile, newFile))
90  previousFile = newFile;
91  }
92  else
93  {
94  stats_vfs_file_add(file.Size());
95  }
96 
97  return &(*ret.first).second;
98 }
99 
100 
101 // rationale: passing in a pre-constructed VfsDirectory and copying that into
102 // our map would be slower and less convenient for the caller.
104 {
105  std::pair<VfsPath, VfsDirectory> value = std::make_pair(name.string(), VfsDirectory());
106  std::pair<VfsSubdirectories::iterator, bool> ret = m_subdirectories.insert(value);
107  return &(*ret.first).second;
108 }
109 
110 
112 {
113  m_files.erase(name.string());
114 }
115 
116 
118 {
119  VfsFiles::iterator it = m_files.find(name.string());
120  if(it == m_files.end())
121  return 0;
122  return &it->second;
123 }
124 
125 
127 {
128  VfsSubdirectories::iterator it = m_subdirectories.find(name.string());
129  if(it == m_subdirectories.end())
130  return 0;
131  return &it->second;
132 }
133 
134 
136 {
137  if(!cpu_CAS(&m_shouldPopulate, 0, 1))
138  DEBUG_WARN_ERR(ERR::LOGIC); // caller didn't check ShouldPopulate
139  m_realDirectory = realDirectory;
140 }
141 
142 
144 {
145  return cpu_CAS(&m_shouldPopulate, 1, 0); // test and reset
146 }
147 
148 
150 {
151  m_shouldPopulate = 1;
152 }
153 
154 
156 {
157  m_files.clear();
158  m_subdirectories.clear();
159  m_realDirectory.reset();
160  m_shouldPopulate = 0;
161 }
162 
163 
164 //-----------------------------------------------------------------------------
165 
166 std::wstring FileDescription(const VfsFile& file)
167 {
168  wchar_t timestamp[25];
169  const time_t mtime = file.MTime();
170  wcsftime(timestamp, ARRAY_SIZE(timestamp), L"%a %b %d %H:%M:%S %Y", localtime(&mtime));
171 
172  wchar_t buf[200];
173  swprintf_s(buf, ARRAY_SIZE(buf), L"(%c; %6lu; %ls) %ls", file.Loader()->LocationCode(), (unsigned long)file.Size(), timestamp, file.Name().string().c_str());
174  return buf;
175 }
176 
177 
178 std::wstring FileDescriptions(const VfsDirectory& directory, size_t indentLevel)
179 {
180  VfsDirectory::VfsFiles files = directory.Files();
181 
182  std::wstring descriptions;
183  descriptions.reserve(100*files.size());
184 
185  const std::wstring indentation(4*indentLevel, ' ');
186  for(VfsDirectory::VfsFiles::const_iterator it = files.begin(); it != files.end(); ++it)
187  {
188  const VfsFile& file = it->second;
189  descriptions += indentation;
190  descriptions += FileDescription(file);
191  descriptions += L"\n";
192  }
193 
194  return descriptions;
195 }
196 
197 
198 void DirectoryDescriptionR(std::wstring& descriptions, const VfsDirectory& directory, size_t indentLevel)
199 {
200  const std::wstring indentation(4*indentLevel, ' ');
201 
202  const VfsDirectory::VfsSubdirectories& subdirectories = directory.Subdirectories();
203  for(VfsDirectory::VfsSubdirectories::const_iterator it = subdirectories.begin(); it != subdirectories.end(); ++it)
204  {
205  const VfsPath& name = it->first;
206  const VfsDirectory& subdirectory = it->second;
207  descriptions += indentation;
208  descriptions += std::wstring(L"[") + name.string() + L"]\n";
209  descriptions += FileDescriptions(subdirectory, indentLevel+1);
210 
211  DirectoryDescriptionR(descriptions, subdirectory, indentLevel+1);
212  }
213 }
bool ShouldPopulate()
Definition: vfs_tree.cpp:143
const Status LOGIC
Definition: status.h:409
VfsFile * AddFile(const VfsFile &file)
Definition: vfs_tree.cpp:81
const VfsPath & Name() const
Definition: vfs_tree.h:42
void RemoveFile(const VfsPath &name)
remove the given file from the virtual directory (no effect on the physical file).
Definition: vfs_tree.cpp:111
void DirectoryDescriptionR(std::wstring &descriptions, const VfsDirectory &directory, size_t indentLevel)
append each directory&#39;s files&#39; description to the given string.
Definition: vfs_tree.cpp:198
volatile intptr_t m_shouldPopulate
Definition: vfs_tree.h:159
PRealDirectory m_realDirectory
Definition: vfs_tree.h:158
const VfsFiles & Files() const
Definition: vfs_tree.h:116
int swprintf_s(wchar_t *buf, size_t max_chars, const wchar_t *fmt,...) WPRINTF_ARGS(3)
std::wstring FileDescriptions(const VfsDirectory &directory, size_t indentLevel)
Definition: vfs_tree.cpp:178
#define ARRAY_SIZE(name)
size_t Size() const
Definition: vfs_tree.h:47
void SetAssociatedDirectory(const PRealDirectory &realDirectory)
side effect: the next ShouldPopulate() will return true.
Definition: vfs_tree.cpp:135
VfsFiles m_files
Definition: vfs_tree.h:155
VfsFile * GetFile(const VfsPath &name)
Definition: vfs_tree.cpp:117
const VfsSubdirectories & Subdirectories() const
Definition: vfs_tree.h:121
std::wstring FileDescription(const VfsFile &file)
Definition: vfs_tree.cpp:166
Definition: path.h:75
shared_ptr< RealDirectory > PRealDirectory
const String & string() const
Definition: path.h:123
VfsSubdirectories m_subdirectories
Definition: vfs_tree.h:156
size_t Priority() const
Definition: vfs_tree.h:57
time_t MTime() const
Definition: vfs_tree.h:52
shared_ptr< IFileLoader > PIFileLoader
Definition: file_loader.h:39
const PIFileLoader & Loader() const
Definition: vfs_tree.h:62
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Definition: debug.h:331
static bool ShouldReplaceWith(const VfsFile &previousFile, const VfsFile &newFile)
Definition: vfs_tree.cpp:53
void Clear()
empty file and subdirectory lists (e.g.
Definition: vfs_tree.cpp:155
bool cpu_CAS(volatile intptr_t *location, intptr_t expected, intptr_t newValue)
atomic &quot;compare and swap&quot;.
Definition: arm.cpp:36
std::map< VfsPath, VfsFile > VfsFiles
Definition: vfs_tree.h:81
void RequestRepopulate()
ensure the next ShouldPopulate returns true.
Definition: vfs_tree.cpp:149
std::map< VfsPath, VfsDirectory > VfsSubdirectories
Definition: vfs_tree.h:82
#define stats_vfs_file_add(file_size)
Definition: file_stats.h:91
VfsDirectory * GetSubdirectory(const VfsPath &name)
Definition: vfs_tree.cpp:126
VfsFile(const VfsPath &name, size_t size, time_t mtime, size_t priority, const PIFileLoader &provider)
Definition: vfs_tree.cpp:38
VfsDirectory * AddSubdirectory(const VfsPath &name)
Definition: vfs_tree.cpp:103