GNU Octave 10.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-2025 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
42{
43public:
44
46 : m_initialized (false), m_fail (false), m_errmsg (), m_mode (),
47 m_ino (), m_dev (), m_nlink (), m_uid (), m_gid (),
48 m_size (), m_atime (), m_mtime (), m_ctime (), m_rdev (),
49 m_blksize (), m_blocks () { }
50
52 : m_initialized (fs.m_initialized), m_fail (fs.m_fail),
53 m_errmsg (fs.m_errmsg), m_mode (fs.m_mode), m_ino (fs.m_ino),
54 m_dev (fs.m_dev), m_nlink (fs.m_nlink), m_uid (fs.m_uid),
55 m_gid (fs.m_gid), m_size (fs.m_size), m_atime (fs.m_atime),
56 m_mtime (fs.m_mtime), m_ctime (fs.m_ctime), m_rdev (fs.m_rdev),
57 m_blksize (fs.m_blksize), m_blocks (fs.m_blocks) { }
58
59 base_file_stat& operator = (const base_file_stat& fs)
60 {
61 if (this != &fs)
62 {
63 m_initialized = fs.m_initialized;
64 m_fail = fs.m_fail;
65 m_errmsg = fs.m_errmsg;
66 m_mode = fs.m_mode;
67 m_ino = fs.m_ino;
68 m_dev = fs.m_dev;
69 m_nlink = fs.m_nlink;
70 m_uid = fs.m_uid;
71 m_gid = fs.m_gid;
72 m_size = fs.m_size;
73 m_atime = fs.m_atime;
74 m_mtime = fs.m_mtime;
75 m_ctime = fs.m_ctime;
76 m_rdev = fs.m_rdev;
77 m_blksize = fs.m_blksize;
78 m_blocks = fs.m_blocks;
79 }
80
81 return *this;
82 }
83
84 // The minimum difference in file time stamp values.
85 // FIXME: This value should come from the filesystem itself.
86 // How can we get that info?
87 sys::time time_resolution () const
88 {
89 static sys::time resolution (1.0);
90 return resolution;
91 }
92
93 // File status and info. The is_XXX functions will return false for
94 // file_stat objects that are not properly initialized. The others
95 // should all return 0 (or the equivalent, for the given object)
96 // which is likely not meaningful.
97
98 bool is_blk () const;
99 bool is_chr () const;
100 bool is_dir () const;
101 bool is_fifo () const;
102 bool is_lnk () const;
103 bool is_reg () const;
104 bool is_sock () const;
105
106 static bool is_blk (mode_t mode);
107 static bool is_chr (mode_t mode);
108 static bool is_dir (mode_t mode);
109 static bool is_fifo (mode_t mode);
110 static bool is_lnk (mode_t mode);
111 static bool is_reg (mode_t mode);
112 static bool is_sock (mode_t mode);
113
114 static bool have_struct_stat_st_rdev ();
115 static bool have_struct_stat_st_blksize ();
116 static bool have_struct_stat_st_blocks ();
117
118 ino_t ino () const { return m_ino; }
119 dev_t dev () const { return m_dev; }
120
121 nlink_t nlink () const { return m_nlink; }
122
123 uid_t uid () const { return m_uid; }
124 gid_t gid () const { return m_gid; }
125
126 off_t size () const { return m_size; }
127
128 sys::time atime () const { return m_atime; }
129 sys::time mtime () const { return m_mtime; }
130 sys::time ctime () const { return m_ctime; }
131
132 dev_t rdev () const { return m_rdev; }
133
134 long blksize () const { return m_blksize; }
135 long blocks () const { return m_blocks; }
136
137 mode_t mode () const { return m_mode; }
138
139 std::string mode_as_string () const;
140
141 bool ok () const { return m_initialized && ! m_fail; }
142
143 operator bool () const { return ok (); }
144
145 bool exists () const { return ok (); }
146
147 std::string error () const { return ok () ? "" : m_errmsg; }
148
149 // Has the file referenced by this object been modified since TIME?
150 bool is_newer (const sys::time& time) const { return m_mtime > time; }
151
152 // It's nice to be able to hide the file_stat object if we don't
153 // really care about it.
154 static int is_newer (const std::string&, const sys::time&);
155
156protected:
157
158 virtual ~base_file_stat () = default;
159
160 // TRUE means we have already called stat.
162
163 // TRUE means the stat for this file failed.
164 bool m_fail;
165
166 // If a failure occurs, this contains the system error text.
167 std::string m_errmsg;
168
169 // file type and permissions
170 mode_t m_mode;
171
172 // serial number
173 ino_t m_ino;
174
175 // device number
176 dev_t m_dev;
177
178 // number of links
179 nlink_t m_nlink;
180
181 // user ID of owner
182 uid_t m_uid;
183
184 // group ID of owner
185 gid_t m_gid;
186
187 // size in bytes, for regular files
188 off_t m_size;
189
190 // time of last access
191 sys::time m_atime;
192
193 // time of last modification
194 sys::time m_mtime;
195
196 // time of last file status change
197 sys::time m_ctime;
198
199 // device number for special files
200 dev_t m_rdev;
201
202 // best I/O block size
204
205 // number of 512-byte blocks allocated
207};
208
210{
211public:
212
213 // This constructor must remain defined in the cpp file rather than in
214 // the header file (bug #50234).
215 file_stat (const std::string& n = "", bool fl = true);
216
218 : base_file_stat (fs), m_file_name (fs.m_file_name),
219 m_follow_links (fs.m_follow_links) { }
220
222 {
223 if (this != &fs)
224 {
226
227 m_file_name = fs.m_file_name;
228 m_follow_links = fs.m_follow_links;
229 }
230
231 return *this;
232 }
233
234 // This destructor must remain as an empty destructor defined in the
235 // cpp file rather than in the header file (bug #50234).
236 ~file_stat ();
237
238 void get_stats (bool force = false)
239 {
240 if (! m_initialized || force)
241 update_internal (force);
242 }
243
244 void get_stats (const std::string& n, bool force = false)
245 {
246 if (n != m_file_name || ! m_initialized || force)
247 {
248 m_initialized = false;
249
250 m_file_name = n;
251
252 update_internal (force);
253 }
254 }
255
256private:
257
258 // Name of the file.
259 std::string m_file_name;
260
261 // TRUE means follow symbolic links to the ultimate file (stat).
262 // FALSE means get information about the link itself (lstat).
263 bool m_follow_links;
264
265 void update_internal (bool force = false);
266};
267
269{
270public:
271
272 file_fstat () = delete;
273
274 file_fstat (int n) : base_file_stat (), m_fid (n)
275 {
276 update_internal ();
277 }
278
279 OCTAVE_DEFAULT_COPY_MOVE_DELETE (file_fstat)
280
281 void get_stats (bool force = false)
282 {
283 if (! m_initialized || force)
284 update_internal (force);
285 }
286
287 void get_stats (int n, bool force = false)
288 {
289 if (n != m_fid || ! m_initialized || force)
290 {
291 m_initialized = false;
292
293 m_fid = n;
294
295 update_internal (force);
296 }
297 }
298
299private:
300
301 // Open file descriptor.
302 int m_fid;
303
304 void update_internal (bool force = false);
305};
306
307OCTAVE_END_NAMESPACE(sys)
308OCTAVE_END_NAMESPACE(octave)
309
310#endif
sys::time m_atime
Definition file-stat.h:191
std::string m_errmsg
Definition file-stat.h:167
sys::time atime() const
Definition file-stat.h:128
bool exists() const
Definition file-stat.h:145
nlink_t nlink() const
Definition file-stat.h:121
base_file_stat(const base_file_stat &fs)
Definition file-stat.h:51
bool is_newer(const sys::time &time) const
Definition file-stat.h:150
sys::time m_ctime
Definition file-stat.h:197
long blksize() const
Definition file-stat.h:134
std::string error() const
Definition file-stat.h:147
virtual ~base_file_stat()=default
sys::time ctime() const
Definition file-stat.h:130
long blocks() const
Definition file-stat.h:135
base_file_stat & operator=(const base_file_stat &fs)
Definition file-stat.h:59
nlink_t m_nlink
Definition file-stat.h:179
sys::time m_mtime
Definition file-stat.h:194
sys::time mtime() const
Definition file-stat.h:129
sys::time time_resolution() const
Definition file-stat.h:87
bool ok() const
Definition file-stat.h:141
dev_t rdev() const
Definition file-stat.h:132
dev_t dev() const
Definition file-stat.h:119
off_t size() const
Definition file-stat.h:126
ino_t ino() const
Definition file-stat.h:118
uid_t uid() const
Definition file-stat.h:123
mode_t mode() const
Definition file-stat.h:137
gid_t gid() const
Definition file-stat.h:124
file_fstat()=delete
file_fstat(int n)
Definition file-stat.h:274
void get_stats(int n, bool force=false)
Definition file-stat.h:287
file_stat(const file_stat &fs)
Definition file-stat.h:217
void get_stats(bool force=false)
Definition file-stat.h:238
void get_stats(const std::string &n, bool force=false)
Definition file-stat.h:244
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition main.in.cc:55