GNU Octave  3.8.0
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-2013 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <cerrno>
28 #include <cstring>
29 
30 #include <sys/types.h>
31 #include <unistd.h>
32 
33 #include "filemode.h"
34 
35 #include "file-ops.h"
36 #include "file-stat.h"
37 #include "statdefs.h"
38 
39 // FIXME: the is_* and mode_as_string functions are only valid
40 // for initialized objects. If called for an object that is not
41 // initialized, they should throw an exception.
42 
43 bool
45 {
46  return exists () && is_blk (fs_mode);
47 }
48 
49 bool
51 {
52  return exists () && is_chr (fs_mode);
53 }
54 
55 bool
57 {
58  return exists () && is_dir (fs_mode);
59 }
60 
61 bool
63 {
64  return exists () && is_fifo (fs_mode);
65 }
66 
67 bool
69 {
70  return exists () && is_lnk (fs_mode);
71 }
72 
73 bool
75 {
76  return exists () && is_reg (fs_mode);
77 }
78 
79 bool
81 {
82  return exists () && is_sock (fs_mode);
83 }
84 
85 bool
87 {
88 #ifdef S_ISBLK
89  return S_ISBLK (mode);
90 #else
91  return false;
92 #endif
93 }
94 
95 bool
97 {
98 #ifdef S_ISCHR
99  return S_ISCHR (mode);
100 #else
101  return false;
102 #endif
103 }
104 
105 bool
107 {
108 #ifdef S_ISDIR
109  return S_ISDIR (mode);
110 #else
111  return false;
112 #endif
113 }
114 
115 bool
117 {
118 #ifdef S_ISFIFO
119  return S_ISFIFO (mode);
120 #else
121  return false;
122 #endif
123 }
124 
125 bool
127 {
128 #ifdef S_ISLNK
129  return S_ISLNK (mode);
130 #else
131  return false;
132 #endif
133 }
134 
135 bool
137 {
138 #ifdef S_ISREG
139  return S_ISREG (mode);
140 #else
141  return false;
142 #endif
143 }
144 
145 bool
147 {
148 #ifdef S_ISSOCK
149  return S_ISSOCK (mode);
150 #else
151  return false;
152 #endif
153 }
154 
155 std::string
157 {
158  char buf[12];
159 
160  strmode (fs_mode, buf);
161 
162  return std::string (buf);
163 }
164 
165 // Has FILE been modified since TIME? Returns 1 for yes, 0 for no,
166 // and -1 for any error.
167 
168 int
169 base_file_stat::is_newer (const std::string& file, const octave_time& time)
170 {
171  file_stat fs (file);
172 
173  return fs ? fs.is_newer (time) : -1;
174 }
175 
176 // Private stuff:
177 
178 void
180 {
181  if (! initialized || force)
182  {
183  initialized = false;
184  fail = false;
185 
186  std::string full_file_name = file_ops::tilde_expand (file_name);
187 
188 #if defined (__WIN32__)
189  // Remove trailing slash.
190  if (file_ops::is_dir_sep (full_file_name[full_file_name.length () - 1])
191  && full_file_name.length () != 1
192  && ! (full_file_name.length () == 3 && full_file_name[1] == ':'))
193  full_file_name.resize (full_file_name.length () - 1);
194 #endif
195 
196  const char *cname = full_file_name.c_str ();
197 
198  struct stat buf;
199 
200  int status = follow_links
201  ? stat (cname, &buf) : gnulib::lstat (cname, &buf);
202 
203  if (status < 0)
204  {
205  fail = true;
206  errmsg = gnulib::strerror (errno);
207  }
208  else
209  {
210  fs_mode = buf.st_mode;
211  fs_ino = buf.st_ino;
212  fs_dev = buf.st_dev;
213  fs_nlink = buf.st_nlink;
214  fs_uid = buf.st_uid;
215  fs_gid = buf.st_gid;
216  fs_size = buf.st_size;
217  fs_atime = buf.st_atime;
218  fs_mtime = buf.st_mtime;
219  fs_ctime = buf.st_ctime;
220 
221 #if defined (HAVE_STRUCT_STAT_ST_RDEV)
222  fs_rdev = buf.st_rdev;
223 #endif
224 
225 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
226  fs_blksize = buf.st_blksize;
227 #endif
228 
229 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
230  fs_blocks = buf.st_blocks;
231 #endif
232  }
233 
234  initialized = true;
235  }
236 }
237 
238 void
240 {
241  if (! initialized || force)
242  {
243  initialized = false;
244  fail = false;
245 
246  struct stat buf;
247 
248  int status = gnulib::fstat (fid, &buf);
249 
250  if (status < 0)
251  {
252  fail = true;
253  errmsg = gnulib::strerror (errno);
254  }
255  else
256  {
257  fs_mode = buf.st_mode;
258  fs_ino = buf.st_ino;
259  fs_dev = buf.st_dev;
260  fs_nlink = buf.st_nlink;
261  fs_uid = buf.st_uid;
262  fs_gid = buf.st_gid;
263  fs_size = buf.st_size;
264  fs_atime = buf.st_atime;
265  fs_mtime = buf.st_mtime;
266  fs_ctime = buf.st_ctime;
267 
268 #if defined (HAVE_STRUCT_STAT_ST_RDEV)
269  fs_rdev = buf.st_rdev;
270 #endif
271 
272 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE)
273  fs_blksize = buf.st_blksize;
274 #endif
275 
276 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
277  fs_blocks = buf.st_blocks;
278 #endif
279  }
280 
281  initialized = true;
282  }
283 }