HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FS_Info.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: FS_Info.h ( FS Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __FS_Info__
13 #define __FS_Info__
14 
15 #include "FS_API.h"
16 #include <UT/UT_Assert.h>
17 #include <UT/UT_NonCopyable.h>
18 #include <UT/UT_ValArray.h>
19 #include <UT/UT_Array.h>
20 #include <UT/UT_String.h>
21 #include <UT/UT_StringArray.h>
22 #include <UT/UT_SysClone.h>
23 #include <SYS/SYS_Types.h>
24 
25 
26 class FS_Info;
27 class FS_InfoHelper;
28 class FS_IndexFile;
29 
30 
31 /// @brief Make a directory and all the parent directories needed.
32 ///
33 /// @param dir_info @n The directory path to create
34 /// @param mode @n The unix file mode
35 /// @param ignore_umask @n By default, the users umask will be used in
36 /// conjunction with the mode. This parameter will @b force
37 /// the mode to be the given value.
38 ///
39 /// @return Returns false only if a directory creation attempt failed.
40 FS_API bool FSmakeDirs(
41  FS_Info &dir_info,
42  mode_t mode = 0777,
43  bool ignore_umask = false);
44 
45 
46 /// Read, write and execute permissions for files.
48 {
49  FS_READ = 0x01,
50  FS_WRITE = 0x02,
51  FS_EXECUTE = 0x04
52 };
53 
54 /// Helper class for encapsulating file information
56 {
57 public:
58  FS_Stat(time_t f_mtime = 0, int64 fsize = 0, bool is_dir = false, bool is_shortcut = false)
59  : myModTime(f_mtime), mySize(fsize), myDirectory(is_dir), myShortcut(is_shortcut)
60  {
61  }
62  FS_Stat(const FS_Stat &src) = default;
63 
64  // This method is used to maintain the stat information when a
65  // sequence of files is being collapsed into one entry.
66  void updateSequenceStat(const FS_Stat &entry)
67  {
68  // The modification time for a sequence is the most current
69  // of the modification times of its entries.
70  if (myModTime < entry.myModTime)
71  myModTime = entry.myModTime;
72 
73  // Size for a sequence should be the sum of all files
74  mySize += entry.mySize;
75  }
76 
77  FS_Stat &operator=(const FS_Stat &s) = default;
78  bool operator==(const FS_Stat &s)
79  {
80  return (myModTime == s.myModTime
81  && mySize == s.mySize
82  && myDirectory == s.myDirectory
83  && myShortcut == s.myShortcut);
84  }
85 
86  time_t modTime() const { return myModTime; }
87  int64 size() const { return mySize; }
88  bool isDirectory() const { return myDirectory; }
89  bool isShortcut() const { return myShortcut; }
90 
91  time_t myModTime;
93  bool myDirectory:1,
94  myShortcut:1; // only on Windows
95 };
96 
97 
98 /// Class for retrieving file information
99 class FS_API FS_Info final
100 {
101 public:
102  /// Use this constructor to create a new info object. The
103  /// specified file is not actually opened or accessed until one of
104  /// the information gathering methods is called.
105  FS_Info(const char *source);
106 
107  /// Return the path parsed from the source
108  const UT_StringHolder &path() const { return myPath; }
109 
110  /// Return the section parsed from the source
111  const UT_StringHolder &section() const { return mySection; }
112 
113  /// This function returns whether or not the current
114  /// user has rights to access the file in the given
115  /// mode. The mode parameter can be any of FS_READ,
116  /// FS_WRITE, or FS_EXECUTE. You can also or these
117  /// values together to check multiple access types
118  /// simultaneously. If the mode value is 0, the
119  /// function returns @c true if the file exists.
120  bool hasAccess(int mode = 0) const;
121 
122  /// Returns if the path is a directory
123  bool getIsDirectory() const;
124 
125  /// Returns the modification time of the file. The
126  /// returned value can be cast directly to a time_t
127  /// value, as defined in the standard C library.
128  time_t getModTime() const;
129 
130  /// Returns the latest modification time of the
131  /// directory and all of its contents recursively.
132  /// On a file it works like getModTime().
133  time_t getRecursiveMaxModTime() const;
134 
135  /// Returns the file size
136  int64 getFileDataSize() const;
137 
138  /// Returns the file extension
139  UT_String getExtension() const;
140 
141  /// Does bulk load of files and directories from the
142  /// source. If the source specified in the constructor
143  /// is a directory rather than an individual file,
144  /// this method will return a listing of all files and
145  /// directories contained in that directory.
146  bool getContents(UT_StringArray &contents,
147  UT_StringArray *dirs = 0,
148  UT_Array<FS_Stat> *stats = 0,
149  UT_Array<FS_Stat> *dir_stats = 0
150  );
151 
152  /// Treats the source as a path glob pattern and
153  /// expands it to the available files. Note that
154  /// backslash escaping with [] is not supported.
155  /// If support_special_tokens is true, the special
156  /// token <LASTMATCH> can be used in the pattern. It
157  /// will be replaced by the last matched path
158  /// component using a wildcard character.
159  bool glob(UT_StringArray &result,
160  bool only_readable = false,
161  bool support_special_tokens = false);
162 
163  bool exists() const;
164  bool fileExists() const;
165  bool canReadFile() const;
166  bool canWriteFile() const;
167  bool canWriteDirectory() const;
168  bool canDeleteFile() const;
169  static char getNextSepChar(const char *source);
170  static char getPrevSepChar(const char *source);
171  static bool customNavigatePath(UT_String &path, const char *file);
172  static bool getPathOnDisk(UT_String &path, const char *file);
173 
174  static bool isNativePath(const char *path);
175 
176  static bool getContentsFromIndexFile(FS_IndexFile &index,
177  UT_StringArray &contents,
178  UT_StringArray *dirs);
179  static bool getContentsFromIndexFile(
181  UT_StringArray &contents,
182  UT_StringArray *dirs,
183  UT_Array<FS_Stat> *stats,
184  UT_Array<FS_Stat> *dir_stats);
185  static bool getContentsFromDiskPath(const char *path,
186  UT_StringArray &contents,
187  UT_StringArray *dirs,
188  UT_Array<FS_Stat> *stats = NULL,
189  UT_Array<FS_Stat> *dir_stats = NULL);
190 
191  static bool statFile(const char *source,
192  const char *filename,
193  FS_Stat *fstat);
194 
195  // Functions for adding and removing helpers.
196  static void addInfoHelper(FS_InfoHelper *helper);
197  static void removeInfoHelper(FS_InfoHelper *helper);
198 
199 private:
200  static UT_ValArray<FS_InfoHelper *> &getHelpers();
201 
202  UT_StringHolder myPath;
203  UT_StringHolder mySection;
204 };
205 
206 
207 /// This class provides a plug-in method for adding a custom "file system".
208 /// @see FS_ReaderHelper, FS_WriterHelper
210 {
211 public:
213  { FS_Info::addInfoHelper(this); }
214  virtual ~FS_InfoHelper()
215  { FS_Info::removeInfoHelper(this); }
216 
218 
219  /// Determine whether this helper can process the filename.
220  virtual bool canHandle(const char *source) = 0;
221 
222  /// @param source Filename for access check
223  /// @param mode The read/write/execute status @see @ref FS_FileAccessMode
224  virtual bool hasAccess(const char *source, int mode) = 0;
225 
226  /// Return whether the filename is a directory
227  virtual bool getIsDirectory(const char *source) = 0;
228 
229  /// Get the modification timestamp (returns time_t)
230  virtual time_t getModTime(const char *source) = 0;
231 
232  /// Get the file size in bytes
233  virtual int64 getSize (const char *source) = 0;
234 
235  /// Read the contents of a directory
236  /// @param source - The directory to read
237  /// @param contents - The list of files in the directory
238  /// @param dirs - The list of directories in the directory
239  /// If @c dirs is NULL, then all directories should be included in the @c
240  /// contents.
241  virtual bool getContents(const char *source,
242  UT_StringArray &contents,
243  UT_StringArray *dirs) = 0;
244 
245  virtual UT_String getExtension(const char *source)
246  {
248  ext = UT_String(source).fileExtension();
249  return ext;
250  }
251 
252  /// Next separator character
253  virtual char getNextSepChar(const char * /*source*/)
254  { return '/'; }
255  /// Previous separator character
256  virtual char getPrevSepChar(const char * /*source*/)
257  { return '/'; }
258  /// Constructs a new path and returns true, if navigating a file path
259  /// requires something else than appending getNextSepChar() and
260  /// concatenating file. But if concatenating sep char and file is
261  /// sufficient, does nothing and returns false.
262  virtual bool customNavigatePath(UT_String &path, const char *file)
263  { return false; }
264 
265  /// Returns the corresponding the path on disk where it should be located
266  /// for the protocol. The path may not exist yet.
267  virtual bool getPathOnDisk(UT_String &path, const char *file)
268  { return false; }
269 };
270 
271 #endif
272 
GT_API const UT_StringHolder filename
time_t modTime() const
Definition: FS_Info.h:86
const UT_StringHolder & path() const
Return the path parsed from the source.
Definition: FS_Info.h:108
virtual char getPrevSepChar(const char *)
Previous separator character.
Definition: FS_Info.h:256
char * fileExtension()
Definition: UT_String.h:640
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
GLdouble s
Definition: glad.h:3009
virtual char getNextSepChar(const char *)
Next separator character.
Definition: FS_Info.h:253
virtual bool customNavigatePath(UT_String &path, const char *file)
Definition: FS_Info.h:262
**But if you need a result
Definition: thread.h:613
virtual ~FS_InfoHelper()
Definition: FS_Info.h:214
FS_Stat(time_t f_mtime=0, int64 fsize=0, bool is_dir=false, bool is_shortcut=false)
Definition: FS_Info.h:58
FS_API bool FSmakeDirs(FS_Info &dir_info, mode_t mode=0777, bool ignore_umask=false)
Make a directory and all the parent directories needed.
int64 mySize
Definition: FS_Info.h:92
bool myShortcut
Definition: FS_Info.h:93
static void removeInfoHelper(FS_InfoHelper *helper)
time_t myModTime
Definition: FS_Info.h:91
void updateSequenceStat(const FS_Stat &entry)
Definition: FS_Info.h:66
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
OIIO_UTIL_API bool exists(string_view path) noexcept
bool isShortcut() const
Definition: FS_Info.h:89
GLenum mode
Definition: glcorearb.h:99
bool myDirectory
Definition: FS_Info.h:93
Helper class for encapsulating file information.
Definition: FS_Info.h:55
virtual bool getPathOnDisk(UT_String &path, const char *file)
Definition: FS_Info.h:267
int64 size() const
Definition: FS_Info.h:87
bool isDirectory() const
Definition: FS_Info.h:88
LeafData & operator=(const LeafData &)=delete
FS_FileAccessMode
Read, write and execute permissions for files.
Definition: FS_Info.h:47
GLuint index
Definition: glcorearb.h:786
const UT_StringHolder & section() const
Return the section parsed from the source.
Definition: FS_Info.h:111
bool operator==(const FS_Stat &s)
Definition: FS_Info.h:78
static void addInfoHelper(FS_InfoHelper *helper)
Class for retrieving file information.
Definition: FS_Info.h:99
#define FS_API
Definition: FS_API.h:10
GLenum src
Definition: glcorearb.h:1793