Mercurial > hg > octave-lyh
annotate liboctave/file-stat.h @ 8689:ddbe87599331
base_file_stat::is_XXX: return false if object is not initialized
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 05 Feb 2009 17:38:24 -0500 |
parents | 424ba638d8f1 |
children | eb63fbe60fab |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2005, 2006, 2007 |
4 John W. Eaton | |
2926 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2926 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2926 | 21 |
22 */ | |
23 | |
24 #if !defined (octave_file_stat_h) | |
25 #define octave_file_stat_h 1 | |
26 | |
27 #include <string> | |
28 | |
3260 | 29 #include "oct-time.h" |
30 | |
2926 | 31 #ifdef HAVE_SYS_TYPES_H |
32 #include <sys/types.h> | |
33 #endif | |
34 | |
35 class | |
6108 | 36 OCTAVE_API |
8549 | 37 base_file_stat |
2926 | 38 { |
39 public: | |
40 | |
8549 | 41 base_file_stat (void) |
42 : initialized (false), fail (false), errmsg (), fs_mode (), | |
43 fs_ino (), fs_dev (), fs_nlink (), fs_uid (), fs_gid (), | |
44 fs_size (), fs_atime (), fs_mtime (), fs_ctime (), fs_rdev (), | |
45 fs_blksize (), fs_blocks () { } | |
46 | |
47 base_file_stat (const base_file_stat& fs) | |
48 : initialized (fs.initialized), fail (fs.fail), errmsg (fs.errmsg), | |
49 fs_mode (fs.fs_mode), fs_ino (fs.fs_ino), fs_dev (fs.fs_dev), | |
50 fs_nlink (fs.fs_nlink), fs_uid (fs.fs_uid), fs_gid (fs.fs_gid), | |
51 fs_size (fs.fs_size), fs_atime (fs.fs_atime), fs_mtime (fs.fs_mtime), | |
52 fs_ctime (fs.fs_ctime), fs_rdev (fs.fs_rdev), | |
53 fs_blksize (fs.fs_blksize), fs_blocks (fs.fs_blocks) { } | |
54 | |
55 base_file_stat& operator = (const base_file_stat& fs) | |
56 { | |
57 if (this != &fs) | |
2926 | 58 { |
8549 | 59 initialized = fs.initialized; |
60 fail = fs.fail; | |
61 errmsg = fs.errmsg; | |
62 fs_mode = fs.fs_mode; | |
63 fs_ino = fs.fs_ino; | |
64 fs_dev = fs.fs_dev; | |
65 fs_nlink = fs.fs_nlink; | |
66 fs_uid = fs.fs_uid; | |
67 fs_gid = fs.fs_gid; | |
68 fs_size = fs.fs_size; | |
69 fs_atime = fs.fs_atime; | |
70 fs_mtime = fs.fs_mtime; | |
71 fs_ctime = fs.fs_ctime; | |
72 fs_rdev = fs.fs_rdev; | |
73 fs_blksize = fs.fs_blksize; | |
74 fs_blocks = fs.fs_blocks; | |
2926 | 75 } |
76 | |
8549 | 77 return *this; |
78 } | |
2926 | 79 |
8549 | 80 ~base_file_stat (void) { } |
2926 | 81 |
8689
ddbe87599331
base_file_stat::is_XXX: return false if object is not initialized
John W. Eaton <jwe@octave.org>
parents:
8549
diff
changeset
|
82 // File status and info. The is_XXX functions will return false for |
ddbe87599331
base_file_stat::is_XXX: return false if object is not initialized
John W. Eaton <jwe@octave.org>
parents:
8549
diff
changeset
|
83 // file_stat objects that are not properly initialized. The others |
ddbe87599331
base_file_stat::is_XXX: return false if object is not initialized
John W. Eaton <jwe@octave.org>
parents:
8549
diff
changeset
|
84 // should all return 0 (or the equivalent, for the given object) |
ddbe87599331
base_file_stat::is_XXX: return false if object is not initialized
John W. Eaton <jwe@octave.org>
parents:
8549
diff
changeset
|
85 // which is likely not meaningful. |
2926 | 86 |
87 bool is_blk (void) const; | |
88 bool is_chr (void) const; | |
89 bool is_dir (void) const; | |
90 bool is_fifo (void) const; | |
91 bool is_lnk (void) const; | |
92 bool is_reg (void) const; | |
93 bool is_sock (void) const; | |
94 | |
5476 | 95 static bool is_blk (mode_t mode); |
96 static bool is_chr (mode_t mode); | |
97 static bool is_dir (mode_t mode); | |
98 static bool is_fifo (mode_t mode); | |
99 static bool is_lnk (mode_t mode); | |
100 static bool is_reg (mode_t mode); | |
101 static bool is_sock (mode_t mode); | |
102 | |
2926 | 103 ino_t ino (void) const { return fs_ino; } |
104 dev_t dev (void) const { return fs_dev; } | |
105 | |
106 nlink_t nlink (void) const { return fs_nlink; } | |
107 | |
108 uid_t uid (void) const { return fs_uid; } | |
109 gid_t gid (void) const { return fs_gid; } | |
110 | |
111 off_t size (void) const { return fs_size; } | |
112 | |
3260 | 113 octave_time atime (void) const { return fs_atime; } |
114 octave_time mtime (void) const { return fs_mtime; } | |
115 octave_time ctime (void) const { return fs_ctime; } | |
2926 | 116 |
117 dev_t rdev (void) const { return fs_rdev; } | |
118 | |
119 long blksize (void) const { return fs_blksize; } | |
120 long blocks (void) const { return fs_blocks; } | |
121 | |
5476 | 122 mode_t mode (void) const { return fs_mode; } |
123 | |
3504 | 124 std::string mode_as_string (void) const; |
2926 | 125 |
126 bool ok (void) const { return initialized && ! fail; } | |
127 | |
3145 | 128 operator bool () const { return ok (); } |
2926 | 129 |
130 bool exists (void) const { return ok (); } | |
131 | |
3504 | 132 std::string error (void) const { return ok () ? std::string () : errmsg; } |
2926 | 133 |
134 // Has the file referenced by this object been modified since TIME? | |
3260 | 135 bool is_newer (const octave_time& time) const { return fs_mtime > time; } |
2926 | 136 |
137 // It's nice to be able to hide the file_stat object if we don't | |
138 // really care about it. | |
3504 | 139 static int is_newer (const std::string&, const octave_time&); |
2926 | 140 |
8549 | 141 protected: |
2926 | 142 |
143 // TRUE means we have already called stat. | |
144 bool initialized; | |
145 | |
146 // TRUE means the stat for this file failed. | |
147 bool fail; | |
148 | |
149 // If a failure occurs, this contains the system error text. | |
3504 | 150 std::string errmsg; |
2926 | 151 |
152 // file type and permissions | |
153 mode_t fs_mode; | |
154 | |
155 // serial number | |
156 ino_t fs_ino; | |
157 | |
158 // device number | |
159 dev_t fs_dev; | |
160 | |
161 // number of links | |
162 nlink_t fs_nlink; | |
163 | |
164 // user ID of owner | |
165 uid_t fs_uid; | |
166 | |
167 // group ID of owner | |
168 gid_t fs_gid; | |
169 | |
170 // size in bytes, for regular files | |
171 off_t fs_size; | |
172 | |
173 // time of last access | |
3260 | 174 octave_time fs_atime; |
2926 | 175 |
176 // time of last modification | |
3260 | 177 octave_time fs_mtime; |
2926 | 178 |
179 // time of last file status change | |
3260 | 180 octave_time fs_ctime; |
2926 | 181 |
182 // device number for special files | |
183 dev_t fs_rdev; | |
184 | |
185 // best I/O block size | |
186 long fs_blksize; | |
187 | |
188 // number of 512-byte blocks allocated | |
189 long fs_blocks; | |
8549 | 190 }; |
191 | |
192 class | |
193 OCTAVE_API | |
194 file_stat : public base_file_stat | |
195 { | |
196 public: | |
197 | |
198 file_stat (const std::string& n = std::string (), bool fl = true) | |
199 : base_file_stat (), file_name (n), follow_links (fl) | |
200 { | |
201 if (! file_name.empty ()) | |
202 update_internal (); | |
203 } | |
204 | |
205 file_stat (const file_stat& fs) | |
206 : base_file_stat (fs), file_name (fs.file_name), | |
207 follow_links (fs.follow_links) { } | |
208 | |
209 file_stat& operator = (const file_stat& fs) | |
210 { | |
211 if (this != &fs) | |
212 { | |
213 base_file_stat::operator = (fs); | |
214 | |
215 file_name = fs.file_name; | |
216 follow_links = fs.follow_links; | |
217 } | |
218 | |
219 return *this; | |
220 } | |
221 | |
222 ~file_stat (void) { } | |
223 | |
224 void get_stats (bool force = false) | |
225 { | |
226 if (! initialized || force) | |
227 update_internal (force); | |
228 } | |
229 | |
230 void get_stats (const std::string& n, bool force = false) | |
231 { | |
232 if (n != file_name || ! initialized || force) | |
233 { | |
234 initialized = false; | |
235 | |
236 file_name = n; | |
237 | |
238 update_internal (force); | |
239 } | |
240 } | |
241 | |
242 private: | |
243 | |
244 // Name of the file. | |
245 std::string file_name; | |
246 | |
247 // TRUE means follow symbolic links to the ultimate file (stat). | |
248 // FALSE means get information about the link itself (lstat). | |
249 bool follow_links; | |
2926 | 250 |
251 void update_internal (bool force = false); | |
8549 | 252 }; |
2926 | 253 |
8549 | 254 class |
255 OCTAVE_API | |
256 file_fstat : public base_file_stat | |
257 { | |
258 public: | |
259 | |
260 file_fstat (int n) : base_file_stat (), fid (n) | |
261 { | |
262 update_internal (); | |
263 } | |
264 | |
265 file_fstat (const file_fstat& fs) | |
266 : base_file_stat (fs), fid (fs.fid) { } | |
267 | |
268 file_fstat& operator = (const file_fstat& fs) | |
269 { | |
270 if (this != &fs) | |
271 { | |
272 base_file_stat::operator = (fs); | |
273 | |
274 fid = fs.fid; | |
275 } | |
276 | |
277 return *this; | |
278 } | |
279 | |
280 ~file_fstat (void) { } | |
281 | |
282 void get_stats (bool force = false) | |
283 { | |
284 if (! initialized || force) | |
285 update_internal (force); | |
286 } | |
287 | |
288 void get_stats (int n, bool force = false) | |
289 { | |
290 if (n != fid || ! initialized || force) | |
291 { | |
292 initialized = false; | |
293 | |
294 fid = n; | |
295 | |
296 update_internal (force); | |
297 } | |
298 } | |
299 | |
300 private: | |
301 | |
302 // Open file descriptor. | |
303 int fid; | |
304 | |
305 void update_internal (bool force = false); | |
2926 | 306 }; |
307 | |
308 #endif | |
309 | |
310 /* | |
311 ;;; Local Variables: *** | |
312 ;;; mode: C++ *** | |
313 ;;; End: *** | |
314 */ |