Mercurial > hg > octave-nkf
annotate liboctave/file-stat.cc @ 10687:a8ce6bdecce5
Improve documentation strings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 08 Jun 2010 20:22:38 -0700 |
parents | bbe99b2a5ba7 |
children | e4e82740e9cd |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2005, 2006, 2007, |
4 2008, 2009 John W. Eaton | |
2926 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2926 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2926 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <cerrno> | |
10463
bbe99b2a5ba7
undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents:
10447
diff
changeset
|
29 #include <cstring> |
2926 | 30 |
31 #include <sys/types.h> | |
32 #include <unistd.h> | |
33 | |
5871 | 34 #include "file-ops.h" |
2926 | 35 #include "file-stat.h" |
36 #include "statdefs.h" | |
37 | |
5775 | 38 // FIXME -- the is_* and mode_as_string functions are only valid |
2926 | 39 // for initialized objects. If called for an object that is not |
40 // initialized, they should throw an exception. | |
41 | |
42 bool | |
8549 | 43 base_file_stat::is_blk (void) const |
2926 | 44 { |
8690
6e9887f9cf9f
file-stat.cc: use EXISTS instead of OK in previuos change
John W. Eaton <jwe@octave.org>
parents:
8689
diff
changeset
|
45 return exists () && is_blk (fs_mode); |
2926 | 46 } |
47 | |
48 bool | |
8549 | 49 base_file_stat::is_chr (void) const |
2926 | 50 { |
8690
6e9887f9cf9f
file-stat.cc: use EXISTS instead of OK in previuos change
John W. Eaton <jwe@octave.org>
parents:
8689
diff
changeset
|
51 return exists () && is_chr (fs_mode); |
2926 | 52 } |
53 | |
54 bool | |
8549 | 55 base_file_stat::is_dir (void) const |
2926 | 56 { |
8690
6e9887f9cf9f
file-stat.cc: use EXISTS instead of OK in previuos change
John W. Eaton <jwe@octave.org>
parents:
8689
diff
changeset
|
57 return exists () && is_dir (fs_mode); |
5476 | 58 } |
59 | |
60 bool | |
8549 | 61 base_file_stat::is_fifo (void) const |
5476 | 62 { |
8690
6e9887f9cf9f
file-stat.cc: use EXISTS instead of OK in previuos change
John W. Eaton <jwe@octave.org>
parents:
8689
diff
changeset
|
63 return exists () && is_fifo (fs_mode); |
5476 | 64 } |
65 | |
66 bool | |
8549 | 67 base_file_stat::is_lnk (void) const |
5476 | 68 { |
8690
6e9887f9cf9f
file-stat.cc: use EXISTS instead of OK in previuos change
John W. Eaton <jwe@octave.org>
parents:
8689
diff
changeset
|
69 return exists () && is_lnk (fs_mode); |
5476 | 70 } |
71 | |
72 bool | |
8549 | 73 base_file_stat::is_reg (void) const |
5476 | 74 { |
8690
6e9887f9cf9f
file-stat.cc: use EXISTS instead of OK in previuos change
John W. Eaton <jwe@octave.org>
parents:
8689
diff
changeset
|
75 return exists () && is_reg (fs_mode); |
5476 | 76 } |
77 | |
78 bool | |
8549 | 79 base_file_stat::is_sock (void) const |
5476 | 80 { |
8690
6e9887f9cf9f
file-stat.cc: use EXISTS instead of OK in previuos change
John W. Eaton <jwe@octave.org>
parents:
8689
diff
changeset
|
81 return exists () && is_sock (fs_mode); |
5476 | 82 } |
83 | |
84 bool | |
8549 | 85 base_file_stat::is_blk (mode_t mode) |
5476 | 86 { |
87 #ifdef S_ISBLK | |
88 return S_ISBLK (mode); | |
2926 | 89 #else |
90 return false; | |
91 #endif | |
92 } | |
93 | |
94 bool | |
8549 | 95 base_file_stat::is_chr (mode_t mode) |
5476 | 96 { |
97 #ifdef S_ISCHR | |
98 return S_ISCHR (mode); | |
99 #else | |
100 return false; | |
101 #endif | |
102 } | |
103 | |
104 bool | |
8549 | 105 base_file_stat::is_dir (mode_t mode) |
2926 | 106 { |
5476 | 107 #ifdef S_ISDIR |
108 return S_ISDIR (mode); | |
2926 | 109 #else |
110 return false; | |
111 #endif | |
112 } | |
113 | |
114 bool | |
8549 | 115 base_file_stat::is_fifo (mode_t mode) |
2926 | 116 { |
5476 | 117 #ifdef S_ISFIFO |
118 return S_ISFIFO (mode); | |
2926 | 119 #else |
120 return false; | |
121 #endif | |
122 } | |
123 | |
124 bool | |
8549 | 125 base_file_stat::is_lnk (mode_t mode) |
2926 | 126 { |
5476 | 127 #ifdef S_ISLNK |
128 return S_ISLNK (mode); | |
2926 | 129 #else |
130 return false; | |
131 #endif | |
132 } | |
133 | |
134 bool | |
8549 | 135 base_file_stat::is_reg (mode_t mode) |
5476 | 136 { |
137 #ifdef S_ISREG | |
138 return S_ISREG (mode); | |
139 #else | |
140 return false; | |
141 #endif | |
142 } | |
143 | |
144 bool | |
8549 | 145 base_file_stat::is_sock (mode_t mode) |
2926 | 146 { |
147 #ifdef S_ISSOCK | |
5476 | 148 return S_ISSOCK (mode); |
2926 | 149 #else |
150 return false; | |
151 #endif | |
152 } | |
153 | |
3504 | 154 extern "C" void mode_string (unsigned short, char *); |
2926 | 155 |
3504 | 156 std::string |
8549 | 157 base_file_stat::mode_as_string (void) const |
2926 | 158 { |
159 char buf[11]; | |
160 | |
161 mode_string (fs_mode, buf); | |
162 | |
163 buf[10] = '\0'; | |
164 | |
3504 | 165 return std::string (buf); |
2926 | 166 } |
167 | |
168 // Has FILE been modified since TIME? Returns 1 for yes, 0 for no, | |
169 // and -1 for any error. | |
170 | |
171 int | |
8549 | 172 base_file_stat::is_newer (const std::string& file, const octave_time& time) |
2926 | 173 { |
174 file_stat fs (file); | |
175 | |
176 return fs ? fs.is_newer (time) : -1; | |
177 } | |
178 | |
179 // Private stuff: | |
180 | |
181 void | |
182 file_stat::update_internal (bool force) | |
183 { | |
184 if (! initialized || force) | |
185 { | |
186 initialized = false; | |
187 fail = false; | |
5871 | 188 |
189 std::string full_file_name = file_ops::tilde_expand (file_name); | |
2926 | 190 |
6810 | 191 #if defined (__WIN32__) |
192 // Remove trailing slash. | |
193 if (file_ops::is_dir_sep (full_file_name[full_file_name.length () - 1]) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
194 && full_file_name.length () != 1 |
6810 | 195 && ! (full_file_name.length() == 3 && full_file_name[1] == ':')) |
196 full_file_name.resize (full_file_name.length () - 1); | |
197 #endif | |
198 | |
5871 | 199 const char *cname = full_file_name.c_str (); |
2926 | 200 |
201 struct stat buf; | |
202 | |
10411 | 203 int status = follow_links |
204 ? stat (cname, &buf) : gnulib::lstat (cname, &buf); | |
2926 | 205 |
206 if (status < 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
207 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
208 fail = true; |
10411 | 209 errmsg = gnulib::strerror (errno); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
210 } |
2926 | 211 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
212 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
213 fs_mode = buf.st_mode; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
214 fs_ino = buf.st_ino; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
215 fs_dev = buf.st_dev; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
216 fs_nlink = buf.st_nlink; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
217 fs_uid = buf.st_uid; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
218 fs_gid = buf.st_gid; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
219 fs_size = buf.st_size; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
220 fs_atime = buf.st_atime; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
221 fs_mtime = buf.st_mtime; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
222 fs_ctime = buf.st_ctime; |
2926 | 223 |
3887 | 224 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
225 fs_rdev = buf.st_rdev; |
2926 | 226 #endif |
227 | |
3887 | 228 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
229 fs_blksize = buf.st_blksize; |
2926 | 230 #endif |
231 | |
3887 | 232 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
233 fs_blocks = buf.st_blocks; |
2926 | 234 #endif |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
235 } |
2926 | 236 |
237 initialized = true; | |
238 } | |
239 } | |
240 | |
241 void | |
8549 | 242 file_fstat::update_internal (bool force) |
2926 | 243 { |
8549 | 244 if (! initialized || force) |
245 { | |
246 initialized = false; | |
247 fail = false; | |
248 | |
249 struct stat buf; | |
250 | |
10411 | 251 int status = gnulib::fstat (fid, &buf); |
8549 | 252 |
253 if (status < 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
254 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
255 fail = true; |
10411 | 256 errmsg = gnulib::strerror (errno); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
257 } |
8549 | 258 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
259 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
260 fs_mode = buf.st_mode; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
261 fs_ino = buf.st_ino; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
262 fs_dev = buf.st_dev; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
263 fs_nlink = buf.st_nlink; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
264 fs_uid = buf.st_uid; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
265 fs_gid = buf.st_gid; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
266 fs_size = buf.st_size; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
267 fs_atime = buf.st_atime; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
268 fs_mtime = buf.st_mtime; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
269 fs_ctime = buf.st_ctime; |
2926 | 270 |
3887 | 271 #if defined (HAVE_STRUCT_STAT_ST_RDEV) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
272 fs_rdev = buf.st_rdev; |
2926 | 273 #endif |
274 | |
3887 | 275 #if defined (HAVE_STRUCT_STAT_ST_BLKSIZE) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
276 fs_blksize = buf.st_blksize; |
2926 | 277 #endif |
278 | |
3887 | 279 #if defined (HAVE_STRUCT_STAT_ST_BLOCKS) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
280 fs_blocks = buf.st_blocks; |
2926 | 281 #endif |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
282 } |
8549 | 283 |
284 initialized = true; | |
285 } | |
2926 | 286 } |