HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fileUtils.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_TF_FILE_UTILS_H
8 #define PXR_BASE_TF_FILE_UTILS_H
9 
10 /// \file tf/fileUtils.h
11 /// \ingroup group_tf_File
12 /// Definitions of basic file utilities in tf.
13 
14 #include "pxr/pxr.h"
15 #include "pxr/base/tf/api.h"
16 
17 #include <string>
18 #include <vector>
19 #include <functional>
20 
22 
23 /// Returns true if the path exists.
24 ///
25 /// If \p resolveSymlinks is false (default), the path is checked using
26 /// lstat(). if \p resolveSymlinks is true, the path is checked using stat(),
27 /// which resolves all symbolic links in the path.
28 ///
29 /// On Windows, if the path points to a reparse point symlink on a network
30 /// share, even if resolveSymlinks is true we are unable to follow the
31 /// symlink, and so will return true even if the destination of the symlink
32 /// does not exist.
33 TF_API
34 bool TfPathExists(std::string const& path, bool resolveSymlinks = false);
35 
36 /// Returns true if the path exists and is a directory.
37 ///
38 /// If \p resolveSymlinks is false (default), the path is checked using
39 /// lstat(). if \p resolveSymlinks is true, the path is checked using stat(),
40 /// which resolves all symbolic links in the path.
41 ///
42 /// On Windows, if the path points to a reparse point symlink on a network
43 /// share, even if resolveSymlinks is true we are unable to follow the
44 /// symlink, and so will return true even if the destination of the symlink
45 /// does not exist.
46 TF_API
47 bool TfIsDir(std::string const& path, bool resolveSymlinks = false);
48 
49 /// Returns true if the path exists and is a file.
50 ///
51 /// If \p resolveSymlinks is false (default), the path is checked using
52 /// lstat(). if \p resolveSymlinks is true, the path is checked using stat(),
53 /// which resolves all symbolic links in the path.
54 ///
55 /// On Windows, if the path points to a reparse point symlink on a network
56 /// share, even if resolveSymlinks is true we are unable to follow the
57 /// symlink, and so will return true even if the destination of the symlink
58 /// does not exist.
59 TF_API
60 bool TfIsFile(std::string const& path, bool resolveSymlinks = false);
61 
62 /// Returns true if the path exists and is a symbolic link.
63 ///
64 /// On Windows, if the path points to a reparse point symlink on a network
65 /// share, we are unable to follow the symlink, so we will return false for
66 /// this directory even though it is a link. This is because any attempt to
67 /// actually follow this link will fail, so it is safer to pretend it is not
68 /// actually a link.
69 TF_API
70 bool TfIsLink(std::string const& path);
71 
72 /// Returns true if the file or directory at \p path is writable.
73 ///
74 /// For this function to return true, the file must exist and be writable by
75 /// the effective user, effective group, or all users. This function
76 /// dereferences symbolic links, returning whether or not the resolved file or
77 /// directory path is writable. If the file or directory does not exist, this
78 /// function returns false.
79 TF_API
80 bool TfIsWritable(std::string const& path);
81 
82 /// Returns true if the path is an empty directory.
83 TF_API
84 bool TfIsDirEmpty(std::string const& path);
85 
86 /// Creates a symbolic link from \p src to \p dst.
87 TF_API
88 bool TfSymlink(std::string const& src, std::string const& dst);
89 
90 /// Deletes a file at path.
91 TF_API
92 bool TfDeleteFile(std::string const& path);
93 
94 /// Creates a directory.
95 ///
96 /// If the directory cannot be created, this function returns false. If no
97 /// mode is specified, the default mode is 0777. If the specified path already
98 /// exists, or an error occurs while creating the directory, this method
99 /// returns false.
100 TF_API
101 bool TfMakeDir(std::string const& path, int mode=-1);
102 
103 /// Creates a directory hierarchy.
104 ///
105 /// If any element of the path cannot be created, this function will return
106 /// false. The specified mode will be used to create all new directories. If
107 /// no mode is specified, the default mode of \c TfMakeDir is used. If the
108 /// target directory exists, this function returns false if \p existOk is
109 /// false.
110 TF_API
111 bool TfMakeDirs(std::string const& path, int mode=-1, bool existOk=false);
112 
113 /// Function type for TfWalkDirs.
114 ///
115 /// This function is called once for each directory visited by TfWalkDirs.
116 /// The first parameter is the directory path; if the topmost directory passed
117 /// to TfWalkDirs is relative, this path will also be relative. The second
118 /// parameter is a vector of subdirectory names, relative to the directory
119 /// path. This parameter is a pointer, allowing the subdirectory list to be
120 /// modified, thus controlling which directories are visited. Note that
121 /// modifying the subdirectory vector has no effect when TfWalkDirs is called
122 /// with \c topDown set to \c false. The final parameter is a vector of file
123 /// names found in the directory path. The returned value determines whether
124 /// the walk should be terminated (\c false), or continue (\c true).
125 typedef std::function<bool (std::string const&,
126  std::vector<std::string> *,
127  std::vector<std::string> const&)> TfWalkFunction;
128 
129 /// TfRmTree error handler function.
130 ///
131 /// The first parameter is the path which caused the error (file or directory),
132 /// and the second parameter is an error message indicating why the error
133 /// occurred.
134 typedef std::function<void (std::string const&,
135  std::string const&)> TfWalkErrorHandler;
136 
137 /// error handler to use when you want to ignore errors
138 ///
139 /// When calling TfWalkDirs/ChmodTree/RmTree and you want to ignore errors,
140 /// you can pass in this public error handler which will ignore all the
141 /// errors.
142 TF_API
143 void TfWalkIgnoreErrorHandler(std::string const& path, std::string const& msg);
144 
145 /// Directory tree walker.
146 ///
147 /// This function attempts to be as compatible as possible with Python's
148 /// os.walk() function.
149 ///
150 /// For each directory in the tree rooted at \p top (including \p top itself,
151 /// but excluding '.' and '..'), the std::function \p fn is called with
152 /// three arguments: \c dirpath, \c dirnames, and \c filenames.
153 ///
154 /// \c dirpath is a string, the path to the directory. \c dirnames is a list
155 /// of the names of the subdirectories in \c dirpath (excluding '.' and '..').
156 /// \c filenames is a list of the names of the non-directory files in
157 /// \c dirpath. Note that the names in the sets are just names, with no path
158 /// components. To get a full path (which begins with \p top) to a file or
159 /// directory in \c dirpath, use \c TfStringCatPaths(dirpath, name).
160 ///
161 /// If optional argument \p topDown is true, or not specified, \p fn is called
162 /// for a directory before any subdirectories. If topdown is false, \p fn is
163 /// called for a directory after all subdirectories. Additionally, when
164 /// \p topDown is true, the walk function can modify the \c dirnames set in
165 /// place. This can be used to prune the search, or to impose a specific
166 /// visitation order. Modifying \c dirnames when \p topDown is false has no
167 /// effect, since the directories in \c dirnames have already been visited
168 /// by the time they are passed to \p fn.
169 ///
170 /// The value returned by the error handler function \p onError determines what
171 /// further action will be taken if an error is encountered. If \c true is
172 /// returned, the walk will continue; if \c false, the walk will not continue.
173 ///
174 /// If \p followLinks is false, symbolic links to directories encountered
175 /// during the walk are passed to the walk function in the \c filenames vector.
176 /// If \p followLinks is true, symbolic links to directories are passed to the
177 /// walk function in the \p dirnames vector, and the walk will recurse into
178 /// these directories.
179 ///
180 /// If \p top is a symbolic link to a directory, it is followed regardless of
181 /// the value of \p followLinks. Calling TfWalkDirs with a file argument
182 /// returns immediately without calling \p fn.
183 TF_API
184 void TfWalkDirs(std::string const& top,
185  TfWalkFunction fn,
186  bool topDown=true,
187  TfWalkErrorHandler onError = 0,
188  bool followLinks = false);
189 
190 /// Recursively delete a directory tree rooted at \p path.
191 ///
192 /// Tf runtime errors are raised if any errors are encountered while deleting
193 /// the specified \p path. Pass in TfWalkIgnoreErrorHandler() to ignore errors.
194 /// Alternately, sending in a custom TfWalkErrorHandler will
195 /// call this handler when errors occur. This handler receives the path which
196 /// caused the error, and a message indicating why the error occurred.
197 TF_API
198 void TfRmTree(std::string const& path,
199  TfWalkErrorHandler onError = 0);
200 
201 /// Return a list containing files and directories in \p path.
202 ///
203 /// A trailing path separator character is appended to directories returned
204 /// in the listing. If \p recursive is true, the directory listing will
205 /// include all subdirectory structure of \p path.
206 TF_API
207 std::vector<std::string> TfListDir(std::string const& path,
208  bool recursive = false);
209 
210 /// Read the contents of \p dirPath and append the names of the contained
211 /// directories, files, and symlinks to \p dirnames, \p filenames, and
212 /// \p symlinknames, respectively.
213 ///
214 /// Return true if \p dirPath 's contents were read successfully. Otherwise
215 /// return false and set \p errMsg with a description of the error if \p errMsg
216 /// is not NULL.
217 ///
218 /// It is safe to pass NULL for any of \p dirnames, \p filenames, and
219 /// \p symlinknames. In that case those elements are not reported
220 TF_API
221 bool
222 TfReadDir(std::string const &dirPath,
223  std::vector<std::string> *dirnames,
224  std::vector<std::string> *filenames,
225  std::vector<std::string> *symlinknames,
226  std::string *errMsg = NULL);
227 
228 /// Touch \p fileName, updating access and modification time to 'now'.
229 ///
230 /// A simple touch-like functionality. Simple in a sense that it does not
231 /// offer as many options as the same-name unix touch command, but otherwise
232 /// is identical to the default touch behavior. If \p create is true and
233 /// the file does not already exist, an empty file gets created, otherwise
234 /// the touch call fails if the file does not already exist.
235 TF_API
236 bool TfTouchFile(std::string const &fileName, bool create=true);
237 
239 
240 #endif // PXR_BASE_TF_FILE_UTILS_H
TF_API bool TfIsFile(std::string const &path, bool resolveSymlinks=false)
TF_API bool TfReadDir(std::string const &dirPath, std::vector< std::string > *dirnames, std::vector< std::string > *filenames, std::vector< std::string > *symlinknames, std::string *errMsg=NULL)
TF_API bool TfMakeDir(std::string const &path, int mode=-1)
#define TF_API
Definition: api.h:23
void
Definition: png.h:1083
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
std::function< bool(std::string const &, std::vector< std::string > *, std::vector< std::string > const &)> TfWalkFunction
Definition: fileUtils.h:127
PXR_NAMESPACE_OPEN_SCOPE TF_API bool TfPathExists(std::string const &path, bool resolveSymlinks=false)
OutGridT const XformOp bool bool
TF_API bool TfSymlink(std::string const &src, std::string const &dst)
Creates a symbolic link from src to dst.
std::function< void(std::string const &, std::string const &)> TfWalkErrorHandler
Definition: fileUtils.h:135
TF_API bool TfIsLink(std::string const &path)
TF_API void TfWalkDirs(std::string const &top, TfWalkFunction fn, bool topDown=true, TfWalkErrorHandler onError=0, bool followLinks=false)
TF_API bool TfDeleteFile(std::string const &path)
Deletes a file at path.
GLenum mode
Definition: glcorearb.h:99
GLenum GLenum dst
Definition: glcorearb.h:1793
TF_API bool TfIsDirEmpty(std::string const &path)
Returns true if the path is an empty directory.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
TF_API bool TfMakeDirs(std::string const &path, int mode=-1, bool existOk=false)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TF_API bool TfIsWritable(std::string const &path)
TF_API bool TfIsDir(std::string const &path, bool resolveSymlinks=false)
GLdouble GLdouble GLdouble top
Definition: glad.h:2817
TF_API void TfWalkIgnoreErrorHandler(std::string const &path, std::string const &msg)
TF_API std::vector< std::string > TfListDir(std::string const &path, bool recursive=false)
TF_API bool TfTouchFile(std::string const &fileName, bool create=true)
TF_API void TfRmTree(std::string const &path, TfWalkErrorHandler onError=0)
GLenum src
Definition: glcorearb.h:1793