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