GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
file-stat.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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 
44 namespace octave
45 {
46  namespace sys
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 
52  bool
54  {
55  return exists () && is_blk (m_mode);
56  }
57 
58  bool
60  {
61  return exists () && is_chr (m_mode);
62  }
63 
64  bool
66  {
67  return exists () && is_dir (m_mode);
68  }
69 
70  bool
72  {
73  return exists () && is_fifo (m_mode);
74  }
75 
76  bool
78  {
79  return exists () && is_lnk (m_mode);
80  }
81 
82  bool
84  {
85  return exists () && is_reg (m_mode);
86  }
87 
88  bool
90  {
91  return exists () && is_sock (m_mode);
92  }
93 
94  bool
95  base_file_stat::is_blk (mode_t mode)
96  {
97  return octave_is_blk_wrapper (mode);
98  }
99 
100  bool
102  {
103  return octave_is_chr_wrapper (mode);
104  }
105 
106  bool
108  {
109  return octave_is_dir_wrapper (mode);
110  }
111 
112  bool
114  {
115  return octave_is_fifo_wrapper (mode);
116  }
117 
118  bool
120  {
121  return octave_is_lnk_wrapper (mode);
122  }
123 
124  bool
126  {
127  return octave_is_reg_wrapper (mode);
128  }
129 
130  bool
132  {
133  return octave_is_sock_wrapper (mode);
134  }
135 
136  bool
138  {
140  }
141 
142  bool
144  {
146  }
147 
148  bool
150  {
152  }
153 
154  std::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 
167  int
168  base_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 
178  file_stat::file_stat (const std::string& n, bool fl)
179  : base_file_stat (), file_name (n), follow_links (fl)
180  {
181  if (! file_name.empty ())
182  update_internal ();
183  }
184 
186 
187  void
189  {
190  if (! initialized || force)
191  {
192  initialized = false;
193  fail = false;
194 
195  std::string full_file_name = sys::file_ops::tilde_expand (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 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 
220  int status
221  = (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, &sys_mtime, &sys_ctime,
226  : octave_lstat_wrapper (cname, &m_mode, &m_ino, &m_dev,
227  &m_nlink, &m_uid, &m_gid, &m_size,
228  &sys_atime, &sys_mtime, &sys_ctime,
229  &m_rdev, &m_blksize, &m_blocks));
230 
231  if (status < 0)
232  {
233  fail = true;
234  errmsg = std::strerror (errno);
235  }
236  else
237  {
238  m_atime = sys::time (sys_atime);
239  m_mtime = sys::time (sys_mtime);
240  m_ctime = sys::time (sys_ctime);
241  }
242 
243  initialized = true;
244  }
245  }
246 
247  void
249  {
250  if (! initialized || force)
251  {
252  initialized = false;
253  fail = false;
254 
255  time_t sys_atime, sys_mtime, sys_ctime;
256 
257  int status
259  &m_nlink, &m_uid, &m_gid, &m_size,
260  &sys_atime, &sys_mtime, &sys_ctime,
261  &m_rdev, &m_blksize, &m_blocks);
262 
263  if (status < 0)
264  {
265  fail = true;
266  errmsg = std::strerror (errno);
267  }
268  else
269  {
270  m_atime = sys::time (sys_atime);
271  m_mtime = sys::time (sys_mtime);
272  m_ctime = sys::time (sys_ctime);
273  }
274 
275  initialized = true;
276  }
277  }
278  }
279 }
bool is_match(const std::string &buffer)
Definition: lo-regexp.cc:440
bool is_lnk(void) const
Definition: file-stat.cc:77
bool is_sock(void) const
Definition: file-stat.cc:89
bool is_blk(void) const
Definition: file-stat.cc:53
bool is_fifo(void) const
Definition: file-stat.cc:71
mode_t mode(void) const
Definition: file-stat.h:139
std::string mode_as_string(void) const
Definition: file-stat.cc:155
static bool have_struct_stat_st_rdev(void)
Definition: file-stat.cc:137
bool is_reg(void) const
Definition: file-stat.cc:83
bool is_dir(void) const
Definition: file-stat.cc:65
bool is_newer(const sys::time &time) const
Definition: file-stat.h:152
static bool have_struct_stat_st_blocks(void)
Definition: file-stat.cc:149
static bool have_struct_stat_st_blksize(void)
Definition: file-stat.cc:143
bool exists(void) const
Definition: file-stat.h:147
bool is_chr(void) const
Definition: file-stat.cc:59
static std::string make_absolute(const std::string &s, const std::string &dot_path=get_current_directory())
Definition: oct-env.cc:133
void update_internal(bool force=false)
Definition: file-stat.cc:248
std::string file_name
Definition: file-stat.h:263
void update_internal(bool force=false)
Definition: file-stat.cc:188
file_stat(const std::string &n="", bool fl=true)
Definition: file-stat.cc:178
octave_idx_type n
Definition: mx-inlines.cc:753
bool is_dir_sep(char c)
Definition: file-ops.cc:280
std::string tilde_expand(const std::string &name)
Definition: file-ops.cc:286
bool octave_is_lnk_wrapper(mode_t mode)
bool octave_is_chr_wrapper(mode_t mode)
bool octave_is_fifo_wrapper(mode_t mode)
bool octave_have_struct_stat_st_rdev(void)
bool octave_have_struct_stat_st_blocks(void)
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, time_t *mtime, time_t *ctime, dev_t *rdev, long *blksize, long *blocks)
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_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, time_t *mtime, time_t *ctime, dev_t *rdev, long *blksize, long *blocks)
bool octave_is_sock_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, time_t *mtime, time_t *ctime, dev_t *rdev, long *blksize, long *blocks)
bool octave_have_struct_stat_st_blksize(void)
void octave_strmode_wrapper(mode_t mode, char *buffer)