HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_FileUtil.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: UT_FileUtil.h ( UT Library, C++)
7  *
8  * COMMENTS: This verb class provides a wide assortment of
9  * file utility functions.
10  */
11 
12 #ifndef __UT_FileUtil__
13 #define __UT_FileUtil__
14 
15 #include "UT_API.h"
16 #include <iosfwd>
17 #include <stdio.h>
18 #include <SYS/SYS_Types.h>
19 #include "UT_NTUtil.h" // for mode_t
20 
21 class UT_String;
22 class UT_WorkBuffer;
23 
24 // Here are flags used the by the file utility functions:
25 // If this is set, the showPrompt will be called before any destructive
26 // action.
27 #define UT_FILEUTIL_PROMPT 1
28 // If this is set, the original directory will be included in the
29 // sweep.
30 #define UT_FILEUTIL_INCLUSIVE 2
31 
32 // The standard return code is 0 for success, negative for failure.
33 // An appropriate error is passed to the error method which can
34 // be overridden to do something more interesting than dumping to
35 // the console.
37 {
38 public:
39  enum UT_RemoveDepth { IF_EMPTY, ALL_CONTENTS };
40 
41 
43  virtual ~UT_FileUtil() {}
44 
45  /// Returns the file size of that file in the given path. Returns -1 on
46  /// error.
47  static int64 getFileSize(const char *path);
48 
49  /// Returns the file modification time of the file in the given path.
50  /// Returns -1 on error.
51  static exint getFileModTime(const char *path);
52 
53  /// Returns the latest file modification time recusively of all files
54  /// in the given path including the path itself. If the given path
55  /// can be a regular file.
56  /// Returns -1 if no timestamps can be retrieved.
57  static exint getRecursiveDirMaxModTime(const char *path);
58 
59  /// Return the maximum number of file descriptors that are available
60  /// for this process.
61  static int getMaxFileDescriptors();
62 
63  /// Follow the specified symbolic link to the contents, recursing if the
64  /// contents are themselves another symbolic link. This method will fail
65  /// if the specified path is not to a symbolic link, or if the link does
66  /// not point to an existing file when allow_dangling_link is false.
67  static int resolveSymbolicLinks(const char *path,
68  UT_WorkBuffer &contents,
69  bool allow_dangling_link = false);
70 
71  // Copies a file on disk from the source to the dest.
72  // This is only good for smallish files as we allocate a file size
73  // memory buffer.
74  static int copyFile(const char *srcname, const char *dstname);
75  // Recursively copy a directory
76  static int copyDir(const char *srcname, const char *dstname);
77  // Copies a file from a disk into a stream. This is good for
78  // building a .cpio archive.
79  static int copyFileToStream(const char *srcname, std::ostream &os);
80 
81  // Move a file, returns 0 on success.
82  static int moveFile(const char *srcname, const char *dstname);
83  // Delete a file
84  static int removeFile(const char *fname);
85 
86  // Delete a directory
87  static int removeDir(const char *dname,
88  UT_RemoveDepth depth = IF_EMPTY);
89 
90  /// Make a single directory. All leading path components must exist.
91  /// The function returns true if the path already exists or if the path was
92  /// made.
93  /// @param path @n The path to create
94  /// @param mode @n The unix file mode
95  /// @param ignore_umask @n
96  /// By default, the users umask will be used in conjunction with the
97  /// mode. This parameter will @b force the mode to be the given value.
98  static bool makeDir(const char *path, mode_t mode=0777,
99  bool ignore_umask = false);
100 
101  /// Make a directory and all the parent directories needed
102  /// @param path @n The path to create
103  /// @param mode @n The unix file mode
104  /// @param ignore_umask @n
105  /// By default, the users umask will be used in conjunction with the
106  /// mode. This parameter will @b force the mode to be the given value.
107  static bool makeDirs(const char *path, mode_t mode=0777,
108  bool ignore_umask = false);
109 
110  /// Locks a file for exclusive access. Will not return until the file lock
111  /// is achiveved or an error occurs. Equivalent to calling writeLockFile().
112  /// Note: On Linux, if any file descriptor referring to this file is
113  /// closed, the lock will be automatically be closed. Be very careful!
114  /// Returns true if the file lock succeeded, and false otherwise.
115  static bool lockFile(int fd);
116 
117  /// Unlocks a file that was locked for exclusive access with lockFile,
118  /// above.
119  /// Returns true if the unlock succeeded, and false otherwise.
120  static bool unlockFile(int fd);
121 
122  /// Locks a file for write access. Will not return until the file lock
123  /// is achiveved or an error occurs.
124  /// Note: On Linux, if any file descriptor referring to this file is
125  /// closed, the lock will be automatically be closed. Be very careful!
126  /// Returns true if the file lock succeeded, and false otherwise.
127  static bool writeLockFile(int fd);
128 
129  /// Locks a file for read access. Will not return until the file lock
130  /// is achiveved or an error occurs.
131  /// Note: On Linux, if any file descriptor referring to this file is
132  /// closed, the lock will be automatically be closed. Be very careful!
133  /// Returns true if the file lock succeeded, and false otherwise.
134  static bool readLockFile(int fd);
135 
136  // This navigates up the given path the specified number of levels.
137  // It adds a \0 at the correct slash, leaving path to be the
138  // proper path name.
139  // eg: upDirectory("foo/bar.vex", 1) = "foo"
140  // upDriectory("foo/bar/", 1) = "foo"
141  static void upDirectory(char *path, int levels);
142 
143  // This scans the (ascii) file fname for any line which starts
144  // with the prefix. If readonly is not true, it writes out a new
145  // version of the file lacking those lines. The return is the
146  // number of lines that would be removed, or negative if error.
147  static int removeLinesFromFile(const char *fname,
148  const char *prefix, int readonly = 0);
149 
150  // This is of the form
151  // removeOverrideFiles("d:/hsite/houdini5",
152  // "subnet/Sop",
153  // "foo.ds", "Dialog Script", UT_FILEUTIL_PROMPT);
154  // If the stripinfo is specified, it will do a removeLinesFromFile
155  // with stripinfo rather than delete.
156  int removeOverrideFiles(const char *newbasepath,
157  const char *relpath, const char *fname,
158  const char *english, int flags,
159  const char *stripinfo = 0);
160 
161  /// Parses the file in search of the given XML element and outputs
162  /// its contents into the string argument. Returns true uppon success
163  /// or false on failure.
164  static bool readXMLElementFromFile( const char * xml_file_path,
165  const char * element_name,
166  UT_String & element_data );
167 
168  // These are UI methods you can override to gain different behaviours
169  // for your instance:
170  // Severity is the same as UI_Manager::UI_ErrorType.
171  virtual void showError(const char *error, int severity = 2);
172  // 0 means Cancel, 1 OK.
173  virtual int showPrompt(const char *prompt);
174 };
175 
176 /// A class that allows you to create and lock a file for exclusive access.
177 /// When instances of this class go out of scope, the locked file is
178 /// automatically unlocked and closed.
180 {
181 public:
182  /// Open and lock a file for exclusive reading and writing.
183  UT_AutoFileLock(const char *filename)
184  {
185  // Try opening it as if it exists first, then try creating it.
186  myFile = fopen(filename, "rb+");
187  if (!myFile)
188  myFile = fopen(filename, "wb+");
189 
190  if (myFile)
191  myLockSucceeded = UT_FileUtil::lockFile(fileno(myFile));
192  else
193  myLockSucceeded = false;
194  }
195  /// Open and lock a file for read-only (exclusive=false),
196  /// or for read-write (exclusive=true). If filename does not exist, then
197  /// it is opened as read-write.
198  UT_AutoFileLock(const char *filename, bool exclusive)
199  {
200  // Try opening it as if it exists first, then try creating it.
201  myFile = fopen(filename, exclusive ? "rb+" : "rb");
202  if (myFile)
203  {
204  if (exclusive)
205  myLockSucceeded = UT_FileUtil::writeLockFile(fileno(myFile));
206  else
207  myLockSucceeded = UT_FileUtil::readLockFile(fileno(myFile));
208  }
209  else
210  {
211  myFile = fopen(filename, "wb+");
212  if (myFile)
213  myLockSucceeded = UT_FileUtil::writeLockFile(fileno(myFile));
214  else
215  myLockSucceeded = false;
216  }
217  }
218 
220  {
221  if (myFile)
222  {
223  if (myLockSucceeded)
224  UT_FileUtil::unlockFile(fileno(myFile));
225 
226  fclose(myFile);
227  }
228  }
229 
230  /// Get the file pointer for this locked file.
231  FILE *getFile() const
232  {
233  return myFile;
234  }
235 
236  /// Return whether the file is actually locked.
237  bool isLocked() const
238  {
239  return myLockSucceeded;
240  }
241 
242 private:
243  FILE *myFile;
244  bool myLockSucceeded;
245 };
246 
247 #endif
248 
static bool unlockFile(int fd)
static bool lockFile(int fd)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
GLbitfield flags
Definition: glcorearb.h:1595
#define UT_API
Definition: UT_API.h:12
bool isLocked() const
Return whether the file is actually locked.
Definition: UT_FileUtil.h:237
long long int64
Definition: SYS_Types.h:100
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:475
int64 exint
Definition: SYS_Types.h:109
GLenum GLenum severity
Definition: glcorearb.h:2538
GLsizei levels
Definition: glcorearb.h:2223
FILE * getFile() const
Get the file pointer for this locked file.
Definition: UT_FileUtil.h:231
GLenum mode
Definition: glcorearb.h:98
static bool writeLockFile(int fd)
UT_AutoFileLock(const char *filename)
Open and lock a file for exclusive reading and writing.
Definition: UT_FileUtil.h:183
static bool readLockFile(int fd)
UT_AutoFileLock(const char *filename, bool exclusive)
Definition: UT_FileUtil.h:198
virtual ~UT_FileUtil()
Definition: UT_FileUtil.h:43