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.cc
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 (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <ctime>
31#include <cerrno>
32#include <cstring>
33
34#include "file-ops.h"
35#include "file-stat.h"
36#include "stat-wrappers.h"
37#include "strmode-wrapper.h"
38
39#if defined (OCTAVE_USE_WINDOWS_API)
40# include "lo-regexp.h"
41# include "oct-env.h"
42#endif
43
45
47
48// FIXME: the is_* and mode_as_string functions are only valid
49// for initialized objects. If called for an object that is not
50// initialized, they should throw an exception.
51
52bool
54{
55 return exists () && is_blk (m_mode);
56}
57
58bool
60{
61 return exists () && is_chr (m_mode);
62}
63
64bool
66{
67 return exists () && is_dir (m_mode);
68}
69
70bool
72{
73 return exists () && is_fifo (m_mode);
74}
75
76bool
78{
79 return exists () && is_lnk (m_mode);
80}
81
82bool
84{
85 return exists () && is_reg (m_mode);
86}
87
88bool
90{
91 return exists () && is_sock (m_mode);
92}
93
94bool
96{
98}
99
100bool
102{
104}
105
106bool
108{
110}
111
112bool
114{
116}
117
118bool
120{
122}
123
124bool
126{
128}
129
130bool
132{
134}
135
136bool
138{
139 return ::octave_have_struct_stat_st_rdev ();
140}
141
142bool
147
148bool
153
154std::string
156{
157 char buf[12];
158
160
161 return std::string (buf);
162}
163
164// Has FILE been modified since TIME? Returns 1 for yes, 0 for no,
165// and -1 for any error.
166
167int
168base_file_stat::is_newer (const std::string& file,
169 const sys::time& time)
170{
171 file_stat fs (file);
172
173 return fs ? fs.is_newer (time) : -1;
174}
175
176// Private stuff:
177
178file_stat::file_stat (const std::string& n, bool fl)
179 : base_file_stat (), m_file_name (n), m_follow_links (fl)
180{
181 if (! m_file_name.empty ())
182 update_internal ();
183}
184
186
187void
188file_stat::update_internal (bool force)
189{
190 if (! m_initialized || force)
191 {
192 m_initialized = false;
193 m_fail = false;
194
195 std::string full_file_name = sys::file_ops::tilde_expand (m_file_name);
196
197#if defined (OCTAVE_USE_WINDOWS_API)
198 full_file_name = sys::env::make_absolute (full_file_name);
199
200 // Remove trailing slashes
201 while (full_file_name.length () > 1
202 && sys::file_ops::is_dir_sep (full_file_name.back ()))
203 full_file_name.pop_back ();
204
205 // If path is a root (like "C:" or "\\SERVER\share"), add a
206 // trailing backslash.
207 // FIXME: This pattern does not match all possible UNC roots:
208 // https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/62e862f4-2a51-452e-8eeb-dc4ff5ee33cc
209 static const regexp pat (R"(^\\\\[\w.-]+\\[\w\$-]+$)");
210 if ((full_file_name.length () == 2 && full_file_name[1] == ':')
211 || (full_file_name.length () > 4 && full_file_name[0] == '\\'
212 && full_file_name[1] == '\\' && pat.is_match (full_file_name)))
213 full_file_name.push_back ('\\');
214#endif
215
216 const char *cname = full_file_name.c_str ();
217
218 time_t sys_atime, sys_mtime, sys_ctime;
219 long int atime_nsec, mtime_nsec, ctime_nsec;
220
221 int status
222 = (m_follow_links
223 ? octave_stat_wrapper (cname, &m_mode, &m_ino, &m_dev,
224 &m_nlink, &m_uid, &m_gid, &m_size,
225 &sys_atime, &atime_nsec, &sys_mtime,
226 &mtime_nsec, &sys_ctime, &ctime_nsec,
228 : octave_lstat_wrapper (cname, &m_mode, &m_ino, &m_dev,
229 &m_nlink, &m_uid, &m_gid, &m_size,
230 &sys_atime, &atime_nsec, &sys_mtime,
231 &mtime_nsec, &sys_ctime, &ctime_nsec,
233
234 if (status < 0)
235 {
236 m_fail = true;
237 m_errmsg = std::strerror (errno);
238 }
239 else
240 {
241 m_atime = sys::time (static_cast<OCTAVE_TIME_T> (sys_atime),
242 atime_nsec/1000);
243 m_mtime = sys::time (static_cast<OCTAVE_TIME_T> (sys_mtime),
244 mtime_nsec/1000);
245 m_ctime = sys::time (static_cast<OCTAVE_TIME_T> (sys_ctime),
246 ctime_nsec/1000);
247 }
248
249 m_initialized = true;
250 }
251}
252
253void
254file_fstat::update_internal (bool force)
255{
256 if (! m_initialized || force)
257 {
258 m_initialized = false;
259 m_fail = false;
260
261 time_t sys_atime, sys_mtime, sys_ctime;
262 long int atime_nsec, mtime_nsec, ctime_nsec;
263
264 int status
265 = octave_fstat_wrapper (m_fid, &m_mode, &m_ino, &m_dev,
266 &m_nlink, &m_uid, &m_gid, &m_size,
267 &sys_atime, &atime_nsec, &sys_mtime,
268 &mtime_nsec, &sys_ctime, &ctime_nsec,
270
271 if (status < 0)
272 {
273 m_fail = true;
274 m_errmsg = std::strerror (errno);
275 }
276 else
277 {
278 m_atime = sys::time (static_cast<OCTAVE_TIME_T> (sys_atime),
279 atime_nsec/1000);
280 m_mtime = sys::time (static_cast<OCTAVE_TIME_T> (sys_mtime),
281 mtime_nsec/1000);
282 m_ctime = sys::time (static_cast<OCTAVE_TIME_T> (sys_ctime),
283 ctime_nsec/1000);
284 }
285
286 m_initialized = true;
287 }
288}
289
290OCTAVE_END_NAMESPACE(sys)
291OCTAVE_END_NAMESPACE(octave)
bool is_blk() const
Definition file-stat.cc:53
sys::time m_atime
Definition file-stat.h:191
bool is_fifo() const
Definition file-stat.cc:71
std::string m_errmsg
Definition file-stat.h:167
bool is_dir() const
Definition file-stat.cc:65
bool exists() const
Definition file-stat.h:145
bool is_newer(const sys::time &time) const
Definition file-stat.h:150
sys::time m_ctime
Definition file-stat.h:197
static bool have_struct_stat_st_blocks()
Definition file-stat.cc:149
static bool have_struct_stat_st_blksize()
Definition file-stat.cc:143
bool is_sock() const
Definition file-stat.cc:89
nlink_t m_nlink
Definition file-stat.h:179
bool is_reg() const
Definition file-stat.cc:83
sys::time m_mtime
Definition file-stat.h:194
std::string mode_as_string() const
Definition file-stat.cc:155
bool is_lnk() const
Definition file-stat.cc:77
bool is_chr() const
Definition file-stat.cc:59
static bool have_struct_stat_st_rdev()
Definition file-stat.cc:137
mode_t mode() const
Definition file-stat.h:137
file_stat(const std::string &n="", bool fl=true)
Definition file-stat.cc:178
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
bool octave_is_lnk_wrapper(mode_t mode)
int octave_lstat_wrapper(const char *lname, mode_t *mode, ino_t *ino, dev_t *dev, nlink_t *nlink, uid_t *uid, gid_t *gid, off_t *size, time_t *atime, long int *atime_nsec, time_t *mtime, long int *mtime_nsec, time_t *ctime, long int *ctime_nsec, dev_t *rdev, long *blksize, long *blocks)
bool octave_is_chr_wrapper(mode_t mode)
bool octave_is_fifo_wrapper(mode_t mode)
bool octave_have_struct_stat_st_blocks(void)
bool octave_is_blk_wrapper(mode_t mode)
bool octave_is_dir_wrapper(mode_t mode)
bool octave_is_reg_wrapper(mode_t mode)
int octave_stat_wrapper(const char *fname, mode_t *mode, ino_t *ino, dev_t *dev, nlink_t *nlink, uid_t *uid, gid_t *gid, off_t *size, time_t *atime, long int *atime_nsec, time_t *mtime, long int *mtime_nsec, time_t *ctime, long int *ctime_nsec, dev_t *rdev, long *blksize, long *blocks)
int octave_fstat_wrapper(int fid, mode_t *mode, ino_t *ino, dev_t *dev, nlink_t *nlink, uid_t *uid, gid_t *gid, off_t *size, time_t *atime, long int *atime_nsec, time_t *mtime, long int *mtime_nsec, time_t *ctime, long int *ctime_nsec, dev_t *rdev, long *blksize, long *blocks)
bool octave_is_sock_wrapper(mode_t mode)
bool octave_have_struct_stat_st_blksize(void)
void octave_strmode_wrapper(mode_t mode, char *buffer)