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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cerrno> |
|
28 #include <cstring> |
|
29 |
|
30 #ifdef HAVE_UNISTD_H |
|
31 #ifdef HAVE_SYS_TYPES_H |
|
32 #include <sys/types.h> |
|
33 #endif |
|
34 #include <unistd.h> |
|
35 #endif |
|
36 |
|
37 #include "file-stat.h" |
|
38 #include "statdefs.h" |
|
39 |
3141
|
40 #if !defined (HAVE_LSTAT) |
|
41 static inline int |
|
42 lstat (const char *name, struct stat *buf) |
|
43 { |
|
44 return stat (name, buf); |
|
45 } |
|
46 #endif |
2926
|
47 |
|
48 // XXX FIXME XXX -- 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 |
|
53 file_stat::is_blk (void) const |
|
54 { |
|
55 #ifdef S_ISBLK |
|
56 return S_ISBLK (fs_mode); |
|
57 #else |
|
58 return false; |
|
59 #endif |
|
60 } |
|
61 |
|
62 bool |
|
63 file_stat::is_chr (void) const |
|
64 { |
|
65 #ifdef S_ISCHR |
|
66 return S_ISCHR (fs_mode); |
|
67 #else |
|
68 return false; |
|
69 #endif |
|
70 } |
|
71 |
|
72 bool |
|
73 file_stat::is_dir (void) const |
|
74 { |
|
75 #ifdef S_ISDIR |
|
76 return S_ISDIR (fs_mode); |
|
77 #else |
|
78 return false; |
|
79 #endif |
|
80 } |
|
81 |
|
82 bool |
|
83 file_stat::is_fifo (void) const |
|
84 { |
|
85 #ifdef S_ISFIFO |
|
86 return S_ISFIFO (fs_mode); |
|
87 #else |
|
88 return false; |
|
89 #endif |
|
90 } |
|
91 |
|
92 bool |
|
93 file_stat::is_lnk (void) const |
|
94 { |
|
95 #ifdef S_ISLNK |
|
96 return S_ISLNK (fs_mode); |
|
97 #else |
|
98 return false; |
|
99 #endif |
|
100 } |
|
101 |
|
102 bool |
|
103 file_stat::is_reg (void) const |
|
104 { |
|
105 #ifdef S_ISREG |
|
106 return S_ISREG (fs_mode); |
|
107 #else |
|
108 return false; |
|
109 #endif |
|
110 } |
|
111 |
|
112 bool |
|
113 file_stat::is_sock (void) const |
|
114 { |
|
115 #ifdef S_ISSOCK |
|
116 return S_ISSOCK (fs_mode); |
|
117 #else |
|
118 return false; |
|
119 #endif |
|
120 } |
|
121 |
|
122 extern "C" void mode_string (); |
|
123 |
|
124 string |
|
125 file_stat::mode_as_string (void) const |
|
126 { |
|
127 char buf[11]; |
|
128 |
|
129 mode_string (fs_mode, buf); |
|
130 |
|
131 buf[10] = '\0'; |
|
132 |
|
133 return string (buf); |
|
134 } |
|
135 |
|
136 // Has FILE been modified since TIME? Returns 1 for yes, 0 for no, |
|
137 // and -1 for any error. |
|
138 |
|
139 int |
3260
|
140 file_stat::is_newer (const string& file, const octave_time& time) |
2926
|
141 { |
|
142 file_stat fs (file); |
|
143 |
|
144 return fs ? fs.is_newer (time) : -1; |
|
145 } |
|
146 |
|
147 // Private stuff: |
|
148 |
|
149 void |
|
150 file_stat::update_internal (bool force) |
|
151 { |
|
152 if (! initialized || force) |
|
153 { |
|
154 initialized = false; |
|
155 fail = false; |
|
156 |
|
157 const char *cname = file_name.c_str (); |
|
158 |
|
159 struct stat buf; |
|
160 |
3141
|
161 int status = follow_links ? stat (cname, &buf) : lstat (cname, &buf); |
2926
|
162 |
|
163 if (status < 0) |
|
164 { |
|
165 fail = true; |
|
166 errmsg = strerror (errno); |
|
167 } |
|
168 else |
|
169 { |
|
170 fs_mode = buf.st_mode; |
|
171 fs_ino = buf.st_ino; |
|
172 fs_dev = buf.st_dev; |
|
173 fs_nlink = buf.st_nlink; |
|
174 fs_uid = buf.st_uid; |
|
175 fs_gid = buf.st_gid; |
|
176 fs_size = buf.st_size; |
|
177 fs_atime = buf.st_atime; |
|
178 fs_mtime = buf.st_mtime; |
|
179 fs_ctime = buf.st_ctime; |
|
180 |
|
181 #if defined (HAVE_ST_RDEV) |
|
182 fs_rdev = buf.st_rdev; |
|
183 #endif |
|
184 |
|
185 #if defined (HAVE_ST_BLKSIZE) |
|
186 fs_blksize = buf.st_blksize; |
|
187 #endif |
|
188 |
|
189 #if defined (HAVE_ST_BLOCKS) |
|
190 fs_blocks = buf.st_blocks; |
|
191 #endif |
|
192 } |
|
193 |
|
194 initialized = true; |
|
195 } |
|
196 } |
|
197 |
|
198 void |
|
199 file_stat::copy (const file_stat& fs) |
|
200 { |
|
201 file_name = fs.file_name; |
|
202 follow_links = fs.follow_links; |
|
203 initialized = fs.initialized; |
|
204 fail = fs.fail; |
|
205 errmsg = fs.errmsg; |
|
206 fs_mode = fs.fs_mode; |
|
207 fs_ino = fs.fs_ino; |
|
208 fs_dev = fs.fs_dev; |
|
209 fs_nlink = fs.fs_nlink; |
|
210 fs_uid = fs.fs_uid; |
|
211 fs_gid = fs.fs_gid; |
|
212 fs_size = fs.fs_size; |
|
213 fs_atime = fs.fs_atime; |
|
214 fs_mtime = fs.fs_mtime; |
|
215 fs_ctime = fs.fs_ctime; |
|
216 |
|
217 #if defined (HAVE_ST_RDEV) |
|
218 fs_rdev = fs.fs_rdev; |
|
219 #endif |
|
220 |
|
221 #if defined (HAVE_ST_BLKSIZE) |
|
222 fs_blksize = fs.fs_blksize; |
|
223 #endif |
|
224 |
|
225 #if defined (HAVE_ST_BLOCKS) |
|
226 fs_blocks = fs.fs_blocks; |
|
227 #endif |
|
228 } |
|
229 |
|
230 /* |
|
231 ;;; Local Variables: *** |
|
232 ;;; mode: C++ *** |
|
233 ;;; End: *** |
|
234 */ |