Pyrogenesis  trunk
file_system.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 #ifndef INCLUDED_FILE_SYSTEM
28 #define INCLUDED_FILE_SYSTEM
29 
30 #include "lib/os_path.h"
31 #include "lib/posix/posix_filesystem.h" // mode_t
32 
33 #include <vector>
34 
35 bool DirectoryExists(const OsPath& path);
36 bool FileExists(const OsPath& pathname);
37 
38 u64 FileSize(const OsPath& pathname);
39 
40 
41 // (bundling size and mtime avoids a second expensive call to stat())
42 class CFileInfo
43 {
44 public:
46  {
47  }
48 
49  CFileInfo(const OsPath& name, off_t size, time_t mtime)
50  : name(name), size(size), mtime(mtime)
51  {
52  }
53 
54  const OsPath& Name() const
55  {
56  return name;
57  }
58 
59  off_t Size() const
60  {
61  return size;
62  }
63 
64  time_t MTime() const
65  {
66  return mtime;
67  }
68 
69 private:
72  time_t mtime;
73 };
74 
75 Status GetFileInfo(const OsPath& pathname, CFileInfo* fileInfo);
76 
77 typedef std::vector<CFileInfo> CFileInfos;
78 typedef std::vector<OsPath> DirectoryNames;
79 
80 Status GetDirectoryEntries(const OsPath& path, CFileInfos* files, DirectoryNames* subdirectoryNames);
81 
82 // same as boost::filesystem::create_directories, except that mkdir is invoked with
83 // <mode> instead of 0755.
84 // If the breakpoint is enabled, debug_break will be called if the directory didn't exist and couldn't be created.
85 Status CreateDirectories(const OsPath& path, mode_t mode, bool breakpoint = true);
86 
87 Status DeleteDirectory(const OsPath& dirPath);
88 
89 Status CopyFile(const OsPath& path, const OsPath& newPath, bool override_if_exists = false);
90 
91 Status RenameFile(const OsPath& path, const OsPath& newPath);
92 
93 #endif // #ifndef INCLUDED_FILE_SYSTEM
time_t MTime() const
Definition: file_system.h:64
CFileInfo(const OsPath &name, off_t size, time_t mtime)
Definition: file_system.h:49
Status RenameFile(const OsPath &path, const OsPath &newPath)
Definition: file_system.cpp:194
const OsPath & Name() const
Definition: file_system.h:54
off_t Size() const
Definition: file_system.h:59
bool DirectoryExists(const OsPath &path)
Definition: file_system.cpp:36
OsPath name
Definition: file_system.h:70
uint64_t u64
Definition: types.h:40
Status GetDirectoryEntries(const OsPath &path, CFileInfos *files, DirectoryNames *subdirectoryNames)
Definition: file_system.cpp:86
__int64 off_t
Definition: wposix_types.h:91
Definition: path.h:79
Status DeleteDirectory(const OsPath &dirPath)
Definition: file_system.cpp:166
off_t size
Definition: file_system.h:71
CFileInfo()
Definition: file_system.h:45
i64 Status
Error handling system.
Definition: status.h:169
bool FileExists(const OsPath &pathname)
Definition: file_system.cpp:48
time_t mtime
Definition: file_system.h:72
Status GetFileInfo(const OsPath &pathname, CFileInfo *fileInfo)
Definition: file_system.cpp:64
std::vector< OsPath > DirectoryNames
Definition: file_system.h:78
Status CreateDirectories(const OsPath &path, mode_t mode, bool breakpoint=true)
Definition: file_system.cpp:132
u64 FileSize(const OsPath &pathname)
Definition: file_system.cpp:56
std::vector< CFileInfo > CFileInfos
Definition: file_system.h:77
Definition: file_system.h:42
Status CopyFile(const OsPath &path, const OsPath &newPath, bool override_if_exists=false)
Definition: file_system.cpp:213