GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
file-stat.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <ctime>
28 #include <cerrno>
29 #include <cstring>
30 
31 #include "file-ops.h"
32 #include "file-stat.h"
33 #include "stat-wrappers.h"
34 #include "strmode-wrapper.h"
35 
36 namespace octave
37 {
38  namespace sys
39  {
40  // FIXME: the is_* and mode_as_string functions are only valid
41  // for initialized objects. If called for an object that is not
42  // initialized, they should throw an exception.
43 
44  bool
46  {
47  return exists () && is_blk (m_mode);
48  }
49 
50  bool
52  {
53  return exists () && is_chr (m_mode);
54  }
55 
56  bool
58  {
59  return exists () && is_dir (m_mode);
60  }
61 
62  bool
64  {
65  return exists () && is_fifo (m_mode);
66  }
67 
68  bool
70  {
71  return exists () && is_lnk (m_mode);
72  }
73 
74  bool
76  {
77  return exists () && is_reg (m_mode);
78  }
79 
80  bool
82  {
83  return exists () && is_sock (m_mode);
84  }
85 
86  bool
88  {
89  return octave_is_blk_wrapper (mode);
90  }
91 
92  bool
94  {
95  return octave_is_chr_wrapper (mode);
96  }
97 
98  bool
100  {
101  return octave_is_dir_wrapper (mode);
102  }
103 
104  bool
106  {
107  return octave_is_fifo_wrapper (mode);
108  }
109 
110  bool
112  {
113  return octave_is_lnk_wrapper (mode);
114  }
115 
116  bool
118  {
119  return octave_is_reg_wrapper (mode);
120  }
121 
122  bool
124  {
125  return octave_is_sock_wrapper (mode);
126  }
127 
128  bool
130  {
132  }
133 
134  bool
136  {
138  }
139 
140  bool
142  {
144  }
145 
148  {
149  char buf[12];
150 
152 
153  return std::string (buf);
154  }
155 
156  // Has FILE been modified since TIME? Returns 1 for yes, 0 for no,
157  // and -1 for any error.
158 
159  int
161  const octave::sys::time& time)
162  {
163  file_stat fs (file);
164 
165  return fs ? fs.is_newer (time) : -1;
166  }
167 
168  // Private stuff:
169 
170  file_stat::file_stat (const std::string& n, bool fl)
171  : base_file_stat (), file_name (n), follow_links (fl)
172  {
173  if (! file_name.empty ())
174  update_internal ();
175  }
176 
177  inline file_stat::~file_stat () { }
178 
179  void
181  {
182  if (! initialized || force)
183  {
184  initialized = false;
185  fail = false;
186 
188 
189 #if defined (__WIN32__)
190  // Remove trailing slash.
191  if (octave::sys::file_ops::is_dir_sep (full_file_name[full_file_name.length () - 1])
192  && full_file_name.length () != 1
193  && ! (full_file_name.length () == 3 && full_file_name[1] == ':'))
194  full_file_name.resize (full_file_name.length () - 1);
195 #endif
196 
197  const char *cname = full_file_name.c_str ();
198 
199  time_t sys_atime, sys_mtime, sys_ctime;
200 
201  int status
202  = (follow_links
203  ? octave_stat_wrapper (cname, &m_mode, &m_ino, &m_dev,
204  &m_nlink, &m_uid, &m_gid, &m_size,
205  &sys_atime, &sys_mtime, &sys_ctime,
207  : octave_lstat_wrapper (cname, &m_mode, &m_ino, &m_dev,
208  &m_nlink, &m_uid, &m_gid, &m_size,
209  &sys_atime, &sys_mtime, &sys_ctime,
210  &m_rdev, &m_blksize, &m_blocks));
211 
212  if (status < 0)
213  {
214  fail = true;
215  errmsg = std::strerror (errno);
216  }
217  else
218  {
219  m_atime = octave::sys::time (sys_atime);
220  m_mtime = octave::sys::time (sys_mtime);
221  m_ctime = octave::sys::time (sys_ctime);
222  }
223 
224  initialized = true;
225  }
226  }
227 
228  void
230  {
231  if (! initialized || force)
232  {
233  initialized = false;
234  fail = false;
235 
236  time_t sys_atime, sys_mtime, sys_ctime;
237 
238  int status
240  &m_nlink, &m_uid, &m_gid, &m_size,
241  &sys_atime, &sys_mtime, &sys_ctime,
242  &m_rdev, &m_blksize, &m_blocks);
243 
244  if (status < 0)
245  {
246  fail = true;
247  errmsg = std::strerror (errno);
248  }
249  else
250  {
251  m_atime = octave::sys::time (sys_atime);
252  m_mtime = octave::sys::time (sys_mtime);
253  m_ctime = octave::sys::time (sys_ctime);
254  }
255 
256  initialized = true;
257  }
258  }
259  }
260 }
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:120
bool octave_is_fifo_wrapper(mode_t mode)
bool is_lnk(void) const
Definition: file-stat.cc:69
file_stat(const std::string &n="", bool fl=true)
Definition: file-stat.cc:170
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6386
void update_internal(bool force=false)
Definition: file-stat.cc:180
bool is_fifo(void) const
Definition: file-stat.cc:63
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)
Definition: stat-wrappers.c:93
bool is_newer(const octave::sys::time &time) const
Definition: file-stat.h:149
static std::string tilde_expand(const std::string &)
Definition: file-ops.cc:301
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_dir_wrapper(mode_t mode)
bool is_reg(void) const
Definition: file-stat.cc:75
bool octave_is_lnk_wrapper(mode_t mode)
bool octave_is_reg_wrapper(mode_t mode)
octave::sys::time m_mtime
Definition: file-stat.h:193
bool is_dir(void) const
Definition: file-stat.cc:57
bool octave_is_chr_wrapper(mode_t mode)
bool octave_is_blk_wrapper(mode_t mode)
static bool have_struct_stat_st_blocks(void)
Definition: file-stat.cc:141
static bool have_struct_stat_st_rdev(void)
Definition: file-stat.cc:129
bool is_sock(void) const
Definition: file-stat.cc:81
bool exists(void) const
Definition: file-stat.h:144
bool octave_have_struct_stat_st_blksize(void)
bool octave_is_sock_wrapper(mode_t mode)
bool octave_have_struct_stat_st_rdev(void)
bool is_chr(void) const
Definition: file-stat.cc:51
std::string file_name
Definition: file-stat.h:260
octave::sys::time m_atime
Definition: file-stat.h:190
octave::sys::file_stat fs(filename)
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)
void update_internal(bool force=false)
Definition: file-stat.cc:229
void octave_strmode_wrapper(mode_t mode, char *buffer)
bool is_blk(void) const
Definition: file-stat.cc:45
static bool have_struct_stat_st_blksize(void)
Definition: file-stat.cc:135
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
static bool is_dir_sep(char c)
Definition: file-ops.h:90
octave::sys::time m_ctime
Definition: file-stat.h:196
std::string mode_as_string(void) const
Definition: file-stat.cc:147