Pixar Arch
Loading...
Searching...
No Matches
fileSystem.h
Go to the documentation of this file.
1// Copyright 2016 Pixar
2//
3// Licensed under the terms set forth in the LICENSE.txt file available at
4// https://openusd.org/license.
5//
6// Modified by Jeremy Retailleau.
7
8#ifndef PXR_ARCH_FILE_SYSTEM_H
9#define PXR_ARCH_FILE_SYSTEM_H
10
13
14#include "./api.h"
15#include "./defines.h"
16#include "./inttypes.h"
17#include <memory>
18#include <cstdio>
19#include <string>
20#include <set>
21
22#include <fcntl.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25
26#if defined(ARCH_OS_LINUX)
27#include <unistd.h>
28#include <sys/statfs.h>
29#include <glob.h>
30#elif defined(ARCH_OS_DARWIN)
31#include <unistd.h>
32#include <sys/mount.h>
33#include <glob.h>
34#elif defined(ARCH_OS_WINDOWS)
35#include <io.h>
36#include <windows.h>
37#include <stringapiset.h>
38#endif
39
40namespace pxr {
41
42#if !defined(ARCH_OS_WINDOWS)
43 #ifdef _POSIX_VERSION
44 #include <limits.h> /* for PATH_MAX */
45 #else
46 #include <sys/param.h> /* for MAXPATHLEN */
47 #endif
48#else
49 // XXX -- Should probably have ARCH_ macro for this.
50 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
51
52 // See https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx
53 // XXX -- Should probably have Arch enum for these.
54 #define F_OK 0 // Test for existence.
55 #define X_OK 1 // Test for execute permission.
56 #define W_OK 2 // Test for write permission.
57 #define R_OK 4 // Test for read permission.
58#endif
59
60#if defined(ARCH_OS_WINDOWS)
61 #define ARCH_GLOB_NOCHECK 1
62 #define ARCH_GLOB_MARK 2
63 #define ARCH_GLOB_NOSORT 4
64#else
65 #define ARCH_GLOB_NOCHECK GLOB_NOCHECK
66 #define ARCH_GLOB_MARK GLOB_MARK
67 #define ARCH_GLOB_NOSORT GLOB_NOSORT
68#endif
69#define ARCH_GLOB_DEFAULT (ARCH_GLOB_NOCHECK | ARCH_GLOB_MARK)
70
71#ifndef ARCH_PATH_MAX
72 #ifdef PATH_MAX
73 #define ARCH_PATH_MAX PATH_MAX
74 #else
75 #ifdef MAXPATHLEN
76 #define ARCH_PATH_MAX MAXPATHLEN
77 #else
78 #ifdef _MAX_PATH
79 #define ARCH_PATH_MAX _MAX_PATH
80 #else
81 #define ARCH_PATH_MAX 1024
82 #endif
83 #endif
84 #endif
85#endif
86
87#if defined(ARCH_OS_WINDOWS)
88 #define ARCH_PATH_SEP "\\"
89 #define ARCH_PATH_LIST_SEP ";"
90 #define ARCH_REL_PATH_IDENT ".\\"
91#else
92 #define ARCH_PATH_SEP "/"
93 #define ARCH_PATH_LIST_SEP ":"
94 #define ARCH_REL_PATH_IDENT "./"
95#endif
96
97#if defined(ARCH_OS_WINDOWS)
98typedef struct __stat64 ArchStatType;
99#else
100typedef struct stat ArchStatType;
101#endif
102
106
112ARCH_API FILE*
113ArchOpenFile(char const* fileName, char const* mode);
114
115ARCH_API int ArchChmod(const char* path, int mode);
116ARCH_API int ArchCloseFile(int fd);
117ARCH_API int ArchUnlinkFile(const char* path);
118ARCH_API int ArchFileAccess(const char* path, int mode);
119ARCH_API FILE* ArchFdOpen(int fd, const char* mode);
120ARCH_API int ArchFileNo(FILE* file);
121ARCH_API int ArchFileIsaTTY(int fd);
122ARCH_API int ArchRmDir(const char* path);
123
127ARCH_API int64_t ArchGetFileLength(const char* fileName);
128ARCH_API int64_t ArchGetFileLength(FILE *file);
129
134ARCH_API std::string ArchGetFileName(FILE *file);
135
142ARCH_API bool ArchStatIsWritable(const ArchStatType *st);
143
149ARCH_API bool ArchGetModificationTime(const char* pathname, double* time);
150
155ARCH_API double ArchGetModificationTime(const ArchStatType& st);
156
166ARCH_API std::string ArchNormPath(const std::string& path,
167 bool stripDriveSpecifier = false);
168
173ARCH_API std::string ArchAbsPath(const std::string& path);
174
180ARCH_API bool ArchGetStatMode(const char *pathname, int *mode);
181
191ARCH_API const char *ArchGetTmpDir();
192
206ARCH_API
207std::string ArchMakeTmpFileName(const std::string& prefix,
208 const std::string& suffix = std::string());
209
219ARCH_API
220int ArchMakeTmpFile(const std::string& prefix, std::string* pathname = 0);
221
230ARCH_API
231int ArchMakeTmpFile(const std::string& tmpdir,
232 const std::string& prefix, std::string* pathname = 0);
233
242ARCH_API
243std::string ArchMakeTmpSubdir(const std::string& tmpdir,
244 const std::string& prefix);
245
246// Helper 'deleter' for use with std::unique_ptr for file mappings.
248 Arch_Unmapper() : _length(~0) {}
249 explicit Arch_Unmapper(size_t length) : _length(length) {}
250 ARCH_API void operator()(char *mapStart) const;
251 ARCH_API void operator()(char const *mapStart) const;
252 size_t GetLength() const { return _length; }
253private:
254 size_t _length;
255};
256
261using ArchConstFileMapping = std::unique_ptr<char const, Arch_Unmapper>;
262using ArchMutableFileMapping = std::unique_ptr<char, Arch_Unmapper>;
263
265inline size_t
267 return m.get_deleter().GetLength();
268}
269
271inline size_t
273 return m.get_deleter().GetLength();
274}
275
280ARCH_API
282ArchMapFileReadOnly(FILE *file, std::string *errMsg=nullptr);
283
285ARCH_API
287ArchMapFileReadOnly(std::string const& path, std::string *errMsg=nullptr);
288
295ARCH_API
297ArchMapFileReadWrite(FILE *file, std::string *errMsg=nullptr);
298
300ARCH_API
302ArchMapFileReadWrite(std::string const& path, std::string *errMsg=nullptr);
303
305 ArchMemAdviceNormal, // Treat range with default behavior.
306 ArchMemAdviceWillNeed, // OS may prefetch this range.
307 ArchMemAdviceDontNeed, // OS may free resources related to this range.
308 ArchMemAdviceRandomAccess, // Prefetching may not be beneficial.
309};
310
315ARCH_API
316void ArchMemAdvise(void const *addr, size_t len, ArchMemAdvice adv);
317
330ARCH_API
331bool
333 void const *addr, size_t len, unsigned char *pageMap);
334
339ARCH_API
340int64_t ArchPRead(FILE *file, void *buffer, size_t count, int64_t offset);
341
346ARCH_API
347int64_t ArchPWrite(FILE *file, void const *bytes, size_t count, int64_t offset);
348
351ARCH_API
352std::string ArchReadLink(const char* path);
353
355 ArchFileAdviceNormal, // Treat range with default behavior.
356 ArchFileAdviceWillNeed, // OS may prefetch this range.
357 ArchFileAdviceDontNeed, // OS may free resources related to this range.
358 ArchFileAdviceRandomAccess, // Prefetching may not be beneficial.
359};
360
365ARCH_API
366void ArchFileAdvise(FILE *file, int64_t offset, size_t count,
367 ArchFileAdvice adv);
368
369#if defined(ARCH_OS_WINDOWS)
370
372inline std::string ArchWindowsUtf16ToUtf8(const std::wstring &wstr)
373{
374 if (wstr.empty()) return std::string();
375 // first call is only to get required size for string
376 int size = WideCharToMultiByte(
377 CP_UTF8, 0, wstr.data(), (int)wstr.size(), NULL, 0, NULL, NULL);
378 if (size == 0) return std::string();
379 std::string str(size, 0);
380 if (WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(),
381 &str[0], size, NULL, NULL) == 0) {
382 return std::string();
383 }
384 return str;
385}
386
388inline std::wstring ArchWindowsUtf8ToUtf16(const std::string &str)
389{
390 if (str.empty()) return std::wstring();
391 // first call is only to get required size for wstring
392 int size = MultiByteToWideChar(
393 CP_UTF8, 0, str.data(), (int)str.size(), NULL, 0);
394 if (size == 0) return std::wstring();
395 std::wstring wstr(size, 0);
396 if(MultiByteToWideChar(
397 CP_UTF8, 0, str.data(), (int)str.size(), &wstr[0], size) == 0) {
398 return std::wstring();
399 }
400 return wstr;
401}
402
403#endif
404
405} // namespace pxr
406
407#endif // PXR_ARCH_FILE_SYSTEM_H
Define integral types.
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 FILE * ArchOpenFile(char const *fileName, char const *mode)
Opens a file.
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 std::string ArchNormPath(const std::string &path, bool stripDriveSpecifier=false)
Normalizes the specified path, eliminating double slashes, etc.
size_t ArchGetFileMappingLength(ArchConstFileMapping const &m)
Return the length of an ArchConstFileMapping.
Definition fileSystem.h:266
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 int ArchMakeTmpFile(const std::string &prefix, std::string *pathname=0)
Create a temporary file, in a system-determined temporary directory.
ARCH_API bool ArchGetModificationTime(const char *pathname, double *time)
Returns the modification time (mtime) in seconds for a file.
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 ArchReadLink(const char *path)
Returns the value of the symbolic link at path.
ArchMemAdvice
Definition fileSystem.h:304
@ ArchMemAdviceRandomAccess
Definition fileSystem.h:308
@ ArchMemAdviceDontNeed
Definition fileSystem.h:307
@ ArchMemAdviceWillNeed
Definition fileSystem.h:306
@ ArchMemAdviceNormal
Definition fileSystem.h:305
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 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 int ArchUnlinkFile(const char *path)
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 int64_t ArchGetFileLength(const char *fileName)
Return the length of a file in bytes.
std::unique_ptr< char, Arch_Unmapper > ArchMutableFileMapping
Definition fileSystem.h:262
ARCH_API int ArchCloseFile(int fd)
struct stat ArchStatType
Definition fileSystem.h:100
std::unique_ptr< char const, Arch_Unmapper > ArchConstFileMapping
ArchConstFileMapping and ArchMutableFileMapping are std::unique_ptr<char const *, ....
Definition fileSystem.h:261
ARCH_API std::string ArchGetFileName(FILE *file)
Return a filename for this file, if one can be obtained.
ARCH_API int ArchRmDir(const char *path)
ARCH_API std::string ArchAbsPath(const std::string &path)
Returns the canonical absolute path of the specified filename.
ARCH_API int ArchFileIsaTTY(int fd)
ARCH_API int ArchChmod(const char *path, int mode)
ARCH_API bool ArchGetStatMode(const char *pathname, int *mode)
Returns the permissions mode (mode_t) for the given pathname.
ARCH_API FILE * ArchFdOpen(int fd, const char *mode)
ArchFileAdvice
Definition fileSystem.h:354
@ ArchFileAdviceWillNeed
Definition fileSystem.h:356
@ ArchFileAdviceDontNeed
Definition fileSystem.h:357
@ ArchFileAdviceRandomAccess
Definition fileSystem.h:358
@ ArchFileAdviceNormal
Definition fileSystem.h:355
ARCH_API int ArchFileAccess(const char *path, int mode)
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 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 const char * ArchGetTmpDir()
Return the path to a temporary directory for this platform.
ARCH_API int ArchFileNo(FILE *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.
Definition fileSystem.h:247
size_t GetLength() const
Definition fileSystem.h:252
Arch_Unmapper()
Definition fileSystem.h:248
Arch_Unmapper(size_t length)
Definition fileSystem.h:249
ARCH_API void operator()(char *mapStart) const
ARCH_API void operator()(char const *mapStart) const