GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
file-stat.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 John W. Eaton
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
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
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
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_file_stat_h)
24 #define octave_file_stat_h 1
25 
26 #include <string>
27 
28 #include "oct-time.h"
29 
30 #include <sys/types.h>
31 
32 class
33 OCTAVE_API
35 {
36 public:
37 
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 
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)
55  {
56  initialized = fs.initialized;
57  fail = fs.fail;
58  errmsg = fs.errmsg;
59  fs_mode = fs.fs_mode;
60  fs_ino = fs.fs_ino;
61  fs_dev = fs.fs_dev;
62  fs_nlink = fs.fs_nlink;
63  fs_uid = fs.fs_uid;
64  fs_gid = fs.fs_gid;
65  fs_size = fs.fs_size;
66  fs_atime = fs.fs_atime;
67  fs_mtime = fs.fs_mtime;
68  fs_ctime = fs.fs_ctime;
69  fs_rdev = fs.fs_rdev;
70  fs_blksize = fs.fs_blksize;
71  fs_blocks = fs.fs_blocks;
72  }
73 
74  return *this;
75  }
76 
77  // The minimum difference in file time stamp values.
78  // FIXME: This value should come from the filesystem itself.
79  // How can we get that info?
81  {
82  static octave_time resolution (1.0);
83  return resolution;
84  }
85 
86  // File status and info. The is_XXX functions will return false for
87  // file_stat objects that are not properly initialized. The others
88  // should all return 0 (or the equivalent, for the given object)
89  // which is likely not meaningful.
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 
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 
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 
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; }
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 
126  mode_t mode (void) const { return fs_mode; }
127 
128  std::string mode_as_string (void) const;
129 
130  bool ok (void) const { return initialized && ! fail; }
131 
132  operator bool () const { return ok (); }
133 
134  bool exists (void) const { return ok (); }
135 
136  std::string error (void) const { return ok () ? std::string () : errmsg; }
137 
138  // Has the file referenced by this object been modified since TIME?
139  bool is_newer (const octave_time& time) const { return fs_mtime > time; }
140 
141  // It's nice to be able to hide the file_stat object if we don't
142  // really care about it.
143  static int is_newer (const std::string&, const octave_time&);
144 
145 protected:
146 
147  virtual ~base_file_stat (void) { }
148 
149  // TRUE means we have already called stat.
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.
156  std::string errmsg;
157 
158  // file type and permissions
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
181 
182  // time of last modification
184 
185  // time of last file status change
187 
188  // device number for special files
189  dev_t fs_rdev;
190 
191  // best I/O block size
193 
194  // number of 512-byte blocks allocated
195  long fs_blocks;
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  {
220 
221  file_name = fs.file_name;
222  follow_links = fs.follow_links;
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  {
240  initialized = false;
241 
242  file_name = n;
243 
244  update_internal (force);
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).
256 
257  void update_internal (bool force = false);
258 };
259 
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  {
279 
280  fid = fs.fid;
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  {
298  initialized = false;
299 
300  fid = n;
301 
302  update_internal (force);
303  }
304  }
305 
306 private:
307 
308  // Open file descriptor.
309  int fid;
310 
311  void update_internal (bool force = false);
312 };
313 
314 #endif
ino_t ino(void) const
Definition: file-stat.h:107
int bool
Definition: mex.h:56
octave_time ctime(void) const
Definition: file-stat.h:119
std::string error(void) const
Definition: file-stat.h:136
octave_time fs_ctime
Definition: file-stat.h:186
dev_t dev(void) const
Definition: file-stat.h:108
~file_fstat(void)
Definition: file-stat.h:286
long blocks(void) const
Definition: file-stat.h:124
mode_t mode(void) const
Definition: file-stat.h:126
~file_stat(void)
Definition: file-stat.h:228
void get_stats(int n, bool force=false)
Definition: file-stat.h:294
octave_time atime(void) const
Definition: file-stat.h:117
octave_time fs_atime
Definition: file-stat.h:180
base_file_stat(const base_file_stat &fs)
Definition: file-stat.h:44
dev_t rdev(void) const
Definition: file-stat.h:121
file_fstat(const file_fstat &fs)
Definition: file-stat.h:271
bool is_newer(const octave_time &time) const
Definition: file-stat.h:139
bool initialized
Definition: file-stat.h:150
base_file_stat & operator=(const base_file_stat &fs)
Definition: file-stat.h:52
octave_time fs_mtime
Definition: file-stat.h:183
bool follow_links
Definition: file-stat.h:255
file_fstat(int n)
Definition: file-stat.h:266
void get_stats(const std::string &n, bool force=false)
Definition: file-stat.h:236
base_file_stat(void)
Definition: file-stat.h:38
bool exists(void) const
Definition: file-stat.h:134
#define mode_t
Definition: statdefs.h:34
file_stat(const file_stat &fs)
Definition: file-stat.h:211
virtual ~base_file_stat(void)
Definition: file-stat.h:147
std::string errmsg
Definition: file-stat.h:156
file_stat(const std::string &n=std::string(), bool fl=true)
Definition: file-stat.h:204
nlink_t nlink(void) const
Definition: file-stat.h:110
bool ok(void) const
Definition: file-stat.h:130
octave_time mtime(void) const
Definition: file-stat.h:118
std::string file_name
Definition: file-stat.h:251
long blksize(void) const
Definition: file-stat.h:123
mode_t fs_mode
Definition: file-stat.h:159
void get_stats(bool force=false)
Definition: file-stat.h:230
nlink_t fs_nlink
Definition: file-stat.h:168
octave_time time_resolution(void) const
Definition: file-stat.h:80
void get_stats(bool force=false)
Definition: file-stat.h:288
off_t size(void) const
Definition: file-stat.h:115
uid_t uid(void) const
Definition: file-stat.h:112
gid_t gid(void) const
Definition: file-stat.h:113