00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _FILE_UTIL_H
00034 #define _FILE_UTIL_H
00035
00036 #include <sys/types.h>
00037 #include <sys/stat.h>
00038 #include <fcntl.h>
00039 #include <string>
00040
00046 #ifndef S_IRGRP
00047
00048 #define S_IRGRP S_IRUSR
00049 #define S_IWGRP S_IWUSR
00050 #define S_IXGRP S_IXUSR
00051 #define S_IROTH S_IRUSR
00052 #define S_IWOTH S_IWUSR
00053 #define S_IXOTH S_IXUSR
00054 #endif
00055
00056 #include "Collections.h"
00057
00058 typedef int pipefdpair_t[2];
00059
00072 class FileUtil {
00073 public:
00074 class AccessResult {
00075 public:
00076 AccessResult() : access(false), exists(false) { }
00077 AccessResult(bool access, bool exists, const std::string& error) :
00078 access(access), exists(exists), error(error) { }
00079 bool access;
00080 bool exists;
00081 std::string error;
00082 };
00083
00084 public:
00085 static int open(const std::string& path, int flags, mode_t mode) throw(FileException);
00086 static int open(const std::string& path, int flags = O_RDONLY) throw(FileException);
00087 static int create(const std::string& path, mode_t mode) throw(FileException) {
00088 return open(path, O_WRONLY|O_TRUNC|O_CREAT, mode);
00089 }
00090 static void ftruncate(int fd, off_t length) throw(FileException);
00091 static void mkdir(const std::string& path, mode_t mode, bool createParents = false)
00092 throw(FileException);
00093 static void chdir(const std::string& path) throw(FileException);
00094 static int mktemp(std::string& name) throw(FileException);
00095 static void rename(const std::string& oldpath, const std::string& newpath) throw(FileException);
00096 static void symlink(const std::string& oldpath, const std::string& newpath) throw(FileException);
00097 static void unlink(const std::string& path) throw(FileException);
00098 static void rmdir(const std::string& path) throw(FileException);
00099 static void chmod(const std::string& path, mode_t mode) throw(FileException);
00100 static void fchmod(int fd, mode_t mode) throw(FileException);
00101 static void utime(const std::string& path, time_t mtime)
00102 throw(FileException);
00103 static void utime(const std::string& path, time_t mtime, time_t atime)
00104 throw(FileException);
00105 static int close(int fd);
00106 static bool fileExists(const std::string& path, bool doLstat = false);
00107 static bool fileIsReg(const std::string& path, bool doLstat = false);
00108 static bool fileIsLink(const std::string& path);
00109 static bool fileIsDir(const std::string& path, bool doLstat = false);
00110
00111 static AccessResult canAccess(const std::string& path, int mode)
00112 throw (FileException);
00113 static void fstatWithErr(int fd, struct stat& buf) throw (FileException);
00114 static void statWithErr(const mace::string& path, struct stat& buf)
00115 throw (FileException);
00116 static void lstatWithErr(const mace::string& path, struct stat& buf)
00117 throw (FileException);
00118 static void fstat(int fd, struct stat& buf) throw (FileException) {
00119 fstatWithErr(fd, buf);
00120 }
00121 static void stat(const mace::string& path, struct stat& buf)
00122 throw (FileException) {
00123 statWithErr(path, buf);
00124 }
00125 static void lstat(const mace::string& path, struct stat& buf)
00126 throw (FileException) {
00127 lstatWithErr(path, buf);
00128 }
00129
00130
00131 static off_t seek(int fd, off_t offset, int whence = SEEK_SET)
00132 throw (FileException);
00133 static void write(int fd, const char* buf, size_t size, bool log = false)
00134 throw (WriteException) {
00135 write(fd, std::string(buf, size), log);
00136 }
00137 static void write(int fd, const std::string& buf, bool log = false)
00138 throw (WriteException);
00139 static std::string read(int s) throw (ReadException);
00140 static int read(int fd, char* buf, size_t count);
00141 static size_t read(int s, mace::string& sr) throw (ReadException);
00142 static std::string bufferedRead(int s, size_t len) throw (ReadException);
00143 static size_t bufferedRead(int s, mace::string& sr, size_t len, bool clear = true) throw (ReadException);
00144 static void saveFile(const std::string& path, const std::string& data,
00145 const std::string& backup = "", bool removeBackup = false)
00146 throw (FileException);
00147 static std::string loadFile(const std::string& path) throw (FileException);
00148 static void remapPipeStdinStdout(pipefdpair_t p2c, pipefdpair_t c2p)
00149 throw (IOException);
00150 static void remapPipeStdinStdout(int rpipe, int wpipe) throw (IOException);
00151 static void dup2close(int oldfd, int newfd) throw (IOException);
00152 static void readDir(const std::string& path, StringList& dirlist, bool sort = false)
00153 throw (FileException);
00154 static std::string readlink(const std::string& path) throw (FileException);
00155 static std::string basename(const std::string& buf);
00156 static std::string dirname(const std::string& buf);
00157 static std::string realpath(const std::string& path) throw (FileException);
00158
00159
00160 public:
00161 static const int STDIN = 0;
00162 static const int STDOUT = 1;
00163 static const int STDERR = 2;
00164 static const int READ_INDEX = 0;
00165 static const int WRITE_INDEX = 1;
00166
00167 private:
00168 static void throwFileException(const std::string& prefix, int err, const std::string& path) throw (FileException);
00169 static std::string getErrorString(int err, const std::string& n, int fd);
00170 static std::string getErrorMessage(int err, const std::string& syscall,
00171 const std::string& path);
00172
00173 static void fullWrite(int fd, const char* ptr, size_t len, bool log = false)
00174 throw (WriteException);
00175
00176
00177
00178 };
00179
00182 #endif // _FILE_UTIL_H