rvm 1.08
|
00001 #ifndef __fs_h__ 00002 #define __fs_h__ 00003 00004 #include <iostream> 00005 #include <string> 00006 #include <vector> 00007 00008 #ifdef HAVE_SYS_TYPES_H 00009 #include <sys/types.h> 00010 #endif 00011 #ifdef HAVE_SYS_STAT_H 00012 #include <sys/stat.h> 00013 #endif 00014 #ifdef HAVE_UNISTD_H 00015 #include <unistd.h> 00016 #endif 00017 #include <pwd.h> 00018 #include <grp.h> 00019 #ifdef HAVE_SYS_PARAM_H 00020 #include <sys/param.h> 00021 #endif 00022 #ifdef HAVE_SYS_MOUNT_H 00023 #include <sys/mount.h> 00024 #endif 00025 #ifdef HAVE_SIGNAL_H 00026 #include <signal.h> 00027 #endif 00028 00029 /* 00030 * *** NASTY KLUDGE *** 00031 * 00032 * _FILE_OFFSET_BITS == 64 is needed for a 64-bit off_t, but on sun-solaris2.8 00033 * it interferes with struct statvfs64, which needs _LARGE_FILES instead. 00034 * Surely there is a proper way to do this... 00035 * 00036 */ 00037 #ifdef HAVE_SYS_STATVFS_H 00038 # ifdef sun 00039 # ifdef _FILE_OFFSET_BITS 00040 # if _FILE_OFFSET_BITS == 64 00041 # define __HOLD_FILE_OFFSET_BITS_64 00042 # undef _FILE_OFFSET_BITS 00043 # endif 00044 # endif 00045 # endif 00046 #include <sys/statvfs.h> 00047 # ifdef __HOLD_FILE_OFFSET_BITS_64 00048 # define _FILE_OFFSET_BITS 64 00049 # undef __HOLD_FILE_OFFSET_BITS_64 00050 # endif 00051 #endif 00052 00053 #ifdef HAVE_SYS_VFS_H 00054 #include <sys/vfs.h> 00055 #endif 00056 00057 #include "types.h" 00058 00059 #ifdef HAVE_SYS_STATVFS_H 00060 #define STATFS statvfs 00061 #else 00062 #define STATFS statfs 00063 #endif 00064 00065 #include <string.h> 00066 00067 const std::string cwd(void); 00068 const pid_t pid(void); 00069 const pid_t parent_pid(void); 00070 00071 bool absolute_path(const std::string& a_path); 00072 bool relative_path(const std::string& a_path); 00073 std::string reform_path(const std::string& a_path); 00074 std::string permute_path(const std::string& a_path); 00075 std::string path_basename(const std::string& a_path); 00076 std::string path_dirname(const std::string& a_path); 00077 std::string mk_absolute_path( 00078 const std::string a_path, 00079 const std::string a_rel_path 00080 ); 00081 std::string mk_relative_path( 00082 const std::string a_path_to, 00083 const std::string a_path_from 00084 ); 00085 bool exists(const std::string& a_path); 00086 bool readable(const std::string& a_path); 00087 bool writable(const std::string& a_path); 00088 bool executable(const std::string& a_path); 00089 #ifdef S_ISFIFO 00090 bool is_fifo_special(const std::string& a_path); 00091 #endif 00092 #ifdef S_ISCHR 00093 bool is_char_special(const std::string& a_path); 00094 #endif 00095 #ifdef S_ISDIR 00096 bool is_dir(const std::string& a_path); 00097 #endif 00098 #ifdef S_ISREG 00099 bool is_file(const std::string& a_path); 00100 #endif 00101 #ifdef S_ISBLK 00102 bool is_block_special(const std::string& a_path); 00103 #endif 00104 #ifdef S_ISLNK 00105 bool is_link(const std::string& a_path); 00106 #endif 00107 #ifdef S_ISSOCK 00108 bool is_socket(const std::string& a_path); 00109 #endif 00110 #ifdef S_ISDOOR 00111 bool is_door(const std::string& a_path); 00112 #endif 00113 void mk_dir(const std::string& a_path); 00114 void rm_dir(const std::string a_path); 00115 void rm_file(const std::string a_path); 00116 void mk_dirhier(const std::string a_path); 00117 void rename_file(const std::string a_from, const std::string a_to); 00118 void mk_symlink(const std::string a_from, const std::string a_to); 00119 void mk_relative_symlink(const std::string a_from, const std::string a_to); 00120 00121 /** Retrieve information about a file or directory */ 00122 class filestatus 00123 { 00124 public: 00125 enum filetype { 00126 #ifdef S_ISFIFO 00127 type_fifo_special, 00128 #endif 00129 #ifdef S_ISCHR 00130 type_character_special, 00131 #endif 00132 #ifdef S_ISDIR 00133 type_directory, 00134 #endif 00135 #ifdef S_ISREG 00136 type_regular_file, 00137 #endif 00138 #ifdef S_ISBLK 00139 type_block_special, 00140 #endif 00141 #ifdef S_ISLNK 00142 type_link, 00143 #endif 00144 #ifdef S_ISSOCK 00145 type_socket, 00146 #endif 00147 #ifdef S_ISDOOR 00148 type_door, 00149 #endif 00150 type_unknown 00151 }; 00152 typedef mode_t mode_type; 00153 typedef ino_t inode_type; 00154 typedef dev_t device_type; 00155 typedef uint32 major_type; 00156 typedef uint32 minor_type; 00157 typedef nlink_t num_links_type; 00158 typedef uid_t uid_type; 00159 typedef gid_t gid_type; 00160 typedef uint64 size_type; 00161 typedef time_t time_type; 00162 00163 filestatus(); 00164 filestatus(const std::string a_path); 00165 ~filestatus(); 00166 00167 void path(const std::string a_path); 00168 const std::string path(void) const; 00169 const filetype type(void) const; 00170 const std::string link(void) const; 00171 00172 const mode_type mode(void) const; 00173 const inode_type inode(void) const; 00174 const device_type dev(void) const; 00175 const device_type rdev(void) const; 00176 const major_type get_major(void) const; 00177 const minor_type get_minor(void) const; 00178 const num_links_type num_links(void) const; 00179 const uid_type uid(void) const; 00180 const gid_type gid(void) const; 00181 const size_type size(void) const; 00182 const time_type last_access_time(void) const; 00183 const time_type last_modification_time(void) const; 00184 const time_type last_status_change_time(void) const; 00185 const size_type blocksize(void) const; 00186 const size_type blocks(void) const; 00187 const bool uid_is_found(void) const; 00188 const bool gid_is_found(void) const; 00189 const std::string uid_name(void) const; 00190 const std::string gid_name(void) const; 00191 00192 void clear(void); 00193 00194 #ifdef S_ISFIFO 00195 const bool is_fifo_special(void) const; 00196 #endif 00197 #ifdef S_ISCHR 00198 const bool is_character_special(void) const; 00199 #endif 00200 #ifdef S_ISBLK 00201 const bool is_block_special(void) const; 00202 #endif 00203 #ifdef S_ISLNK 00204 const bool is_link(void) const; 00205 #endif 00206 #ifdef S_ISSOCK 00207 const bool is_socket(void) const; 00208 #endif 00209 #ifdef S_ISDOOR 00210 const bool is_door(void) const; 00211 #endif 00212 #ifdef S_ISDIR 00213 const bool is_directory(void) const; 00214 #endif 00215 #ifdef S_ISREG 00216 const bool is_regular_file(void) const; 00217 #endif 00218 00219 #ifdef S_IRUSR 00220 const bool user_can_read(void) const; 00221 #endif 00222 #ifdef S_IWUSR 00223 const bool user_can_write(void) const; 00224 #endif 00225 #ifdef S_IXUSR 00226 const bool user_can_execute(void) const; 00227 #endif 00228 #ifdef S_IRGRP 00229 const bool group_can_read(void) const; 00230 #endif 00231 #ifdef S_IWGRP 00232 const bool group_can_write(void) const; 00233 #endif 00234 #ifdef S_IXGRP 00235 const bool group_can_execute(void) const; 00236 #endif 00237 #ifdef S_IROTH 00238 const bool other_can_read(void) const; 00239 #endif 00240 #ifdef S_IWOTH 00241 const bool other_can_write(void) const; 00242 #endif 00243 #ifdef S_IXOTH 00244 const bool other_can_execute(void) const; 00245 #endif 00246 #ifdef S_ISUID 00247 const bool is_set_uid(void) const; 00248 #endif 00249 #ifdef S_ISGID 00250 const bool is_set_gid(void) const; 00251 #endif 00252 #ifdef S_ISVTX 00253 const bool is_set_sticky(void) const; 00254 #endif 00255 00256 private: 00257 std::string m_path; 00258 struct stat m_stat; 00259 std::string m_link; 00260 major_type m_major; 00261 minor_type m_minor; 00262 bool m_uidfound; 00263 bool m_gidfound; 00264 std::string m_uname; 00265 std::string m_gname; 00266 }; 00267 00268 /** Retrieve a list of files in a subdirectory that match a given wildcard 00269 filename 00270 00271 Pathnames to the files are not included. 00272 */ 00273 class subdirectory : public std::vector<std::string> 00274 { 00275 public: 00276 typedef std::vector<std::string> type; 00277 00278 subdirectory(); 00279 subdirectory(const subdirectory& a_class); 00280 subdirectory(const std::string a_path, const std::string a_filter = "*"); 00281 ~subdirectory(); 00282 00283 void assign(const subdirectory& a_class); 00284 00285 const type& 00286 path(const std::string a_path, const std::string a_filter = "*"); 00287 00288 subdirectory& operator=(const subdirectory& a_class); 00289 00290 private: 00291 }; 00292 00293 void rm_recursive(const std::string a_path); 00294 00295 /** Retrieve a list of pathnames that match a given wildcard path 00296 00297 Pathnames to the files are included. Depending on the wildcard path given, 00298 files listed may be in multiple directories. 00299 */ 00300 class directory : public std::vector<std::string> 00301 { 00302 public: 00303 typedef std::vector<std::string> type; 00304 00305 directory(); 00306 directory(const directory& a_class); 00307 directory(const std::string& a_str); 00308 ~directory(); 00309 00310 const type& path(const std::string& a_path); 00311 00312 private: 00313 }; 00314 00315 /** Retrieve information about a filesystem */ 00316 class filesystem { 00317 public: 00318 typedef uint64 size_type; 00319 00320 filesystem(); 00321 filesystem(const std::string& a_path); 00322 00323 void clear(void); 00324 00325 void path(const std::string& a_path); 00326 const std::string path(void) const; 00327 const size_type blocksize(void) const; 00328 const size_type total_blocks(void) const; 00329 const size_type free_blocks(void) const; 00330 const size_type used_blocks(void) const; 00331 const size_type total_inodes(void) const; 00332 const size_type free_inodes(void) const; 00333 const size_type used_inodes(void) const; 00334 00335 filesystem& operator=(const filesystem& a_class); 00336 00337 private: 00338 std::string m_path; 00339 struct STATFS m_statfs; 00340 }; 00341 00342 /** A simple locking mechanism */ 00343 class simple_lock { 00344 public: 00345 typedef pid_t pid_type; 00346 00347 simple_lock(); 00348 simple_lock(const std::string& a_lockfile); 00349 ~simple_lock(); 00350 00351 void clear(void); 00352 00353 void lockfile(const std::string& a_lockfile); 00354 const std::string lockfile(void) const; 00355 00356 const pid_type locked_by(void) const; 00357 const bool is_locked(void) const; 00358 bool lock(void); 00359 void unlock(void); 00360 private: 00361 std::string m_lockfile; 00362 }; 00363 00364 #endif