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 |
|
82 // File status and info. These should only be called for objects |
|
83 // that are already properly initialized. |
|
84 |
|
85 bool is_blk (void) const; |
|
86 bool is_chr (void) const; |
|
87 bool is_dir (void) const; |
|
88 bool is_fifo (void) const; |
|
89 bool is_lnk (void) const; |
|
90 bool is_reg (void) const; |
|
91 bool is_sock (void) const; |
|
92 |
5476
|
93 static bool is_blk (mode_t mode); |
|
94 static bool is_chr (mode_t mode); |
|
95 static bool is_dir (mode_t mode); |
|
96 static bool is_fifo (mode_t mode); |
|
97 static bool is_lnk (mode_t mode); |
|
98 static bool is_reg (mode_t mode); |
|
99 static bool is_sock (mode_t mode); |
|
100 |
2926
|
101 ino_t ino (void) const { return fs_ino; } |
|
102 dev_t dev (void) const { return fs_dev; } |
|
103 |
|
104 nlink_t nlink (void) const { return fs_nlink; } |
|
105 |
|
106 uid_t uid (void) const { return fs_uid; } |
|
107 gid_t gid (void) const { return fs_gid; } |
|
108 |
|
109 off_t size (void) const { return fs_size; } |
|
110 |
3260
|
111 octave_time atime (void) const { return fs_atime; } |
|
112 octave_time mtime (void) const { return fs_mtime; } |
|
113 octave_time ctime (void) const { return fs_ctime; } |
2926
|
114 |
|
115 dev_t rdev (void) const { return fs_rdev; } |
|
116 |
|
117 long blksize (void) const { return fs_blksize; } |
|
118 long blocks (void) const { return fs_blocks; } |
|
119 |
5476
|
120 mode_t mode (void) const { return fs_mode; } |
|
121 |
3504
|
122 std::string mode_as_string (void) const; |
2926
|
123 |
|
124 bool ok (void) const { return initialized && ! fail; } |
|
125 |
3145
|
126 operator bool () const { return ok (); } |
2926
|
127 |
|
128 bool exists (void) const { return ok (); } |
|
129 |
3504
|
130 std::string error (void) const { return ok () ? std::string () : errmsg; } |
2926
|
131 |
|
132 // Has the file referenced by this object been modified since TIME? |
3260
|
133 bool is_newer (const octave_time& time) const { return fs_mtime > time; } |
2926
|
134 |
|
135 // It's nice to be able to hide the file_stat object if we don't |
|
136 // really care about it. |
3504
|
137 static int is_newer (const std::string&, const octave_time&); |
2926
|
138 |
8549
|
139 protected: |
2926
|
140 |
|
141 // TRUE means we have already called stat. |
|
142 bool initialized; |
|
143 |
|
144 // TRUE means the stat for this file failed. |
|
145 bool fail; |
|
146 |
|
147 // If a failure occurs, this contains the system error text. |
3504
|
148 std::string errmsg; |
2926
|
149 |
|
150 // file type and permissions |
|
151 mode_t fs_mode; |
|
152 |
|
153 // serial number |
|
154 ino_t fs_ino; |
|
155 |
|
156 // device number |
|
157 dev_t fs_dev; |
|
158 |
|
159 // number of links |
|
160 nlink_t fs_nlink; |
|
161 |
|
162 // user ID of owner |
|
163 uid_t fs_uid; |
|
164 |
|
165 // group ID of owner |
|
166 gid_t fs_gid; |
|
167 |
|
168 // size in bytes, for regular files |
|
169 off_t fs_size; |
|
170 |
|
171 // time of last access |
3260
|
172 octave_time fs_atime; |
2926
|
173 |
|
174 // time of last modification |
3260
|
175 octave_time fs_mtime; |
2926
|
176 |
|
177 // time of last file status change |
3260
|
178 octave_time fs_ctime; |
2926
|
179 |
|
180 // device number for special files |
|
181 dev_t fs_rdev; |
|
182 |
|
183 // best I/O block size |
|
184 long fs_blksize; |
|
185 |
|
186 // number of 512-byte blocks allocated |
|
187 long fs_blocks; |
8549
|
188 }; |
|
189 |
|
190 class |
|
191 OCTAVE_API |
|
192 file_stat : public base_file_stat |
|
193 { |
|
194 public: |
|
195 |
|
196 file_stat (const std::string& n = std::string (), bool fl = true) |
|
197 : base_file_stat (), file_name (n), follow_links (fl) |
|
198 { |
|
199 if (! file_name.empty ()) |
|
200 update_internal (); |
|
201 } |
|
202 |
|
203 file_stat (const file_stat& fs) |
|
204 : base_file_stat (fs), file_name (fs.file_name), |
|
205 follow_links (fs.follow_links) { } |
|
206 |
|
207 file_stat& operator = (const file_stat& fs) |
|
208 { |
|
209 if (this != &fs) |
|
210 { |
|
211 base_file_stat::operator = (fs); |
|
212 |
|
213 file_name = fs.file_name; |
|
214 follow_links = fs.follow_links; |
|
215 } |
|
216 |
|
217 return *this; |
|
218 } |
|
219 |
|
220 ~file_stat (void) { } |
|
221 |
|
222 void get_stats (bool force = false) |
|
223 { |
|
224 if (! initialized || force) |
|
225 update_internal (force); |
|
226 } |
|
227 |
|
228 void get_stats (const std::string& n, bool force = false) |
|
229 { |
|
230 if (n != file_name || ! initialized || force) |
|
231 { |
|
232 initialized = false; |
|
233 |
|
234 file_name = n; |
|
235 |
|
236 update_internal (force); |
|
237 } |
|
238 } |
|
239 |
|
240 private: |
|
241 |
|
242 // Name of the file. |
|
243 std::string file_name; |
|
244 |
|
245 // TRUE means follow symbolic links to the ultimate file (stat). |
|
246 // FALSE means get information about the link itself (lstat). |
|
247 bool follow_links; |
2926
|
248 |
|
249 void update_internal (bool force = false); |
8549
|
250 }; |
2926
|
251 |
8549
|
252 class |
|
253 OCTAVE_API |
|
254 file_fstat : public base_file_stat |
|
255 { |
|
256 public: |
|
257 |
|
258 file_fstat (int n) : base_file_stat (), fid (n) |
|
259 { |
|
260 update_internal (); |
|
261 } |
|
262 |
|
263 file_fstat (const file_fstat& fs) |
|
264 : base_file_stat (fs), fid (fs.fid) { } |
|
265 |
|
266 file_fstat& operator = (const file_fstat& fs) |
|
267 { |
|
268 if (this != &fs) |
|
269 { |
|
270 base_file_stat::operator = (fs); |
|
271 |
|
272 fid = fs.fid; |
|
273 } |
|
274 |
|
275 return *this; |
|
276 } |
|
277 |
|
278 ~file_fstat (void) { } |
|
279 |
|
280 void get_stats (bool force = false) |
|
281 { |
|
282 if (! initialized || force) |
|
283 update_internal (force); |
|
284 } |
|
285 |
|
286 void get_stats (int n, bool force = false) |
|
287 { |
|
288 if (n != fid || ! initialized || force) |
|
289 { |
|
290 initialized = false; |
|
291 |
|
292 fid = n; |
|
293 |
|
294 update_internal (force); |
|
295 } |
|
296 } |
|
297 |
|
298 private: |
|
299 |
|
300 // Open file descriptor. |
|
301 int fid; |
|
302 |
|
303 void update_internal (bool force = false); |
2926
|
304 }; |
|
305 |
|
306 #endif |
|
307 |
|
308 /* |
|
309 ;;; Local Variables: *** |
|
310 ;;; mode: C++ *** |
|
311 ;;; End: *** |
|
312 */ |