1765
|
1 // file-ops.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1996 John W. Eaton |
|
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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cerrno> |
1802
|
29 #include <cstdio> |
|
30 #include <cstdlib> |
1765
|
31 #include <cstring> |
|
32 |
|
33 #ifdef HAVE_UNISTD_H |
|
34 #include <sys/types.h> |
|
35 #include <unistd.h> |
|
36 #endif |
|
37 |
|
38 #include "file-ops.h" |
1802
|
39 #include "lo-error.h" |
1775
|
40 #include "statdefs.h" |
|
41 |
|
42 // These must come after <sys/types.h> and <sys/stat.h>. |
|
43 |
1773
|
44 #include "safe-lstat.h" |
|
45 #include "safe-stat.h" |
1765
|
46 |
|
47 // XXX FIXME XXX -- the is_* and mode_as_string functions are only valid |
|
48 // for initialized objects. If called for an object that is not |
|
49 // initialized, they should throw an exception. |
|
50 |
|
51 bool |
|
52 file_stat::is_blk (void) const |
|
53 { |
|
54 return S_ISBLK (fs_mode); |
|
55 } |
|
56 |
|
57 bool |
|
58 file_stat::is_chr (void) const |
|
59 { |
|
60 return S_ISCHR (fs_mode); |
|
61 } |
|
62 |
|
63 bool |
|
64 file_stat::is_dir (void) const |
|
65 { |
|
66 return S_ISDIR (fs_mode); |
|
67 } |
|
68 |
|
69 bool |
|
70 file_stat::is_fifo (void) const |
|
71 { |
|
72 return S_ISFIFO (fs_mode); |
|
73 } |
|
74 |
|
75 bool |
|
76 file_stat::is_lnk (void) const |
|
77 { |
|
78 return S_ISLNK (fs_mode); |
|
79 } |
|
80 |
|
81 bool |
|
82 file_stat::is_reg (void) const |
|
83 { |
|
84 return S_ISREG (fs_mode); |
|
85 } |
|
86 |
|
87 bool |
|
88 file_stat::is_sock (void) const |
|
89 { |
|
90 return S_ISSOCK (fs_mode); |
|
91 } |
|
92 |
|
93 extern "C" void mode_string (); |
|
94 |
|
95 string |
|
96 file_stat::mode_as_string (void) const |
|
97 { |
|
98 char buf[11]; |
|
99 |
|
100 mode_string (fs_mode, buf); |
|
101 |
|
102 buf[10] = '\0'; |
|
103 |
|
104 return buf; |
|
105 } |
|
106 |
|
107 // Private stuff: |
|
108 |
|
109 void |
|
110 file_stat::update_internal (bool force) |
|
111 { |
|
112 if (! initialized || force) |
|
113 { |
|
114 initialized = false; |
|
115 fail = false; |
|
116 |
|
117 const char *cname = file_name.c_str (); |
|
118 |
|
119 struct stat buf; |
|
120 |
|
121 int status = follow_links |
1776
|
122 ? SAFE_STAT (cname, &buf) : SAFE_LSTAT (cname, &buf); |
1765
|
123 |
|
124 if (status < 0) |
|
125 { |
|
126 fail = true; |
|
127 errmsg = strerror (errno); |
|
128 } |
|
129 else |
|
130 { |
|
131 fs_mode = buf.st_mode; |
|
132 fs_ino = buf.st_ino; |
|
133 fs_dev = buf.st_dev; |
|
134 fs_nlink = buf.st_nlink; |
|
135 fs_uid = buf.st_uid; |
|
136 fs_gid = buf.st_gid; |
|
137 fs_size = buf.st_size; |
|
138 fs_atime = buf.st_atime; |
|
139 fs_mtime = buf.st_mtime; |
|
140 fs_ctime = buf.st_ctime; |
|
141 |
|
142 #if defined (HAVE_ST_RDEV) |
|
143 fs_rdev = buf.st_rdev; |
|
144 #endif |
|
145 |
|
146 #if defined (HAVE_ST_BLKSIZE) |
|
147 fs_blksize = buf.st_blksize; |
|
148 #endif |
|
149 |
|
150 #if defined (HAVE_ST_BLOCKS) |
|
151 fs_blocks = buf.st_blocks; |
|
152 #endif |
|
153 } |
|
154 |
|
155 initialized = true; |
|
156 } |
|
157 } |
|
158 |
1779
|
159 void |
|
160 file_stat::copy (const file_stat& fs) |
|
161 { |
|
162 file_name = fs.file_name; |
|
163 follow_links = fs.follow_links; |
|
164 initialized = fs.initialized; |
|
165 fail = fs.fail; |
|
166 errmsg = fs.errmsg; |
|
167 fs_mode = fs.fs_mode; |
|
168 fs_ino = fs.fs_ino; |
|
169 fs_dev = fs.fs_dev; |
|
170 fs_nlink = fs.fs_nlink; |
|
171 fs_uid = fs.fs_uid; |
|
172 fs_gid = fs.fs_gid; |
|
173 fs_size = fs.fs_size; |
|
174 fs_atime = fs.fs_atime; |
|
175 fs_mtime = fs.fs_mtime; |
|
176 fs_ctime = fs.fs_ctime; |
|
177 |
|
178 #if defined (HAVE_ST_RDEV) |
|
179 fs_rdev = fs.fs_rdev; |
|
180 #endif |
|
181 |
|
182 #if defined (HAVE_ST_BLKSIZE) |
|
183 fs_blksize = fs.fs_blksize; |
|
184 #endif |
|
185 |
|
186 #if defined (HAVE_ST_BLOCKS) |
|
187 fs_blocks = fs.fs_blocks; |
|
188 #endif |
|
189 } |
|
190 |
1765
|
191 // Functions for octave. |
|
192 |
|
193 // Has FILE been modified since TIME? Returns 1 for yes, 0 for no, |
|
194 // and -1 for any error. |
|
195 int |
|
196 is_newer (const string& file, time_t time) |
|
197 { |
|
198 file_stat fs (file); |
|
199 |
|
200 return fs ? fs.is_newer (time) : -1; |
|
201 } |
|
202 |
|
203 int |
1772
|
204 oct_mkdir (const string& name, mode_t mode) |
1765
|
205 { |
|
206 return mkdir (name.c_str (), mode); |
|
207 } |
|
208 |
|
209 int |
1773
|
210 oct_mkfifo (const string& name, mode_t mode) |
1765
|
211 { |
1773
|
212 return mkfifo (name.c_str (), mode); |
1765
|
213 } |
|
214 |
|
215 int |
1772
|
216 oct_rename (const string& from, const string& to) |
1765
|
217 { |
|
218 return rename (from.c_str (), to.c_str ()); |
|
219 } |
|
220 |
|
221 int |
1773
|
222 oct_rmdir (const string& name) |
1765
|
223 { |
1773
|
224 return rmdir (name.c_str ()); |
1765
|
225 } |
|
226 |
1802
|
227 string |
|
228 oct_tempnam (void) |
|
229 { |
|
230 string retval; |
|
231 |
|
232 char *tmp = tempnam (0, "oct-"); |
|
233 |
|
234 if (tmp) |
|
235 { |
|
236 retval = tmp; |
|
237 |
|
238 free (tmp); |
|
239 } |
|
240 else |
|
241 (*current_liboctave_error_handler) ("can't open temporary file!"); |
|
242 |
|
243 return retval; |
|
244 } |
|
245 |
|
246 |
1765
|
247 int |
1772
|
248 oct_umask (mode_t mode) |
1765
|
249 { |
|
250 #if defined (HAVE_UMASK) |
|
251 return umask (mode); |
|
252 #else |
|
253 return 0; |
|
254 #endif |
|
255 } |
|
256 |
1773
|
257 int |
|
258 oct_unlink (const string& name) |
|
259 { |
|
260 return unlink (name.c_str ()); |
|
261 } |
|
262 |
1765
|
263 /* |
|
264 ;;; Local Variables: *** |
|
265 ;;; mode: C++ *** |
|
266 ;;; page-delimiter: "^/\\*" *** |
|
267 ;;; End: *** |
|
268 */ |