fs.h

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

Generated on Wed Jun 21 10:50:03 2006 for rvm by  doxygen 1.4.2