All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fileSystem.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_ARCH_FILE_SYSTEM_H
25 #define PXR_BASE_ARCH_FILE_SYSTEM_H
26 
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/arch/api.h"
33 #include "pxr/base/arch/defines.h"
34 #include "pxr/base/arch/inttypes.h"
35 #include <memory>
36 #include <cstdio>
37 #include <string>
38 #include <set>
39 
40 #include <fcntl.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 
44 #if defined(ARCH_OS_LINUX)
45 #include <unistd.h>
46 #include <sys/statfs.h>
47 #include <glob.h>
48 #elif defined(ARCH_OS_DARWIN)
49 #include <unistd.h>
50 #include <sys/mount.h>
51 #include <glob.h>
52 #elif defined(ARCH_OS_WINDOWS)
53 #include <io.h>
54 #endif
55 
56 PXR_NAMESPACE_OPEN_SCOPE
57 
60 #if !defined(ARCH_OS_WINDOWS)
61  #ifdef _POSIX_VERSION
62  #include <limits.h> /* for PATH_MAX */
63  #else
64  #include <sys/param.h> /* for MAXPATHLEN */
65  #endif
66 #else
67  // XXX -- Should probably have ARCH_ macro for this.
68  #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
69 
70  // See https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx
71  // XXX -- Should probably have Arch enum for these.
72  #define F_OK 0 // Test for existence.
73  #define X_OK 1 // Test for execute permission.
74  #define W_OK 2 // Test for write permission.
75  #define R_OK 4 // Test for read permission.
76 #endif
77 
78 #if defined(ARCH_OS_WINDOWS)
79  #define ARCH_GLOB_NOCHECK 1
80  #define ARCH_GLOB_MARK 2
81  #define ARCH_GLOB_NOSORT 4
82 #else
83  #define ARCH_GLOB_NOCHECK GLOB_NOCHECK
84  #define ARCH_GLOB_MARK GLOB_MARK
85  #define ARCH_GLOB_NOSORT GLOB_NOSORT
86 #endif
87 #define ARCH_GLOB_DEFAULT (ARCH_GLOB_NOCHECK | ARCH_GLOB_MARK)
88 
89 #ifndef ARCH_PATH_MAX
90  #ifdef PATH_MAX
91  #define ARCH_PATH_MAX PATH_MAX
92  #else
93  #ifdef MAXPATHLEN
94  #define ARCH_PATH_MAX MAXPATHLEN
95  #else
96  #ifdef _MAX_PATH
97  #define ARCH_PATH_MAX _MAX_PATH
98  #else
99  #define ARCH_PATH_MAX 1024
100  #endif
101  #endif
102  #endif
103 #endif
104 
105 #if defined(ARCH_OS_WINDOWS)
106  #define ARCH_PATH_SEP "\\"
107  #define ARCH_PATH_LIST_SEP ";"
108  #define ARCH_REL_PATH_IDENT ".\\"
109 #else
110  #define ARCH_PATH_SEP "/"
111  #define ARCH_PATH_LIST_SEP ":"
112  #define ARCH_REL_PATH_IDENT "./"
113 #endif
114 
115 #if defined(ARCH_OS_WINDOWS)
116 typedef struct __stat64 ArchStatType;
117 #else
118 typedef struct stat ArchStatType;
119 #endif
120 
125 
131 ARCH_API FILE*
132 ArchOpenFile(char const* fileName, char const* mode);
133 
134 #if defined(ARCH_OS_WINDOWS)
135 # define ArchChmod(path, mode) _chmod(path, mode)
136 #else
137 # define ArchChmod(path, mode) chmod(path, mode)
138 #endif
139 
140 #if defined(ARCH_OS_WINDOWS)
141 # define ArchCloseFile(fd) _close(fd)
142 #else
143 # define ArchCloseFile(fd) close(fd)
144 #endif
145 
146 #if defined(ARCH_OS_WINDOWS)
147 # define ArchUnlinkFile(path) _unlink(path)
148 #else
149 # define ArchUnlinkFile(path) unlink(path)
150 #endif
151 
152 #if defined(ARCH_OS_WINDOWS)
153  ARCH_API int ArchFileAccess(const char* path, int mode);
154 #else
155 # define ArchFileAccess(path, mode) access(path, mode)
156 #endif
157 
158 #if defined(ARCH_OS_WINDOWS)
159 # define ArchFdOpen(fd, mode) _fdopen(fd, mode)
160 #else
161 # define ArchFdOpen(fd, mode) fdopen(fd, mode)
162 #endif
163 
164 #if defined(ARCH_OS_WINDOWS)
165 # define ArchFileNo(stream) _fileno(stream)
166 #else
167 # define ArchFileNo(stream) fileno(stream)
168 #endif
169 
170 #if defined(ARCH_OS_WINDOWS)
171 # define ArchFileIsaTTY(stream) _isatty(stream)
172 #else
173 # define ArchFileIsaTTY(stream) isatty(stream)
174 #endif
175 
176 #if defined(ARCH_OS_WINDOWS)
177  ARCH_API int ArchRmDir(const char* path);
178 #else
179 # define ArchRmDir(path) rmdir(path)
180 #endif
181 
185 ARCH_API int64_t ArchGetFileLength(const char* fileName);
186 ARCH_API int64_t ArchGetFileLength(FILE *file);
187 
189 ARCH_API std::string ArchGetFileName(FILE *file);
190 
197 ARCH_API bool ArchStatIsWritable(const ArchStatType *st);
198 
204 ARCH_API bool ArchGetModificationTime(const char* pathname, double* time);
205 
210 ARCH_API double ArchGetModificationTime(const ArchStatType& st);
211 
221 ARCH_API std::string ArchNormPath(const std::string& path,
222  bool stripDriveSpecifier = false);
223 
228 ARCH_API std::string ArchAbsPath(const std::string& path);
229 
235 ARCH_API bool ArchGetStatMode(const char *pathname, int *mode);
236 
246 ARCH_API const char *ArchGetTmpDir();
247 
261 ARCH_API
262 std::string ArchMakeTmpFileName(const std::string& prefix,
263  const std::string& suffix = std::string());
264 
274 ARCH_API
275 int ArchMakeTmpFile(const std::string& prefix, std::string* pathname = 0);
276 
285 ARCH_API
286 int ArchMakeTmpFile(const std::string& tmpdir,
287  const std::string& prefix, std::string* pathname = 0);
288 
297 ARCH_API
298 std::string ArchMakeTmpSubdir(const std::string& tmpdir,
299  const std::string& prefix);
300 
301 // Helper 'deleter' for use with std::unique_ptr for file mappings.
302 struct Arch_Unmapper {
303  Arch_Unmapper() : _length(~0) {}
304  explicit Arch_Unmapper(size_t length) : _length(length) {}
305  ARCH_API void operator()(char *mapStart) const;
306  ARCH_API void operator()(char const *mapStart) const;
307  size_t GetLength() const { return _length; }
308 private:
309  size_t _length;
310 };
311 
316 using ArchConstFileMapping = std::unique_ptr<char const, Arch_Unmapper>;
317 using ArchMutableFileMapping = std::unique_ptr<char, Arch_Unmapper>;
318 
320 inline size_t
322  return m.get_deleter().GetLength();
323 }
324 
326 inline size_t
327 ArchGetFileMappingLength(ArchMutableFileMapping const &m) {
328  return m.get_deleter().GetLength();
329 }
330 
335 ARCH_API
337 ArchMapFileReadOnly(FILE *file, std::string *errMsg=nullptr);
338 
340 ARCH_API
342 ArchMapFileReadOnly(std::string const& path, std::string *errMsg=nullptr);
343 
350 ARCH_API
351 ArchMutableFileMapping
352 ArchMapFileReadWrite(FILE *file, std::string *errMsg=nullptr);
353 
355 ARCH_API
356 ArchMutableFileMapping
357 ArchMapFileReadWrite(std::string const& path, std::string *errMsg=nullptr);
358 
359 enum ArchMemAdvice {
360  ArchMemAdviceNormal, // Treat range with default behavior.
361  ArchMemAdviceWillNeed, // OS may prefetch this range.
362  ArchMemAdviceDontNeed, // OS may free resources related to this range.
363  ArchMemAdviceRandomAccess, // Prefetching may not be beneficial.
364 };
365 
370 ARCH_API
371 void ArchMemAdvise(void const *addr, size_t len, ArchMemAdvice adv);
372 
385 ARCH_API
386 bool
388  void const *addr, size_t len, unsigned char *pageMap);
389 
394 ARCH_API
395 int64_t ArchPRead(FILE *file, void *buffer, size_t count, int64_t offset);
396 
401 ARCH_API
402 int64_t ArchPWrite(FILE *file, void const *bytes, size_t count, int64_t offset);
403 
406 ARCH_API
407 std::string ArchReadLink(const char* path);
408 
409 enum ArchFileAdvice {
410  ArchFileAdviceNormal, // Treat range with default behavior.
411  ArchFileAdviceWillNeed, // OS may prefetch this range.
412  ArchFileAdviceDontNeed, // OS may free resources related to this range.
413  ArchFileAdviceRandomAccess, // Prefetching may not be beneficial.
414 };
415 
420 ARCH_API
421 void ArchFileAdvise(FILE *file, int64_t offset, size_t count,
422  ArchFileAdvice adv);
423 
425 
426 PXR_NAMESPACE_CLOSE_SCOPE
427 
428 #endif // PXR_BASE_ARCH_FILE_SYSTEM_H
ARCH_API FILE * ArchOpenFile(char const *fileName, char const *mode)
Opens a file.
ARCH_API void ArchMemAdvise(void const *addr, size_t len, ArchMemAdvice adv)
Advise the OS regarding how the application intends to access a range of memory.
ARCH_API std::string ArchAbsPath(const std::string &path)
Returns the canonical absolute path of the specified filename.
ARCH_API void ArchFileAdvise(FILE *file, int64_t offset, size_t count, ArchFileAdvice adv)
Advise the OS regarding how the application intends to access a range of bytes in a file...
ARCH_API std::string ArchNormPath(const std::string &path, bool stripDriveSpecifier=false)
Normalizes the specified path, eliminating double slashes, etc.
ARCH_API ArchConstFileMapping ArchMapFileReadOnly(FILE *file, std::string *errMsg=nullptr)
Privately map the passed file into memory and return a unique_ptr to the read-only mapped contents...
std::unique_ptr< char const, Arch_Unmapper > ArchConstFileMapping
ArchConstFileMapping and ArchMutableFileMapping are std::unique_ptr&lt;char const *, ...
Definition: fileSystem.h:316
ARCH_API ArchMutableFileMapping ArchMapFileReadWrite(FILE *file, std::string *errMsg=nullptr)
Privately map the passed file into memory and return a unique_ptr to the copy-on-write mapped content...
ARCH_API const char * ArchGetTmpDir()
Return the path to a temporary directory for this platform.
ARCH_API int ArchMakeTmpFile(const std::string &prefix, std::string *pathname=0)
Create a temporary file, in a system-determined temporary directory.
ARCH_API std::string ArchReadLink(const char *path)
Returns the value of the symbolic link at path.
ARCH_API int64_t ArchGetFileLength(const char *fileName)
Return the length of a file in bytes.
size_t ArchGetFileMappingLength(ArchConstFileMapping const &m)
Return the length of an ArchConstFileMapping.
Definition: fileSystem.h:321
ARCH_API bool ArchGetStatMode(const char *pathname, int *mode)
Returns the permissions mode (mode_t) for the given pathname.
Define integral types.
ARCH_API bool ArchStatIsWritable(const ArchStatType *st)
Returns true if the data in stat struct st indicates that the target file or directory is writable...
ARCH_API bool ArchQueryMappedMemoryResidency(void const *addr, size_t len, unsigned char *pageMap)
Report whether or not the mapped virtual memory pages starting at addr for len bytes are resident in ...
Defines useful mathematical limits.
ARCH_API std::string ArchGetFileName(FILE *file)
Return a filename for this file, if one can be obtained.
ARCH_API int64_t ArchPRead(FILE *file, void *buffer, size_t count, int64_t offset)
Read up to count bytes from offset in file into buffer.
ARCH_API std::string ArchMakeTmpSubdir(const std::string &tmpdir, const std::string &prefix)
Create a temporary sub-direcrory, in a given temporary directory.
ARCH_API int64_t ArchPWrite(FILE *file, void const *bytes, size_t count, int64_t offset)
Write up to count bytes from buffer to file at offset.
ARCH_API bool ArchGetModificationTime(const char *pathname, double *time)
Returns the modification time (mtime) in seconds for a file.
ARCH_API std::string ArchMakeTmpFileName(const std::string &prefix, const std::string &suffix=std::string())
Make a temporary file name, in a system-determined temporary directory.