| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* unixfsys.d - Unix file system interface |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <string.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <limits.h> |
| 18 |
|
| 19 |
#ifndef _MSC_VER |
| 20 |
# include <unistd.h> |
| 21 |
#else |
| 22 |
# include <io.h> |
| 23 |
# include <direct.h> |
| 24 |
# define F_OK 0 |
| 25 |
typedef int mode_t; |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <sys/types.h> |
| 29 |
#include <ecl/ecl.h> |
| 30 |
#ifdef HAVE_PWD_H |
| 31 |
# include <pwd.h> |
| 32 |
#endif |
| 33 |
#include <sys/stat.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <ecl/ecl-inl.h> |
| 36 |
#include <ecl/internal.h> |
| 37 |
#ifdef HAVE_DIRENT_H |
| 38 |
# include <dirent.h> |
| 39 |
#else |
| 40 |
# if !defined(_MSC_VER) |
| 41 |
# include <sys/dir.h> |
| 42 |
# endif |
| 43 |
#endif |
| 44 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 45 |
# include <windows.h> |
| 46 |
# undef ERROR |
| 47 |
#endif |
| 48 |
#include <fcntl.h> |
| 49 |
#include <errno.h> |
| 50 |
|
| 51 |
#if defined(ECL_MS_WINDOWS_HOST) && defined(ECL_UNICODE) |
| 52 |
cl_object |
| 53 |
ecl_make_simple_filename(const ecl_filename_char *x, cl_fixnum size) |
| 54 |
{ |
| 55 |
if (size < 0) { |
| 56 |
size = wcslen(x); |
| 57 |
} |
| 58 |
cl_object vec = si_make_vector(@'ext::byte16', /* element-type */ |
| 59 |
ecl_make_fixnum(size+1), /* size */ |
| 60 |
ECL_NIL, /* adjustable */ |
| 61 |
ECL_NIL, /* fillp */ |
| 62 |
ECL_NIL, /* displaced */ |
| 63 |
ECL_NIL); /* displaced-offset */ |
| 64 |
for (cl_index i = 0; i < size; i++) { |
| 65 |
vec->vector.self.b16[i] = x[i]; |
| 66 |
} |
| 67 |
vec->vector.self.b16[size] = 0; |
| 68 |
/* use the same trick as for base strings to store the null |
| 69 |
* terminator: allocate one element more, adjust fill-pointer and |
| 70 |
* dimension */ |
| 71 |
vec->vector.fillp = vec->vector.dim = size; |
| 72 |
return vec; |
| 73 |
} |
| 74 |
|
| 75 |
cl_object |
| 76 |
ecl_concatenate_filename(cl_object x, cl_object y) |
| 77 |
{ |
| 78 |
cl_index size = x->vector.fillp + y->vector.fillp; |
| 79 |
cl_object vec = si_make_vector(@'ext::byte16', /* element-type */ |
| 80 |
ecl_make_fixnum(size+1), /* size */ |
| 81 |
ECL_NIL, /* adjustable */ |
| 82 |
ECL_NIL, /* fillp */ |
| 83 |
ECL_NIL, /* displaced */ |
| 84 |
ECL_NIL); /* displaced-offset */ |
| 85 |
if (x->vector.elttype == ecl_aet_bc) { |
| 86 |
for (cl_index i = 0; i < x->vector.fillp; i++) { |
| 87 |
vec->vector.self.b16[i] = x->vector.self.bc[i]; |
| 88 |
} |
| 89 |
} else if (x->vector.elttype == ecl_aet_b16) { |
| 90 |
for (cl_index i = 0; i < x->vector.fillp; i++) { |
| 91 |
vec->vector.self.b16[i] = x->vector.self.b16[i]; |
| 92 |
} |
| 93 |
} else { |
| 94 |
ecl_internal_error("Wrong type for first argument to ecl_concatenate_filename"); |
| 95 |
} |
| 96 |
if (y->vector.elttype == ecl_aet_bc) { |
| 97 |
for (cl_index i = 0; i < y->vector.fillp; i++) { |
| 98 |
vec->vector.self.b16[i + x->vector.fillp] = y->vector.self.bc[i]; |
| 99 |
} |
| 100 |
} else if (y->vector.elttype == ecl_aet_b16) { |
| 101 |
for (cl_index i = 0; i < y->vector.fillp; i++) { |
| 102 |
vec->vector.self.b16[i + x->vector.fillp] = y->vector.self.b16[i]; |
| 103 |
} |
| 104 |
} else { |
| 105 |
ecl_internal_error("Wrong type for second argument to ecl_concatenate_filename"); |
| 106 |
} |
| 107 |
vec->vector.self.b16[size] = 0; |
| 108 |
vec->vector.fillp = vec->vector.dim = size; |
| 109 |
return vec; |
| 110 |
} |
| 111 |
|
| 112 |
cl_object |
| 113 |
ecl_alloc_filename(cl_index len, cl_object adjustable) |
| 114 |
{ |
| 115 |
cl_object ret = si_make_vector(@'ext::byte16', /* element-type */ |
| 116 |
ecl_make_fixnum(len+1),/* size */ |
| 117 |
adjustable, /* adjustable */ |
| 118 |
ecl_make_fixnum(0), /* fillp */ |
| 119 |
ECL_NIL, /* displaced */ |
| 120 |
ECL_NIL); /* displaced-offset */ |
| 121 |
ret->vector.self.b16[len] = 0; |
| 122 |
return ret; |
| 123 |
} |
| 124 |
|
| 125 |
cl_object |
| 126 |
ecl_decode_filename(cl_object x, cl_object len) |
| 127 |
{ |
| 128 |
return si_octets_to_string(5, x, |
| 129 |
@':end', len, |
| 130 |
@':external-format', @':ucs-2'); |
| 131 |
} |
| 132 |
|
| 133 |
cl_object |
| 134 |
ecl_encode_filename(cl_object x, cl_object len) |
| 135 |
{ |
| 136 |
return si_string_to_octets(9, x, |
| 137 |
@':end', len, |
| 138 |
@':null-terminate', ECL_T, |
| 139 |
@':element-type', @'ext::byte16', |
| 140 |
@':external-format', @':ucs-2'); |
| 141 |
} |
| 142 |
|
| 143 |
#else |
| 144 |
cl_object |
| 145 |
ecl_decode_filename(cl_object x, cl_object len) |
| 146 |
{ |
| 147 |
return si_octets_to_string(3, x, @':end', len); |
| 148 |
} |
| 149 |
|
| 150 |
cl_object |
| 151 |
ecl_encode_filename(cl_object x, cl_object len) |
| 152 |
{ |
| 153 |
return si_string_to_octets(7, x, |
| 154 |
@':end', len, |
| 155 |
@':null-terminate', ECL_T, |
| 156 |
@':element-type', @'base-char'); |
| 157 |
} |
| 158 |
#endif |
| 159 |
|
| 160 |
static int |
| 161 |
safe_chdir(const ecl_filename_char *path, cl_object prefix) |
| 162 |
{ |
| 163 |
if (prefix != ECL_NIL) { |
| 164 |
cl_object aux = ecl_make_constant_filename(path,-1); |
| 165 |
aux = ecl_concatenate_filename(prefix, aux); |
| 166 |
return safe_chdir(ecl_filename_self(aux), ECL_NIL); |
| 167 |
} else { |
| 168 |
int output; |
| 169 |
ecl_disable_interrupts(); |
| 170 |
output = ecl_chdir((ecl_filename_char *)path); |
| 171 |
ecl_enable_interrupts(); |
| 172 |
return output; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
static int |
| 177 |
safe_stat(const ecl_filename_char *path, ecl_stat_struct *sb) |
| 178 |
{ |
| 179 |
int output; |
| 180 |
ecl_disable_interrupts(); |
| 181 |
output = ecl_stat(path, sb); |
| 182 |
ecl_enable_interrupts(); |
| 183 |
return output; |
| 184 |
} |
| 185 |
|
| 186 |
#ifdef HAVE_LSTAT |
| 187 |
static int |
| 188 |
safe_lstat(const ecl_filename_char *path, ecl_stat_struct *sb) |
| 189 |
{ |
| 190 |
int output; |
| 191 |
ecl_disable_interrupts(); |
| 192 |
output = lstat(path, sb); |
| 193 |
ecl_enable_interrupts(); |
| 194 |
return output; |
| 195 |
} |
| 196 |
#endif |
| 197 |
|
| 198 |
/* |
| 199 |
* Finds current directory by using getcwd() with an adjustable |
| 200 |
* string which grows until it can host the whole path. |
| 201 |
*/ |
| 202 |
static cl_object |
| 203 |
current_dir(void) { |
| 204 |
ecl_filename_char *output; |
| 205 |
const ecl_filename_char *ok; |
| 206 |
#ifdef _MSC_VER |
| 207 |
ecl_filename_char *c; |
| 208 |
#endif |
| 209 |
cl_index size = 128; |
| 210 |
|
| 211 |
do { |
| 212 |
output = ecl_alloc_atomic((size+2)*sizeof(ecl_filename_char)); |
| 213 |
ecl_disable_interrupts(); |
| 214 |
ok = ecl_getcwd(output, size); |
| 215 |
if (ok == NULL && errno != ERANGE) { |
| 216 |
perror("ext::getcwd error"); |
| 217 |
ecl_internal_error("Can't work without CWD"); |
| 218 |
} |
| 219 |
ecl_enable_interrupts(); |
| 220 |
size += 256; |
| 221 |
} while (ok == NULL); |
| 222 |
size = ecl_fstrlen(output); |
| 223 |
#ifdef _MSC_VER |
| 224 |
for (c = output; *c; c++) { |
| 225 |
if (*c == '\\') *c = '/'; |
| 226 |
} |
| 227 |
#endif |
| 228 |
if (output[size-1] != '/') { |
| 229 |
output[size++] = '/'; |
| 230 |
output[size] = '\0'; |
| 231 |
} |
| 232 |
return ecl_make_constant_filename(output, size); |
| 233 |
} |
| 234 |
|
| 235 |
/* |
| 236 |
* Using a certain path, guess the type of the object it points to. |
| 237 |
*/ |
| 238 |
|
| 239 |
static cl_object |
| 240 |
file_kind(ecl_filename_char *filename, bool follow_links) { |
| 241 |
cl_object output; |
| 242 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 243 |
DWORD dw; |
| 244 |
ecl_disable_interrupts(); |
| 245 |
dw = ecl_GetFileAttributes( filename ); |
| 246 |
if (dw == -1) |
| 247 |
output = ECL_NIL; |
| 248 |
else if ( dw & FILE_ATTRIBUTE_DIRECTORY ) |
| 249 |
output = @':directory'; |
| 250 |
else |
| 251 |
output = @':file'; |
| 252 |
ecl_enable_interrupts(); |
| 253 |
#elif defined(EMSCRIPTEN) && defined(HAVE_DIRENT_H) |
| 254 |
/* We don't want to call stat unless we have to on emscripten |
| 255 |
* because getting the size of a file may involve a network call for |
| 256 |
* lazy loading files. Since we don't care about the size but only |
| 257 |
* the kind of file we have, we can use other functions to get |
| 258 |
* the information. */ |
| 259 |
ecl_stat_struct buf; |
| 260 |
DIR *dir; |
| 261 |
if (!follow_links) { |
| 262 |
char buffer; |
| 263 |
if (readlink(filename, &buffer, 1) >= 0) |
| 264 |
return @':link'; |
| 265 |
else if (errno == ENOENT) |
| 266 |
return ECL_NIL; |
| 267 |
} |
| 268 |
dir = opendir(filename); |
| 269 |
if (dir != NULL) { |
| 270 |
closedir(dir); |
| 271 |
output = @':directory'; |
| 272 |
} else if (errno == ENOENT) |
| 273 |
output = ECL_NIL; |
| 274 |
else if (errno == ENOTDIR) |
| 275 |
output = @':file'; |
| 276 |
else if (safe_stat(filename, &buf) < 0) |
| 277 |
output = ECL_NIL; |
| 278 |
else if (S_ISDIR(buf.st_mode)) |
| 279 |
output = @':directory'; |
| 280 |
else if (S_ISREG(buf.st_mode)) |
| 281 |
output = @':file'; |
| 282 |
# ifdef S_ISFIFO |
| 283 |
else if (S_ISFIFO(buf.st_mode)) |
| 284 |
output = @':fifo'; |
| 285 |
# endif |
| 286 |
else |
| 287 |
output = @':special'; |
| 288 |
#else |
| 289 |
ecl_stat_struct buf; |
| 290 |
# ifdef HAVE_LSTAT |
| 291 |
if ((follow_links? safe_stat : safe_lstat)(filename, &buf) < 0) |
| 292 |
# else |
| 293 |
if (safe_stat(filename, &buf) < 0) |
| 294 |
# endif |
| 295 |
output = ECL_NIL; |
| 296 |
# ifdef HAVE_LSTAT |
| 297 |
else if (S_ISLNK(buf.st_mode)) |
| 298 |
output = @':link'; |
| 299 |
# endif |
| 300 |
else if (S_ISDIR(buf.st_mode)) |
| 301 |
output = @':directory'; |
| 302 |
else if (S_ISREG(buf.st_mode)) |
| 303 |
output = @':file'; |
| 304 |
# ifdef S_ISFIFO |
| 305 |
else if (S_ISFIFO(buf.st_mode)) |
| 306 |
output = @':fifo'; |
| 307 |
# endif |
| 308 |
else |
| 309 |
output = @':special'; |
| 310 |
#endif |
| 311 |
return output; |
| 312 |
} |
| 313 |
|
| 314 |
cl_object |
| 315 |
si_file_kind(cl_object filename, cl_object follow_links) { |
| 316 |
filename = si_coerce_to_filename(filename); |
| 317 |
@(return file_kind(ecl_filename_self(filename), !Null(follow_links))); |
| 318 |
} |
| 319 |
|
| 320 |
#if defined(HAVE_LSTAT) && !defined(ECL_MS_WINDOWS_HOST) |
| 321 |
static cl_object |
| 322 |
si_readlink(cl_object filename) { |
| 323 |
/* Given a filename which is a symlink, this routine returns |
| 324 |
* the value of this link in the form of a pathname. */ |
| 325 |
cl_index size = 128, written; |
| 326 |
char *output; |
| 327 |
cl_object kind; |
| 328 |
do { |
| 329 |
/* We reserve 2 characters for trailing '/' and '\0' */ |
| 330 |
output = ecl_alloc_atomic(size+2); |
| 331 |
ecl_disable_interrupts(); |
| 332 |
written = readlink(ecl_filename_self(filename), output, size); |
| 333 |
ecl_enable_interrupts(); |
| 334 |
if (written == -1) { |
| 335 |
return ECL_NIL; |
| 336 |
} |
| 337 |
size += 256; |
| 338 |
} while (written == size-256); |
| 339 |
output[written] = '\0'; |
| 340 |
kind = file_kind(output, FALSE); |
| 341 |
if (kind == @':directory') { |
| 342 |
output[written++] = '/'; |
| 343 |
output[written] = '\0'; |
| 344 |
} |
| 345 |
return ecl_decode_filename(ecl_make_constant_filename(output, written), ECL_NIL); |
| 346 |
} |
| 347 |
#endif /* HAVE_LSTAT */ |
| 348 |
|
| 349 |
static cl_object |
| 350 |
enter_directory(cl_object base_dir, cl_object subdir, bool ignore_if_failure) |
| 351 |
{ |
| 352 |
/* Assuming we start in "base_dir", enter a subdirectory named by |
| 353 |
* "subdir", which may be a string, :UP, :ABSOLUTE or :RELATIVE. |
| 354 |
* If the operation succeeds, return the truename of the resulting |
| 355 |
* path -- resolving any links in the process. */ |
| 356 |
cl_object aux, output, kind; |
| 357 |
if (subdir == @':absolute') { |
| 358 |
return cl_make_pathname(4, @':directory', ecl_list1(subdir), |
| 359 |
@':defaults', base_dir); |
| 360 |
} else if (subdir == @':relative') { |
| 361 |
/* Nothing to do */ |
| 362 |
return base_dir; |
| 363 |
} else if (subdir == @':up') { |
| 364 |
aux = @".."; |
| 365 |
} else { |
| 366 |
aux = subdir; |
| 367 |
} |
| 368 |
/* We now compose a new path based on the base directory and |
| 369 |
* the new component. We have to verify that the new pathname is |
| 370 |
* a directory and if it is a link recover the true name. */ |
| 371 |
aux = ecl_append(base_dir->pathname.directory, ecl_list1(aux)); |
| 372 |
output = cl_make_pathname(4, @':directory', aux, @':defaults', base_dir); |
| 373 |
aux = ecl_namestring(output, ECL_NAMESTRING_FORCE_BASE_STRING); |
| 374 |
/* We remove the trailing '/' from the namestring because the |
| 375 |
* POSIX library does not like it. */ |
| 376 |
ecl_filename_self(aux)[--aux->base_string.fillp] = 0; |
| 377 |
kind = file_kind(ecl_filename_self(aux), FALSE); |
| 378 |
if (kind == ECL_NIL) { |
| 379 |
if (ignore_if_failure) return ECL_NIL; |
| 380 |
FEcannot_open(output); |
| 381 |
#ifdef HAVE_LSTAT |
| 382 |
} else if (kind == @':link') { |
| 383 |
output = cl_truename(ecl_merge_pathnames(si_readlink(aux), |
| 384 |
base_dir, @':default')); |
| 385 |
if (output->pathname.name != ECL_NIL || |
| 386 |
output->pathname.type != ECL_NIL) |
| 387 |
goto WRONG_DIR; |
| 388 |
return output; |
| 389 |
#endif |
| 390 |
} else if (kind != @':directory') { |
| 391 |
WRONG_DIR: |
| 392 |
if (ignore_if_failure) return ECL_NIL; |
| 393 |
FEerror("The directory~& ~S~&in pathname~& ~S~&" |
| 394 |
"actually points to a file or special device.", |
| 395 |
2, subdir, base_dir); |
| 396 |
} |
| 397 |
if (subdir == @':up') { |
| 398 |
cl_object newdir= output->pathname.directory; |
| 399 |
newdir = ecl_nbutlast(newdir, 2); |
| 400 |
if (Null(newdir)) { |
| 401 |
if (ignore_if_failure) return ECL_NIL; |
| 402 |
FEerror("Pathname contained an :UP component " |
| 403 |
"that goes above the base directory:" |
| 404 |
"~& ~S", 1, output); |
| 405 |
} |
| 406 |
output->pathname.directory = newdir; |
| 407 |
} |
| 408 |
return output; |
| 409 |
} |
| 410 |
|
| 411 |
static cl_object |
| 412 |
make_absolute_pathname(cl_object orig_pathname) |
| 413 |
{ |
| 414 |
/* INV: si_coerce_to_file_pathname creates an absolute pathname */ |
| 415 |
return si_coerce_to_file_pathname(orig_pathname); |
| 416 |
} |
| 417 |
|
| 418 |
static cl_object |
| 419 |
make_base_pathname(cl_object pathname) |
| 420 |
{ |
| 421 |
return ecl_make_pathname(pathname->pathname.host, |
| 422 |
pathname->pathname.device, |
| 423 |
ecl_list1(@':absolute'), |
| 424 |
ECL_NIL, ECL_NIL, ECL_NIL, @':local'); |
| 425 |
} |
| 426 |
|
| 427 |
#define FOLLOW_SYMLINKS 1 |
| 428 |
|
| 429 |
static cl_object |
| 430 |
file_truename(cl_object pathname, cl_object filename, int flags) |
| 431 |
{ |
| 432 |
cl_object kind; |
| 433 |
if (Null(pathname)) { |
| 434 |
if (Null(filename)) { |
| 435 |
ecl_internal_error("file_truename:" |
| 436 |
" both FILENAME and PATHNAME are null!"); |
| 437 |
} |
| 438 |
pathname = cl_pathname(filename); |
| 439 |
} else if (Null(filename)) { |
| 440 |
filename = ecl_namestring(pathname, ECL_NAMESTRING_FORCE_BASE_STRING); |
| 441 |
if (Null(filename)) { |
| 442 |
FEerror("Unprintable pathname ~S found in TRUENAME", 1, pathname); |
| 443 |
} |
| 444 |
} |
| 445 |
kind = file_kind(ecl_filename_self(filename), FALSE); |
| 446 |
if (kind == ECL_NIL) { |
| 447 |
FEcannot_open(pathname); |
| 448 |
#ifdef HAVE_LSTAT |
| 449 |
} else if (kind == @':link' && (flags & FOLLOW_SYMLINKS)) { |
| 450 |
/* The link might be a relative pathname. In that case |
| 451 |
* we have to merge with the original pathname. On |
| 452 |
* the other hand, if the link is broken – return file |
| 453 |
* truename "as is". */ |
| 454 |
ecl_stat_struct filestatus; |
| 455 |
if (safe_stat(ecl_filename_self(filename), &filestatus) < 0) { |
| 456 |
@(return pathname kind); |
| 457 |
} |
| 458 |
filename = si_readlink(filename); |
| 459 |
pathname = ecl_make_pathname(pathname->pathname.host, |
| 460 |
pathname->pathname.device, |
| 461 |
pathname->pathname.directory, |
| 462 |
ECL_NIL, ECL_NIL, ECL_NIL, @':local'); |
| 463 |
pathname = ecl_merge_pathnames(filename, pathname, @':default'); |
| 464 |
return cl_truename(pathname); |
| 465 |
#endif |
| 466 |
} else if (kind == @':directory'){ |
| 467 |
/* If the pathname is a directory but we have supplied |
| 468 |
a file name, correct the type by appending a directory |
| 469 |
separator and re-parsing again the namestring */ |
| 470 |
if (pathname->pathname.name != ECL_NIL || |
| 471 |
pathname->pathname.type != ECL_NIL) { |
| 472 |
pathname = ecl_concatenate_filename(filename, @"/"); |
| 473 |
pathname = ecl_decode_filename(pathname, ECL_NIL); |
| 474 |
pathname = cl_truename(pathname); |
| 475 |
} |
| 476 |
} |
| 477 |
/* ECL does not contemplate version numbers |
| 478 |
in directory pathnames */ |
| 479 |
if (pathname->pathname.name == ECL_NIL && |
| 480 |
pathname->pathname.type == ECL_NIL) { |
| 481 |
/* We have to destructively change the |
| 482 |
* pathname version here. Otherwise |
| 483 |
* merge_pathnames will not do it. It is |
| 484 |
* safe because coerce_to_file_pathname |
| 485 |
* created a copy. */ |
| 486 |
pathname->pathname.version = ECL_NIL; |
| 487 |
} else { |
| 488 |
pathname->pathname.version = @':newest'; |
| 489 |
} |
| 490 |
@(return pathname kind); |
| 491 |
} |
| 492 |
|
| 493 |
/* |
| 494 |
* Search the actual name of the directory of a pathname, |
| 495 |
* going through links if they exist. Default is |
| 496 |
* current directory |
| 497 |
*/ |
| 498 |
cl_object |
| 499 |
cl_truename(cl_object orig_pathname) |
| 500 |
{ |
| 501 |
if (!Null(cl_streamp(orig_pathname))) |
| 502 |
@(return ecl_stream_truename(orig_pathname)); |
| 503 |
|
| 504 |
cl_object pathname = make_absolute_pathname(orig_pathname); |
| 505 |
cl_object base_dir = make_base_pathname(pathname); |
| 506 |
cl_object dir; |
| 507 |
/* We process the directory part of the filename, removing all |
| 508 |
* possible symlinks. To do so, we inspect recursively the |
| 509 |
* directory which contains our file, and come back. We also have to |
| 510 |
* ensure that the filename itself does not point to a symlink: if so, |
| 511 |
* then we resolve the value of the symlink and continue traversing |
| 512 |
* the filesystem. |
| 513 |
*/ |
| 514 |
for (dir = pathname->pathname.directory; !Null(dir); dir = ECL_CONS_CDR(dir)) |
| 515 |
{ |
| 516 |
base_dir = enter_directory(base_dir, ECL_CONS_CAR(dir), 0); |
| 517 |
} |
| 518 |
pathname = ecl_merge_pathnames(base_dir, pathname, @':default'); |
| 519 |
@(return file_truename(pathname, ECL_NIL, FOLLOW_SYMLINKS)); |
| 520 |
} |
| 521 |
|
| 522 |
int |
| 523 |
ecl_backup_open(const ecl_filename_char *filename, int option, int mode) |
| 524 |
{ |
| 525 |
cl_index length = ecl_fstrlen(filename); |
| 526 |
ecl_filename_char *backupfilename = ecl_alloc_atomic((length + 5)*sizeof(ecl_filename_char)); |
| 527 |
if (backupfilename == NULL) { |
| 528 |
FElibc_error("Cannot allocate memory for backup filename", 0); |
| 529 |
} |
| 530 |
|
| 531 |
ecl_fstrcat(ecl_fstrcpy(backupfilename, filename), ecl_fstr(".BAK")); |
| 532 |
ecl_disable_interrupts(); |
| 533 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 534 |
/* Windows' rename doesn't replace an existing file */ |
| 535 |
if (ecl_access(backupfilename, F_OK) == 0 && ecl_unlink(backupfilename)) { |
| 536 |
ecl_enable_interrupts(); |
| 537 |
FElibc_error("Cannot remove the file ~S", 1, |
| 538 |
ecl_decode_filename(ecl_make_constant_filename(backupfilename,-1), ECL_NIL)); |
| 539 |
} |
| 540 |
#endif |
| 541 |
if (ecl_rename(filename, backupfilename)) { |
| 542 |
ecl_enable_interrupts(); |
| 543 |
FElibc_error("Cannot rename the file ~S to ~S.", 2, |
| 544 |
ecl_decode_filename(ecl_make_constant_filename(filename,-1), ECL_NIL), |
| 545 |
ecl_decode_filename(ecl_make_constant_filename(backupfilename,-1), ECL_NIL)); |
| 546 |
} |
| 547 |
ecl_enable_interrupts(); |
| 548 |
ecl_dealloc(backupfilename); |
| 549 |
return ecl_open(filename, option, mode); |
| 550 |
} |
| 551 |
|
| 552 |
cl_object |
| 553 |
ecl_file_len(int f) |
| 554 |
{ |
| 555 |
ecl_stat_struct filestatus; |
| 556 |
memset(&filestatus, 0, sizeof(filestatus)); |
| 557 |
ecl_disable_interrupts(); |
| 558 |
ecl_fstat(f, &filestatus); |
| 559 |
ecl_enable_interrupts(); |
| 560 |
#ifdef S_ISFIFO |
| 561 |
if (S_ISFIFO(filestatus.st_mode)) { |
| 562 |
return ECL_NIL; |
| 563 |
} else { |
| 564 |
return ecl_make_integer(filestatus.st_size); |
| 565 |
} |
| 566 |
#else |
| 567 |
return ecl_make_integer(filestatus.st_size); |
| 568 |
#endif |
| 569 |
} |
| 570 |
|
| 571 |
@(defun rename-file (oldn newn &key (if_exists @':error')) |
| 572 |
cl_object old_filename, new_filename, old_truename, new_truename; |
| 573 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 574 |
int error; |
| 575 |
#endif |
| 576 |
@ |
| 577 |
|
| 578 |
/* 1) Get the old filename, and complain if it has wild components, |
| 579 |
* or if it does not exist. Notice that the filename to be renamed |
| 580 |
* is not the truename, because we might be renaming a symbolic link. |
| 581 |
*/ |
| 582 |
old_truename = cl_truename(oldn); |
| 583 |
old_filename = si_coerce_to_filename(old_truename); |
| 584 |
|
| 585 |
/* 2) Create the new file name. */ |
| 586 |
newn = ecl_merge_pathnames(newn, oldn, @':newest'); |
| 587 |
new_filename = si_coerce_to_filename(newn); |
| 588 |
|
| 589 |
while (if_exists == @':error' || if_exists == ECL_NIL) |
| 590 |
{ |
| 591 |
if (file_kind(ecl_filename_self(new_filename), TRUE) == ECL_NIL) { |
| 592 |
if_exists = ECL_T; |
| 593 |
break; |
| 594 |
} |
| 595 |
/* if the file already exists */ |
| 596 |
if (if_exists == @':error') { |
| 597 |
const char *msg = "When trying to rename ~S, ~S already exists"; |
| 598 |
if_exists = |
| 599 |
si_signal_simple_error |
| 600 |
(6, @'file-error', /* condition */ |
| 601 |
@':supersede', /* continuable */ |
| 602 |
/* format */ |
| 603 |
ecl_make_constant_base_string(msg,strlen(msg)), |
| 604 |
cl_list(2, oldn, newn), /* format args */ |
| 605 |
@':pathname', /* file-error options */ |
| 606 |
new_filename); |
| 607 |
if (if_exists == ECL_T) if_exists= @':error'; |
| 608 |
} |
| 609 |
if (if_exists == ECL_NIL) { |
| 610 |
@(return ECL_NIL ECL_NIL ECL_NIL); |
| 611 |
} |
| 612 |
} |
| 613 |
if (ecl_unlikely(if_exists != @':supersede' && if_exists != ECL_T)) { |
| 614 |
/* invalid key */ |
| 615 |
FEerror("~S is an illegal IF-EXISTS option for RENAME-FILE.", |
| 616 |
1, if_exists); |
| 617 |
} |
| 618 |
{ |
| 619 |
ecl_disable_interrupts(); |
| 620 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 621 |
error = SetErrorMode(0); |
| 622 |
if (ecl_MoveFile(ecl_filename_self(old_filename), |
| 623 |
ecl_filename_self(new_filename))) { |
| 624 |
SetErrorMode(error); |
| 625 |
goto SUCCESS; |
| 626 |
} |
| 627 |
switch (GetLastError()) { |
| 628 |
case ERROR_ALREADY_EXISTS: |
| 629 |
case ERROR_FILE_EXISTS: |
| 630 |
break; |
| 631 |
default: |
| 632 |
goto FAILURE_CLOBBER; |
| 633 |
}; |
| 634 |
if (ecl_MoveFileEx(ecl_filename_self(old_filename), |
| 635 |
ecl_filename_self(new_filename), |
| 636 |
MOVEFILE_REPLACE_EXISTING)) { |
| 637 |
SetErrorMode(error); |
| 638 |
goto SUCCESS; |
| 639 |
} |
| 640 |
/* fallback on old behavior */ |
| 641 |
ecl_DeleteFile(ecl_filename_self(new_filename)); |
| 642 |
if (ecl_MoveFile(ecl_filename_self(old_filename), |
| 643 |
ecl_filename_self(new_filename))) { |
| 644 |
SetErrorMode(error); |
| 645 |
goto SUCCESS; |
| 646 |
} |
| 647 |
/* fall through */ |
| 648 |
#else |
| 649 |
if (ecl_rename(ecl_filename_self(old_filename), |
| 650 |
ecl_filename_self(new_filename)) == 0) { |
| 651 |
goto SUCCESS; |
| 652 |
} |
| 653 |
#endif |
| 654 |
} |
| 655 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 656 |
FAILURE_CLOBBER: |
| 657 |
#endif |
| 658 |
ecl_enable_interrupts(); |
| 659 |
{ |
| 660 |
cl_object c_error = _ecl_strerror(errno); |
| 661 |
const char *msg = "Unable to rename file ~S to ~S.~%C library error: ~S"; |
| 662 |
si_signal_simple_error |
| 663 |
(6, @'file-error', /* condition */ |
| 664 |
ECL_NIL, /* continuable */ |
| 665 |
ecl_make_constant_base_string(msg,strlen(msg)), /* format */ |
| 666 |
cl_list(3, oldn, newn, c_error), /* format args */ |
| 667 |
@':pathname', /* file-error options */ |
| 668 |
oldn); |
| 669 |
} |
| 670 |
|
| 671 |
SUCCESS: |
| 672 |
ecl_enable_interrupts(); |
| 673 |
new_truename = cl_truename(newn); |
| 674 |
@(return newn old_truename new_truename); |
| 675 |
@) |
| 676 |
|
| 677 |
static int |
| 678 |
directory_pathname_p(cl_object path) |
| 679 |
{ |
| 680 |
return (path->pathname.name == ECL_NIL) && |
| 681 |
(path->pathname.type == ECL_NIL); |
| 682 |
} |
| 683 |
|
| 684 |
cl_object |
| 685 |
cl_delete_file(cl_object file) |
| 686 |
{ |
| 687 |
cl_object path = cl_pathname(file); |
| 688 |
int isdir = directory_pathname_p(path); |
| 689 |
cl_object filename = si_coerce_to_filename(path); |
| 690 |
int ok; |
| 691 |
|
| 692 |
ecl_disable_interrupts(); |
| 693 |
ok = (isdir? ecl_rmdir : ecl_unlink)(ecl_filename_self(filename)); |
| 694 |
ecl_enable_interrupts(); |
| 695 |
|
| 696 |
if (ok < 0) { |
| 697 |
const char *msg = |
| 698 |
isdir? |
| 699 |
"Cannot delete the directory ~S.~%C library error: ~S" : |
| 700 |
"Cannot delete the file ~S.~%C library error: ~S"; |
| 701 |
cl_object c_error = _ecl_strerror(errno); |
| 702 |
si_signal_simple_error |
| 703 |
(6, @'file-error', /* condition */ |
| 704 |
ECL_T, /* continuable */ |
| 705 |
ecl_make_constant_base_string(msg,strlen(msg)), /* format */ |
| 706 |
cl_list(2, file, c_error), /* format args */ |
| 707 |
@':pathname', /* file-error options */ |
| 708 |
file); |
| 709 |
} |
| 710 |
@(return ECL_T); |
| 711 |
} |
| 712 |
|
| 713 |
cl_object |
| 714 |
cl_probe_file(cl_object file) |
| 715 |
{ |
| 716 |
/* INV: Both SI:FILE-KIND and TRUENAME complain if "file" has wildcards */ |
| 717 |
@(return (si_file_kind(file, ECL_T) != ECL_NIL? cl_truename(file) : ECL_NIL)); |
| 718 |
} |
| 719 |
|
| 720 |
cl_object |
| 721 |
cl_file_write_date(cl_object file) |
| 722 |
{ |
| 723 |
cl_object time, filename = si_coerce_to_filename(file); |
| 724 |
ecl_stat_struct filestatus; |
| 725 |
if (safe_stat(ecl_filename_self(filename), &filestatus) < 0) { |
| 726 |
time = ECL_NIL; |
| 727 |
} else { |
| 728 |
time = UTC_time_to_universal_time(filestatus.st_mtime); |
| 729 |
} |
| 730 |
@(return time); |
| 731 |
} |
| 732 |
|
| 733 |
cl_object |
| 734 |
cl_file_author(cl_object file) |
| 735 |
{ |
| 736 |
cl_object output, filename = si_coerce_to_filename(file); |
| 737 |
ecl_stat_struct filestatus; |
| 738 |
if (safe_stat(ecl_filename_self(filename), &filestatus) < 0) { |
| 739 |
const char *msg = "Unable to read file author for ~S." |
| 740 |
"~%C library error: ~S"; |
| 741 |
cl_object c_error = _ecl_strerror(errno); |
| 742 |
si_signal_simple_error |
| 743 |
(6, @'file-error', /* condition */ |
| 744 |
ECL_T, /* continuable */ |
| 745 |
ecl_make_constant_base_string(msg,strlen(msg)), /* format */ |
| 746 |
cl_list(2, file, c_error), /* format args */ |
| 747 |
@':pathname', /* file-error options */ |
| 748 |
file); |
| 749 |
} |
| 750 |
#ifdef HAVE_PWD_H |
| 751 |
{ |
| 752 |
struct passwd *pwent; |
| 753 |
ecl_disable_interrupts(); |
| 754 |
pwent = getpwuid(filestatus.st_uid); |
| 755 |
ecl_enable_interrupts(); |
| 756 |
output = ecl_make_simple_base_string(pwent->pw_name,-1); |
| 757 |
output = si_octets_to_string(1, output); |
| 758 |
} |
| 759 |
#else |
| 760 |
output = @"UNKNOWN"; |
| 761 |
#endif |
| 762 |
@(return output); |
| 763 |
} |
| 764 |
|
| 765 |
cl_object |
| 766 |
ecl_homedir_pathname(cl_object user) |
| 767 |
{ |
| 768 |
cl_index i; |
| 769 |
cl_object namestring; |
| 770 |
const ecl_filename_char *h; |
| 771 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 772 |
const ecl_filename_char *d; |
| 773 |
#endif |
| 774 |
if (!Null(user)){ |
| 775 |
#ifdef HAVE_PWD_H |
| 776 |
struct passwd *pwent = NULL; |
| 777 |
#endif |
| 778 |
ecl_filename_char *p; |
| 779 |
i = ecl_length(user); |
| 780 |
/* This ensures that our string has the right length |
| 781 |
and it is terminated with a '\0' */ |
| 782 |
user = ecl_encode_filename(user, ECL_NIL); |
| 783 |
p = ecl_filename_self(user); |
| 784 |
if (i > 0 && *p == '~') { |
| 785 |
p++; |
| 786 |
i--; |
| 787 |
} |
| 788 |
if (i == 0) |
| 789 |
return ecl_homedir_pathname(ECL_NIL); |
| 790 |
#ifdef HAVE_PWD_H |
| 791 |
pwent = getpwnam(p); |
| 792 |
if (pwent == NULL) |
| 793 |
FEerror("Unknown user ~S.", 1, user); |
| 794 |
namestring = ecl_make_simple_filename(pwent->pw_dir,-1); |
| 795 |
#endif |
| 796 |
FEerror("Unknown user ~S.", 1, user); |
| 797 |
} else if ((h = ecl_getenv(ecl_fstr("HOME")))) { |
| 798 |
namestring = ecl_make_simple_filename(h,-1); |
| 799 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 800 |
} else if ((h = ecl_getenv(ecl_fstr("HOMEPATH"))) && (d = ecl_getenv(ecl_fstr("HOMEDRIVE")))) { |
| 801 |
namestring = ecl_concatenate_filename(ecl_make_constant_filename(d,-1), |
| 802 |
ecl_make_constant_filename(h,-1)); |
| 803 |
#endif |
| 804 |
} else { |
| 805 |
namestring = @"/"; |
| 806 |
} |
| 807 |
if (ecl_filename_self(namestring)[0] == '~') { |
| 808 |
FEerror("Not a valid home pathname ~S", 1, namestring); |
| 809 |
} |
| 810 |
i = namestring->base_string.fillp; |
| 811 |
if (!IS_DIR_SEPARATOR(ecl_filename_self(namestring)[i-1])) |
| 812 |
namestring = ecl_concatenate_filename(namestring, |
| 813 |
si_coerce_to_base_string(ECL_CODE_CHAR(DIR_SEPARATOR))); |
| 814 |
namestring = ecl_decode_filename(namestring, ECL_NIL); |
| 815 |
return cl_parse_namestring(3, namestring, ECL_NIL, ECL_NIL); |
| 816 |
} |
| 817 |
|
| 818 |
@(defun user_homedir_pathname (&optional host) |
| 819 |
@ |
| 820 |
/* Ignore optional host argument. */ |
| 821 |
@(return ecl_homedir_pathname(ECL_NIL)); |
| 822 |
@) |
| 823 |
|
| 824 |
static bool |
| 825 |
string_match(const ecl_filename_char *s, cl_object pattern) |
| 826 |
{ |
| 827 |
if (pattern == ECL_NIL || pattern == @':wild') { |
| 828 |
return 1; |
| 829 |
} else { |
| 830 |
cl_object string_decoded = ecl_decode_filename(ecl_make_constant_filename(s,-1), ECL_NIL); |
| 831 |
return ecl_string_match(string_decoded, 0, string_decoded->base_string.fillp, |
| 832 |
pattern, 0, ecl_length(pattern)); |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
static inline cl_object |
| 837 |
parse_directory_entry(const ecl_filename_char *text, cl_object text_mask, cl_object prefix, cl_object pathname_mask, int flags) |
| 838 |
{ |
| 839 |
const cl_env_ptr the_env = ecl_process_env(); |
| 840 |
cl_object component, component_string, component_path, kind; |
| 841 |
if (text[0] == '.' && |
| 842 |
(text[1] == '\0' || |
| 843 |
(text[1] == '.' && text[2] == '\0'))) |
| 844 |
return ECL_NIL; |
| 845 |
if (!string_match(text, text_mask)) |
| 846 |
return ECL_NIL; |
| 847 |
component = ecl_make_constant_filename(text,-1); |
| 848 |
component = ecl_concatenate_filename(prefix, component); |
| 849 |
component_string = ecl_decode_filename(component, ECL_NIL); |
| 850 |
component_path = cl_pathname(component_string); |
| 851 |
if (!Null(pathname_mask)) { |
| 852 |
if (Null(cl_pathname_match_p(component_string, pathname_mask))) |
| 853 |
return ECL_NIL; |
| 854 |
} |
| 855 |
component_path = file_truename(component_path, component, flags); |
| 856 |
kind = ecl_nth_value(the_env, 1); |
| 857 |
return CONS(component_path, kind); |
| 858 |
} |
| 859 |
|
| 860 |
/* |
| 861 |
* list_current_directory() lists the files and directories which are contained |
| 862 |
* in the current working directory (as given by current_dir()). If ONLY_DIR is |
| 863 |
* true, the list is made of only the directories -- a propert which is checked |
| 864 |
* by following the symlinks. |
| 865 |
*/ |
| 866 |
static cl_object |
| 867 |
list_directory(cl_object base_dir, cl_object text_mask, cl_object pathname_mask, |
| 868 |
int flags) |
| 869 |
{ |
| 870 |
cl_object out = ECL_NIL; |
| 871 |
cl_object prefix = ecl_namestring(base_dir, ECL_NAMESTRING_FORCE_BASE_STRING); |
| 872 |
cl_object entry; |
| 873 |
|
| 874 |
#ifdef ECL_MS_WINDOWS_HOST |
| 875 |
ecl_WIN32_FIND_DATA fd; |
| 876 |
HANDLE hFind = NULL; |
| 877 |
BOOL found = FALSE; |
| 878 |
|
| 879 |
ecl_disable_interrupts(); |
| 880 |
for (;;) { |
| 881 |
if (hFind == NULL) { |
| 882 |
cl_object aux = @".\\*"; |
| 883 |
cl_object mask = ecl_concatenate_filename(prefix, aux); |
| 884 |
hFind = ecl_FindFirstFile(ecl_filename_self(mask), &fd); |
| 885 |
if (hFind == INVALID_HANDLE_VALUE) { |
| 886 |
out = ECL_NIL; |
| 887 |
goto OUTPUT; |
| 888 |
} |
| 889 |
found = TRUE; |
| 890 |
} else { |
| 891 |
found = ecl_FindNextFile(hFind, &fd); |
| 892 |
} |
| 893 |
|
| 894 |
if (!found) |
| 895 |
break; |
| 896 |
|
| 897 |
if (!Null(entry = parse_directory_entry(fd.cFileName, text_mask, prefix, pathname_mask, flags))) |
| 898 |
out = CONS(entry, out); |
| 899 |
} |
| 900 |
FindClose(hFind); |
| 901 |
#elif defined(HAVE_DIRENT_H) |
| 902 |
DIR *dir; |
| 903 |
struct dirent *d_entry; |
| 904 |
|
| 905 |
ecl_disable_interrupts(); |
| 906 |
dir = opendir(ecl_filename_self(prefix)); |
| 907 |
if (dir == NULL) { |
| 908 |
out = ECL_NIL; |
| 909 |
goto OUTPUT; |
| 910 |
} |
| 911 |
|
| 912 |
while ((d_entry = readdir(dir))) { |
| 913 |
if (!Null(entry = parse_directory_entry(d_entry->d_name, text_mask, prefix, pathname_mask, flags))) |
| 914 |
out = CONS(entry, out); |
| 915 |
} |
| 916 |
closedir(dir); |
| 917 |
#else /* sys/dir.h as in SYSV */ |
| 918 |
FILE *fp; |
| 919 |
char iobuffer[BUFSIZ]; |
| 920 |
DIRECTORY dir; |
| 921 |
|
| 922 |
ecl_disable_interrupts(); |
| 923 |
fp = ecl_fopen(ecl_filename_self(prefix), OPEN_R); |
| 924 |
if (fp == NULL) { |
| 925 |
out = ECL_NIL; |
| 926 |
goto OUTPUT; |
| 927 |
} |
| 928 |
setbuf(fp, iobuffer); |
| 929 |
for (;;) { |
| 930 |
if (fread(&dir, sizeof(DIRECTORY), 1, fp) <= 0) |
| 931 |
break; |
| 932 |
if (dir.d_ino == 0) |
| 933 |
continue; |
| 934 |
|
| 935 |
if (!Null(entry = parse_directory_entry(dir.d_name, text_mask, prefix, pathname_mask, flags))) |
| 936 |
out = CONS(entry, out); |
| 937 |
} |
| 938 |
fclose(fp); |
| 939 |
#endif |
| 940 |
|
| 941 |
OUTPUT: |
| 942 |
ecl_enable_interrupts(); |
| 943 |
return cl_nreverse(out); |
| 944 |
} |
| 945 |
|
| 946 |
/* |
| 947 |
* dir_files() lists all files which are contained in the current directory and |
| 948 |
* which match the masks in PATHNAME. This routine is essentially a wrapper for |
| 949 |
* list_current_directory(), which transforms the list of strings into a list |
| 950 |
* of pathnames. BASEDIR is the truename of the current directory and it is |
| 951 |
* used to build these pathnames. |
| 952 |
*/ |
| 953 |
static cl_object |
| 954 |
dir_files(cl_object base_dir, cl_object pathname, int flags) |
| 955 |
{ |
| 956 |
cl_object all_files, output = ECL_NIL; |
| 957 |
cl_object mask; |
| 958 |
cl_object name = pathname->pathname.name; |
| 959 |
cl_object type = pathname->pathname.type; |
| 960 |
if (name == ECL_NIL && type == ECL_NIL) { |
| 961 |
return cl_list(1, base_dir); |
| 962 |
} |
| 963 |
mask = ecl_make_pathname(ECL_NIL, ECL_NIL, ECL_NIL, |
| 964 |
name, type, pathname->pathname.version, |
| 965 |
@':local'); |
| 966 |
for (all_files = list_directory(base_dir, ECL_NIL, mask, flags); |
| 967 |
!Null(all_files); |
| 968 |
all_files = ECL_CONS_CDR(all_files)) |
| 969 |
{ |
| 970 |
cl_object record = ECL_CONS_CAR(all_files); |
| 971 |
cl_object new = ECL_CONS_CAR(record); |
| 972 |
cl_object kind = ECL_CONS_CDR(record); |
| 973 |
if (kind != @':directory') { |
| 974 |
output = CONS(new, output); |
| 975 |
} |
| 976 |
} |
| 977 |
return output; |
| 978 |
} |
| 979 |
|
| 980 |
/* |
| 981 |
* dir_recursive() performs the dirty job of DIRECTORY. The routine moves |
| 982 |
* through the filesystem looking for files and directories which match |
| 983 |
* the masks in the arguments PATHNAME and DIRECTORY, collecting them in a |
| 984 |
* list. |
| 985 |
*/ |
| 986 |
static cl_object |
| 987 |
dir_recursive(cl_object base_dir, cl_object directory, cl_object filemask, int flags) |
| 988 |
{ |
| 989 |
cl_object item, output = ECL_NIL; |
| 990 |
AGAIN: |
| 991 |
/* There are several possibilities here: |
| 992 |
* |
| 993 |
* 1) The list of subdirectories DIRECTORY is empty, and only PATHNAME |
| 994 |
* remains to be inspected. If there is no file name or type, then |
| 995 |
* we simply output the truename of the current directory. Otherwise |
| 996 |
* we have to find a file which corresponds to the description. |
| 997 |
*/ |
| 998 |
if (directory == ECL_NIL) { |
| 999 |
return ecl_nconc(dir_files(base_dir, filemask, flags), output); |
| 1000 |
} |
| 1001 |
/* |
| 1002 |
* 2) We have not yet exhausted the DIRECTORY component of the |
| 1003 |
* pathname. We have to enter some subdirectory, determined by |
| 1004 |
* CAR(DIRECTORY) and scan it. |
| 1005 |
*/ |
| 1006 |
item = ECL_CONS_CAR(directory); |
| 1007 |
|
| 1008 |
if (item == @':wild' || ecl_wild_string_p(item)) { |
| 1009 |
/* |
| 1010 |
* 2.1) If CAR(DIRECTORY) is a string or :WILD, we have to |
| 1011 |
* enter & scan all subdirectories in our curent directory. |
| 1012 |
*/ |
| 1013 |
cl_object next_dir = list_directory(base_dir, item, ECL_NIL, flags); |
| 1014 |
for (; !Null(next_dir); next_dir = ECL_CONS_CDR(next_dir)) { |
| 1015 |
cl_object record = ECL_CONS_CAR(next_dir); |
| 1016 |
cl_object component = ECL_CONS_CAR(record); |
| 1017 |
cl_object kind = ECL_CONS_CDR(record); |
| 1018 |
if (kind != @':directory') |
| 1019 |
continue; |
| 1020 |
item = dir_recursive(cl_pathname(component), |
| 1021 |
ECL_CONS_CDR(directory), |
| 1022 |
filemask, flags); |
| 1023 |
output = ecl_nconc(item, output); |
| 1024 |
} |
| 1025 |
} else if (item == @':wild-inferiors') { |
| 1026 |
/* |
| 1027 |
* 2.2) If CAR(DIRECTORY) is :WILD-INFERIORS, we have to do |
| 1028 |
* scan all subdirectories from _all_ levels, looking for a |
| 1029 |
* tree that matches the remaining part of DIRECTORY. |
| 1030 |
*/ |
| 1031 |
cl_object next_dir = list_directory(base_dir, ECL_NIL, ECL_NIL, flags); |
| 1032 |
for (; !Null(next_dir); next_dir = ECL_CONS_CDR(next_dir)) { |
| 1033 |
cl_object record = ECL_CONS_CAR(next_dir); |
| 1034 |
cl_object component = ECL_CONS_CAR(record); |
| 1035 |
cl_object kind = ECL_CONS_CDR(record); |
| 1036 |
if (kind != @':directory') |
| 1037 |
continue; |
| 1038 |
item = dir_recursive(cl_pathname(component), |
| 1039 |
directory, filemask, flags); |
| 1040 |
output = ecl_nconc(item, output); |
| 1041 |
} |
| 1042 |
directory = ECL_CONS_CDR(directory); |
| 1043 |
goto AGAIN; |
| 1044 |
} else { /* :ABSOLUTE, :RELATIVE, :UP, component without wildcards */ |
| 1045 |
/* |
| 1046 |
* 2.2) If CAR(DIRECTORY) is :ABSOLUTE, :RELATIVE or :UP we update |
| 1047 |
* the directory to reflect the root, the current or the parent one. |
| 1048 |
*/ |
| 1049 |
base_dir = enter_directory(base_dir, item, 1); |
| 1050 |
/* |
| 1051 |
* If enter_directory() fails, we simply ignore this path. This is |
| 1052 |
* what other implementations do and is consistent with the behavior |
| 1053 |
* for the file part. |
| 1054 |
*/ |
| 1055 |
if (Null(base_dir)) |
| 1056 |
return output; |
| 1057 |
directory = ECL_CONS_CDR(directory); |
| 1058 |
goto AGAIN; |
| 1059 |
} |
| 1060 |
return output; |
| 1061 |
} |
| 1062 |
|
| 1063 |
@(defun directory (mask &key (resolve_symlinks ECL_T) &allow_other_keys) |
| 1064 |
cl_object base_dir; |
| 1065 |
cl_object output; |
| 1066 |
@ |
| 1067 |
mask = si_coerce_to_file_pathname(mask); |
| 1068 |
mask = make_absolute_pathname(mask); |
| 1069 |
base_dir = make_base_pathname(mask); |
| 1070 |
output = dir_recursive(base_dir, mask->pathname.directory, mask, |
| 1071 |
Null(resolve_symlinks)? 0 : FOLLOW_SYMLINKS); |
| 1072 |
@(return output); |
| 1073 |
@) |
| 1074 |
|
| 1075 |
@(defun ext::getcwd (&optional (change_d_p_d ECL_NIL)) |
| 1076 |
cl_object output; |
| 1077 |
@ |
| 1078 |
output = cl_parse_namestring(3, ecl_decode_filename(current_dir(), ECL_NIL), ECL_NIL, ECL_NIL); |
| 1079 |
if (!Null(change_d_p_d)) { |
| 1080 |
ECL_SETQ(the_env, @'*default-pathname-defaults*', output); |
| 1081 |
} |
| 1082 |
@(return output); |
| 1083 |
@) |
| 1084 |
|
| 1085 |
cl_object |
| 1086 |
si_get_library_pathname(void) |
| 1087 |
{ |
| 1088 |
cl_object s = cl_core.library_pathname; |
| 1089 |
if (!Null(s)) { |
| 1090 |
goto OUTPUT_UNCHANGED; |
| 1091 |
} else { |
| 1092 |
ecl_filename_char *v = ecl_getenv(ecl_fstr("ECLDIR")); |
| 1093 |
if (v) { |
| 1094 |
s = ecl_make_constant_filename(v,-1); |
| 1095 |
goto OUTPUT; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 1099 |
{ |
| 1100 |
ecl_filename_char *buffer; |
| 1101 |
HMODULE hnd; |
| 1102 |
cl_index len, ep; |
| 1103 |
s = ecl_alloc_adjustable_filename(cl_core.path_max); |
| 1104 |
buffer = ecl_filename_self(s); |
| 1105 |
ecl_disable_interrupts(); |
| 1106 |
hnd = GetModuleHandle("ecl.dll"); |
| 1107 |
len = ecl_GetModuleFileName(hnd, buffer, cl_core.path_max-1); |
| 1108 |
ecl_enable_interrupts(); |
| 1109 |
if (len == 0) { |
| 1110 |
FEerror("GetModuleFileName failed (last error = ~S)", |
| 1111 |
1, ecl_make_fixnum(GetLastError())); |
| 1112 |
} |
| 1113 |
/* GetModuleFileName returns a file name. We have to strip |
| 1114 |
* the directory component. */ |
| 1115 |
for (; len > 0 && buffer[len-1] != '\\'; len--); |
| 1116 |
buffer[len] = '\0'; |
| 1117 |
s->base_string.fillp = len; |
| 1118 |
} |
| 1119 |
#else |
| 1120 |
s = ecl_make_constant_base_string(ECLDIR "/",-1); |
| 1121 |
#endif |
| 1122 |
OUTPUT: |
| 1123 |
{ |
| 1124 |
if (file_kind(ecl_filename_self(s), TRUE) == ECL_NIL) { |
| 1125 |
s = current_dir(); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
cl_core.library_pathname = ecl_decode_filename(s, ECL_NIL); |
| 1129 |
OUTPUT_UNCHANGED: |
| 1130 |
@(return cl_core.library_pathname); |
| 1131 |
} |
| 1132 |
|
| 1133 |
@(defun ext::chdir (directory &optional (change_d_p_d ECL_T)) |
| 1134 |
cl_object previous = si_getcwd(0); |
| 1135 |
cl_object namestring; |
| 1136 |
@ |
| 1137 |
/* This will fail if the new directory does not exist */ |
| 1138 |
directory = cl_truename(directory); |
| 1139 |
if (directory->pathname.name != ECL_NIL || |
| 1140 |
directory->pathname.type != ECL_NIL) |
| 1141 |
FEerror("~A is not a directory pathname.", 1, directory); |
| 1142 |
namestring = ecl_namestring(directory, |
| 1143 |
ECL_NAMESTRING_TRUNCATE_IF_ERROR | |
| 1144 |
ECL_NAMESTRING_FORCE_BASE_STRING); |
| 1145 |
if (safe_chdir(ecl_filename_self(namestring), ECL_NIL) < 0) { |
| 1146 |
cl_object c_error = _ecl_strerror(errno); |
| 1147 |
const char *msg = "Can't change the current directory to ~A." |
| 1148 |
"~%C library error: ~S"; |
| 1149 |
si_signal_simple_error |
| 1150 |
(6, @'file-error', /* condition */ |
| 1151 |
ECL_T, /* continuable */ |
| 1152 |
/* format */ |
| 1153 |
ecl_make_constant_base_string(msg,strlen(msg)), |
| 1154 |
cl_list(2, directory, c_error), /* format args */ |
| 1155 |
@':pathname', /* file-error options */ |
| 1156 |
directory); |
| 1157 |
} else if (change_d_p_d != ECL_NIL) { |
| 1158 |
ECL_SETQ(the_env, @'*default-pathname-defaults*', directory); |
| 1159 |
} |
| 1160 |
@(return previous); |
| 1161 |
@) |
| 1162 |
|
| 1163 |
cl_object |
| 1164 |
si_mkdir(cl_object directory, cl_object mode) |
| 1165 |
{ |
| 1166 |
int modeint, ok; |
| 1167 |
cl_object filename; |
| 1168 |
{ |
| 1169 |
/* Ensure a clean string, without trailing slashes, |
| 1170 |
* and null terminated */ |
| 1171 |
cl_index last = ecl_length(directory); |
| 1172 |
if (last > 1) { |
| 1173 |
ecl_character c = ecl_char(directory, last-1); |
| 1174 |
if (IS_DIR_SEPARATOR(c)) |
| 1175 |
last--; |
| 1176 |
} |
| 1177 |
filename = ecl_encode_filename(directory, ecl_make_fixnum(last)); |
| 1178 |
} |
| 1179 |
|
| 1180 |
if (ecl_unlikely(!ECL_FIXNUMP(mode) || |
| 1181 |
ecl_fixnum_minusp(mode) || |
| 1182 |
ecl_fixnum_greater(mode, ecl_make_fixnum(0777)))) { |
| 1183 |
FEwrong_type_nth_arg(@[si::mkdir], 2, mode, |
| 1184 |
ecl_make_integer_type(ecl_make_fixnum(0), |
| 1185 |
ecl_make_fixnum(0777))); |
| 1186 |
} |
| 1187 |
modeint = ecl_fixnum(mode); |
| 1188 |
ecl_disable_interrupts(); |
| 1189 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 1190 |
ok = ecl_mkdir(ecl_filename_self(filename)); |
| 1191 |
#else |
| 1192 |
ok = ecl_mkdir(ecl_filename_self(filename), modeint); |
| 1193 |
#endif |
| 1194 |
ecl_enable_interrupts(); |
| 1195 |
|
| 1196 |
if (ok < 0 && errno == EEXIST) { |
| 1197 |
@(return ECL_NIL); |
| 1198 |
} else if (ecl_unlikely(ok < 0)) { |
| 1199 |
cl_object c_error = _ecl_strerror(errno); |
| 1200 |
const char *msg = "Could not create directory ~S" |
| 1201 |
"~%C library error: ~S"; |
| 1202 |
si_signal_simple_error |
| 1203 |
(6, @'file-error', /* condition */ |
| 1204 |
ECL_T, /* continuable */ |
| 1205 |
/* format */ |
| 1206 |
ecl_make_constant_base_string(msg,strlen(msg)), |
| 1207 |
cl_list(2, filename, c_error), /* format args */ |
| 1208 |
@':pathname', /* file-error options */ |
| 1209 |
filename); |
| 1210 |
} |
| 1211 |
@(return filename); |
| 1212 |
} |
| 1213 |
|
| 1214 |
cl_object |
| 1215 |
si_mkstemp(cl_object template) |
| 1216 |
{ |
| 1217 |
cl_object output; |
| 1218 |
cl_index l; |
| 1219 |
int fd; |
| 1220 |
|
| 1221 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 1222 |
cl_object phys, dir, file; |
| 1223 |
ecl_filename_char strTempDir[MAX_PATH]; |
| 1224 |
ecl_filename_char strTempFileName[MAX_PATH]; |
| 1225 |
ecl_filename_char *s; |
| 1226 |
int ok; |
| 1227 |
|
| 1228 |
phys = cl_translate_logical_pathname(1, template); |
| 1229 |
dir = cl_make_pathname(8, |
| 1230 |
@':type', ECL_NIL, |
| 1231 |
@':name', ECL_NIL, |
| 1232 |
@':version', ECL_NIL, |
| 1233 |
@':defaults', phys); |
| 1234 |
dir = si_coerce_to_filename(dir); |
| 1235 |
file = cl_file_namestring(phys); |
| 1236 |
|
| 1237 |
l = dir->base_string.fillp; |
| 1238 |
ecl_fstrcpy(strTempDir, ecl_filename_self(dir)); |
| 1239 |
strTempDir[l] = 0; |
| 1240 |
for (s = strTempDir; *s; s++) |
| 1241 |
if (*s == '/') |
| 1242 |
*s = '\\'; |
| 1243 |
|
| 1244 |
ecl_disable_interrupts(); |
| 1245 |
ok = ecl_GetTempFileName(strTempDir, ecl_filename_self(file), 0, |
| 1246 |
strTempFileName); |
| 1247 |
ecl_enable_interrupts(); |
| 1248 |
if (!ok) { |
| 1249 |
output = ECL_NIL; |
| 1250 |
} else { |
| 1251 |
output = ecl_make_simple_filename(strTempFileName,-1); |
| 1252 |
} |
| 1253 |
#else |
| 1254 |
template = si_coerce_to_filename(template); |
| 1255 |
l = template->base_string.fillp; |
| 1256 |
output = ecl_alloc_simple_filename(l + 6); |
| 1257 |
ecl_fstrcat(ecl_fstrcpy(ecl_filename_self(output), ecl_filename_self(template)), |
| 1258 |
ecl_fstr("XXXXXX")); |
| 1259 |
|
| 1260 |
ecl_disable_interrupts(); |
| 1261 |
# ifdef HAVE_MKSTEMP |
| 1262 |
fd = mkstemp(ecl_filename_self(output)); |
| 1263 |
# else |
| 1264 |
if (mktemp(ecl_filename_self(output))) { |
| 1265 |
fd = ecl_open(ecl_filename_self(output), O_CREAT|O_TRUNC, 0666); |
| 1266 |
} else { |
| 1267 |
fd = -1; |
| 1268 |
} |
| 1269 |
# endif |
| 1270 |
ecl_enable_interrupts(); |
| 1271 |
|
| 1272 |
if (fd < 0) { |
| 1273 |
output = ECL_NIL; |
| 1274 |
} else { |
| 1275 |
close(fd); |
| 1276 |
} |
| 1277 |
#endif |
| 1278 |
@(return (Null(output)? output : cl_truename(ecl_decode_filename(output, ECL_NIL)))); |
| 1279 |
} |
| 1280 |
|
| 1281 |
cl_object |
| 1282 |
si_rmdir(cl_object directory) |
| 1283 |
{ |
| 1284 |
return cl_delete_file(cl_make_pathname(6, @':name', ECL_NIL, |
| 1285 |
@':type', ECL_NIL, |
| 1286 |
@':defaults', directory)); |
| 1287 |
} |
| 1288 |
|
| 1289 |
cl_object |
| 1290 |
si_copy_file(cl_object orig, cl_object dest) |
| 1291 |
{ |
| 1292 |
FILE *in, *out; |
| 1293 |
int ok = 0; |
| 1294 |
orig = si_coerce_to_filename(orig); |
| 1295 |
dest = si_coerce_to_filename(dest); |
| 1296 |
ecl_disable_interrupts(); |
| 1297 |
in = ecl_fopen(ecl_filename_self(orig), OPEN_R); |
| 1298 |
if (in) { |
| 1299 |
out = ecl_fopen(ecl_filename_self(dest), OPEN_W); |
| 1300 |
if (out) { |
| 1301 |
unsigned char *buffer = ecl_alloc_atomic(1024); |
| 1302 |
cl_index size; |
| 1303 |
do { |
| 1304 |
size = fread(buffer, 1, 1024, in); |
| 1305 |
fwrite(buffer, 1, size, out); |
| 1306 |
} while (size == 1024); |
| 1307 |
ok = 1; |
| 1308 |
fclose(out); |
| 1309 |
} |
| 1310 |
fclose(in); |
| 1311 |
} |
| 1312 |
ecl_enable_interrupts(); |
| 1313 |
@(return (ok? ECL_T : ECL_NIL)); |
| 1314 |
} |
| 1315 |
|
| 1316 |
cl_object |
| 1317 |
si_chmod(cl_object file, cl_object mode) |
| 1318 |
{ |
| 1319 |
mode_t code = ecl_to_uint32_t(mode); |
| 1320 |
cl_object filename = si_coerce_to_filename(file); |
| 1321 |
unlikely_if (ecl_chmod(ecl_filename_self(filename), code)) { |
| 1322 |
cl_object c_error = _ecl_strerror(errno); |
| 1323 |
const char *msg = "Unable to change mode of file ~S to value ~O" |
| 1324 |
"~%C library error: ~S"; |
| 1325 |
si_signal_simple_error |
| 1326 |
(6, @'file-error', /* condition */ |
| 1327 |
ECL_T, /* continuable */ |
| 1328 |
/* format */ |
| 1329 |
ecl_make_constant_base_string(msg,strlen(msg)), |
| 1330 |
cl_list(3, file, mode, c_error), /* format args */ |
| 1331 |
@':pathname', /* file-error options */ |
| 1332 |
file); |
| 1333 |
} |
| 1334 |
@(return); |
| 1335 |
} |