Loading...
Searching...
No Matches
fileSystem.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_ARCH_FILE_SYSTEM_H
8#define PXR_BASE_ARCH_FILE_SYSTEM_H
9
13
14#include "pxr/pxr.h"
15#include "pxr/base/arch/api.h"
16#include "pxr/base/arch/defines.h"
18#include <memory>
19#include <cstdio>
20#include <string>
21#include <set>
22
23#include <fcntl.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26
27#if defined(ARCH_OS_LINUX) || defined(ARCH_OS_WASM_VM)
28#include <unistd.h>
29#include <sys/statfs.h>
30#include <glob.h>
31#elif defined(ARCH_OS_DARWIN)
32#include <unistd.h>
33#include <sys/mount.h>
34#include <glob.h>
35#elif defined(ARCH_OS_WINDOWS)
36#include <io.h>
37#endif
38
39PXR_NAMESPACE_OPEN_SCOPE
40
43#if !defined(ARCH_OS_WINDOWS)
44 #ifdef _POSIX_VERSION
45 #include <limits.h> /* for PATH_MAX */
46 #else
47 #include <sys/param.h> /* for MAXPATHLEN */
48 #endif
49#else
50 // XXX -- Should probably have ARCH_ macro for this.
51 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
52
53 // See https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx
54 // XXX -- Should probably have Arch enum for these.
55 #define F_OK 0 // Test for existence.
56 #define X_OK 1 // Test for execute permission.
57 #define W_OK 2 // Test for write permission.
58 #define R_OK 4 // Test for read permission.
59#endif
60
61#if defined(ARCH_OS_WINDOWS)
62 #define ARCH_GLOB_NOCHECK 1
63 #define ARCH_GLOB_MARK 2
64 #define ARCH_GLOB_NOSORT 4
65#else
66 #define ARCH_GLOB_NOCHECK GLOB_NOCHECK
67 #define ARCH_GLOB_MARK GLOB_MARK
68 #define ARCH_GLOB_NOSORT GLOB_NOSORT
69#endif
70#define ARCH_GLOB_DEFAULT (ARCH_GLOB_NOCHECK | ARCH_GLOB_MARK)
71
72#ifndef ARCH_PATH_MAX
73 #ifdef PATH_MAX
74 #define ARCH_PATH_MAX PATH_MAX
75 #else
76 #ifdef MAXPATHLEN
77 #define ARCH_PATH_MAX MAXPATHLEN
78 #else
79 #ifdef _MAX_PATH
80 #define ARCH_PATH_MAX _MAX_PATH
81 #else
82 #define ARCH_PATH_MAX 1024
83 #endif
84 #endif
85 #endif
86#endif
87
88#if defined(ARCH_OS_WINDOWS)
89 #define ARCH_PATH_SEP "\\"
90 #define ARCH_PATH_LIST_SEP ";"
91 #define ARCH_REL_PATH_IDENT ".\\"
92#else
93 #define ARCH_PATH_SEP "/"
94 #define ARCH_PATH_LIST_SEP ":"
95 #define ARCH_REL_PATH_IDENT "./"
96#endif
97
98#if defined(ARCH_OS_WINDOWS)
99typedef struct __stat64 ArchStatType;
100#else
101typedef struct stat ArchStatType;
102#endif
103
108
114ARCH_API FILE*
115ArchOpenFile(char const* fileName, char const* mode);
116
117#if defined(ARCH_OS_WINDOWS)
118# define ArchChmod(path, mode) _chmod(path, mode)
119#else
120# define ArchChmod(path, mode) chmod(path, mode)
121#endif
122
123#if defined(ARCH_OS_WINDOWS)
124# define ArchCloseFile(fd) _close(fd)
125#else
126# define ArchCloseFile(fd) close(fd)
127#endif
128
129#if defined(ARCH_OS_WINDOWS)
130# define ArchUnlinkFile(path) _unlink(path)
131#else
132# define ArchUnlinkFile(path) unlink(path)
133#endif
134
135#if defined(ARCH_OS_WINDOWS)
136 ARCH_API int ArchFileAccess(const char* path, int mode);
137#else
138# define ArchFileAccess(path, mode) access(path, mode)
139#endif
140
141#if defined(ARCH_OS_WINDOWS)
142# define ArchFdOpen(fd, mode) _fdopen(fd, mode)
143#else
144# define ArchFdOpen(fd, mode) fdopen(fd, mode)
145#endif
146
147#if defined(ARCH_OS_WINDOWS)
148# define ArchFileNo(stream) _fileno(stream)
149#else
150# define ArchFileNo(stream) fileno(stream)
151#endif
152
153#if defined(ARCH_OS_WINDOWS)
154# define ArchFileIsaTTY(stream) _isatty(stream)
155#else
156# define ArchFileIsaTTY(stream) isatty(stream)
157#endif
158
159#if defined(ARCH_OS_WINDOWS)
160 ARCH_API int ArchRmDir(const char* path);
161#else
162# define ArchRmDir(path) rmdir(path)
163#endif
164
168ARCH_API int64_t ArchGetFileLength(const char* fileName);
169ARCH_API int64_t ArchGetFileLength(FILE *file);
170
175ARCH_API std::string ArchGetFileName(FILE *file);
176
183ARCH_API bool ArchStatIsWritable(const ArchStatType *st);
184
190ARCH_API bool ArchGetModificationTime(const char* pathname, double* time);
191
196ARCH_API double ArchGetModificationTime(const ArchStatType& st);
197
207ARCH_API std::string ArchNormPath(const std::string& path,
208 bool stripDriveSpecifier = false);
209
214ARCH_API std::string ArchAbsPath(const std::string& path);
215
221ARCH_API bool ArchGetStatMode(const char *pathname, int *mode);
222
232ARCH_API const char *ArchGetTmpDir();
233
247ARCH_API
248std::string ArchMakeTmpFileName(const std::string& prefix,
249 const std::string& suffix = std::string());
250
260ARCH_API
261int ArchMakeTmpFile(const std::string& prefix, std::string* pathname = 0);
262
271ARCH_API
272int ArchMakeTmpFile(const std::string& tmpdir,
273 const std::string& prefix, std::string* pathname = 0);
274
283ARCH_API
284std::string ArchMakeTmpSubdir(const std::string& tmpdir,
285 const std::string& prefix);
286
287// Helper 'deleter' for use with std::unique_ptr for file mappings.
288struct Arch_Unmapper {
289 Arch_Unmapper() : _length(~0) {}
290 explicit Arch_Unmapper(size_t length) : _length(length) {}
291 ARCH_API void operator()(char *mapStart) const;
292 ARCH_API void operator()(char const *mapStart) const;
293 size_t GetLength() const { return _length; }
294private:
295 size_t _length;
296};
297
302using ArchConstFileMapping = std::unique_ptr<char const, Arch_Unmapper>;
303using ArchMutableFileMapping = std::unique_ptr<char, Arch_Unmapper>;
304
306inline size_t
308 return m.get_deleter().GetLength();
309}
310
312inline size_t
313ArchGetFileMappingLength(ArchMutableFileMapping const &m) {
314 return m.get_deleter().GetLength();
315}
316
321ARCH_API
323ArchMapFileReadOnly(FILE *file, std::string *errMsg=nullptr);
324
326ARCH_API
328ArchMapFileReadOnly(std::string const& path, std::string *errMsg=nullptr);
329
336ARCH_API
337ArchMutableFileMapping
338ArchMapFileReadWrite(FILE *file, std::string *errMsg=nullptr);
339
341ARCH_API
342ArchMutableFileMapping
343ArchMapFileReadWrite(std::string const& path, std::string *errMsg=nullptr);
344
345enum ArchMemAdvice {
346 ArchMemAdviceNormal, // Treat range with default behavior.
347 ArchMemAdviceWillNeed, // OS may prefetch this range.
348 ArchMemAdviceDontNeed, // OS may free resources related to this range.
349 ArchMemAdviceRandomAccess, // Prefetching may not be beneficial.
350};
351
356ARCH_API
357void ArchMemAdvise(void const *addr, size_t len, ArchMemAdvice adv);
358
371ARCH_API
372bool
374 void const *addr, size_t len, unsigned char *pageMap);
375
380ARCH_API
381int64_t ArchPRead(FILE *file, void *buffer, size_t count, int64_t offset);
382
387ARCH_API
388int64_t ArchPWrite(FILE *file, void const *bytes, size_t count, int64_t offset);
389
392ARCH_API
393std::string ArchReadLink(const char* path);
394
395enum ArchFileAdvice {
396 ArchFileAdviceNormal, // Treat range with default behavior.
397 ArchFileAdviceWillNeed, // OS may prefetch this range.
398 ArchFileAdviceDontNeed, // OS may free resources related to this range.
399 ArchFileAdviceRandomAccess, // Prefetching may not be beneficial.
400};
401
406ARCH_API
407void ArchFileAdvise(FILE *file, int64_t offset, size_t count,
408 ArchFileAdvice adv);
409
410#if defined(ARCH_OS_WINDOWS)
411
413ARCH_API std::string ArchWindowsUtf16ToUtf8(const std::wstring &wstr);
414
416ARCH_API std::wstring ArchWindowsUtf8ToUtf16(const std::string &str);
417
418#endif
419
421
422PXR_NAMESPACE_CLOSE_SCOPE
423
424#endif // PXR_BASE_ARCH_FILE_SYSTEM_H
ARCH_API const char * ArchGetTmpDir()
Return the path to a temporary directory for this platform.
std::unique_ptr< char const, Arch_Unmapper > ArchConstFileMapping
ArchConstFileMapping and ArchMutableFileMapping are std::unique_ptr<char const *, ....
Definition: fileSystem.h:302
ARCH_API std::string ArchReadLink(const char *path)
Returns the value of the symbolic link at path.
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 ArchGetFileName(FILE *file)
Return a filename for this file, if one can be obtained.
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 ...
ARCH_API FILE * ArchOpenFile(char const *fileName, char const *mode)
Opens a file.
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.
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 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 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 ArchMakeTmpFileName(const std::string &prefix, const std::string &suffix=std::string())
Make a temporary file name, in a system-determined temporary directory.
size_t ArchGetFileMappingLength(ArchConstFileMapping const &m)
Return the length of an ArchConstFileMapping.
Definition: fileSystem.h:307
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 std::string ArchAbsPath(const std::string &path)
Returns the canonical absolute path of the specified filename.
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 int ArchMakeTmpFile(const std::string &prefix, std::string *pathname=0)
Create a temporary file, in a system-determined temporary directory.
ARCH_API int64_t ArchGetFileLength(const char *fileName)
Return the length of a file in bytes.
ARCH_API std::string ArchNormPath(const std::string &path, bool stripDriveSpecifier=false)
Normalizes the specified path, eliminating double slashes, etc.
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 bool ArchGetStatMode(const char *pathname, int *mode)
Returns the permissions mode (mode_t) for the given pathname.
Define integral types.
Defines useful mathematical limits.