Mercurial > hg > octave-nkf
annotate liboctave/file-ops.cc @ 10175:d354be89b2c3
use link module from gnulib
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 21 Jan 2010 02:37:26 -0500 |
parents | eb64bf1c6107 |
children | a0665fa8798c |
rev | line source |
---|---|
1765 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006, |
8920 | 4 2007, 2008 John W. Eaton |
1765 | 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. | |
1765 | 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/>. | |
1765 | 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 | |
3503 | 33 #include <iostream> |
4726 | 34 #include <vector> |
3040 | 35 |
2443 | 36 #ifdef HAVE_SYS_TYPES_H |
1765 | 37 #include <sys/types.h> |
2443 | 38 #endif |
2926 | 39 |
40 #ifdef HAVE_UNISTD_H | |
1765 | 41 #include <unistd.h> |
42 #endif | |
43 | |
5476 | 44 #include "dir-ops.h" |
1765 | 45 #include "file-ops.h" |
5476 | 46 #include "file-stat.h" |
2926 | 47 #include "oct-env.h" |
2934 | 48 #include "oct-passwd.h" |
3710 | 49 #include "pathlen.h" |
5476 | 50 #include "quit.h" |
1775 | 51 #include "statdefs.h" |
2926 | 52 #include "str-vec.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8327
diff
changeset
|
53 #include "oct-locbuf.h" |
1765 | 54 |
8009
d936b21b3a6b
file_ops: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
55 file_ops::static_members *file_ops::static_members::instance = 0; |
d936b21b3a6b
file_ops: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
56 |
d936b21b3a6b
file_ops: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
57 file_ops::static_members::static_members (void) |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
58 : |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
59 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)) |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
60 xdir_sep_char ('\\'), |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
61 xdir_sep_str ("\\"), |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
62 #else |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
63 xdir_sep_char ('/'), |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
64 xdir_sep_str ("/"), |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
65 #endif |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
66 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
67 xdir_sep_chars ("/\\") |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
68 #else |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
69 xdir_sep_chars (xdir_sep_str) |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
70 #endif |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
71 { } |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
72 |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
73 bool |
8009
d936b21b3a6b
file_ops: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
74 file_ops::static_members::instance_ok (void) |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
75 { |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
76 bool retval = true; |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
77 |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
78 if (! instance) |
8009
d936b21b3a6b
file_ops: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
79 instance = new static_members (); |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
80 |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
81 if (! instance) |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
82 { |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
83 (*current_liboctave_error_handler) |
8009
d936b21b3a6b
file_ops: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
84 ("unable to create file_ops::static_members object!"); |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
85 |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
86 retval = false; |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
87 } |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
88 |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
89 return retval; |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
90 } |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
91 |
2937 | 92 #define NOT_SUPPORTED(nm) \ |
4062 | 93 nm ": not supported on this system" |
2937 | 94 |
2433 | 95 // We provide a replacement for mkdir(). |
96 | |
1765 | 97 int |
10172
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
98 file_ops::mkdir (const std::string& nm, mode_t md) |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
99 { |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
100 std::string msg; |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
101 return mkdir (nm, md, msg); |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
102 } |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
103 |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
104 int |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
105 file_ops::mkdir (const std::string& name, mode_t mode, std::string& msg) |
2668 | 106 { |
3504 | 107 msg = std::string (); |
2668 | 108 |
2937 | 109 int status = -1; |
110 | |
10172
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
111 status = octave_mkdir (name.c_str (), mode); |
2668 | 112 |
113 if (status < 0) | |
3504 | 114 { |
115 using namespace std; | |
116 msg = ::strerror (errno); | |
117 } | |
2668 | 118 |
119 return status; | |
120 } | |
121 | |
2433 | 122 // I don't know how to emulate this on systems that don't provide it. |
123 | |
1765 | 124 int |
10172
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
125 file_ops::mkfifo (const std::string& nm, mode_t md) |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
126 { |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
127 std::string msg; |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
128 return mkfifo (nm, md, msg); |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
129 } |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
130 |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
131 int |
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
132 file_ops::mkfifo (const std::string& name, mode_t mode, std::string& msg) |
2668 | 133 { |
3504 | 134 msg = std::string (); |
2668 | 135 |
2937 | 136 int status = -1; |
137 | |
10028
52a248732bb6
use mkfifo module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10027
diff
changeset
|
138 // With gnulib we will always have mkfifo, but some systems like MinGW |
52a248732bb6
use mkfifo module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10027
diff
changeset
|
139 // don't have working mkfifo functions. On those systems, mkfifo will |
52a248732bb6
use mkfifo module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10027
diff
changeset
|
140 // always return -1 and set errno. |
52a248732bb6
use mkfifo module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10027
diff
changeset
|
141 |
10172
96ed9db3345c
provide wrappers for mkdir and mkfifo
John W. Eaton <jwe@octave.org>
parents:
10170
diff
changeset
|
142 status = octave_mkfifo (name.c_str (), mode); |
2668 | 143 |
144 if (status < 0) | |
3504 | 145 { |
146 using namespace std; | |
147 msg = ::strerror (errno); | |
148 } | |
2668 | 149 |
150 return status; | |
151 } | |
152 | |
3710 | 153 // I don't know how to emulate this on systems that don't provide it. |
154 | |
155 int | |
156 file_ops::link (const std::string& old_name, const std::string& new_name) | |
157 { | |
158 std::string msg; | |
159 return link (old_name, new_name, msg); | |
160 } | |
161 | |
162 int | |
163 file_ops::link (const std::string& old_name, | |
164 const std::string& new_name, std::string& msg) | |
165 { | |
166 msg = std::string (); | |
167 | |
168 int status = -1; | |
169 | |
10175
d354be89b2c3
use link module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10174
diff
changeset
|
170 status = octave_link (old_name.c_str (), new_name.c_str ()); |
3710 | 171 |
172 if (status < 0) | |
173 { | |
174 using namespace std; | |
175 msg = ::strerror (errno); | |
176 } | |
177 | |
178 return status; | |
179 } | |
180 | |
181 // I don't know how to emulate this on systems that don't provide it. | |
182 | |
183 int | |
184 file_ops::symlink (const std::string& old_name, const std::string& new_name) | |
185 { | |
186 std::string msg; | |
187 return symlink (old_name, new_name, msg); | |
188 } | |
189 | |
190 int | |
191 file_ops::symlink (const std::string& old_name, | |
192 const std::string& new_name, std::string& msg) | |
193 { | |
194 msg = std::string (); | |
195 | |
196 int status = -1; | |
197 | |
198 #if defined (HAVE_SYMLINK) | |
4577 | 199 |
9430
bfc7b000a229
file-ops.cc (file_ops::symlink, file_ops::readlink): avoid incorrectly sized buffer
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
200 status = ::symlink (old_name.c_str (), new_name.c_str ()); |
3710 | 201 |
202 if (status < 0) | |
203 { | |
204 using namespace std; | |
205 msg = ::strerror (errno); | |
206 } | |
207 #else | |
208 msg = NOT_SUPPORTED ("symlink"); | |
209 #endif | |
210 | |
211 return status; | |
212 } | |
213 | |
214 // We provide a replacement for rename(). | |
215 | |
216 int | |
217 file_ops::readlink (const std::string& path, std::string& result) | |
218 { | |
219 std::string msg; | |
220 return readlink (path, result, msg); | |
221 } | |
222 | |
223 int | |
224 file_ops::readlink (const std::string& path, std::string& result, | |
225 std::string& msg) | |
226 { | |
227 int status = -1; | |
228 | |
229 msg = std::string (); | |
230 | |
4066 | 231 #if defined (HAVE_READLINK) |
3710 | 232 char buf[MAXPATHLEN+1]; |
233 | |
9430
bfc7b000a229
file-ops.cc (file_ops::symlink, file_ops::readlink): avoid incorrectly sized buffer
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
234 status = ::readlink (path.c_str (), buf, MAXPATHLEN); |
3710 | 235 |
236 if (status < 0) | |
237 { | |
238 using namespace std; | |
239 msg = ::strerror (errno); | |
240 } | |
241 else | |
242 { | |
243 buf[status] = '\0'; | |
3769 | 244 result = std::string (buf); |
3710 | 245 status = 0; |
246 } | |
247 #else | |
248 msg = NOT_SUPPORTED ("rename"); | |
249 #endif | |
250 | |
251 return status; | |
252 } | |
253 | |
2433 | 254 // We provide a replacement for rename(). |
255 | |
1765 | 256 int |
3504 | 257 file_ops::rename (const std::string& from, const std::string& to) |
1765 | 258 { |
3504 | 259 std::string msg; |
2937 | 260 return rename (from, to, msg); |
1765 | 261 } |
262 | |
2668 | 263 int |
3504 | 264 file_ops::rename (const std::string& from, const std::string& to, |
265 std::string& msg) | |
2668 | 266 { |
2937 | 267 int status = -1; |
268 | |
3504 | 269 msg = std::string (); |
2668 | 270 |
10170
1dffc8b2fca7
use rename module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10169
diff
changeset
|
271 status = octave_rename (from.c_str (), to.c_str ()); |
2668 | 272 |
273 if (status < 0) | |
3504 | 274 { |
275 using namespace std; | |
276 msg = ::strerror (errno); | |
277 } | |
2668 | 278 |
279 return status; | |
280 } | |
281 | |
2433 | 282 // We provide a replacement for rmdir(). |
283 | |
1765 | 284 int |
3504 | 285 file_ops::rmdir (const std::string& name) |
1765 | 286 { |
3504 | 287 std::string msg; |
2937 | 288 return rmdir (name, msg); |
1765 | 289 } |
290 | |
2668 | 291 int |
3504 | 292 file_ops::rmdir (const std::string& name, std::string& msg) |
2668 | 293 { |
3504 | 294 msg = std::string (); |
2668 | 295 |
2937 | 296 int status = -1; |
297 | |
10169
06bd6e57f889
use rmdir module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
298 status = octave_rmdir (name.c_str ()); |
2668 | 299 |
300 if (status < 0) | |
3504 | 301 { |
302 using namespace std; | |
303 msg = ::strerror (errno); | |
304 } | |
2668 | 305 |
306 return status; | |
307 } | |
308 | |
5476 | 309 // And a version that works recursively. |
310 | |
311 int | |
312 file_ops::recursive_rmdir (const std::string& name) | |
313 { | |
314 std::string msg; | |
315 return recursive_rmdir (name, msg); | |
316 } | |
317 | |
318 int | |
319 file_ops::recursive_rmdir (const std::string& name, std::string& msg) | |
320 { | |
321 msg = std::string (); | |
322 | |
323 int status = 0; | |
324 | |
325 dir_entry dir (name); | |
326 | |
327 if (dir) | |
328 { | |
329 string_vector dirlist = dir.read (); | |
330 | |
331 for (octave_idx_type i = 0; i < dirlist.length (); i++) | |
332 { | |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
10038
diff
changeset
|
333 octave_quit (); |
5476 | 334 |
335 std::string nm = dirlist[i]; | |
336 | |
337 // Skip current directory and parent. | |
338 if (nm == "." || nm == "..") | |
339 continue; | |
340 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
341 std::string fullnm = name + file_ops::dir_sep_str () + nm; |
5476 | 342 |
343 // Get info about the file. Don't follow links. | |
344 file_stat fs (fullnm, false); | |
345 | |
346 if (fs) | |
347 { | |
348 if (fs.is_dir ()) | |
349 { | |
350 status = recursive_rmdir (fullnm, msg); | |
351 | |
352 if (status < 0) | |
353 break; | |
354 } | |
355 else | |
356 { | |
357 status = unlink (fullnm, msg); | |
358 | |
359 if (status < 0) | |
360 break; | |
361 } | |
362 } | |
363 else | |
364 { | |
365 msg = fs.error (); | |
366 break; | |
367 } | |
368 } | |
369 | |
370 if (status >= 0) | |
6363 | 371 { |
372 dir.close (); | |
373 status = file_ops::rmdir (name, msg); | |
374 } | |
5476 | 375 } |
376 else | |
377 { | |
378 status = -1; | |
379 | |
380 msg = dir.error (); | |
381 } | |
382 | |
383 return status; | |
384 } | |
385 | |
5138 | 386 std::string |
387 file_ops::canonicalize_file_name (const std::string& name) | |
388 { | |
389 std::string msg; | |
390 return canonicalize_file_name (name, msg); | |
391 } | |
392 | |
393 std::string | |
394 file_ops::canonicalize_file_name (const std::string& name, std::string& msg) | |
395 { | |
396 msg = std::string (); | |
397 | |
398 std::string retval; | |
399 | |
400 #if defined (HAVE_CANONICALIZE_FILE_NAME) | |
401 | |
402 char *tmp = ::canonicalize_file_name (name.c_str ()); | |
403 | |
404 if (tmp) | |
405 { | |
406 retval = tmp; | |
407 ::free (tmp); | |
408 } | |
409 | |
410 #elif defined (HAVE_RESOLVEPATH) | |
411 | |
412 #if !defined (errno) | |
413 extern int errno; | |
414 #endif | |
415 | |
416 #if !defined (__set_errno) | |
417 # define __set_errno(Val) errno = (Val) | |
418 #endif | |
419 | |
420 if (name.empty ()) | |
421 { | |
422 __set_errno (ENOENT); | |
423 return retval; | |
424 } | |
425 | |
426 // All known hosts with resolvepath (e.g. Solaris 7) don't turn | |
427 // relative names into absolute ones, so prepend the working | |
428 // directory if the path is not absolute. | |
429 | |
5149 | 430 std::string absolute_name |
431 = octave_env::make_absolute (name, octave_env::getcwd ()); | |
5138 | 432 |
5149 | 433 size_t resolved_size = absolute_name.length (); |
5138 | 434 |
6208 | 435 while (true) |
5138 | 436 { |
437 resolved_size = 2 * resolved_size + 1; | |
438 | |
439 OCTAVE_LOCAL_BUFFER (char, resolved, resolved_size); | |
440 | |
5149 | 441 int resolved_len |
442 = ::resolvepath (absolute_name.c_str (), resolved, resolved_size); | |
5138 | 443 |
444 if (resolved_len < 0) | |
445 break; | |
446 | |
447 if (resolved_len < resolved_size) | |
448 { | |
449 retval = resolved; | |
450 break; | |
451 } | |
452 } | |
453 | |
6208 | 454 #elif defined (__WIN32__) |
455 | |
456 int n = 1024; | |
457 | |
458 std::string win_path (n, '\0'); | |
459 | |
460 while (true) | |
461 { | |
7520 | 462 int status = GetFullPathName (name.c_str (), n, &win_path[0], 0); |
6208 | 463 |
464 if (status == 0) | |
465 break; | |
466 else if (status < n) | |
467 { | |
468 win_path.resize (status); | |
469 retval = win_path; | |
470 break; | |
471 } | |
472 else | |
473 { | |
474 n *= 2; | |
475 win_path.resize (n); | |
476 } | |
477 } | |
478 | |
6271 | 479 #elif defined (HAVE_REALPATH) |
480 | |
481 #if !defined (__set_errno) | |
482 # define __set_errno(Val) errno = (Val) | |
483 #endif | |
484 | |
485 if (name.empty ()) | |
486 { | |
487 __set_errno (ENOENT); | |
488 return retval; | |
489 } | |
490 | |
491 OCTAVE_LOCAL_BUFFER (char, buf, PATH_MAX); | |
492 | |
6273 | 493 if (::realpath (name.c_str (), buf)) |
494 retval = buf; | |
6271 | 495 |
5138 | 496 #else |
497 | |
5775 | 498 // FIXME -- provide replacement here... |
5138 | 499 retval = name; |
500 | |
501 #endif | |
502 | |
503 if (retval.empty ()) | |
504 { | |
505 using namespace std; | |
506 msg = ::strerror (errno); | |
507 } | |
508 | |
509 return retval; | |
510 } | |
511 | |
2433 | 512 // We provide a replacement for tempnam(). |
513 | |
3504 | 514 std::string |
515 file_ops::tempnam (const std::string& dir, const std::string& pfx) | |
1802 | 516 { |
3504 | 517 std::string msg; |
2937 | 518 return tempnam (dir, pfx, msg); |
519 } | |
520 | |
3504 | 521 std::string |
522 file_ops::tempnam (const std::string& dir, const std::string& pfx, | |
523 std::string& msg) | |
2937 | 524 { |
3504 | 525 msg = std::string (); |
2937 | 526 |
3504 | 527 std::string retval; |
2937 | 528 |
529 const char *pdir = dir.empty () ? 0 : dir.c_str (); | |
1802 | 530 |
2937 | 531 const char *ppfx = pfx.empty () ? 0 : pfx.c_str (); |
532 | |
533 char *tmp = ::tempnam (pdir, ppfx); | |
1802 | 534 |
535 if (tmp) | |
536 { | |
537 retval = tmp; | |
538 | |
2937 | 539 ::free (tmp); |
1802 | 540 } |
541 else | |
3504 | 542 { |
543 using namespace std; | |
544 msg = ::strerror (errno); | |
545 } | |
1802 | 546 |
547 return retval; | |
548 } | |
549 | |
3040 | 550 // The following tilde-expansion code was stolen and adapted from |
551 // readline. | |
2926 | 552 |
3040 | 553 // The default value of tilde_additional_prefixes. This is set to |
554 // whitespace preceding a tilde so that simple programs which do not | |
555 // perform any word separation get desired behaviour. | |
556 static const char *default_prefixes[] = { " ~", "\t~", ":~", 0 }; | |
557 | |
558 // The default value of tilde_additional_suffixes. This is set to | |
559 // whitespace or newline so that simple programs which do not perform | |
560 // any word separation get desired behaviour. | |
561 static const char *default_suffixes[] = { " ", "\n", ":", 0 }; | |
562 | |
563 // If non-null, this contains the address of a function that the | |
564 // application wants called before trying the standard tilde | |
565 // expansions. The function is called with the text sans tilde, and | |
566 // returns a malloc()'ed string which is the expansion, or a NULL | |
567 // pointer if the expansion fails. | |
568 file_ops::tilde_expansion_hook file_ops::tilde_expansion_preexpansion_hook = 0; | |
569 | |
570 // If non-null, this contains the address of a function to call if the | |
571 // standard meaning for expanding a tilde fails. The function is | |
572 // called with the text (sans tilde, as in "foo"), and returns a | |
573 // malloc()'ed string which is the expansion, or a NULL pointer if | |
574 // there is no expansion. | |
575 file_ops::tilde_expansion_hook file_ops::tilde_expansion_failure_hook = 0; | |
576 | |
577 // When non-null, this is a NULL terminated array of strings which are | |
578 // duplicates for a tilde prefix. Bash uses this to expand `=~' and | |
579 // `:~'. | |
580 string_vector file_ops::tilde_additional_prefixes = default_prefixes; | |
581 | |
582 // When non-null, this is a NULL terminated array of strings which | |
583 // match the end of a username, instead of just "/". Bash sets this | |
584 // to `:' and `=~'. | |
585 string_vector file_ops::tilde_additional_suffixes = default_suffixes; | |
586 | |
587 // Find the start of a tilde expansion in S, and return the index | |
588 // of the tilde which starts the expansion. Place the length of the | |
589 // text which identified this tilde starter in LEN, excluding the | |
590 // tilde itself. | |
591 | |
592 static size_t | |
3504 | 593 tilde_find_prefix (const std::string& s, size_t& len) |
3040 | 594 { |
595 len = 0; | |
596 | |
597 size_t s_len = s.length (); | |
598 | |
599 if (s_len == 0 || s[0] == '~') | |
600 return 0; | |
601 | |
602 string_vector prefixes = file_ops::tilde_additional_prefixes; | |
603 | |
604 if (! prefixes.empty ()) | |
605 { | |
606 for (size_t i = 0; i < s_len; i++) | |
607 { | |
608 for (int j = 0; j < prefixes.length (); j++) | |
609 { | |
610 size_t pfx_len = prefixes[j].length (); | |
611 | |
612 if (prefixes[j].compare (s.substr (i, pfx_len)) == 0) | |
613 { | |
614 len = pfx_len - 1; | |
615 return i + len; | |
616 } | |
617 } | |
618 } | |
619 } | |
620 | |
621 return s_len; | |
622 } | |
623 | |
624 // Find the end of a tilde expansion in S, and return the index | |
625 // of the character which ends the tilde definition. | |
626 | |
627 static size_t | |
3504 | 628 tilde_find_suffix (const std::string& s) |
3040 | 629 { |
630 size_t s_len = s.length (); | |
631 | |
632 string_vector suffixes = file_ops::tilde_additional_suffixes; | |
633 | |
634 size_t i = 0; | |
635 | |
636 for ( ; i < s_len; i++) | |
637 { | |
6694 | 638 if (file_ops::is_dir_sep (s[i])) |
3040 | 639 break; |
640 | |
641 if (! suffixes.empty ()) | |
642 { | |
643 for (int j = 0; j < suffixes.length (); j++) | |
644 { | |
645 size_t sfx_len = suffixes[j].length (); | |
646 | |
647 if (suffixes[j].compare (s.substr (i, sfx_len)) == 0) | |
648 return i; | |
649 } | |
650 } | |
651 } | |
652 | |
653 return i; | |
654 } | |
655 | |
656 // Take FNAME and return the tilde prefix we want expanded. | |
657 | |
3504 | 658 static std::string |
659 isolate_tilde_prefix (const std::string& fname) | |
3040 | 660 { |
661 size_t f_len = fname.length (); | |
662 | |
663 size_t len = 1; | |
664 | |
6694 | 665 while (len < f_len && ! file_ops::is_dir_sep (fname[len])) |
3040 | 666 len++; |
667 | |
668 return fname.substr (1, len); | |
669 } | |
670 | |
671 // Do the work of tilde expansion on FILENAME. FILENAME starts with a | |
672 // tilde. | |
673 | |
3504 | 674 static std::string |
675 tilde_expand_word (const std::string& filename) | |
3040 | 676 { |
677 size_t f_len = filename.length (); | |
678 | |
679 if (f_len == 0 || filename[0] != '~') | |
680 return filename; | |
681 | |
682 // A leading `~/' or a bare `~' is *always* translated to the value | |
683 // of $HOME or the home directory of the current user, regardless of | |
684 // any preexpansion hook. | |
685 | |
6694 | 686 if (f_len == 1 || file_ops::is_dir_sep (filename[1])) |
3040 | 687 return octave_env::get_home_directory () + filename.substr (1); |
688 | |
3504 | 689 std::string username = isolate_tilde_prefix (filename); |
3040 | 690 |
691 size_t user_len = username.length (); | |
692 | |
3504 | 693 std::string dirname; |
3040 | 694 |
695 if (file_ops::tilde_expansion_preexpansion_hook) | |
696 { | |
3504 | 697 std::string expansion |
3040 | 698 = file_ops::tilde_expansion_preexpansion_hook (username); |
699 | |
700 if (! expansion.empty ()) | |
3074 | 701 return expansion + filename.substr (user_len+1); |
3040 | 702 } |
703 | |
704 // No preexpansion hook, or the preexpansion hook failed. Look in the | |
705 // password database. | |
706 | |
707 octave_passwd pw = octave_passwd::getpwnam (username); | |
708 | |
709 if (! pw) | |
710 { | |
711 // If the calling program has a special syntax for expanding tildes, | |
712 // and we couldn't find a standard expansion, then let them try. | |
713 | |
714 if (file_ops::tilde_expansion_failure_hook) | |
715 { | |
3504 | 716 std::string expansion |
3040 | 717 = file_ops::tilde_expansion_failure_hook (username); |
718 | |
719 if (! expansion.empty ()) | |
3074 | 720 dirname = expansion + filename.substr (user_len+1); |
3040 | 721 } |
722 | |
723 // If we don't have a failure hook, or if the failure hook did not | |
724 // expand the tilde, return a copy of what we were passed. | |
725 | |
726 if (dirname.length () == 0) | |
727 dirname = filename; | |
728 } | |
729 else | |
3074 | 730 dirname = pw.dir () + filename.substr (user_len+1); |
3040 | 731 |
732 return dirname; | |
733 } | |
734 | |
735 // If NAME has a leading ~ or ~user, Unix-style, expand it to the | |
736 // user's home directory. If no ~, or no <pwd.h>, just return NAME. | |
737 | |
3504 | 738 std::string |
739 file_ops::tilde_expand (const std::string& name) | |
2926 | 740 { |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
741 if (name.find ('~') == std::string::npos) |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
742 return name; |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
743 else |
3040 | 744 { |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
745 std::string result; |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
746 |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
747 size_t name_len = name.length (); |
2926 | 748 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
749 // Scan through S expanding tildes as we come to them. |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
750 |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
751 size_t pos = 0; |
3040 | 752 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
753 while (1) |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
754 { |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
755 if (pos > name_len) |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
756 break; |
3040 | 757 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
758 size_t len; |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
759 |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
760 // Make START point to the tilde which starts the expansion. |
2926 | 761 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
762 size_t start = tilde_find_prefix (name.substr (pos), len); |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
763 |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
764 result.append (name.substr (pos, start)); |
3040 | 765 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
766 // Advance STRING to the starting tilde. |
2926 | 767 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
768 pos += start; |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
769 |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
770 // Make FINI be the index of one after the last character of the |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
771 // username. |
2926 | 772 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
773 size_t fini = tilde_find_suffix (name.substr (pos)); |
2926 | 774 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
775 // If both START and FINI are zero, we are all done. |
2926 | 776 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
777 if (! (start || fini)) |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
778 break; |
2926 | 779 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
780 // Expand the entire tilde word, and copy it into RESULT. |
2947 | 781 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
782 std::string tilde_word = name.substr (pos, fini); |
3040 | 783 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
784 pos += fini; |
2926 | 785 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
786 std::string expansion = tilde_expand_word (tilde_word); |
3040 | 787 |
8327
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
788 result.append (expansion); |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
789 } |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
790 |
4a7a943581d0
Fast return case for file_ops::tilde_expand
David Bateman <dbateman@free.fr>
parents:
8009
diff
changeset
|
791 return result; |
2926 | 792 } |
793 } | |
794 | |
795 // A vector version of the above. | |
796 | |
797 string_vector | |
798 file_ops::tilde_expand (const string_vector& names) | |
799 { | |
800 string_vector retval; | |
801 | |
802 int n = names.length (); | |
803 | |
804 retval.resize (n); | |
805 | |
806 for (int i = 0; i < n; i++) | |
807 retval[i] = file_ops::tilde_expand (names[i]); | |
808 | |
809 return retval; | |
810 } | |
1802 | 811 |
1765 | 812 int |
2926 | 813 file_ops::umask (mode_t mode) |
1765 | 814 { |
10173 | 815 return octave_umask (mode); |
1765 | 816 } |
817 | |
1773 | 818 int |
3504 | 819 file_ops::unlink (const std::string& name) |
1773 | 820 { |
3504 | 821 std::string msg; |
2937 | 822 return unlink (name, msg); |
1773 | 823 } |
824 | |
2668 | 825 int |
3504 | 826 file_ops::unlink (const std::string& name, std::string& msg) |
2668 | 827 { |
3504 | 828 msg = std::string (); |
2668 | 829 |
2937 | 830 int status = -1; |
831 | |
10174
eb64bf1c6107
use unlink module from gnulib
John W. Eaton <jwe@octave.org>
parents:
10173
diff
changeset
|
832 status = octave_unlink (name.c_str ()); |
2668 | 833 |
834 if (status < 0) | |
3504 | 835 { |
836 using namespace std; | |
837 msg = ::strerror (errno); | |
838 } | |
2668 | 839 |
840 return status; | |
841 } | |
842 | |
7272 | 843 std::string |
844 file_ops::concat (const std::string& dir, const std::string& file) | |
845 { | |
846 return dir.empty () | |
847 ? file | |
848 : (is_dir_sep (dir[dir.length()-1]) | |
849 ? dir + file | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7520
diff
changeset
|
850 : dir + file_ops::dir_sep_char () + file); |
7272 | 851 } |