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 <cstdint>
21#include <string>
22#include <set>
23
24#include <fcntl.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27
28#if defined(ARCH_OS_LINUX) || defined(ARCH_OS_WASM_VM)
29#include <unistd.h>
30#include <sys/statfs.h>
31#include <glob.h>
32#elif defined(ARCH_OS_DARWIN)
33#include <unistd.h>
34#include <sys/mount.h>
35#include <glob.h>
36#elif defined(ARCH_OS_WINDOWS)
37#include <io.h>
38#endif
39
40PXR_NAMESPACE_OPEN_SCOPE
41
44#if !defined(ARCH_OS_WINDOWS)
45 #ifdef _POSIX_VERSION
46 #include <limits.h> /* for PATH_MAX */
47 #else
48 #include <sys/param.h> /* for MAXPATHLEN */
49 #endif
50#else
51 // XXX -- Should probably have ARCH_ macro for this.
52 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
53
54 // See https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx
55 // XXX -- Should probably have Arch enum for these.
56 #define F_OK 0 // Test for existence.
57 #define X_OK 1 // Test for execute permission.
58 #define W_OK 2 // Test for write permission.
59 #define R_OK 4 // Test for read permission.
60#endif
61
62#if defined(ARCH_OS_WINDOWS)
63 #define ARCH_GLOB_NOCHECK 1
64 #define ARCH_GLOB_MARK 2
65 #define ARCH_GLOB_NOSORT 4
66#else
67 #define ARCH_GLOB_NOCHECK GLOB_NOCHECK
68 #define ARCH_GLOB_MARK GLOB_MARK
69 #define ARCH_GLOB_NOSORT GLOB_NOSORT
70#endif
71#define ARCH_GLOB_DEFAULT (ARCH_GLOB_NOCHECK | ARCH_GLOB_MARK)
72
73#ifndef ARCH_PATH_MAX
74 #ifdef PATH_MAX
75 #define ARCH_PATH_MAX PATH_MAX
76 #else
77 #ifdef MAXPATHLEN
78 #define ARCH_PATH_MAX MAXPATHLEN
79 #else
80 #ifdef _MAX_PATH
81 #define ARCH_PATH_MAX _MAX_PATH
82 #else
83 #define ARCH_PATH_MAX 1024
84 #endif
85 #endif
86 #endif
87#endif
88
89#if defined(ARCH_OS_WINDOWS)
90 #define ARCH_PATH_SEP "\\"
91 #define ARCH_PATH_LIST_SEP ";"
92 #define ARCH_REL_PATH_IDENT ".\\"
93#else
94 #define ARCH_PATH_SEP "/"
95 #define ARCH_PATH_LIST_SEP ":"
96 #define ARCH_REL_PATH_IDENT "./"
97#endif
98
99#if defined(ARCH_OS_WINDOWS)
100typedef struct __stat64 ArchStatType;
101#else
102typedef struct stat ArchStatType;
103#endif
104
109
115ARCH_API FILE*
116ArchOpenFile(char const* fileName, char const* mode);
117
118#if defined(ARCH_OS_WINDOWS)
119# define ArchChmod(path, mode) _chmod(path, mode)
120#else
121# define ArchChmod(path, mode) chmod(path, mode)
122#endif
123
124#if defined(ARCH_OS_WINDOWS)
125# define ArchCloseFile(fd) _close(fd)
126#else
127# define ArchCloseFile(fd) close(fd)
128#endif
129
130#if defined(ARCH_OS_WINDOWS)
131# define ArchUnlinkFile(path) _unlink(path)
132#else
133# define ArchUnlinkFile(path) unlink(path)
134#endif
135
136#if defined(ARCH_OS_WINDOWS)
137 ARCH_API int ArchWindowsFileAccess(const char* path, uint32_t dwAccessMask);
138 ARCH_API int ArchFileAccess(const char* path, int mode);
139#else
140# define ArchFileAccess(path, mode) access(path, mode)
141#endif
142
143#if defined(ARCH_OS_WINDOWS)
144# define ArchFdOpen(fd, mode) _fdopen(fd, mode)
145#else
146# define ArchFdOpen(fd, mode) fdopen(fd, mode)
147#endif
148
149#if defined(ARCH_OS_WINDOWS)
150# define ArchFileNo(stream) _fileno(stream)
151#else
152# define ArchFileNo(stream) fileno(stream)
153#endif
154
155#if defined(ARCH_OS_WINDOWS)
156# define ArchFileIsaTTY(stream) _isatty(stream)
157#else
158# define ArchFileIsaTTY(stream) isatty(stream)
159#endif
160
161#if defined(ARCH_OS_WINDOWS)
162 ARCH_API int ArchRmDir(const char* path);
163#else
164# define ArchRmDir(path) rmdir(path)
165#endif
166
170ARCH_API int64_t ArchGetFileLength(const char* fileName);
171ARCH_API int64_t ArchGetFileLength(FILE *file);
172
177ARCH_API std::string ArchGetFileName(FILE *file);
178
185ARCH_API bool ArchStatIsWritable(const ArchStatType *st);
186
192ARCH_API bool ArchGetModificationTime(const char* pathname, double* time);
193
198ARCH_API double ArchGetModificationTime(const ArchStatType& st);
199
209ARCH_API std::string ArchNormPath(const std::string& path,
210 bool stripDriveSpecifier = false);
211
216ARCH_API std::string ArchAbsPath(const std::string& path);
217
223ARCH_API bool ArchGetStatMode(const char *pathname, int *mode);
224
234ARCH_API const char *ArchGetTmpDir();
235
249ARCH_API
250std::string ArchMakeTmpFileName(const std::string& prefix,
251 const std::string& suffix = std::string());
252
262ARCH_API
263int ArchMakeTmpFile(const std::string& prefix, std::string* pathname = 0);
264
273ARCH_API
274int ArchMakeTmpFile(const std::string& tmpdir,
275 const std::string& prefix, std::string* pathname = 0);
276
285ARCH_API
286std::string ArchMakeTmpSubdir(const std::string& tmpdir,
287 const std::string& prefix);
288
289// Helper 'deleter' for use with std::unique_ptr for file mappings.
290struct Arch_Unmapper {
291 Arch_Unmapper() : _length(~0) {}
292 explicit Arch_Unmapper(size_t length) : _length(length) {}
293 ARCH_API void operator()(char *mapStart) const;
294 ARCH_API void operator()(char const *mapStart) const;
295 size_t GetLength() const { return _length; }
296private:
297 size_t _length;
298};
299
304using ArchConstFileMapping = std::unique_ptr<char const, Arch_Unmapper>;
305using ArchMutableFileMapping = std::unique_ptr<char, Arch_Unmapper>;
306
308inline size_t
310 return m.get_deleter().GetLength();
311}
312
314inline size_t
315ArchGetFileMappingLength(ArchMutableFileMapping const &m) {
316 return m.get_deleter().GetLength();
317}
318
323ARCH_API
325ArchMapFileReadOnly(FILE *file, std::string *errMsg=nullptr);
326
328ARCH_API
330ArchMapFileReadOnly(std::string const& path, std::string *errMsg=nullptr);
331
338ARCH_API
339ArchMutableFileMapping
340ArchMapFileReadWrite(FILE *file, std::string *errMsg=nullptr);
341
343ARCH_API
344ArchMutableFileMapping
345ArchMapFileReadWrite(std::string const& path, std::string *errMsg=nullptr);
346
347enum ArchMemAdvice {
348 ArchMemAdviceNormal, // Treat range with default behavior.
349 ArchMemAdviceWillNeed, // OS may prefetch this range.
350 ArchMemAdviceDontNeed, // OS may free resources related to this range.
351 ArchMemAdviceRandomAccess, // Prefetching may not be beneficial.
352};
353
358ARCH_API
359void ArchMemAdvise(void const *addr, size_t len, ArchMemAdvice adv);
360
373ARCH_API
374bool
376 void const *addr, size_t len, unsigned char *pageMap);
377
382ARCH_API
383int64_t ArchPRead(FILE *file, void *buffer, size_t count, int64_t offset);
384
389ARCH_API
390int64_t ArchPWrite(FILE *file, void const *bytes, size_t count, int64_t offset);
391
394ARCH_API
395std::string ArchReadLink(const char* path);
396
397enum ArchFileAdvice {
398 ArchFileAdviceNormal, // Treat range with default behavior.
399 ArchFileAdviceWillNeed, // OS may prefetch this range.
400 ArchFileAdviceDontNeed, // OS may free resources related to this range.
401 ArchFileAdviceRandomAccess, // Prefetching may not be beneficial.
402};
403
408ARCH_API
409void ArchFileAdvise(FILE *file, int64_t offset, size_t count,
410 ArchFileAdvice adv);
411
412#if defined(ARCH_OS_WINDOWS)
413
415ARCH_API std::string ArchWindowsUtf16ToUtf8(const std::wstring &wstr);
416
418ARCH_API std::wstring ArchWindowsUtf8ToUtf16(const std::string &str);
419
420#endif
421
423
424PXR_NAMESPACE_CLOSE_SCOPE
425
426#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:304
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:309
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.