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