All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <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)
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
41PXR_NAMESPACE_OPEN_SCOPE
42
45#if !defined(ARCH_OS_WINDOWS)
46 #ifdef _POSIX_VERSION
47 #include <limits.h> /* for PATH_MAX */
48 #else
49 #include <sys/param.h> /* for MAXPATHLEN */
50 #endif
51#else
52 // XXX -- Should probably have ARCH_ macro for this.
53 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
54
55 // See https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx
56 // XXX -- Should probably have Arch enum for these.
57 #define F_OK 0 // Test for existence.
58 #define X_OK 1 // Test for execute permission.
59 #define W_OK 2 // Test for write permission.
60 #define R_OK 4 // Test for read permission.
61#endif
62
63#if defined(ARCH_OS_WINDOWS)
64 #define ARCH_GLOB_NOCHECK 1
65 #define ARCH_GLOB_MARK 2
66 #define ARCH_GLOB_NOSORT 4
67#else
68 #define ARCH_GLOB_NOCHECK GLOB_NOCHECK
69 #define ARCH_GLOB_MARK GLOB_MARK
70 #define ARCH_GLOB_NOSORT GLOB_NOSORT
71#endif
72#define ARCH_GLOB_DEFAULT (ARCH_GLOB_NOCHECK | ARCH_GLOB_MARK)
73
74#ifndef ARCH_PATH_MAX
75 #ifdef PATH_MAX
76 #define ARCH_PATH_MAX PATH_MAX
77 #else
78 #ifdef MAXPATHLEN
79 #define ARCH_PATH_MAX MAXPATHLEN
80 #else
81 #ifdef _MAX_PATH
82 #define ARCH_PATH_MAX _MAX_PATH
83 #else
84 #define ARCH_PATH_MAX 1024
85 #endif
86 #endif
87 #endif
88#endif
89
90#if defined(ARCH_OS_WINDOWS)
91 #define ARCH_PATH_SEP "\\"
92 #define ARCH_PATH_LIST_SEP ";"
93 #define ARCH_REL_PATH_IDENT ".\\"
94#else
95 #define ARCH_PATH_SEP "/"
96 #define ARCH_PATH_LIST_SEP ":"
97 #define ARCH_REL_PATH_IDENT "./"
98#endif
99
100#if defined(ARCH_OS_WINDOWS)
101typedef struct __stat64 ArchStatType;
102#else
103typedef struct stat ArchStatType;
104#endif
105
110
116ARCH_API FILE*
117ArchOpenFile(char const* fileName, char const* mode);
118
119#if defined(ARCH_OS_WINDOWS)
120# define ArchChmod(path, mode) _chmod(path, mode)
121#else
122# define ArchChmod(path, mode) chmod(path, mode)
123#endif
124
125#if defined(ARCH_OS_WINDOWS)
126# define ArchCloseFile(fd) _close(fd)
127#else
128# define ArchCloseFile(fd) close(fd)
129#endif
130
131#if defined(ARCH_OS_WINDOWS)
132# define ArchUnlinkFile(path) _unlink(path)
133#else
134# define ArchUnlinkFile(path) unlink(path)
135#endif
136
137#if defined(ARCH_OS_WINDOWS)
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
415inline std::string ArchWindowsUtf16ToUtf8(const std::wstring &wstr)
416{
417 if (wstr.empty()) return std::string();
418 // first call is only to get required size for 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();
426 }
427 return str;
428}
429
431inline std::wstring ArchWindowsUtf8ToUtf16(const std::string &str)
432{
433 if (str.empty()) return std::wstring();
434 // first call is only to get required size for 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();
442 }
443 return wstr;
444}
445
446#endif
447
449
450PXR_NAMESPACE_CLOSE_SCOPE
451
452#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.