2926
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2926
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cerrno> |
|
29 #include <cstring> |
|
30 |
|
31 #ifdef HAVE_UNISTD_H |
|
32 #ifdef HAVE_SYS_TYPES_H |
|
33 #include <sys/types.h> |
|
34 #endif |
|
35 #include <unistd.h> |
|
36 #endif |
|
37 |
|
38 #include "file-stat.h" |
|
39 #include "statdefs.h" |
|
40 |
3141
|
41 #if !defined (HAVE_LSTAT) |
|
42 static inline int |
|
43 lstat (const char *name, struct stat *buf) |
|
44 { |
|
45 return stat (name, buf); |
|
46 } |
|
47 #endif |
2926
|
48 |
|
49 // XXX FIXME XXX -- the is_* and mode_as_string functions are only valid |
|
50 // for initialized objects. If called for an object that is not |
|
51 // initialized, they should throw an exception. |
|
52 |
|
53 bool |
|
54 file_stat::is_blk (void) const |
|
55 { |
5476
|
56 return is_blk (fs_mode); |
2926
|
57 } |
|
58 |
|
59 bool |
|
60 file_stat::is_chr (void) const |
|
61 { |
5476
|
62 return is_chr (fs_mode); |
2926
|
63 } |
|
64 |
|
65 bool |
|
66 file_stat::is_dir (void) const |
|
67 { |
5476
|
68 return is_dir (fs_mode); |
|
69 } |
|
70 |
|
71 bool |
|
72 file_stat::is_fifo (void) const |
|
73 { |
|
74 return is_fifo (fs_mode); |
|
75 } |
|
76 |
|
77 bool |
|
78 file_stat::is_lnk (void) const |
|
79 { |
|
80 return is_lnk (fs_mode); |
|
81 } |
|
82 |
|
83 bool |
|
84 file_stat::is_reg (void) const |
|
85 { |
|
86 return is_reg (fs_mode); |
|
87 } |
|
88 |
|
89 bool |
|
90 file_stat::is_sock (void) const |
|
91 { |
|
92 return is_sock (fs_mode); |
|
93 } |
|
94 |
|
95 bool |
|
96 file_stat::is_blk (mode_t mode) |
|
97 { |
|
98 #ifdef S_ISBLK |
|
99 return S_ISBLK (mode); |
2926
|
100 #else |
|
101 return false; |
|
102 #endif |
|
103 } |
|
104 |
|
105 bool |
5476
|
106 file_stat::is_chr (mode_t mode) |
|
107 { |
|
108 #ifdef S_ISCHR |
|
109 return S_ISCHR (mode); |
|
110 #else |
|
111 return false; |
|
112 #endif |
|
113 } |
|
114 |
|
115 bool |
|
116 file_stat::is_dir (mode_t mode) |
2926
|
117 { |
5476
|
118 #ifdef S_ISDIR |
|
119 return S_ISDIR (mode); |
2926
|
120 #else |
|
121 return false; |
|
122 #endif |
|
123 } |
|
124 |
|
125 bool |
5476
|
126 file_stat::is_fifo (mode_t mode) |
2926
|
127 { |
5476
|
128 #ifdef S_ISFIFO |
|
129 return S_ISFIFO (mode); |
2926
|
130 #else |
|
131 return false; |
|
132 #endif |
|
133 } |
|
134 |
|
135 bool |
5476
|
136 file_stat::is_lnk (mode_t mode) |
2926
|
137 { |
5476
|
138 #ifdef S_ISLNK |
|
139 return S_ISLNK (mode); |
2926
|
140 #else |
|
141 return false; |
|
142 #endif |
|
143 } |
|
144 |
|
145 bool |
5476
|
146 file_stat::is_reg (mode_t mode) |
|
147 { |
|
148 #ifdef S_ISREG |
|
149 return S_ISREG (mode); |
|
150 #else |
|
151 return false; |
|
152 #endif |
|
153 } |
|
154 |
|
155 bool |
|
156 file_stat::is_sock (mode_t mode) |
2926
|
157 { |
|
158 #ifdef S_ISSOCK |
5476
|
159 return S_ISSOCK (mode); |
2926
|
160 #else |
|
161 return false; |
|
162 #endif |
|
163 } |
|
164 |
3504
|
165 extern "C" void mode_string (unsigned short, char *); |
2926
|
166 |
3504
|
167 std::string |
2926
|
168 file_stat::mode_as_string (void) const |
|
169 { |
|
170 char buf[11]; |
|
171 |
|
172 mode_string (fs_mode, buf); |
|
173 |
|
174 buf[10] = '\0'; |
|
175 |
3504
|
176 return std::string (buf); |
2926
|
177 } |
|
178 |
|
179 // Has FILE been modified since TIME? Returns 1 for yes, 0 for no, |
|
180 // and -1 for any error. |
|
181 |
|
182 int |
3504
|
183 file_stat::is_newer (const std::string& file, const octave_time& time) |
2926
|
184 { |
|
185 file_stat fs (file); |
|
186 |
|
187 return fs ? fs.is_newer (time) : -1; |
|
188 } |
|
189 |
|
190 // Private stuff: |
|
191 |
|
192 void |
|
193 file_stat::update_internal (bool force) |
|
194 { |
|
195 if (! initialized || force) |
|
196 { |
|
197 initialized = false; |
|
198 fail = false; |
|
199 |
|
200 const char *cname = file_name.c_str (); |
|
201 |
|
202 struct stat buf; |
|
203 |
3141
|
204 int status = follow_links ? stat (cname, &buf) : lstat (cname, &buf); |
2926
|
205 |
|
206 if (status < 0) |
|
207 { |
3504
|
208 using namespace std; |
|
209 |
2926
|
210 fail = true; |
|
211 errmsg = strerror (errno); |
|
212 } |
|
213 else |
|
214 { |
|
215 fs_mode = buf.st_mode; |
|
216 fs_ino = buf.st_ino; |
|
217 fs_dev = buf.st_dev; |
|
218 fs_nlink = buf.st_nlink; |
|
219 fs_uid = buf.st_uid; |
|
220 fs_gid = buf.st_gid; |
|
221 fs_size = buf.st_size; |
|
222 fs_atime = buf.st_atime; |
|
223 fs_mtime = buf.st_mtime; |
|
224 fs_ctime = buf.st_ctime; |
|
225 |
3887
|
226 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
2926
|
227 fs_rdev = buf.st_rdev; |
|
228 #endif |
|
229 |
3887
|
230 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
2926
|
231 fs_blksize = buf.st_blksize; |
|
232 #endif |
|
233 |
3887
|
234 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
2926
|
235 fs_blocks = buf.st_blocks; |
|
236 #endif |
|
237 } |
|
238 |
|
239 initialized = true; |
|
240 } |
|
241 } |
|
242 |
|
243 void |
|
244 file_stat::copy (const file_stat& fs) |
|
245 { |
|
246 file_name = fs.file_name; |
|
247 follow_links = fs.follow_links; |
|
248 initialized = fs.initialized; |
|
249 fail = fs.fail; |
|
250 errmsg = fs.errmsg; |
|
251 fs_mode = fs.fs_mode; |
|
252 fs_ino = fs.fs_ino; |
|
253 fs_dev = fs.fs_dev; |
|
254 fs_nlink = fs.fs_nlink; |
|
255 fs_uid = fs.fs_uid; |
|
256 fs_gid = fs.fs_gid; |
|
257 fs_size = fs.fs_size; |
|
258 fs_atime = fs.fs_atime; |
|
259 fs_mtime = fs.fs_mtime; |
|
260 fs_ctime = fs.fs_ctime; |
|
261 |
3887
|
262 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
2926
|
263 fs_rdev = fs.fs_rdev; |
|
264 #endif |
|
265 |
3887
|
266 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
2926
|
267 fs_blksize = fs.fs_blksize; |
|
268 #endif |
|
269 |
3887
|
270 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
2926
|
271 fs_blocks = fs.fs_blocks; |
|
272 #endif |
|
273 } |
|
274 |
|
275 /* |
|
276 ;;; Local Variables: *** |
|
277 ;;; mode: C++ *** |
|
278 ;;; End: *** |
|
279 */ |