7#ifndef PXR_BASE_ARCH_FILE_SYSTEM_H
8#define PXR_BASE_ARCH_FILE_SYSTEM_H
15#include "pxr/base/arch/api.h"
16#include "pxr/base/arch/defines.h"
27#if defined(ARCH_OS_LINUX)
29#include <sys/statfs.h>
31#elif defined(ARCH_OS_DARWIN)
35#elif defined(ARCH_OS_WINDOWS)
38#include <stringapiset.h>
41PXR_NAMESPACE_OPEN_SCOPE
45#if !defined(ARCH_OS_WINDOWS)
49 #include <sys/param.h>
53 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
63#if defined(ARCH_OS_WINDOWS)
64 #define ARCH_GLOB_NOCHECK 1
65 #define ARCH_GLOB_MARK 2
66 #define ARCH_GLOB_NOSORT 4
68 #define ARCH_GLOB_NOCHECK GLOB_NOCHECK
69 #define ARCH_GLOB_MARK GLOB_MARK
70 #define ARCH_GLOB_NOSORT GLOB_NOSORT
72#define ARCH_GLOB_DEFAULT (ARCH_GLOB_NOCHECK | ARCH_GLOB_MARK)
76 #define ARCH_PATH_MAX PATH_MAX
79 #define ARCH_PATH_MAX MAXPATHLEN
82 #define ARCH_PATH_MAX _MAX_PATH
84 #define ARCH_PATH_MAX 1024
90#if defined(ARCH_OS_WINDOWS)
91 #define ARCH_PATH_SEP "\\"
92 #define ARCH_PATH_LIST_SEP ";"
93 #define ARCH_REL_PATH_IDENT ".\\"
95 #define ARCH_PATH_SEP "/"
96 #define ARCH_PATH_LIST_SEP ":"
97 #define ARCH_REL_PATH_IDENT "./"
100#if defined(ARCH_OS_WINDOWS)
101typedef struct __stat64 ArchStatType;
103typedef struct stat ArchStatType;
119#if defined(ARCH_OS_WINDOWS)
120# define ArchChmod(path, mode) _chmod(path, mode)
122# define ArchChmod(path, mode) chmod(path, mode)
125#if defined(ARCH_OS_WINDOWS)
126# define ArchCloseFile(fd) _close(fd)
128# define ArchCloseFile(fd) close(fd)
131#if defined(ARCH_OS_WINDOWS)
132# define ArchUnlinkFile(path) _unlink(path)
134# define ArchUnlinkFile(path) unlink(path)
137#if defined(ARCH_OS_WINDOWS)
138 ARCH_API
int ArchFileAccess(
const char* path,
int mode);
140# define ArchFileAccess(path, mode) access(path, mode)
143#if defined(ARCH_OS_WINDOWS)
144# define ArchFdOpen(fd, mode) _fdopen(fd, mode)
146# define ArchFdOpen(fd, mode) fdopen(fd, mode)
149#if defined(ARCH_OS_WINDOWS)
150# define ArchFileNo(stream) _fileno(stream)
152# define ArchFileNo(stream) fileno(stream)
155#if defined(ARCH_OS_WINDOWS)
156# define ArchFileIsaTTY(stream) _isatty(stream)
158# define ArchFileIsaTTY(stream) isatty(stream)
161#if defined(ARCH_OS_WINDOWS)
162 ARCH_API
int ArchRmDir(
const char* path);
164# define ArchRmDir(path) rmdir(path)
210 bool stripDriveSpecifier =
false);
251 const std::string& suffix = std::string());
275 const std::string& prefix, std::string* pathname = 0);
287 const std::string& prefix);
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; }
305using ArchMutableFileMapping = std::unique_ptr<char, Arch_Unmapper>;
310 return m.get_deleter().GetLength();
316 return m.get_deleter().GetLength();
339ArchMutableFileMapping
344ArchMutableFileMapping
349 ArchMemAdviceWillNeed,
350 ArchMemAdviceDontNeed,
351 ArchMemAdviceRandomAccess,
376 void const *addr,
size_t len,
unsigned char *pageMap);
383int64_t
ArchPRead(FILE *file,
void *buffer,
size_t count, int64_t offset);
390int64_t
ArchPWrite(FILE *file,
void const *bytes,
size_t count, int64_t offset);
398 ArchFileAdviceNormal,
399 ArchFileAdviceWillNeed,
400 ArchFileAdviceDontNeed,
401 ArchFileAdviceRandomAccess,
412#if defined(ARCH_OS_WINDOWS)
415inline std::string ArchWindowsUtf16ToUtf8(
const std::wstring &wstr)
417 if (wstr.empty())
return std::string();
419 int size = WideCharToMultiByte(
420 CP_UTF8, 0, wstr.data(), (
int)wstr.size(), NULL, 0, NULL, NULL);
421 if (size == 0)
return std::string();
422 std::string str(size, 0);
423 if (WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (
int)wstr.size(),
424 &str[0], size, NULL, NULL) == 0) {
425 return std::string();
431inline std::wstring ArchWindowsUtf8ToUtf16(
const std::string &str)
433 if (str.empty())
return std::wstring();
435 int size = MultiByteToWideChar(
436 CP_UTF8, 0, str.data(), (
int)str.size(), NULL, 0);
437 if (size == 0)
return std::wstring();
438 std::wstring wstr(size, 0);
439 if(MultiByteToWideChar(
440 CP_UTF8, 0, str.data(), (
int)str.size(), &wstr[0], size) == 0) {
441 return std::wstring();
450PXR_NAMESPACE_CLOSE_SCOPE
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 *, ....
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.
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.
Defines useful mathematical limits.