Mercurial > hg > octave-nkf
annotate liboctave/file-stat.h @ 10182:0522a65bcd56
assume unistd.h and sys/types.h exist
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 21 Jan 2010 15:41:19 -0500 |
parents | 4c0cdbe0acca |
children | cbc402e64d83 |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2005, 2006, 2007, 2009 |
7017 | 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 #include <sys/types.h> |
32 | |
33 class | |
6108 | 34 OCTAVE_API |
8549 | 35 base_file_stat |
2926 | 36 { |
37 public: | |
38 | |
8549 | 39 base_file_stat (void) |
40 : initialized (false), fail (false), errmsg (), fs_mode (), | |
41 fs_ino (), fs_dev (), fs_nlink (), fs_uid (), fs_gid (), | |
42 fs_size (), fs_atime (), fs_mtime (), fs_ctime (), fs_rdev (), | |
43 fs_blksize (), fs_blocks () { } | |
44 | |
45 base_file_stat (const base_file_stat& fs) | |
46 : initialized (fs.initialized), fail (fs.fail), errmsg (fs.errmsg), | |
47 fs_mode (fs.fs_mode), fs_ino (fs.fs_ino), fs_dev (fs.fs_dev), | |
48 fs_nlink (fs.fs_nlink), fs_uid (fs.fs_uid), fs_gid (fs.fs_gid), | |
49 fs_size (fs.fs_size), fs_atime (fs.fs_atime), fs_mtime (fs.fs_mtime), | |
50 fs_ctime (fs.fs_ctime), fs_rdev (fs.fs_rdev), | |
51 fs_blksize (fs.fs_blksize), fs_blocks (fs.fs_blocks) { } | |
52 | |
53 base_file_stat& operator = (const base_file_stat& fs) | |
54 { | |
55 if (this != &fs) | |
2926 | 56 { |
8549 | 57 initialized = fs.initialized; |
58 fail = fs.fail; | |
59 errmsg = fs.errmsg; | |
60 fs_mode = fs.fs_mode; | |
61 fs_ino = fs.fs_ino; | |
62 fs_dev = fs.fs_dev; | |
63 fs_nlink = fs.fs_nlink; | |
64 fs_uid = fs.fs_uid; | |
65 fs_gid = fs.fs_gid; | |
66 fs_size = fs.fs_size; | |
67 fs_atime = fs.fs_atime; | |
68 fs_mtime = fs.fs_mtime; | |
69 fs_ctime = fs.fs_ctime; | |
70 fs_rdev = fs.fs_rdev; | |
71 fs_blksize = fs.fs_blksize; | |
72 fs_blocks = fs.fs_blocks; | |
2926 | 73 } |
74 | |
8549 | 75 return *this; |
76 } | |
2926 | 77 |
8549 | 78 ~base_file_stat (void) { } |
2926 | 79 |
9748
d6b2b708b6b0
load-path: compare directory timestamps with tolerance
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
80 // The minimum difference in file time stamp values. |
d6b2b708b6b0
load-path: compare directory timestamps with tolerance
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
81 // FIXME -- this value should come from the filesystem itself. How |
d6b2b708b6b0
load-path: compare directory timestamps with tolerance
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
82 // can we get that info? |
d6b2b708b6b0
load-path: compare directory timestamps with tolerance
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
83 octave_time time_resolution (void) const |
d6b2b708b6b0
load-path: compare directory timestamps with tolerance
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
84 { |
d6b2b708b6b0
load-path: compare directory timestamps with tolerance
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
85 static octave_time resolution (1.0); |
d6b2b708b6b0
load-path: compare directory timestamps with tolerance
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
86 return resolution; |
d6b2b708b6b0
load-path: compare directory timestamps with tolerance
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
87 } |
d6b2b708b6b0
load-path: compare directory timestamps with tolerance
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
88 |
8689
ddbe87599331
base_file_stat::is_XXX: return false if object is not initialized
John W. Eaton <jwe@octave.org>
parents:
8549
diff
changeset
|
89 // 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
|
90 // 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
|
91 // 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
|
92 // which is likely not meaningful. |
2926 | 93 |
94 bool is_blk (void) const; | |
95 bool is_chr (void) const; | |
96 bool is_dir (void) const; | |
97 bool is_fifo (void) const; | |
98 bool is_lnk (void) const; | |
99 bool is_reg (void) const; | |
100 bool is_sock (void) const; | |
101 | |
5476 | 102 static bool is_blk (mode_t mode); |
103 static bool is_chr (mode_t mode); | |
104 static bool is_dir (mode_t mode); | |
105 static bool is_fifo (mode_t mode); | |
106 static bool is_lnk (mode_t mode); | |
107 static bool is_reg (mode_t mode); | |
108 static bool is_sock (mode_t mode); | |
109 | |
2926 | 110 ino_t ino (void) const { return fs_ino; } |
111 dev_t dev (void) const { return fs_dev; } | |
112 | |
113 nlink_t nlink (void) const { return fs_nlink; } | |
114 | |
115 uid_t uid (void) const { return fs_uid; } | |
116 gid_t gid (void) const { return fs_gid; } | |
117 | |
118 off_t size (void) const { return fs_size; } | |
119 | |
3260 | 120 octave_time atime (void) const { return fs_atime; } |
121 octave_time mtime (void) const { return fs_mtime; } | |
122 octave_time ctime (void) const { return fs_ctime; } | |
2926 | 123 |
124 dev_t rdev (void) const { return fs_rdev; } | |
125 | |
126 long blksize (void) const { return fs_blksize; } | |
127 long blocks (void) const { return fs_blocks; } | |
128 | |
5476 | 129 mode_t mode (void) const { return fs_mode; } |
130 | |
3504 | 131 std::string mode_as_string (void) const; |
2926 | 132 |
133 bool ok (void) const { return initialized && ! fail; } | |
134 | |
3145 | 135 operator bool () const { return ok (); } |
2926 | 136 |
137 bool exists (void) const { return ok (); } | |
138 | |
3504 | 139 std::string error (void) const { return ok () ? std::string () : errmsg; } |
2926 | 140 |
141 // Has the file referenced by this object been modified since TIME? | |
3260 | 142 bool is_newer (const octave_time& time) const { return fs_mtime > time; } |
2926 | 143 |
144 // It's nice to be able to hide the file_stat object if we don't | |
145 // really care about it. | |
3504 | 146 static int is_newer (const std::string&, const octave_time&); |
2926 | 147 |
8549 | 148 protected: |
2926 | 149 |
150 // TRUE means we have already called stat. | |
151 bool initialized; | |
152 | |
153 // TRUE means the stat for this file failed. | |
154 bool fail; | |
155 | |
156 // If a failure occurs, this contains the system error text. | |
3504 | 157 std::string errmsg; |
2926 | 158 |
159 // file type and permissions | |
160 mode_t fs_mode; | |
161 | |
162 // serial number | |
163 ino_t fs_ino; | |
164 | |
165 // device number | |
166 dev_t fs_dev; | |
167 | |
168 // number of links | |
169 nlink_t fs_nlink; | |
170 | |
171 // user ID of owner | |
172 uid_t fs_uid; | |
173 | |
174 // group ID of owner | |
175 gid_t fs_gid; | |
176 | |
177 // size in bytes, for regular files | |
178 off_t fs_size; | |
179 | |
180 // time of last access | |
3260 | 181 octave_time fs_atime; |
2926 | 182 |
183 // time of last modification | |
3260 | 184 octave_time fs_mtime; |
2926 | 185 |
186 // time of last file status change | |
3260 | 187 octave_time fs_ctime; |
2926 | 188 |
189 // device number for special files | |
190 dev_t fs_rdev; | |
191 | |
192 // best I/O block size | |
193 long fs_blksize; | |
194 | |
195 // number of 512-byte blocks allocated | |
196 long fs_blocks; | |
8549 | 197 }; |
198 | |
199 class | |
200 OCTAVE_API | |
201 file_stat : public base_file_stat | |
202 { | |
203 public: | |
204 | |
205 file_stat (const std::string& n = std::string (), bool fl = true) | |
206 : base_file_stat (), file_name (n), follow_links (fl) | |
207 { | |
208 if (! file_name.empty ()) | |
209 update_internal (); | |
210 } | |
211 | |
212 file_stat (const file_stat& fs) | |
213 : base_file_stat (fs), file_name (fs.file_name), | |
214 follow_links (fs.follow_links) { } | |
215 | |
216 file_stat& operator = (const file_stat& fs) | |
217 { | |
218 if (this != &fs) | |
219 { | |
220 base_file_stat::operator = (fs); | |
221 | |
222 file_name = fs.file_name; | |
223 follow_links = fs.follow_links; | |
224 } | |
225 | |
226 return *this; | |
227 } | |
228 | |
229 ~file_stat (void) { } | |
230 | |
231 void get_stats (bool force = false) | |
232 { | |
233 if (! initialized || force) | |
234 update_internal (force); | |
235 } | |
236 | |
237 void get_stats (const std::string& n, bool force = false) | |
238 { | |
239 if (n != file_name || ! initialized || force) | |
240 { | |
241 initialized = false; | |
242 | |
243 file_name = n; | |
244 | |
245 update_internal (force); | |
246 } | |
247 } | |
248 | |
249 private: | |
250 | |
251 // Name of the file. | |
252 std::string file_name; | |
253 | |
254 // TRUE means follow symbolic links to the ultimate file (stat). | |
255 // FALSE means get information about the link itself (lstat). | |
256 bool follow_links; | |
2926 | 257 |
258 void update_internal (bool force = false); | |
8549 | 259 }; |
2926 | 260 |
8549 | 261 class |
262 OCTAVE_API | |
263 file_fstat : public base_file_stat | |
264 { | |
265 public: | |
266 | |
267 file_fstat (int n) : base_file_stat (), fid (n) | |
268 { | |
269 update_internal (); | |
270 } | |
271 | |
272 file_fstat (const file_fstat& fs) | |
273 : base_file_stat (fs), fid (fs.fid) { } | |
274 | |
275 file_fstat& operator = (const file_fstat& fs) | |
276 { | |
277 if (this != &fs) | |
278 { | |
279 base_file_stat::operator = (fs); | |
280 | |
281 fid = fs.fid; | |
282 } | |
283 | |
284 return *this; | |
285 } | |
286 | |
287 ~file_fstat (void) { } | |
288 | |
289 void get_stats (bool force = false) | |
290 { | |
291 if (! initialized || force) | |
292 update_internal (force); | |
293 } | |
294 | |
295 void get_stats (int n, bool force = false) | |
296 { | |
297 if (n != fid || ! initialized || force) | |
298 { | |
299 initialized = false; | |
300 | |
301 fid = n; | |
302 | |
303 update_internal (force); | |
304 } | |
305 } | |
306 | |
307 private: | |
308 | |
309 // Open file descriptor. | |
310 int fid; | |
311 | |
312 void update_internal (bool force = false); | |
2926 | 313 }; |
314 | |
315 #endif |