GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
file-stat.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_file_stat_h)
24 #define octave_file_stat_h 1
25 
26 #include "octave-config.h"
27 
28 #include <string>
29 
30 #include "oct-time.h"
31 
32 #include <sys/types.h>
33 
34 namespace octave
35 {
36  namespace sys
37  {
38  class
39  OCTAVE_API
41  {
42  public:
43 
45  : initialized (false), fail (false), errmsg (), m_mode (),
46  m_ino (), m_dev (), m_nlink (), m_uid (), m_gid (),
47  m_size (), m_atime (), m_mtime (), m_ctime (), m_rdev (),
48  m_blksize (), m_blocks () { }
49 
51  : initialized (fs.initialized), fail (fs.fail), errmsg (fs.errmsg),
52  m_mode (fs.m_mode), m_ino (fs.m_ino), m_dev (fs.m_dev),
53  m_nlink (fs.m_nlink), m_uid (fs.m_uid), m_gid (fs.m_gid),
54  m_size (fs.m_size), m_atime (fs.m_atime), m_mtime (fs.m_mtime),
55  m_ctime (fs.m_ctime), m_rdev (fs.m_rdev),
56  m_blksize (fs.m_blksize), m_blocks (fs.m_blocks) { }
57 
58  base_file_stat& operator = (const base_file_stat& fs)
59  {
60  if (this != &fs)
61  {
63  fail = fs.fail;
64  errmsg = fs.errmsg;
65  m_mode = fs.m_mode;
66  m_ino = fs.m_ino;
67  m_dev = fs.m_dev;
68  m_nlink = fs.m_nlink;
69  m_uid = fs.m_uid;
70  m_gid = fs.m_gid;
71  m_size = fs.m_size;
72  m_atime = fs.m_atime;
73  m_mtime = fs.m_mtime;
74  m_ctime = fs.m_ctime;
75  m_rdev = fs.m_rdev;
76  m_blksize = fs.m_blksize;
77  m_blocks = fs.m_blocks;
78  }
79 
80  return *this;
81  }
82 
83  // The minimum difference in file time stamp values.
84  // FIXME: This value should come from the filesystem itself.
85  // How can we get that info?
87  {
88  static sys::time resolution (1.0);
89  return resolution;
90  }
91 
92  // File status and info. The is_XXX functions will return false for
93  // file_stat objects that are not properly initialized. The others
94  // should all return 0 (or the equivalent, for the given object)
95  // which is likely not meaningful.
96 
97  bool is_blk (void) const;
98  bool is_chr (void) const;
99  bool is_dir (void) const;
100  bool is_fifo (void) const;
101  bool is_lnk (void) const;
102  bool is_reg (void) const;
103  bool is_sock (void) const;
104 
105  static bool is_blk (mode_t mode);
106  static bool is_chr (mode_t mode);
107  static bool is_dir (mode_t mode);
108  static bool is_fifo (mode_t mode);
109  static bool is_lnk (mode_t mode);
110  static bool is_reg (mode_t mode);
111  static bool is_sock (mode_t mode);
112 
113  static bool have_struct_stat_st_rdev (void);
114  static bool have_struct_stat_st_blksize (void);
115  static bool have_struct_stat_st_blocks (void);
116 
117  ino_t ino (void) const { return m_ino; }
118  dev_t dev (void) const { return m_dev; }
119 
120  nlink_t nlink (void) const { return m_nlink; }
121 
122  uid_t uid (void) const { return m_uid; }
123  gid_t gid (void) const { return m_gid; }
124 
125  off_t size (void) const { return m_size; }
126 
127  sys::time atime (void) const { return m_atime; }
128  sys::time mtime (void) const { return m_mtime; }
129  sys::time ctime (void) const { return m_ctime; }
130 
131  dev_t rdev (void) const { return m_rdev; }
132 
133  long blksize (void) const { return m_blksize; }
134  long blocks (void) const { return m_blocks; }
135 
136  mode_t mode (void) const { return m_mode; }
137 
138  std::string mode_as_string (void) const;
139 
140  bool ok (void) const { return initialized && ! fail; }
141 
142  operator bool () const { return ok (); }
143 
144  bool exists (void) const { return ok (); }
145 
146  std::string error (void) const { return ok () ? "" : errmsg; }
147 
148  // Has the file referenced by this object been modified since TIME?
149  bool is_newer (const sys::time& time) const { return m_mtime > time; }
150 
151  // It's nice to be able to hide the file_stat object if we don't
152  // really care about it.
153  static int is_newer (const std::string&, const sys::time&);
154 
155  protected:
156 
157  virtual ~base_file_stat (void) = default;
158 
159  // TRUE means we have already called stat.
161 
162  // TRUE means the stat for this file failed.
163  bool fail;
164 
165  // If a failure occurs, this contains the system error text.
167 
168  // file type and permissions
169  mode_t m_mode;
170 
171  // serial number
172  ino_t m_ino;
173 
174  // device number
175  dev_t m_dev;
176 
177  // number of links
178  nlink_t m_nlink;
179 
180  // user ID of owner
181  uid_t m_uid;
182 
183  // group ID of owner
184  gid_t m_gid;
185 
186  // size in bytes, for regular files
187  off_t m_size;
188 
189  // time of last access
191 
192  // time of last modification
194 
195  // time of last file status change
197 
198  // device number for special files
199  dev_t m_rdev;
200 
201  // best I/O block size
202  long m_blksize;
203 
204  // number of 512-byte blocks allocated
205  long m_blocks;
206  };
207 
208  class
209  OCTAVE_API
210  file_stat : public base_file_stat
211  {
212  public:
213 
214  // This constructor must remain defined in the cpp file rather than in
215  // the header file (bug #50234).
216  file_stat (const std::string& n = "", bool fl = true);
217 
219  : base_file_stat (fs), file_name (fs.file_name),
220  follow_links (fs.follow_links) { }
221 
222  file_stat& operator = (const file_stat& fs)
223  {
224  if (this != &fs)
225  {
227 
228  file_name = fs.file_name;
229  follow_links = fs.follow_links;
230  }
231 
232  return *this;
233  }
234 
235  // This destructor must remain as an empty destructor defined in the
236  // cpp file rather than in the header file (bug #50234).
237  ~file_stat (void);
238 
239  void get_stats (bool force = false)
240  {
241  if (! initialized || force)
242  update_internal (force);
243  }
244 
245  void get_stats (const std::string& n, bool force = false)
246  {
247  if (n != file_name || ! initialized || force)
248  {
249  initialized = false;
250 
251  file_name = n;
252 
253  update_internal (force);
254  }
255  }
256 
257  private:
258 
259  // Name of the file.
261 
262  // TRUE means follow symbolic links to the ultimate file (stat).
263  // FALSE means get information about the link itself (lstat).
265 
266  void update_internal (bool force = false);
267  };
268 
269  class
270  OCTAVE_API
271  file_fstat : public base_file_stat
272  {
273  public:
274 
275  file_fstat (int n) : base_file_stat (), fid (n)
276  {
277  update_internal ();
278  }
279 
281  : base_file_stat (fs), fid (fs.fid) { }
282 
283  file_fstat& operator = (const file_fstat& fs)
284  {
285  if (this != &fs)
286  {
288 
289  fid = fs.fid;
290  }
291 
292  return *this;
293  }
294 
295  ~file_fstat (void) = default;
296 
297  void get_stats (bool force = false)
298  {
299  if (! initialized || force)
300  update_internal (force);
301  }
302 
303  void get_stats (int n, bool force = false)
304  {
305  if (n != fid || ! initialized || force)
306  {
307  initialized = false;
308 
309  fid = n;
310 
311  update_internal (force);
312  }
313  }
314 
315  private:
316 
317  // Open file descriptor.
318  int fid;
319 
320  void update_internal (bool force = false);
321  };
322  }
323 }
324 
325 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
326 
327 OCTAVE_DEPRECATED (4.2, "use 'octave::sys::base_file_stat' instead")
328 typedef octave::sys::base_file_stat base_file_stat;
329 
330 OCTAVE_DEPRECATED (4.2, "use 'octave::sys::file_stat' instead")
331 typedef octave::sys::file_stat file_stat;
332 
333 OCTAVE_DEPRECATED (4.2, "use 'octave::sys::file_fstat' instead")
334 typedef octave::sys::file_fstat file_fstat;
335 
336 #endif
337 
338 #endif
std::string error(void) const
Definition: file-stat.h:146
base_file_stat & operator=(const base_file_stat &fs)
Definition: file-stat.h:58
long blksize(void) const
Definition: file-stat.h:133
void get_stats(const std::string &n, bool force=false)
Definition: file-stat.h:245
sys::time time_resolution(void) const
Definition: file-stat.h:86
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6348
off_t size(void) const
Definition: file-stat.h:125
sys::time ctime(void) const
Definition: file-stat.h:129
ino_t ino(void) const
Definition: file-stat.h:117
file_fstat(const file_fstat &fs)
Definition: file-stat.h:280
sys::time atime(void) const
Definition: file-stat.h:127
is false
Definition: cellfun.cc:400
void get_stats(bool force=false)
Definition: file-stat.h:239
mode_t mode(void) const
Definition: file-stat.h:136
static bool initialized
Definition: defaults.cc:48
file_stat(const file_stat &fs)
Definition: file-stat.h:218
base_file_stat(const base_file_stat &fs)
Definition: file-stat.h:50
gid_t gid(void) const
Definition: file-stat.h:123
void get_stats(int n, bool force=false)
Definition: file-stat.h:303
std::string file_name
Definition: file-stat.h:260
uid_t uid(void) const
Definition: file-stat.h:122
bool exists(void) const
Definition: file-stat.h:144
octave::sys::file_stat fs(filename)
dev_t dev(void) const
Definition: file-stat.h:118
bool ok(void) const
Definition: file-stat.h:140
dev_t rdev(void) const
Definition: file-stat.h:131
long blocks(void) const
Definition: file-stat.h:134
nlink_t nlink(void) const
Definition: file-stat.h:120
int fid
Definition: file-io.cc:625
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
sys::time mtime(void) const
Definition: file-stat.h:128
bool is_newer(const sys::time &time) const
Definition: file-stat.h:149
void get_stats(bool force=false)
Definition: file-stat.h:297