GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
file-stat.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2026 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
39
41{
42public:
43
45 : m_initialized (false), m_fail (false), m_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 : m_initialized (fs.m_initialized), m_fail (fs.m_fail),
52 m_errmsg (fs.m_errmsg), m_mode (fs.m_mode), m_ino (fs.m_ino),
53 m_dev (fs.m_dev), m_nlink (fs.m_nlink), m_uid (fs.m_uid),
54 m_gid (fs.m_gid), m_size (fs.m_size), m_atime (fs.m_atime),
55 m_mtime (fs.m_mtime), 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 {
62 m_initialized = fs.m_initialized;
63 m_fail = fs.m_fail;
64 m_errmsg = fs.m_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?
86 sys::time time_resolution () const
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 () const;
98 bool is_chr () const;
99 bool is_dir () const;
100 bool is_fifo () const;
101 bool is_lnk () const;
102 bool is_reg () const;
103 bool is_sock () 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 ();
114 static bool have_struct_stat_st_blksize ();
115 static bool have_struct_stat_st_blocks ();
116
117 ino_t ino () const { return m_ino; }
118 dev_t dev () const { return m_dev; }
119
120 nlink_t nlink () const { return m_nlink; }
121
122 uid_t uid () const { return m_uid; }
123 gid_t gid () const { return m_gid; }
124
125 off_t size () const { return m_size; }
126
127 sys::time atime () const { return m_atime; }
128 sys::time mtime () const { return m_mtime; }
129 sys::time ctime () const { return m_ctime; }
130
131 dev_t rdev () const { return m_rdev; }
132
133 long blksize () const { return m_blksize; }
134 long blocks () const { return m_blocks; }
135
136 mode_t mode () const { return m_mode; }
137
138 std::string mode_as_string () const;
139
140 bool ok () const { return m_initialized && ! m_fail; }
141
142 operator bool () const { return ok (); }
143
144 bool exists () const { return ok (); }
145
146 std::string error () const { return ok () ? "" : m_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
155protected:
156
157 virtual ~base_file_stat () = default;
158
159 // TRUE means we have already called stat.
161
162 // TRUE means the stat for this file failed.
163 bool m_fail;
164
165 // If a failure occurs, this contains the system error text.
166 std::string m_errmsg;
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
190 sys::time m_atime;
191
192 // time of last modification
193 sys::time m_mtime;
194
195 // time of last file status change
196 sys::time m_ctime;
197
198 // device number for special files
199 dev_t m_rdev;
200
201 // best I/O block size
203
204 // number of 512-byte blocks allocated
206};
207
209{
210public:
211
212 // This constructor must remain defined in the cpp file rather than in
213 // the header file (bug #50234).
214 file_stat (const std::string& n = "", bool fl = true);
215
217 : base_file_stat (fs), m_file_name (fs.m_file_name),
218 m_follow_links (fs.m_follow_links) { }
219
221 {
222 if (this != &fs)
223 {
225
226 m_file_name = fs.m_file_name;
227 m_follow_links = fs.m_follow_links;
228 }
229
230 return *this;
231 }
232
233 // This destructor must remain as an empty destructor defined in the
234 // cpp file rather than in the header file (bug #50234).
235 ~file_stat ();
236
237 void get_stats (bool force = false)
238 {
239 if (! m_initialized || force)
240 update_internal (force);
241 }
242
243 void get_stats (const std::string& n, bool force = false)
244 {
245 if (n != m_file_name || ! m_initialized || force)
246 {
247 m_initialized = false;
248
249 m_file_name = n;
250
251 update_internal (force);
252 }
253 }
254
255private:
256
257 // Name of the file.
258 std::string m_file_name;
259
260 // TRUE means follow symbolic links to the ultimate file (stat).
261 // FALSE means get information about the link itself (lstat).
262 bool m_follow_links;
263
264 void update_internal (bool force = false);
265};
266
268{
269public:
270
271 file_fstat () = delete;
272
273 file_fstat (int n) : base_file_stat (), m_fid (n)
274 {
275 update_internal ();
276 }
277
278 OCTAVE_DEFAULT_COPY_MOVE_DELETE (file_fstat)
279
280 void get_stats (bool force = false)
281 {
282 if (! m_initialized || force)
283 update_internal (force);
284 }
285
286 void get_stats (int n, bool force = false)
287 {
288 if (n != m_fid || ! m_initialized || force)
289 {
290 m_initialized = false;
291
292 m_fid = n;
293
294 update_internal (force);
295 }
296 }
297
298private:
299
300 // Open file descriptor.
301 int m_fid;
302
303 void update_internal (bool force = false);
304};
305
306OCTAVE_END_NAMESPACE(sys)
307OCTAVE_END_NAMESPACE(octave)
308
309#endif
sys::time m_atime
Definition file-stat.h:190
std::string m_errmsg
Definition file-stat.h:166
sys::time atime() const
Definition file-stat.h:127
bool exists() const
Definition file-stat.h:144
nlink_t nlink() const
Definition file-stat.h:120
base_file_stat(const base_file_stat &fs)
Definition file-stat.h:50
bool is_newer(const sys::time &time) const
Definition file-stat.h:149
sys::time m_ctime
Definition file-stat.h:196
long blksize() const
Definition file-stat.h:133
std::string error() const
Definition file-stat.h:146
virtual ~base_file_stat()=default
sys::time ctime() const
Definition file-stat.h:129
long blocks() const
Definition file-stat.h:134
base_file_stat & operator=(const base_file_stat &fs)
Definition file-stat.h:58
nlink_t m_nlink
Definition file-stat.h:178
sys::time m_mtime
Definition file-stat.h:193
sys::time mtime() const
Definition file-stat.h:128
sys::time time_resolution() const
Definition file-stat.h:86
bool ok() const
Definition file-stat.h:140
dev_t rdev() const
Definition file-stat.h:131
dev_t dev() const
Definition file-stat.h:118
off_t size() const
Definition file-stat.h:125
ino_t ino() const
Definition file-stat.h:117
uid_t uid() const
Definition file-stat.h:122
mode_t mode() const
Definition file-stat.h:136
gid_t gid() const
Definition file-stat.h:123
file_fstat()=delete
file_fstat(int n)
Definition file-stat.h:273
void get_stats(int n, bool force=false)
Definition file-stat.h:286
file_stat(const file_stat &fs)
Definition file-stat.h:216
void get_stats(bool force=false)
Definition file-stat.h:237
void get_stats(const std::string &n, bool force=false)
Definition file-stat.h:243
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition main.in.cc:55