GNU Octave 7.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-2022 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
37namespace octave
38{
39 namespace sys
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?
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 (void) const;
101 bool is_chr (void) const;
102 bool is_dir (void) const;
103 bool is_fifo (void) const;
104 bool is_lnk (void) const;
105 bool is_reg (void) const;
106 bool is_sock (void) 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 (void);
117 static bool have_struct_stat_st_blksize (void);
118 static bool have_struct_stat_st_blocks (void);
119
120 ino_t ino (void) const { return m_ino; }
121 dev_t dev (void) const { return m_dev; }
122
123 nlink_t nlink (void) const { return m_nlink; }
124
125 uid_t uid (void) const { return m_uid; }
126 gid_t gid (void) const { return m_gid; }
127
128 off_t size (void) const { return m_size; }
129
130 sys::time atime (void) const { return m_atime; }
131 sys::time mtime (void) const { return m_mtime; }
132 sys::time ctime (void) const { return m_ctime; }
133
134 dev_t rdev (void) const { return m_rdev; }
135
136 long blksize (void) const { return m_blksize; }
137 long blocks (void) const { return m_blocks; }
138
139 mode_t mode (void) const { return m_mode; }
140
141 std::string mode_as_string (void) const;
142
143 bool ok (void) const { return m_initialized && ! m_fail; }
144
145 operator bool () const { return ok (); }
146
147 bool exists (void) const { return ok (); }
148
149 std::string error (void) 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 (void) = 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
194
195 // time of last modification
197
198 // time of last file status change
200
201 // device number for special files
202 dev_t m_rdev;
203
204 // best I/O block size
206
207 // number of 512-byte blocks allocated
209 };
210
211 class
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
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 (void);
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).
268
269 void update_internal (bool force = false);
270 };
271
272 class
275 {
276 public:
277
278 file_fstat (int n) : base_file_stat (), m_fid (n)
279 {
280 update_internal ();
281 }
282
284 : base_file_stat (fs), m_fid (fs.m_fid) { }
285
286 file_fstat& operator = (const file_fstat& fs)
287 {
288 if (this != &fs)
289 {
291
292 m_fid = fs.m_fid;
293 }
294
295 return *this;
296 }
297
298 ~file_fstat (void) = default;
299
300 void get_stats (bool force = false)
301 {
302 if (! m_initialized || force)
303 update_internal (force);
304 }
305
306 void get_stats (int n, bool force = false)
307 {
308 if (n != m_fid || ! m_initialized || force)
309 {
310 m_initialized = false;
311
312 m_fid = n;
313
314 update_internal (force);
315 }
316 }
317
318 private:
319
320 // Open file descriptor.
321 int m_fid;
322
323 void update_internal (bool force = false);
324 };
325 }
326}
327
328#endif
sys::time mtime(void) const
Definition: file-stat.h:131
dev_t dev(void) const
Definition: file-stat.h:121
nlink_t nlink(void) const
Definition: file-stat.h:123
dev_t rdev(void) const
Definition: file-stat.h:134
base_file_stat(const base_file_stat &fs)
Definition: file-stat.h:53
mode_t mode(void) const
Definition: file-stat.h:139
base_file_stat & operator=(const base_file_stat &fs)
Definition: file-stat.h:61
off_t size(void) const
Definition: file-stat.h:128
std::string error(void) const
Definition: file-stat.h:149
sys::time time_resolution(void) const
Definition: file-stat.h:89
sys::time atime(void) const
Definition: file-stat.h:130
ino_t ino(void) const
Definition: file-stat.h:120
bool ok(void) const
Definition: file-stat.h:143
long blksize(void) const
Definition: file-stat.h:136
uid_t uid(void) const
Definition: file-stat.h:125
bool is_newer(const sys::time &time) const
Definition: file-stat.h:152
bool exists(void) const
Definition: file-stat.h:147
gid_t gid(void) const
Definition: file-stat.h:126
long blocks(void) const
Definition: file-stat.h:137
sys::time ctime(void) const
Definition: file-stat.h:132
virtual ~base_file_stat(void)=default
void get_stats(int n, bool force=false)
Definition: file-stat.h:306
void get_stats(bool force=false)
Definition: file-stat.h:300
file_fstat(const file_fstat &fs)
Definition: file-stat.h:283
~file_fstat(void)=default
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
file_stat(const file_stat &fs)
Definition: file-stat.h:221
std::string m_file_name
Definition: file-stat.h:263
#define OCTAVE_API
Definition: main.in.cc:55