rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fs.h
Go to the documentation of this file.
1 #ifndef __fs_h__
2 #define __fs_h__
3 
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 
8 #ifdef HAVE_SYS_TYPES_H
9 #include <sys/types.h>
10 #endif
11 #ifdef HAVE_SYS_STAT_H
12 #include <sys/stat.h>
13 #endif
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <pwd.h>
18 #include <grp.h>
19 #ifdef HAVE_SYS_PARAM_H
20 #include <sys/param.h>
21 #endif
22 #ifdef HAVE_SYS_MOUNT_H
23 #include <sys/mount.h>
24 #endif
25 #ifdef HAVE_SIGNAL_H
26 #include <signal.h>
27 #endif
28 
29 /*
30  * *** NASTY KLUDGE ***
31  *
32  * _FILE_OFFSET_BITS == 64 is needed for a 64-bit off_t, but on sun-solaris2.8
33  * it interferes with struct statvfs64, which needs _LARGE_FILES instead.
34  * Surely there is a proper way to do this...
35  *
36  */
37 #ifdef HAVE_SYS_STATVFS_H
38 # ifdef sun
39 # ifdef _FILE_OFFSET_BITS
40 # if _FILE_OFFSET_BITS == 64
41 # define __HOLD_FILE_OFFSET_BITS_64
42 # undef _FILE_OFFSET_BITS
43 # endif
44 # endif
45 # endif
46 #include <sys/statvfs.h>
47 # ifdef __HOLD_FILE_OFFSET_BITS_64
48 # define _FILE_OFFSET_BITS 64
49 # undef __HOLD_FILE_OFFSET_BITS_64
50 # endif
51 #endif
52 
53 #ifdef HAVE_SYS_VFS_H
54 #include <sys/vfs.h>
55 #endif
56 
57 #include "types.h"
58 
59 #ifdef HAVE_SYS_STATVFS_H
60 #define STATFS statvfs
61 #else
62 #define STATFS statfs
63 #endif
64 
65 #include <string.h>
66 
67 const std::string cwd(void);
68 const pid_t pid(void);
69 const pid_t parent_pid(void);
70 
71 bool absolute_path(const std::string& a_path);
72 bool relative_path(const std::string& a_path);
73 std::string reform_path(const std::string& a_path);
74 std::string permute_path(const std::string& a_path);
75 std::string path_basename(const std::string& a_path);
76 std::string path_dirname(const std::string& a_path);
77 std::string mk_absolute_path(
78  const std::string a_path,
79  const std::string a_rel_path
80  );
81 std::string mk_relative_path(
82  const std::string a_path_to,
83  const std::string a_path_from
84  );
85 bool exists(const std::string& a_path);
86 bool readable(const std::string& a_path);
87 bool writable(const std::string& a_path);
88 bool executable(const std::string& a_path);
89 #ifdef S_ISFIFO
90 bool is_fifo_special(const std::string& a_path);
91 #endif
92 #ifdef S_ISCHR
93 bool is_char_special(const std::string& a_path);
94 #endif
95 #ifdef S_ISDIR
96 bool is_dir(const std::string& a_path);
97 #endif
98 #ifdef S_ISREG
99 bool is_file(const std::string& a_path);
100 #endif
101 #ifdef S_ISBLK
102 bool is_block_special(const std::string& a_path);
103 #endif
104 #ifdef S_ISLNK
105 bool is_link(const std::string& a_path);
106 #endif
107 #ifdef S_ISSOCK
108 bool is_socket(const std::string& a_path);
109 #endif
110 #ifdef S_ISDOOR
111 bool is_door(const std::string& a_path);
112 #endif
113 void mk_dir(const std::string& a_path);
114 void rm_dir(const std::string a_path);
115 void rm_file(const std::string a_path);
116 void mk_dirhier(const std::string a_path);
117 void rename_file(const std::string a_from, const std::string a_to);
118 void mk_symlink(const std::string a_from, const std::string a_to);
119 void mk_relative_symlink(const std::string a_from, const std::string a_to);
120 
121 /** Retrieve information about a file or directory */
123 {
124 public:
125  enum filetype {
126 #ifdef S_ISFIFO
127  type_fifo_special,
128 #endif
129 #ifdef S_ISCHR
130  type_character_special,
131 #endif
132 #ifdef S_ISDIR
133  type_directory,
134 #endif
135 #ifdef S_ISREG
136  type_regular_file,
137 #endif
138 #ifdef S_ISBLK
139  type_block_special,
140 #endif
141 #ifdef S_ISLNK
142  type_link,
143 #endif
144 #ifdef S_ISSOCK
145  type_socket,
146 #endif
147 #ifdef S_ISDOOR
148  type_door,
149 #endif
151  };
152  typedef mode_t mode_type;
153  typedef ino_t inode_type;
154  typedef dev_t device_type;
155  typedef uint32 major_type;
156  typedef uint32 minor_type;
157  typedef nlink_t num_links_type;
158  typedef uid_t uid_type;
159  typedef gid_t gid_type;
160  typedef uint64 size_type;
161  typedef time_t time_type;
162 
163  filestatus();
164  filestatus(const std::string a_path);
165  ~filestatus();
166 
167  void path(const std::string a_path);
168  const std::string path(void) const;
169  const filetype type(void) const;
170  const std::string link(void) const;
171 
172  const mode_type mode(void) const;
173  const inode_type inode(void) const;
174  const device_type dev(void) const;
175  const device_type rdev(void) const;
176  const major_type get_major(void) const;
177  const minor_type get_minor(void) const;
178  const num_links_type num_links(void) const;
179  const uid_type uid(void) const;
180  const gid_type gid(void) const;
181  const size_type size(void) const;
182  const time_type last_access_time(void) const;
183  const time_type last_modification_time(void) const;
184  const time_type last_status_change_time(void) const;
185  const size_type blocksize(void) const;
186  const size_type blocks(void) const;
187  const bool uid_is_found(void) const;
188  const bool gid_is_found(void) const;
189  const std::string uid_name(void) const;
190  const std::string gid_name(void) const;
191 
192  void clear(void);
193 
194 #ifdef S_ISFIFO
195  const bool is_fifo_special(void) const;
196 #endif
197 #ifdef S_ISCHR
198  const bool is_character_special(void) const;
199 #endif
200 #ifdef S_ISBLK
201  const bool is_block_special(void) const;
202 #endif
203 #ifdef S_ISLNK
204  const bool is_link(void) const;
205 #endif
206 #ifdef S_ISSOCK
207  const bool is_socket(void) const;
208 #endif
209 #ifdef S_ISDOOR
210  const bool is_door(void) const;
211 #endif
212 #ifdef S_ISDIR
213  const bool is_directory(void) const;
214 #endif
215 #ifdef S_ISREG
216  const bool is_regular_file(void) const;
217 #endif
218 
219 #ifdef S_IRUSR
220  const bool user_can_read(void) const;
221 #endif
222 #ifdef S_IWUSR
223  const bool user_can_write(void) const;
224 #endif
225 #ifdef S_IXUSR
226  const bool user_can_execute(void) const;
227 #endif
228 #ifdef S_IRGRP
229  const bool group_can_read(void) const;
230 #endif
231 #ifdef S_IWGRP
232  const bool group_can_write(void) const;
233 #endif
234 #ifdef S_IXGRP
235  const bool group_can_execute(void) const;
236 #endif
237 #ifdef S_IROTH
238  const bool other_can_read(void) const;
239 #endif
240 #ifdef S_IWOTH
241  const bool other_can_write(void) const;
242 #endif
243 #ifdef S_IXOTH
244  const bool other_can_execute(void) const;
245 #endif
246 #ifdef S_ISUID
247  const bool is_set_uid(void) const;
248 #endif
249 #ifdef S_ISGID
250  const bool is_set_gid(void) const;
251 #endif
252 #ifdef S_ISVTX
253  const bool is_set_sticky(void) const;
254 #endif
255 
256 private:
257  std::string m_path;
258  struct stat m_stat;
259  std::string m_link;
264  std::string m_uname;
265  std::string m_gname;
266 };
267 
268 /** Retrieve a list of files in a subdirectory that match a given wildcard
269  filename
270 
271  Pathnames to the files are not included.
272  */
273 class subdirectory : public std::vector<std::string>
274 {
275 public:
276  typedef std::vector<std::string> type;
277 
278  subdirectory();
279  subdirectory(const subdirectory& a_class);
280  subdirectory(const std::string a_path, const std::string a_filter = "*");
281  ~subdirectory();
282 
283  void assign(const subdirectory& a_class);
284 
285  const type&
286  path(const std::string a_path, const std::string a_filter = "*");
287 
288  subdirectory& operator=(const subdirectory& a_class);
289 
290 private:
291 };
292 
293 void rm_recursive(const std::string a_path);
294 
295 /** Retrieve a list of pathnames that match a given wildcard path
296 
297  Pathnames to the files are included. Depending on the wildcard path given,
298  files listed may be in multiple directories.
299  */
300 class directory : public std::vector<std::string>
301 {
302 public:
303  typedef std::vector<std::string> type;
304 
305  directory();
306  directory(const directory& a_class);
307  directory(const std::string& a_str);
308  ~directory();
309 
310  const type& path(const std::string& a_path);
311 
312 private:
313 };
314 
315 /** Retrieve information about a filesystem */
316 class filesystem {
317 public:
318  typedef uint64 size_type;
319 
320  filesystem();
321  filesystem(const std::string& a_path);
322 
323  void clear(void);
324 
325  void path(const std::string& a_path);
326  const std::string path(void) const;
327  const size_type blocksize(void) const;
328  const size_type total_blocks(void) const;
329  const size_type free_blocks(void) const;
330  const size_type used_blocks(void) const;
331  const size_type total_inodes(void) const;
332  const size_type free_inodes(void) const;
333  const size_type used_inodes(void) const;
334 
335  filesystem& operator=(const filesystem& a_class);
336 
337 private:
338  std::string m_path;
339  struct STATFS m_statfs;
340 };
341 
342 /** A simple locking mechanism */
343 class simple_lock {
344 public:
345  typedef pid_t pid_type;
346 
347  simple_lock();
348  simple_lock(const std::string& a_lockfile);
349  ~simple_lock();
350 
351  void clear(void);
352 
353  void lockfile(const std::string& a_lockfile);
354  const std::string lockfile(void) const;
355 
356  const pid_type locked_by(void) const;
357  const bool is_locked(void) const;
358  bool lock(void);
359  void unlock(void);
360 private:
361  std::string m_lockfile;
362 };
363 
364 #endif
filesystem()
C'tor.
Definition: fs.cc:1580
Retrieve information about a filesystem.
Definition: fs.h:316
const size_type total_inodes(void) const
Return the filesystem's total number of inodes, if supported by the filesystem, otherwise the result ...
Definition: fs.cc:1662
bool readable(const std::string &a_path)
Return true if the file or directory exists and is readable.
Definition: fs.cc:417
uint64 size_type
Definition: fs.h:160
const type & path(const std::string &a_path)
Retrieve a list of paths that match the wildcard path given.
Definition: fs.cc:1510
Basic types definitions and templates.
~directory()
D'tor.
Definition: fs.cc:1505
bool absolute_path(const std::string &a_path)
Return true if the string looks like an absolute path.
Definition: fs.cc:186
std::string path_basename(const std::string &a_path)
Return everything after the last slash from a path.
Definition: fs.cc:247
std::string path_dirname(const std::string &a_path)
Return everything up to the last slash from a path.
Definition: fs.cc:264
void rm_recursive(const std::string a_path)
Recursively delete the contents of a directory.
Definition: fs.cc:1414
dev_t device_type
Definition: fs.h:154
void mk_dir(const std::string &a_path)
Create a directory.
Definition: fs.cc:599
const size_type blocks(void) const
Return the number of blocks used to store this file.
Definition: fs.cc:1038
filetype
Definition: fs.h:125
uint32 minor_type
Definition: fs.h:156
std::string mk_relative_path(const std::string a_path_to, const std::string a_path_from)
Make the path a_path_to relative from a_path_from, where a_path_to and a_path_from are directory name...
Definition: fs.cc:314
const bool gid_is_found(void) const
If the file's owner's GID is found in the passwd file, return true.
Definition: fs.cc:1054
std::string reform_path(const std::string &a_path)
Reformat a path to remove double slashes.
Definition: fs.cc:205
bool lock(void)
Lock.
Definition: fs.cc:1778
const bool uid_is_found(void) const
If the file's owner's UID is found in the passwd file, return true.
Definition: fs.cc:1048
const size_type blocksize(void) const
Return the filesystem block size.
Definition: fs.cc:1620
const inode_type inode(void) const
Return the file inode.
Definition: fs.cc:952
const std::string link(void) const
If the pathname is a link, return the path it is linked to.
Definition: fs.cc:940
struct STATFS m_statfs
Definition: fs.h:339
bool relative_path(const std::string &a_path)
Return true if the string looks like a relative path.
Definition: fs.cc:195
bool exists(const std::string &a_path)
Return true if the file or directory exists.
Definition: fs.cc:385
uint64 size_type
Definition: fs.h:318
std::string m_uname
Definition: fs.h:264
bool executable(const std::string &a_path)
Return true if the file or directory exists and is executable.
Definition: fs.cc:437
const size_type used_blocks(void) const
Return the filesystem's number of used blocks.
Definition: fs.cc:1650
const pid_t pid(void)
Return the PID of this process.
Definition: fs.cc:162
std::string m_link
Definition: fs.h:259
Retrieve a list of pathnames that match a given wildcard path.
Definition: fs.h:300
~simple_lock()
D'tor.
Definition: fs.cc:1718
void mk_relative_symlink(const std::string a_from, const std::string a_to)
Given a from and to path, create a relative symbolic link.
Definition: fs.cc:770
const time_type last_status_change_time(void) const
Return the last status change time of this file.
Definition: fs.cc:1022
filestatus()
C'tor.
Definition: fs.cc:808
std::string m_path
Definition: fs.h:338
uint32 major_type
Definition: fs.h:155
const size_type blocksize(void) const
Return the blocksize used to store this file.
Definition: fs.cc:1028
directory()
C'tor.
Definition: fs.cc:1488
struct stat m_stat
Definition: fs.h:258
bool writable(const std::string &a_path)
Return true if the file or directory exists and is writable.
Definition: fs.cc:427
const std::string lockfile(void) const
Get the lockfile path.
Definition: fs.cc:1740
const mode_type mode(void) const
Return the file mode.
Definition: fs.cc:946
filesystem & operator=(const filesystem &a_class)
Copy values from another instance.
Definition: fs.cc:1694
std::vector< std::string > type
Definition: fs.h:303
void rm_dir(const std::string a_path)
Remove a directory.
Definition: fs.cc:612
mode_t mode_type
Definition: fs.h:152
void unlock(void)
Unlock.
Definition: fs.cc:1798
A simple locking mechanism.
Definition: fs.h:343
nlink_t num_links_type
Definition: fs.h:157
std::string m_lockfile
Definition: fs.h:361
const type & path(const std::string a_path, const std::string a_filter="*")
Return a vector of strings of a list of files in a subdirectory.
Definition: fs.cc:1361
minor_type m_minor
Definition: fs.h:261
const pid_type locked_by(void) const
Get the PID of the locking process.
Definition: fs.cc:1746
time_t time_type
Definition: fs.h:161
const filetype type(void) const
Return the type of file.
Definition: fs.cc:894
const size_type free_inodes(void) const
Return the filesystem's total number of free inodes, if supported by the filesystem, otherwise the result is system-dependent, but usually 0.
Definition: fs.cc:1674
gid_t gid_type
Definition: fs.h:159
const std::string uid_name(void) const
Return the file's owner's user name (from UID)
Definition: fs.cc:1060
const uid_type uid(void) const
Return the file's owner's UID.
Definition: fs.cc:988
const time_type last_modification_time(void) const
Return the last modification time of this file.
Definition: fs.cc:1016
std::string permute_path(const std::string &a_path)
Reformat a path to remove the begining and trailing slashes, and replace all other slashes with under...
Definition: fs.cc:224
const minor_type get_minor(void) const
If the pathname is a special file, return it's minor number.
Definition: fs.cc:976
void rename_file(const std::string a_from, const std::string a_to)
Rename a file or directory.
Definition: fs.cc:709
const std::string cwd(void)
Return the current working directory.
Definition: fs.cc:148
std::string m_gname
Definition: fs.h:265
const device_type dev(void) const
Return the file's device.
Definition: fs.cc:958
std::string mk_absolute_path(const std::string a_path, const std::string a_rel_path)
Make the path a_rel_path absolute with respect to a_path, where a_rel_path and a_path are directory n...
Definition: fs.cc:282
Retrieve information about a file or directory.
Definition: fs.h:122
uid_t uid_type
Definition: fs.h:158
major_type m_major
Definition: fs.h:260
void clear(void)
Clear the simple_lock object.
Definition: fs.cc:1724
const std::string path(void) const
Return the pathname that this filestatus object has information about.
Definition: fs.cc:888
const size_type used_inodes(void) const
Return the filesystem's number of used inodes.
Definition: fs.cc:1684
pid_t pid_type
Definition: fs.h:345
void mk_dirhier(const std::string a_path)
Recursively create a directory heirarchy.
Definition: fs.cc:683
void clear(void)
Clear all values.
Definition: fs.cc:1312
const size_type free_blocks(void) const
Return the filesystem's number of free blocks.
Definition: fs.cc:1640
const std::string gid_name(void) const
Return the file's owner's group name (from UID)
Definition: fs.cc:1066
~subdirectory()
D'tor.
Definition: fs.cc:1345
std::string m_path
Definition: fs.h:257
const size_type size(void) const
Return the file size in bytes.
Definition: fs.cc:1000
void assign(const subdirectory &a_class)
Assign the contents of a given subdirectory to this subdirectory.
Definition: fs.cc:1350
const gid_type gid(void) const
Return the file's owner's GID.
Definition: fs.cc:994
simple_lock()
C'tor.
Definition: fs.cc:1705
std::vector< std::string > type
Definition: fs.h:276
bool m_uidfound
Definition: fs.h:262
#define STATFS
Definition: fs.h:62
const num_links_type num_links(void) const
Return the number of links to this file.
Definition: fs.cc:982
const time_type last_access_time(void) const
Return the last access time of this file.
Definition: fs.cc:1010
Retrieve a list of files in a subdirectory that match a given wildcard filename.
Definition: fs.h:273
ino_t inode_type
Definition: fs.h:153
void clear(void)
Clear the filesystem object.
Definition: fs.cc:1593
void mk_symlink(const std::string a_from, const std::string a_to)
Create a symbolic link.
Definition: fs.cc:754
const device_type rdev(void) const
Return the file's raw device.
Definition: fs.cc:964
bool m_gidfound
Definition: fs.h:263
const std::string path(void) const
Return the path from which this filesystem information was obtained.
Definition: fs.cc:1614
const major_type get_major(void) const
If the pathname is a special file, return it's major number.
Definition: fs.cc:970
const size_type total_blocks(void) const
Return the filesystem's total number of blocks.
Definition: fs.cc:1630
const pid_t parent_pid(void)
Return the PID of the parent process.
Definition: fs.cc:174
const bool is_locked(void) const
Find out whether or not the lock is in place.
Definition: fs.cc:1765
~filestatus()
D'tor.
Definition: fs.cc:819
subdirectory & operator=(const subdirectory &a_class)
Definition: fs.cc:1404
void rm_file(const std::string a_path)
Remove a file.
Definition: fs.cc:637
subdirectory()
C'tor.
Definition: fs.cc:1327