24 #ifndef PXR_BASE_ARCH_FILE_SYSTEM_H 25 #define PXR_BASE_ARCH_FILE_SYSTEM_H 32 #include "pxr/base/arch/api.h" 33 #include "pxr/base/arch/defines.h" 41 #include <sys/types.h> 44 #if defined(ARCH_OS_LINUX) 46 #include <sys/statfs.h> 48 #elif defined(ARCH_OS_DARWIN) 50 #include <sys/mount.h> 52 #elif defined(ARCH_OS_WINDOWS) 55 #include <stringapiset.h> 58 PXR_NAMESPACE_OPEN_SCOPE
62 #if !defined(ARCH_OS_WINDOWS) 66 #include <sys/param.h> 70 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 74 #define F_OK 0 // Test for existence. 75 #define X_OK 1 // Test for execute permission. 76 #define W_OK 2 // Test for write permission. 77 #define R_OK 4 // Test for read permission. 80 #if defined(ARCH_OS_WINDOWS) 81 #define ARCH_GLOB_NOCHECK 1 82 #define ARCH_GLOB_MARK 2 83 #define ARCH_GLOB_NOSORT 4 85 #define ARCH_GLOB_NOCHECK GLOB_NOCHECK 86 #define ARCH_GLOB_MARK GLOB_MARK 87 #define ARCH_GLOB_NOSORT GLOB_NOSORT 89 #define ARCH_GLOB_DEFAULT (ARCH_GLOB_NOCHECK | ARCH_GLOB_MARK) 93 #define ARCH_PATH_MAX PATH_MAX 96 #define ARCH_PATH_MAX MAXPATHLEN 99 #define ARCH_PATH_MAX _MAX_PATH 101 #define ARCH_PATH_MAX 1024 107 #if defined(ARCH_OS_WINDOWS) 108 #define ARCH_PATH_SEP "\\" 109 #define ARCH_PATH_LIST_SEP ";" 110 #define ARCH_REL_PATH_IDENT ".\\" 112 #define ARCH_PATH_SEP "/" 113 #define ARCH_PATH_LIST_SEP ":" 114 #define ARCH_REL_PATH_IDENT "./" 117 #if defined(ARCH_OS_WINDOWS) 118 typedef struct __stat64 ArchStatType;
120 typedef struct stat ArchStatType;
136 #if defined(ARCH_OS_WINDOWS) 137 # define ArchChmod(path, mode) _chmod(path, mode) 139 # define ArchChmod(path, mode) chmod(path, mode) 142 #if defined(ARCH_OS_WINDOWS) 143 # define ArchCloseFile(fd) _close(fd) 145 # define ArchCloseFile(fd) close(fd) 148 #if defined(ARCH_OS_WINDOWS) 149 # define ArchUnlinkFile(path) _unlink(path) 151 # define ArchUnlinkFile(path) unlink(path) 154 #if defined(ARCH_OS_WINDOWS) 155 ARCH_API
int ArchFileAccess(
const char* path,
int mode);
157 # define ArchFileAccess(path, mode) access(path, mode) 160 #if defined(ARCH_OS_WINDOWS) 161 # define ArchFdOpen(fd, mode) _fdopen(fd, mode) 163 # define ArchFdOpen(fd, mode) fdopen(fd, mode) 166 #if defined(ARCH_OS_WINDOWS) 167 # define ArchFileNo(stream) _fileno(stream) 169 # define ArchFileNo(stream) fileno(stream) 172 #if defined(ARCH_OS_WINDOWS) 173 # define ArchFileIsaTTY(stream) _isatty(stream) 175 # define ArchFileIsaTTY(stream) isatty(stream) 178 #if defined(ARCH_OS_WINDOWS) 179 ARCH_API
int ArchRmDir(
const char* path);
181 # define ArchRmDir(path) rmdir(path) 226 ARCH_API std::string
ArchNormPath(
const std::string& path,
227 bool stripDriveSpecifier =
false);
233 ARCH_API std::string
ArchAbsPath(
const std::string& path);
268 const std::string& suffix = std::string());
280 int ArchMakeTmpFile(
const std::string& prefix, std::string* pathname = 0);
292 const std::string& prefix, std::string* pathname = 0);
304 const std::string& prefix);
307 struct Arch_Unmapper {
308 Arch_Unmapper() : _length(~0) {}
309 explicit Arch_Unmapper(
size_t length) : _length(length) {}
310 ARCH_API
void operator()(
char *mapStart)
const;
311 ARCH_API
void operator()(
char const *mapStart)
const;
312 size_t GetLength()
const {
return _length; }
322 using ArchMutableFileMapping = std::unique_ptr<char, Arch_Unmapper>;
327 return m.get_deleter().GetLength();
333 return m.get_deleter().GetLength();
356 ArchMutableFileMapping
361 ArchMutableFileMapping
366 ArchMemAdviceWillNeed,
367 ArchMemAdviceDontNeed,
368 ArchMemAdviceRandomAccess,
376 void ArchMemAdvise(
void const *addr,
size_t len, ArchMemAdvice adv);
393 void const *addr,
size_t len,
unsigned char *pageMap);
400 int64_t
ArchPRead(FILE *file,
void *buffer,
size_t count, int64_t offset);
407 int64_t
ArchPWrite(FILE *file,
void const *bytes,
size_t count, int64_t offset);
414 enum ArchFileAdvice {
415 ArchFileAdviceNormal,
416 ArchFileAdviceWillNeed,
417 ArchFileAdviceDontNeed,
418 ArchFileAdviceRandomAccess,
429 #if defined(ARCH_OS_WINDOWS) 432 inline std::string ArchWindowsUtf16ToUtf8(
const std::wstring &wstr)
434 if (wstr.empty())
return std::string();
436 int size = WideCharToMultiByte(
437 CP_UTF8, 0, wstr.data(), (int)wstr.size(), NULL, 0, NULL, NULL);
438 if (size == 0)
return std::string();
439 std::string str(size, 0);
440 if (WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(),
441 &str[0], size, NULL, NULL) == 0) {
442 return std::string();
448 inline std::wstring ArchWindowsUtf8ToUtf16(
const std::string &str)
450 if (str.empty())
return std::wstring();
452 int size = MultiByteToWideChar(
453 CP_UTF8, 0, str.data(), (int)str.size(), NULL, 0);
454 if (size == 0)
return std::wstring();
455 std::wstring wstr(size, 0);
456 if(MultiByteToWideChar(
457 CP_UTF8, 0, str.data(), (int)str.size(), &wstr[0], size) == 0) {
458 return std::wstring();
467 PXR_NAMESPACE_CLOSE_SCOPE
469 #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<char const *, ....
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.
ARCH_API bool ArchGetStatMode(const char *pathname, int *mode)
Returns the permissions mode (mode_t) for the given pathname.
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.