Pixar Arch 0.25.8
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 "pxr/arch/pxr.h"
15#include "pxr/arch/api.h"
16#include "pxr/arch/defines.h"
17#include "pxr/arch/inttypes.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#include <windows.h>
38#include <stringapiset.h>
39#endif
40
41ARCH_NAMESPACE_OPEN_SCOPE
42
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
107
113ARCH_API FILE*
114ArchOpenFile(char const* fileName, char const* mode);
115
116ARCH_API int ArchChmod(const char* path, int mode);
117ARCH_API int ArchCloseFile(int fd);
118ARCH_API int ArchUnlinkFile(const char* path);
119ARCH_API int ArchFileAccess(const char* path, int mode);
120ARCH_API FILE* ArchFdOpen(int fd, const char* mode);
121ARCH_API int ArchFileNo(FILE* file);
122ARCH_API int ArchFileIsaTTY(int fd);
123ARCH_API int ArchRmDir(const char* path);
124
128ARCH_API int64_t ArchGetFileLength(const char* fileName);
129ARCH_API int64_t ArchGetFileLength(FILE *file);
130
135ARCH_API std::string ArchGetFileName(FILE *file);
136
143ARCH_API bool ArchStatIsWritable(const ArchStatType *st);
144
150ARCH_API bool ArchGetModificationTime(const char* pathname, double* time);
151
156ARCH_API double ArchGetModificationTime(const ArchStatType& st);
157
167ARCH_API std::string ArchNormPath(const std::string& path,
168 bool stripDriveSpecifier = false);
169
174ARCH_API std::string ArchAbsPath(const std::string& path);
175
181ARCH_API bool ArchGetStatMode(const char *pathname, int *mode);
182
192ARCH_API const char *ArchGetTmpDir();
193
207ARCH_API
208std::string ArchMakeTmpFileName(const std::string& prefix,
209 const std::string& suffix = std::string());
210
220ARCH_API
221int ArchMakeTmpFile(const std::string& prefix, std::string* pathname = 0);
222
231ARCH_API
232int ArchMakeTmpFile(const std::string& tmpdir,
233 const std::string& prefix, std::string* pathname = 0);
234
243ARCH_API
244std::string ArchMakeTmpSubdir(const std::string& tmpdir,
245 const std::string& prefix);
246
247// Helper 'deleter' for use with std::unique_ptr for file mappings.
249 Arch_Unmapper() : _length(~0) {}
250 explicit Arch_Unmapper(size_t length) : _length(length) {}
251 ARCH_API void operator()(char *mapStart) const;
252 ARCH_API void operator()(char const *mapStart) const;
253 size_t GetLength() const { return _length; }
254private:
255 size_t _length;
256};
257
262using ArchConstFileMapping = std::unique_ptr<char const, Arch_Unmapper>;
263using ArchMutableFileMapping = std::unique_ptr<char, Arch_Unmapper>;
264
266inline size_t
268 return m.get_deleter().GetLength();
269}
270
272inline size_t
274 return m.get_deleter().GetLength();
275}
276
281ARCH_API
282ArchConstFileMapping
283ArchMapFileReadOnly(FILE *file, std::string *errMsg=nullptr);
284
286ARCH_API
288ArchMapFileReadOnly(std::string const& path, std::string *errMsg=nullptr);
289
296ARCH_API
298ArchMapFileReadWrite(FILE *file, std::string *errMsg=nullptr);
299
301ARCH_API
303ArchMapFileReadWrite(std::string const& path, std::string *errMsg=nullptr);
304
306 ArchMemAdviceNormal, // Treat range with default behavior.
307 ArchMemAdviceWillNeed, // OS may prefetch this range.
308 ArchMemAdviceDontNeed, // OS may free resources related to this range.
309 ArchMemAdviceRandomAccess, // Prefetching may not be beneficial.
310};
311
316ARCH_API
317void ArchMemAdvise(void const *addr, size_t len, ArchMemAdvice adv);
318
331ARCH_API
332bool
334 void const *addr, size_t len, unsigned char *pageMap);
335
340ARCH_API
341int64_t ArchPRead(FILE *file, void *buffer, size_t count, int64_t offset);
342
347ARCH_API
348int64_t ArchPWrite(FILE *file, void const *bytes, size_t count, int64_t offset);
349
352ARCH_API
353std::string ArchReadLink(const char* path);
354
356 ArchFileAdviceNormal, // Treat range with default behavior.
357 ArchFileAdviceWillNeed, // OS may prefetch this range.
358 ArchFileAdviceDontNeed, // OS may free resources related to this range.
359 ArchFileAdviceRandomAccess, // Prefetching may not be beneficial.
360};
361
366ARCH_API
367void ArchFileAdvise(FILE *file, int64_t offset, size_t count,
368 ArchFileAdvice adv);
369
370#if defined(ARCH_OS_WINDOWS)
371
373inline std::string ArchWindowsUtf16ToUtf8(const std::wstring &wstr)
374{
375 if (wstr.empty()) return std::string();
376 // first call is only to get required size for string
377 int size = WideCharToMultiByte(
378 CP_UTF8, 0, wstr.data(), (int)wstr.size(), NULL, 0, NULL, NULL);
379 if (size == 0) return std::string();
380 std::string str(size, 0);
381 if (WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(),
382 &str[0], size, NULL, NULL) == 0) {
383 return std::string();
384 }
385 return str;
386}
387
389inline std::wstring ArchWindowsUtf8ToUtf16(const std::string &str)
390{
391 if (str.empty()) return std::wstring();
392 // first call is only to get required size for wstring
393 int size = MultiByteToWideChar(
394 CP_UTF8, 0, str.data(), (int)str.size(), NULL, 0);
395 if (size == 0) return std::wstring();
396 std::wstring wstr(size, 0);
397 if(MultiByteToWideChar(
398 CP_UTF8, 0, str.data(), (int)str.size(), &wstr[0], size) == 0) {
399 return std::wstring();
400 }
401 return wstr;
402}
403
404#endif
405
406ARCH_NAMESPACE_CLOSE_SCOPE
407
408#endif // PXR_ARCH_FILE_SYSTEM_H
Define integral types.
bool ArchStatIsWritable(const ArchStatType *st)
Returns true if the data in stat struct st indicates that the target file or directory is writable.
size_t ArchGetFileMappingLength(ArchConstFileMapping const &m)
Return the length of an ArchConstFileMapping.
Definition fileSystem.h:267
bool ArchGetStatMode(const char *pathname, int *mode)
Returns the permissions mode (mode_t) for the given pathname.
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...
int ArchRmDir(const char *path)
int ArchFileIsaTTY(int fd)
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 ...
FILE * ArchOpenFile(char const *fileName, char const *mode)
Opens a file.
int64_t ArchPRead(FILE *file, void *buffer, size_t count, int64_t offset)
Read up to count bytes from offset in file into buffer.
std::string ArchMakeTmpFileName(const std::string &prefix, const std::string &suffix=std::string())
Make a temporary file name, in a system-determined temporary directory.
int ArchFileAccess(const char *path, int mode)
void ArchMemAdvise(void const *addr, size_t len, ArchMemAdvice adv)
Advise the OS regarding how the application intends to access a range of memory.
int ArchCloseFile(int fd)
FILE * ArchFdOpen(int fd, const char *mode)
ArchMemAdvice
Definition fileSystem.h:305
@ ArchMemAdviceRandomAccess
Definition fileSystem.h:309
@ ArchMemAdviceDontNeed
Definition fileSystem.h:308
@ ArchMemAdviceWillNeed
Definition fileSystem.h:307
@ ArchMemAdviceNormal
Definition fileSystem.h:306
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.
const char * ArchGetTmpDir()
Return the path to a temporary directory for this platform.
std::unique_ptr< char, Arch_Unmapper > ArchMutableFileMapping
Definition fileSystem.h:263
std::string ArchNormPath(const std::string &path, bool stripDriveSpecifier=false)
Normalizes the specified path, eliminating double slashes, etc.
struct stat ArchStatType
Definition fileSystem.h:101
std::unique_ptr< char const, Arch_Unmapper > ArchConstFileMapping
ArchConstFileMapping and ArchMutableFileMapping are std::unique_ptr<char const *, ....
Definition fileSystem.h:262
std::string ArchMakeTmpSubdir(const std::string &tmpdir, const std::string &prefix)
Create a temporary sub-direcrory, in a given temporary directory.
bool ArchGetModificationTime(const char *pathname, double *time)
Returns the modification time (mtime) in seconds for a file.
int ArchUnlinkFile(const char *path)
int ArchChmod(const char *path, int mode)
int ArchFileNo(FILE *file)
std::string ArchReadLink(const char *path)
Returns the value of the symbolic link at path.
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.
ArchFileAdvice
Definition fileSystem.h:355
@ ArchFileAdviceWillNeed
Definition fileSystem.h:357
@ ArchFileAdviceDontNeed
Definition fileSystem.h:358
@ ArchFileAdviceRandomAccess
Definition fileSystem.h:359
@ ArchFileAdviceNormal
Definition fileSystem.h:356
int64_t ArchGetFileLength(const char *fileName)
Return the length of a file in bytes.
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::string ArchAbsPath(const std::string &path)
Returns the canonical absolute path of the specified filename.
int ArchMakeTmpFile(const std::string &prefix, std::string *pathname=0)
Create a temporary file, in a system-determined temporary directory.
std::string ArchGetFileName(FILE *file)
Return a filename for this file, if one can be obtained.
size_t GetLength() const
Definition fileSystem.h:253
Arch_Unmapper()
Definition fileSystem.h:249
Arch_Unmapper(size_t length)
Definition fileSystem.h:250
void operator()(char const *mapStart) const
void operator()(char *mapStart) const