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 { |
|
56 #ifdef S_ISBLK |
|
57 return S_ISBLK (fs_mode); |
|
58 #else |
|
59 return false; |
|
60 #endif |
|
61 } |
|
62 |
|
63 bool |
|
64 file_stat::is_chr (void) const |
|
65 { |
|
66 #ifdef S_ISCHR |
|
67 return S_ISCHR (fs_mode); |
|
68 #else |
|
69 return false; |
|
70 #endif |
|
71 } |
|
72 |
|
73 bool |
|
74 file_stat::is_dir (void) const |
|
75 { |
|
76 #ifdef S_ISDIR |
|
77 return S_ISDIR (fs_mode); |
|
78 #else |
|
79 return false; |
|
80 #endif |
|
81 } |
|
82 |
|
83 bool |
|
84 file_stat::is_fifo (void) const |
|
85 { |
|
86 #ifdef S_ISFIFO |
|
87 return S_ISFIFO (fs_mode); |
|
88 #else |
|
89 return false; |
|
90 #endif |
|
91 } |
|
92 |
|
93 bool |
|
94 file_stat::is_lnk (void) const |
|
95 { |
|
96 #ifdef S_ISLNK |
|
97 return S_ISLNK (fs_mode); |
|
98 #else |
|
99 return false; |
|
100 #endif |
|
101 } |
|
102 |
|
103 bool |
|
104 file_stat::is_reg (void) const |
|
105 { |
|
106 #ifdef S_ISREG |
|
107 return S_ISREG (fs_mode); |
|
108 #else |
|
109 return false; |
|
110 #endif |
|
111 } |
|
112 |
|
113 bool |
|
114 file_stat::is_sock (void) const |
|
115 { |
|
116 #ifdef S_ISSOCK |
|
117 return S_ISSOCK (fs_mode); |
|
118 #else |
|
119 return false; |
|
120 #endif |
|
121 } |
|
122 |
3504
|
123 extern "C" void mode_string (unsigned short, char *); |
2926
|
124 |
3504
|
125 std::string |
2926
|
126 file_stat::mode_as_string (void) const |
|
127 { |
|
128 char buf[11]; |
|
129 |
|
130 mode_string (fs_mode, buf); |
|
131 |
|
132 buf[10] = '\0'; |
|
133 |
3504
|
134 return std::string (buf); |
2926
|
135 } |
|
136 |
|
137 // Has FILE been modified since TIME? Returns 1 for yes, 0 for no, |
|
138 // and -1 for any error. |
|
139 |
|
140 int |
3504
|
141 file_stat::is_newer (const std::string& file, const octave_time& time) |
2926
|
142 { |
|
143 file_stat fs (file); |
|
144 |
|
145 return fs ? fs.is_newer (time) : -1; |
|
146 } |
|
147 |
|
148 // Private stuff: |
|
149 |
|
150 void |
|
151 file_stat::update_internal (bool force) |
|
152 { |
|
153 if (! initialized || force) |
|
154 { |
|
155 initialized = false; |
|
156 fail = false; |
|
157 |
|
158 const char *cname = file_name.c_str (); |
|
159 |
|
160 struct stat buf; |
|
161 |
3141
|
162 int status = follow_links ? stat (cname, &buf) : lstat (cname, &buf); |
2926
|
163 |
|
164 if (status < 0) |
|
165 { |
3504
|
166 using namespace std; |
|
167 |
2926
|
168 fail = true; |
|
169 errmsg = strerror (errno); |
|
170 } |
|
171 else |
|
172 { |
|
173 fs_mode = buf.st_mode; |
|
174 fs_ino = buf.st_ino; |
|
175 fs_dev = buf.st_dev; |
|
176 fs_nlink = buf.st_nlink; |
|
177 fs_uid = buf.st_uid; |
|
178 fs_gid = buf.st_gid; |
|
179 fs_size = buf.st_size; |
|
180 fs_atime = buf.st_atime; |
|
181 fs_mtime = buf.st_mtime; |
|
182 fs_ctime = buf.st_ctime; |
|
183 |
3887
|
184 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
2926
|
185 fs_rdev = buf.st_rdev; |
|
186 #endif |
|
187 |
3887
|
188 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
2926
|
189 fs_blksize = buf.st_blksize; |
|
190 #endif |
|
191 |
3887
|
192 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
2926
|
193 fs_blocks = buf.st_blocks; |
|
194 #endif |
|
195 } |
|
196 |
|
197 initialized = true; |
|
198 } |
|
199 } |
|
200 |
|
201 void |
|
202 file_stat::copy (const file_stat& fs) |
|
203 { |
|
204 file_name = fs.file_name; |
|
205 follow_links = fs.follow_links; |
|
206 initialized = fs.initialized; |
|
207 fail = fs.fail; |
|
208 errmsg = fs.errmsg; |
|
209 fs_mode = fs.fs_mode; |
|
210 fs_ino = fs.fs_ino; |
|
211 fs_dev = fs.fs_dev; |
|
212 fs_nlink = fs.fs_nlink; |
|
213 fs_uid = fs.fs_uid; |
|
214 fs_gid = fs.fs_gid; |
|
215 fs_size = fs.fs_size; |
|
216 fs_atime = fs.fs_atime; |
|
217 fs_mtime = fs.fs_mtime; |
|
218 fs_ctime = fs.fs_ctime; |
|
219 |
3887
|
220 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
2926
|
221 fs_rdev = fs.fs_rdev; |
|
222 #endif |
|
223 |
3887
|
224 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
2926
|
225 fs_blksize = fs.fs_blksize; |
|
226 #endif |
|
227 |
3887
|
228 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
2926
|
229 fs_blocks = fs.fs_blocks; |
|
230 #endif |
|
231 } |
|
232 |
|
233 /* |
|
234 ;;; Local Variables: *** |
|
235 ;;; mode: C++ *** |
|
236 ;;; End: *** |
|
237 */ |