GNU Octave  9.1.0
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-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_file_stat_h)
27 #define octave_file_stat_h 1
28 
29 #include "octave-config.h"
30 
31 #include <string>
32 
33 #include "oct-time.h"
34 
35 #include <sys/types.h>
36 
38 
40 
41 class
44 {
45 public:
46 
48  : m_initialized (false), m_fail (false), m_errmsg (), m_mode (),
49  m_ino (), m_dev (), m_nlink (), m_uid (), m_gid (),
50  m_size (), m_atime (), m_mtime (), m_ctime (), m_rdev (),
51  m_blksize (), m_blocks () { }
52 
54  : m_initialized (fs.m_initialized), m_fail (fs.m_fail),
55  m_errmsg (fs.m_errmsg), m_mode (fs.m_mode), m_ino (fs.m_ino),
56  m_dev (fs.m_dev), m_nlink (fs.m_nlink), m_uid (fs.m_uid),
57  m_gid (fs.m_gid), m_size (fs.m_size), m_atime (fs.m_atime),
58  m_mtime (fs.m_mtime), m_ctime (fs.m_ctime), m_rdev (fs.m_rdev),
59  m_blksize (fs.m_blksize), m_blocks (fs.m_blocks) { }
60 
61  base_file_stat& operator = (const base_file_stat& fs)
62  {
63  if (this != &fs)
64  {
65  m_initialized = fs.m_initialized;
66  m_fail = fs.m_fail;
67  m_errmsg = fs.m_errmsg;
68  m_mode = fs.m_mode;
69  m_ino = fs.m_ino;
70  m_dev = fs.m_dev;
71  m_nlink = fs.m_nlink;
72  m_uid = fs.m_uid;
73  m_gid = fs.m_gid;
74  m_size = fs.m_size;
75  m_atime = fs.m_atime;
76  m_mtime = fs.m_mtime;
77  m_ctime = fs.m_ctime;
78  m_rdev = fs.m_rdev;
79  m_blksize = fs.m_blksize;
80  m_blocks = fs.m_blocks;
81  }
82 
83  return *this;
84  }
85 
86  // The minimum difference in file time stamp values.
87  // FIXME: This value should come from the filesystem itself.
88  // How can we get that info?
89  sys::time time_resolution () const
90  {
91  static sys::time resolution (1.0);
92  return resolution;
93  }
94 
95  // File status and info. The is_XXX functions will return false for
96  // file_stat objects that are not properly initialized. The others
97  // should all return 0 (or the equivalent, for the given object)
98  // which is likely not meaningful.
99 
100  bool is_blk () const;
101  bool is_chr () const;
102  bool is_dir () const;
103  bool is_fifo () const;
104  bool is_lnk () const;
105  bool is_reg () const;
106  bool is_sock () const;
107 
108  static bool is_blk (mode_t mode);
109  static bool is_chr (mode_t mode);
110  static bool is_dir (mode_t mode);
111  static bool is_fifo (mode_t mode);
112  static bool is_lnk (mode_t mode);
113  static bool is_reg (mode_t mode);
114  static bool is_sock (mode_t mode);
115 
116  static bool have_struct_stat_st_rdev ();
117  static bool have_struct_stat_st_blksize ();
118  static bool have_struct_stat_st_blocks ();
119 
120  ino_t ino () const { return m_ino; }
121  dev_t dev () const { return m_dev; }
122 
123  nlink_t nlink () const { return m_nlink; }
124 
125  uid_t uid () const { return m_uid; }
126  gid_t gid () const { return m_gid; }
127 
128  off_t size () const { return m_size; }
129 
130  sys::time atime () const { return m_atime; }
131  sys::time mtime () const { return m_mtime; }
132  sys::time ctime () const { return m_ctime; }
133 
134  dev_t rdev () const { return m_rdev; }
135 
136  long blksize () const { return m_blksize; }
137  long blocks () const { return m_blocks; }
138 
139  mode_t mode () const { return m_mode; }
140 
141  std::string mode_as_string () const;
142 
143  bool ok () const { return m_initialized && ! m_fail; }
144 
145  operator bool () const { return ok (); }
146 
147  bool exists () const { return ok (); }
148 
149  std::string error () const { return ok () ? "" : m_errmsg; }
150 
151  // Has the file referenced by this object been modified since TIME?
152  bool is_newer (const sys::time& time) const { return m_mtime > time; }
153 
154  // It's nice to be able to hide the file_stat object if we don't
155  // really care about it.
156  static int is_newer (const std::string&, const sys::time&);
157 
158 protected:
159 
160  virtual ~base_file_stat () = default;
161 
162  // TRUE means we have already called stat.
164 
165  // TRUE means the stat for this file failed.
166  bool m_fail;
167 
168  // If a failure occurs, this contains the system error text.
169  std::string m_errmsg;
170 
171  // file type and permissions
172  mode_t m_mode;
173 
174  // serial number
175  ino_t m_ino;
176 
177  // device number
178  dev_t m_dev;
179 
180  // number of links
181  nlink_t m_nlink;
182 
183  // user ID of owner
184  uid_t m_uid;
185 
186  // group ID of owner
187  gid_t m_gid;
188 
189  // size in bytes, for regular files
190  off_t m_size;
191 
192  // time of last access
193  sys::time m_atime;
194 
195  // time of last modification
196  sys::time m_mtime;
197 
198  // time of last file status change
199  sys::time m_ctime;
200 
201  // device number for special files
202  dev_t m_rdev;
203 
204  // best I/O block size
205  long m_blksize;
206 
207  // number of 512-byte blocks allocated
208  long m_blocks;
209 };
210 
211 class
213 file_stat : public base_file_stat
214 {
215 public:
216 
217  // This constructor must remain defined in the cpp file rather than in
218  // the header file (bug #50234).
219  file_stat (const std::string& n = "", bool fl = true);
220 
221  file_stat (const file_stat& fs)
222  : base_file_stat (fs), m_file_name (fs.m_file_name),
223  m_follow_links (fs.m_follow_links) { }
224 
225  file_stat& operator = (const file_stat& fs)
226  {
227  if (this != &fs)
228  {
230 
231  m_file_name = fs.m_file_name;
232  m_follow_links = fs.m_follow_links;
233  }
234 
235  return *this;
236  }
237 
238  // This destructor must remain as an empty destructor defined in the
239  // cpp file rather than in the header file (bug #50234).
240  ~file_stat ();
241 
242  void get_stats (bool force = false)
243  {
244  if (! m_initialized || force)
245  update_internal (force);
246  }
247 
248  void get_stats (const std::string& n, bool force = false)
249  {
250  if (n != m_file_name || ! m_initialized || force)
251  {
252  m_initialized = false;
253 
254  m_file_name = n;
255 
256  update_internal (force);
257  }
258  }
259 
260 private:
261 
262  // Name of the file.
263  std::string m_file_name;
264 
265  // TRUE means follow symbolic links to the ultimate file (stat).
266  // FALSE means get information about the link itself (lstat).
267  bool m_follow_links;
268 
269  void update_internal (bool force = false);
270 };
271 
272 class
274 file_fstat : public base_file_stat
275 {
276 public:
277 
278  file_fstat () = delete;
279 
280  file_fstat (int n) : base_file_stat (), m_fid (n)
281  {
282  update_internal ();
283  }
284 
285  OCTAVE_DEFAULT_COPY_MOVE_DELETE (file_fstat)
286 
287  void get_stats (bool force = false)
288  {
289  if (! m_initialized || force)
290  update_internal (force);
291  }
292 
293  void get_stats (int n, bool force = false)
294  {
295  if (n != m_fid || ! m_initialized || force)
296  {
297  m_initialized = false;
298 
299  m_fid = n;
300 
301  update_internal (force);
302  }
303  }
304 
305 private:
306 
307  // Open file descriptor.
308  int m_fid;
309 
310  void update_internal (bool force = false);
311 };
312 
313 OCTAVE_END_NAMESPACE(sys)
314 OCTAVE_END_NAMESPACE(octave)
315 
316 #endif
sys::time m_atime
Definition: file-stat.h:193
std::string m_errmsg
Definition: file-stat.h:169
sys::time atime() const
Definition: file-stat.h:130
base_file_stat & operator=(const base_file_stat &fs)
Definition: file-stat.h:61
bool exists() const
Definition: file-stat.h:147
nlink_t nlink() const
Definition: file-stat.h:123
base_file_stat(const base_file_stat &fs)
Definition: file-stat.h:53
bool is_newer(const sys::time &time) const
Definition: file-stat.h:152
sys::time m_ctime
Definition: file-stat.h:199
mode_t m_mode
Definition: file-stat.h:172
long blksize() const
Definition: file-stat.h:136
std::string error() const
Definition: file-stat.h:149
virtual ~base_file_stat()=default
sys::time ctime() const
Definition: file-stat.h:132
long blocks() const
Definition: file-stat.h:137
nlink_t m_nlink
Definition: file-stat.h:181
sys::time m_mtime
Definition: file-stat.h:196
sys::time mtime() const
Definition: file-stat.h:131
sys::time time_resolution() const
Definition: file-stat.h:89
bool ok() const
Definition: file-stat.h:143
dev_t rdev() const
Definition: file-stat.h:134
dev_t dev() const
Definition: file-stat.h:121
bool m_initialized
Definition: file-stat.h:163
off_t size() const
Definition: file-stat.h:128
ino_t ino() const
Definition: file-stat.h:120
uid_t uid() const
Definition: file-stat.h:125
mode_t mode() const
Definition: file-stat.h:139
gid_t gid() const
Definition: file-stat.h:126
file_fstat()=delete
file_fstat(int n)
Definition: file-stat.h:280
void get_stats(int n, bool force=false)
Definition: file-stat.h:293
file_stat(const file_stat &fs)
Definition: file-stat.h:221
void get_stats(bool force=false)
Definition: file-stat.h:242
void get_stats(const std::string &n, bool force=false)
Definition: file-stat.h:248
Definition: oct-time.h:64
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition: main.cc:55
octave_idx_type n
Definition: mx-inlines.cc:761