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