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