| 1 |
/* |
| 2 |
* tclFCmd.c |
| 3 |
* |
| 4 |
* This file implements the generic portion of file manipulation |
| 5 |
* subcommands of the "file" command. |
| 6 |
* |
| 7 |
* Copyright (c) 1996-1998 Sun Microsystems, Inc. |
| 8 |
* |
| 9 |
* See the file "license.terms" for information on usage and redistribution of |
| 10 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "tclInt.h" |
| 14 |
#include "tclFileSystem.h" |
| 15 |
|
| 16 |
/* |
| 17 |
* Declarations for local functions defined in this file: |
| 18 |
*/ |
| 19 |
|
| 20 |
static int CopyRenameOneFile(Tcl_Interp *interp, |
| 21 |
Tcl_Obj *srcPathPtr, Tcl_Obj *destPathPtr, |
| 22 |
int copyFlag, int force); |
| 23 |
static Tcl_Obj * FileBasename(Tcl_Interp *interp, Tcl_Obj *pathPtr); |
| 24 |
static int FileCopyRename(Tcl_Interp *interp, |
| 25 |
int objc, Tcl_Obj *const objv[], int copyFlag); |
| 26 |
static int FileForceOption(Tcl_Interp *interp, |
| 27 |
int objc, Tcl_Obj *const objv[], int *forcePtr); |
| 28 |
|
| 29 |
/* |
| 30 |
*--------------------------------------------------------------------------- |
| 31 |
* |
| 32 |
* TclFileRenameCmd |
| 33 |
* |
| 34 |
* This function implements the "rename" subcommand of the "file" |
| 35 |
* command. Filename arguments need to be translated to native format |
| 36 |
* before being passed to platform-specific code that implements rename |
| 37 |
* functionality. |
| 38 |
* |
| 39 |
* Results: |
| 40 |
* A standard Tcl result. |
| 41 |
* |
| 42 |
* Side effects: |
| 43 |
* See the user documentation. |
| 44 |
* |
| 45 |
*--------------------------------------------------------------------------- |
| 46 |
*/ |
| 47 |
|
| 48 |
int |
| 49 |
TclFileRenameCmd( |
| 50 |
ClientData clientData, /* Unused */ |
| 51 |
Tcl_Interp *interp, /* Interp for error reporting or recursive |
| 52 |
* calls in the case of a tricky rename. */ |
| 53 |
int objc, /* Number of arguments. */ |
| 54 |
Tcl_Obj *const objv[]) /* Argument strings passed to Tcl_FileCmd. */ |
| 55 |
{ |
| 56 |
return FileCopyRename(interp, objc, objv, 0); |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
*--------------------------------------------------------------------------- |
| 61 |
* |
| 62 |
* TclFileCopyCmd |
| 63 |
* |
| 64 |
* This function implements the "copy" subcommand of the "file" command. |
| 65 |
* Filename arguments need to be translated to native format before being |
| 66 |
* passed to platform-specific code that implements copy functionality. |
| 67 |
* |
| 68 |
* Results: |
| 69 |
* A standard Tcl result. |
| 70 |
* |
| 71 |
* Side effects: |
| 72 |
* See the user documentation. |
| 73 |
* |
| 74 |
*--------------------------------------------------------------------------- |
| 75 |
*/ |
| 76 |
|
| 77 |
int |
| 78 |
TclFileCopyCmd( |
| 79 |
ClientData clientData, /* Unused */ |
| 80 |
Tcl_Interp *interp, /* Used for error reporting or recursive calls |
| 81 |
* in the case of a tricky copy. */ |
| 82 |
int objc, /* Number of arguments. */ |
| 83 |
Tcl_Obj *const objv[]) /* Argument strings passed to Tcl_FileCmd. */ |
| 84 |
{ |
| 85 |
return FileCopyRename(interp, objc, objv, 1); |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
*--------------------------------------------------------------------------- |
| 90 |
* |
| 91 |
* FileCopyRename -- |
| 92 |
* |
| 93 |
* Performs the work of TclFileRenameCmd and TclFileCopyCmd. See |
| 94 |
* comments for those functions. |
| 95 |
* |
| 96 |
* Results: |
| 97 |
* See above. |
| 98 |
* |
| 99 |
* Side effects: |
| 100 |
* See above. |
| 101 |
* |
| 102 |
*--------------------------------------------------------------------------- |
| 103 |
*/ |
| 104 |
|
| 105 |
static int |
| 106 |
FileCopyRename( |
| 107 |
Tcl_Interp *interp, /* Used for error reporting. */ |
| 108 |
int objc, /* Number of arguments. */ |
| 109 |
Tcl_Obj *const objv[], /* Argument strings passed to Tcl_FileCmd. */ |
| 110 |
int copyFlag) /* If non-zero, copy source(s). Otherwise, |
| 111 |
* rename them. */ |
| 112 |
{ |
| 113 |
int i, result, force; |
| 114 |
Tcl_StatBuf statBuf; |
| 115 |
Tcl_Obj *target; |
| 116 |
|
| 117 |
i = FileForceOption(interp, objc - 1, objv + 1, &force); |
| 118 |
if (i < 0) { |
| 119 |
return TCL_ERROR; |
| 120 |
} |
| 121 |
i++; |
| 122 |
if ((objc - i) < 2) { |
| 123 |
Tcl_WrongNumArgs(interp, 1, objv, |
| 124 |
"?-option value ...? source ?source ...? target"); |
| 125 |
return TCL_ERROR; |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* If target doesn't exist or isn't a directory, try the copy/rename. More |
| 130 |
* than 2 arguments is only valid if the target is an existing directory. |
| 131 |
*/ |
| 132 |
|
| 133 |
target = objv[objc - 1]; |
| 134 |
if (Tcl_FSConvertToPathType(interp, target) != TCL_OK) { |
| 135 |
return TCL_ERROR; |
| 136 |
} |
| 137 |
|
| 138 |
result = TCL_OK; |
| 139 |
|
| 140 |
/* |
| 141 |
* Call Tcl_FSStat() so that if target is a symlink that points to a |
| 142 |
* directory we will put the sources in that directory instead of |
| 143 |
* overwriting the symlink. |
| 144 |
*/ |
| 145 |
|
| 146 |
if ((Tcl_FSStat(target, &statBuf) != 0) || !S_ISDIR(statBuf.st_mode)) { |
| 147 |
if ((objc - i) > 2) { |
| 148 |
errno = ENOTDIR; |
| 149 |
Tcl_PosixError(interp); |
| 150 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 151 |
"error %s: target \"%s\" is not a directory", |
| 152 |
(copyFlag?"copying":"renaming"), TclGetString(target))); |
| 153 |
result = TCL_ERROR; |
| 154 |
} else { |
| 155 |
/* |
| 156 |
* Even though already have target == translated(objv[i+1]), pass |
| 157 |
* the original argument down, so if there's an error, the error |
| 158 |
* message will reflect the original arguments. |
| 159 |
*/ |
| 160 |
|
| 161 |
result = CopyRenameOneFile(interp, objv[i], objv[i + 1], copyFlag, |
| 162 |
force); |
| 163 |
} |
| 164 |
return result; |
| 165 |
} |
| 166 |
|
| 167 |
/* |
| 168 |
* Move each source file into target directory. Extract the basename from |
| 169 |
* each source, and append it to the end of the target path. |
| 170 |
*/ |
| 171 |
|
| 172 |
for ( ; i<objc-1 ; i++) { |
| 173 |
Tcl_Obj *jargv[2]; |
| 174 |
Tcl_Obj *source, *newFileName; |
| 175 |
|
| 176 |
source = FileBasename(interp, objv[i]); |
| 177 |
if (source == NULL) { |
| 178 |
result = TCL_ERROR; |
| 179 |
break; |
| 180 |
} |
| 181 |
jargv[0] = objv[objc - 1]; |
| 182 |
jargv[1] = source; |
| 183 |
newFileName = TclJoinPath(2, jargv, 1); |
| 184 |
Tcl_IncrRefCount(newFileName); |
| 185 |
result = CopyRenameOneFile(interp, objv[i], newFileName, copyFlag, |
| 186 |
force); |
| 187 |
Tcl_DecrRefCount(newFileName); |
| 188 |
Tcl_DecrRefCount(source); |
| 189 |
|
| 190 |
if (result == TCL_ERROR) { |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
return result; |
| 195 |
} |
| 196 |
|
| 197 |
/* |
| 198 |
*--------------------------------------------------------------------------- |
| 199 |
* |
| 200 |
* TclFileMakeDirsCmd |
| 201 |
* |
| 202 |
* This function implements the "mkdir" subcommand of the "file" command. |
| 203 |
* Filename arguments need to be translated to native format before being |
| 204 |
* passed to platform-specific code that implements mkdir functionality. |
| 205 |
* |
| 206 |
* Results: |
| 207 |
* A standard Tcl result. |
| 208 |
* |
| 209 |
* Side effects: |
| 210 |
* See the user documentation. |
| 211 |
* |
| 212 |
*---------------------------------------------------------------------- |
| 213 |
*/ |
| 214 |
|
| 215 |
int |
| 216 |
TclFileMakeDirsCmd( |
| 217 |
ClientData clientData, /* Unused */ |
| 218 |
Tcl_Interp *interp, /* Used for error reporting. */ |
| 219 |
int objc, /* Number of arguments */ |
| 220 |
Tcl_Obj *const objv[]) /* Argument strings passed to Tcl_FileCmd. */ |
| 221 |
{ |
| 222 |
Tcl_Obj *errfile = NULL; |
| 223 |
int result, i, j, pobjc; |
| 224 |
Tcl_Obj *split = NULL; |
| 225 |
Tcl_Obj *target = NULL; |
| 226 |
Tcl_StatBuf statBuf; |
| 227 |
|
| 228 |
result = TCL_OK; |
| 229 |
for (i = 1; i < objc; i++) { |
| 230 |
if (Tcl_FSConvertToPathType(interp, objv[i]) != TCL_OK) { |
| 231 |
result = TCL_ERROR; |
| 232 |
break; |
| 233 |
} |
| 234 |
|
| 235 |
split = Tcl_FSSplitPath(objv[i], &pobjc); |
| 236 |
Tcl_IncrRefCount(split); |
| 237 |
if (pobjc == 0) { |
| 238 |
errno = ENOENT; |
| 239 |
errfile = objv[i]; |
| 240 |
break; |
| 241 |
} |
| 242 |
for (j = 0; j < pobjc; j++) { |
| 243 |
int errCount = 2; |
| 244 |
|
| 245 |
target = Tcl_FSJoinPath(split, j + 1); |
| 246 |
Tcl_IncrRefCount(target); |
| 247 |
|
| 248 |
createDir: |
| 249 |
|
| 250 |
/* |
| 251 |
* Call Tcl_FSStat() so that if target is a symlink that points to |
| 252 |
* a directory we will create subdirectories in that directory. |
| 253 |
*/ |
| 254 |
|
| 255 |
if (Tcl_FSStat(target, &statBuf) == 0) { |
| 256 |
if (!S_ISDIR(statBuf.st_mode)) { |
| 257 |
errno = EEXIST; |
| 258 |
errfile = target; |
| 259 |
goto done; |
| 260 |
} |
| 261 |
} else if (errno != ENOENT) { |
| 262 |
/* |
| 263 |
* If Tcl_FSStat() failed and the error is anything other than |
| 264 |
* non-existence of the target, throw the error. |
| 265 |
*/ |
| 266 |
|
| 267 |
errfile = target; |
| 268 |
goto done; |
| 269 |
} else if (Tcl_FSCreateDirectory(target) != TCL_OK) { |
| 270 |
/* |
| 271 |
* Create might have failed because of being in a race |
| 272 |
* condition with another process trying to create the same |
| 273 |
* subdirectory. |
| 274 |
*/ |
| 275 |
|
| 276 |
if (errno == EEXIST) { |
| 277 |
/* Be aware other workers could delete it immediately after |
| 278 |
* creation, so give this worker still one chance (repeat once), |
| 279 |
* see [270f78ca95] for description of the race-condition. |
| 280 |
* Don't repeat the create always (to avoid endless loop). */ |
| 281 |
if (--errCount > 0) { |
| 282 |
goto createDir; |
| 283 |
} |
| 284 |
/* Already tried, with delete in-between directly after |
| 285 |
* creation, so just continue (assume created successful). */ |
| 286 |
goto nextPart; |
| 287 |
} |
| 288 |
|
| 289 |
/* return with error */ |
| 290 |
errfile = target; |
| 291 |
goto done; |
| 292 |
} |
| 293 |
|
| 294 |
nextPart: |
| 295 |
/* |
| 296 |
* Forget about this sub-path. |
| 297 |
*/ |
| 298 |
|
| 299 |
Tcl_DecrRefCount(target); |
| 300 |
target = NULL; |
| 301 |
} |
| 302 |
Tcl_DecrRefCount(split); |
| 303 |
split = NULL; |
| 304 |
} |
| 305 |
|
| 306 |
done: |
| 307 |
if (errfile != NULL) { |
| 308 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 309 |
"can't create directory \"%s\": %s", |
| 310 |
TclGetString(errfile), Tcl_PosixError(interp))); |
| 311 |
result = TCL_ERROR; |
| 312 |
} |
| 313 |
if (split != NULL) { |
| 314 |
Tcl_DecrRefCount(split); |
| 315 |
} |
| 316 |
if (target != NULL) { |
| 317 |
Tcl_DecrRefCount(target); |
| 318 |
} |
| 319 |
return result; |
| 320 |
} |
| 321 |
|
| 322 |
/* |
| 323 |
*---------------------------------------------------------------------- |
| 324 |
* |
| 325 |
* TclFileDeleteCmd |
| 326 |
* |
| 327 |
* This function implements the "delete" subcommand of the "file" |
| 328 |
* command. |
| 329 |
* |
| 330 |
* Results: |
| 331 |
* A standard Tcl result. |
| 332 |
* |
| 333 |
* Side effects: |
| 334 |
* See the user documentation. |
| 335 |
* |
| 336 |
*---------------------------------------------------------------------- |
| 337 |
*/ |
| 338 |
|
| 339 |
int |
| 340 |
TclFileDeleteCmd( |
| 341 |
ClientData clientData, /* Unused */ |
| 342 |
Tcl_Interp *interp, /* Used for error reporting */ |
| 343 |
int objc, /* Number of arguments */ |
| 344 |
Tcl_Obj *const objv[]) /* Argument strings passed to Tcl_FileCmd. */ |
| 345 |
{ |
| 346 |
int i, force, result; |
| 347 |
Tcl_Obj *errfile; |
| 348 |
Tcl_Obj *errorBuffer = NULL; |
| 349 |
|
| 350 |
i = FileForceOption(interp, objc - 1, objv + 1, &force); |
| 351 |
if (i < 0) { |
| 352 |
return TCL_ERROR; |
| 353 |
} |
| 354 |
|
| 355 |
errfile = NULL; |
| 356 |
result = TCL_OK; |
| 357 |
|
| 358 |
for (i++ ; i < objc; i++) { |
| 359 |
Tcl_StatBuf statBuf; |
| 360 |
|
| 361 |
errfile = objv[i]; |
| 362 |
if (Tcl_FSConvertToPathType(interp, objv[i]) != TCL_OK) { |
| 363 |
result = TCL_ERROR; |
| 364 |
goto done; |
| 365 |
} |
| 366 |
|
| 367 |
/* |
| 368 |
* Call lstat() to get info so can delete symbolic link itself. |
| 369 |
*/ |
| 370 |
|
| 371 |
if (Tcl_FSLstat(objv[i], &statBuf) != 0) { |
| 372 |
result = TCL_ERROR; |
| 373 |
} else if (S_ISDIR(statBuf.st_mode)) { |
| 374 |
/* |
| 375 |
* We own a reference count on errorBuffer, if it was set as a |
| 376 |
* result of this call. |
| 377 |
*/ |
| 378 |
|
| 379 |
result = Tcl_FSRemoveDirectory(objv[i], force, &errorBuffer); |
| 380 |
if (result != TCL_OK) { |
| 381 |
if ((force == 0) && (errno == EEXIST)) { |
| 382 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 383 |
"error deleting \"%s\": directory not empty", |
| 384 |
TclGetString(objv[i]))); |
| 385 |
Tcl_PosixError(interp); |
| 386 |
goto done; |
| 387 |
} |
| 388 |
|
| 389 |
/* |
| 390 |
* If possible, use the untranslated name for the file. |
| 391 |
*/ |
| 392 |
|
| 393 |
errfile = errorBuffer; |
| 394 |
|
| 395 |
/* |
| 396 |
* FS supposed to check between translated objv and errfile. |
| 397 |
*/ |
| 398 |
|
| 399 |
if (Tcl_FSEqualPaths(objv[i], errfile)) { |
| 400 |
errfile = objv[i]; |
| 401 |
} |
| 402 |
} |
| 403 |
} else { |
| 404 |
result = Tcl_FSDeleteFile(objv[i]); |
| 405 |
} |
| 406 |
|
| 407 |
if (result != TCL_OK) { |
| 408 |
|
| 409 |
/* |
| 410 |
* Avoid possible race condition (file/directory deleted after call |
| 411 |
* of lstat), so bypass ENOENT because not an error, just a no-op |
| 412 |
*/ |
| 413 |
if (errno == ENOENT) { |
| 414 |
result = TCL_OK; |
| 415 |
continue; |
| 416 |
} |
| 417 |
/* |
| 418 |
* It is important that we break on error, otherwise we might end |
| 419 |
* up owning reference counts on numerous errorBuffers. |
| 420 |
*/ |
| 421 |
result = TCL_ERROR; |
| 422 |
break; |
| 423 |
} |
| 424 |
} |
| 425 |
if (result != TCL_OK) { |
| 426 |
if (errfile == NULL) { |
| 427 |
/* |
| 428 |
* We try to accommodate poor error results from our Tcl_FS calls. |
| 429 |
*/ |
| 430 |
|
| 431 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 432 |
"error deleting unknown file: %s", |
| 433 |
Tcl_PosixError(interp))); |
| 434 |
} else { |
| 435 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 436 |
"error deleting \"%s\": %s", |
| 437 |
TclGetString(errfile), Tcl_PosixError(interp))); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
done: |
| 442 |
if (errorBuffer != NULL) { |
| 443 |
Tcl_DecrRefCount(errorBuffer); |
| 444 |
} |
| 445 |
return result; |
| 446 |
} |
| 447 |
|
| 448 |
/* |
| 449 |
*--------------------------------------------------------------------------- |
| 450 |
* |
| 451 |
* CopyRenameOneFile |
| 452 |
* |
| 453 |
* Copies or renames specified source file or directory hierarchy to the |
| 454 |
* specified target. |
| 455 |
* |
| 456 |
* Results: |
| 457 |
* A standard Tcl result. |
| 458 |
* |
| 459 |
* Side effects: |
| 460 |
* Target is overwritten if the force flag is set. Attempting to |
| 461 |
* copy/rename a file onto a directory or a directory onto a file will |
| 462 |
* always result in an error. |
| 463 |
* |
| 464 |
*---------------------------------------------------------------------- |
| 465 |
*/ |
| 466 |
|
| 467 |
static int |
| 468 |
CopyRenameOneFile( |
| 469 |
Tcl_Interp *interp, /* Used for error reporting. */ |
| 470 |
Tcl_Obj *source, /* Pathname of file to copy. May need to be |
| 471 |
* translated. */ |
| 472 |
Tcl_Obj *target, /* Pathname of file to create/overwrite. May |
| 473 |
* need to be translated. */ |
| 474 |
int copyFlag, /* If non-zero, copy files. Otherwise, rename |
| 475 |
* them. */ |
| 476 |
int force) /* If non-zero, overwrite target file if it |
| 477 |
* exists. Otherwise, error if target already |
| 478 |
* exists. */ |
| 479 |
{ |
| 480 |
int result; |
| 481 |
Tcl_Obj *errfile, *errorBuffer; |
| 482 |
Tcl_Obj *actualSource=NULL; /* If source is a link, then this is the real |
| 483 |
* file/directory. */ |
| 484 |
Tcl_StatBuf sourceStatBuf, targetStatBuf; |
| 485 |
|
| 486 |
if (Tcl_FSConvertToPathType(interp, source) != TCL_OK) { |
| 487 |
return TCL_ERROR; |
| 488 |
} |
| 489 |
if (Tcl_FSConvertToPathType(interp, target) != TCL_OK) { |
| 490 |
return TCL_ERROR; |
| 491 |
} |
| 492 |
|
| 493 |
errfile = NULL; |
| 494 |
errorBuffer = NULL; |
| 495 |
result = TCL_ERROR; |
| 496 |
|
| 497 |
/* |
| 498 |
* We want to copy/rename links and not the files they point to, so we use |
| 499 |
* lstat(). If target is a link, we also want to replace the link and not |
| 500 |
* the file it points to, so we also use lstat() on the target. |
| 501 |
*/ |
| 502 |
|
| 503 |
if (Tcl_FSLstat(source, &sourceStatBuf) != 0) { |
| 504 |
errfile = source; |
| 505 |
goto done; |
| 506 |
} |
| 507 |
if (Tcl_FSLstat(target, &targetStatBuf) != 0) { |
| 508 |
if (errno != ENOENT) { |
| 509 |
errfile = target; |
| 510 |
goto done; |
| 511 |
} |
| 512 |
} else { |
| 513 |
if (force == 0) { |
| 514 |
errno = EEXIST; |
| 515 |
errfile = target; |
| 516 |
goto done; |
| 517 |
} |
| 518 |
|
| 519 |
/* |
| 520 |
* Prevent copying or renaming a file onto itself. On Windows since |
| 521 |
* 8.5 we do get an inode number, however the unsigned short field is |
| 522 |
* insufficient to accept the Win32 API file id so it is truncated to |
| 523 |
* 16 bits and we get collisions. See bug #2015723. |
| 524 |
*/ |
| 525 |
|
| 526 |
#if !defined(_WIN32) && !defined(__CYGWIN__) |
| 527 |
if ((sourceStatBuf.st_ino != 0) && (targetStatBuf.st_ino != 0)) { |
| 528 |
if ((sourceStatBuf.st_ino == targetStatBuf.st_ino) && |
| 529 |
(sourceStatBuf.st_dev == targetStatBuf.st_dev)) { |
| 530 |
result = TCL_OK; |
| 531 |
goto done; |
| 532 |
} |
| 533 |
} |
| 534 |
#endif |
| 535 |
|
| 536 |
/* |
| 537 |
* Prevent copying/renaming a file onto a directory and vice-versa. |
| 538 |
* This is a policy decision based on the fact that existing |
| 539 |
* implementations of copy and rename on all platforms also prevent |
| 540 |
* this. |
| 541 |
*/ |
| 542 |
|
| 543 |
if (S_ISDIR(sourceStatBuf.st_mode) |
| 544 |
&& !S_ISDIR(targetStatBuf.st_mode)) { |
| 545 |
errno = EISDIR; |
| 546 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 547 |
"can't overwrite file \"%s\" with directory \"%s\"", |
| 548 |
TclGetString(target), TclGetString(source))); |
| 549 |
goto done; |
| 550 |
} |
| 551 |
if (!S_ISDIR(sourceStatBuf.st_mode) |
| 552 |
&& S_ISDIR(targetStatBuf.st_mode)) { |
| 553 |
errno = EISDIR; |
| 554 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 555 |
"can't overwrite directory \"%s\" with file \"%s\"", |
| 556 |
TclGetString(target), TclGetString(source))); |
| 557 |
goto done; |
| 558 |
} |
| 559 |
|
| 560 |
/* |
| 561 |
* The destination exists, but appears to be ok to over-write, and |
| 562 |
* -force is given. We now try to adjust permissions to ensure the |
| 563 |
* operation succeeds. If we can't adjust permissions, we'll let the |
| 564 |
* actual copy/rename return an error later. |
| 565 |
*/ |
| 566 |
|
| 567 |
{ |
| 568 |
Tcl_Obj *perm; |
| 569 |
int index; |
| 570 |
|
| 571 |
TclNewLiteralStringObj(perm, "u+w"); |
| 572 |
Tcl_IncrRefCount(perm); |
| 573 |
if (TclFSFileAttrIndex(target, "-permissions", &index) == TCL_OK) { |
| 574 |
Tcl_FSFileAttrsSet(NULL, index, target, perm); |
| 575 |
} |
| 576 |
Tcl_DecrRefCount(perm); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
if (copyFlag == 0) { |
| 581 |
result = Tcl_FSRenameFile(source, target); |
| 582 |
if (result == TCL_OK) { |
| 583 |
goto done; |
| 584 |
} |
| 585 |
|
| 586 |
if (errno == EINVAL) { |
| 587 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 588 |
"error renaming \"%s\" to \"%s\": trying to rename a" |
| 589 |
" volume or move a directory into itself", |
| 590 |
TclGetString(source), TclGetString(target))); |
| 591 |
goto done; |
| 592 |
} else if (errno != EXDEV) { |
| 593 |
errfile = target; |
| 594 |
goto done; |
| 595 |
} |
| 596 |
|
| 597 |
/* |
| 598 |
* The rename failed because the move was across file systems. Fall |
| 599 |
* through to copy file and then remove original. Note that the |
| 600 |
* low-level Tcl_FSRenameFileProc in the filesystem is allowed to |
| 601 |
* implement cross-filesystem moves itself, if it desires. |
| 602 |
*/ |
| 603 |
} |
| 604 |
|
| 605 |
actualSource = source; |
| 606 |
Tcl_IncrRefCount(actualSource); |
| 607 |
|
| 608 |
/* |
| 609 |
* Activate the following block to copy files instead of links. However |
| 610 |
* Tcl's semantics currently say we should copy links, so any such change |
| 611 |
* should be the subject of careful study on the consequences. |
| 612 |
* |
| 613 |
* Perhaps there could be an optional flag to 'file copy' to dictate which |
| 614 |
* approach to use, with the default being _not_ to have this block |
| 615 |
* active. |
| 616 |
*/ |
| 617 |
|
| 618 |
#if 0 |
| 619 |
#ifdef S_ISLNK |
| 620 |
if (copyFlag && S_ISLNK(sourceStatBuf.st_mode)) { |
| 621 |
/* |
| 622 |
* We want to copy files not links. Therefore we must follow the link. |
| 623 |
* There are two purposes to this 'stat' call here. First we want to |
| 624 |
* know if the linked-file/dir actually exists, and second, in the |
| 625 |
* block of code which follows, some 20 lines down, we want to check |
| 626 |
* if the thing is a file or directory. |
| 627 |
*/ |
| 628 |
|
| 629 |
if (Tcl_FSStat(source, &sourceStatBuf) != 0) { |
| 630 |
/* |
| 631 |
* Actual file doesn't exist. |
| 632 |
*/ |
| 633 |
|
| 634 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 635 |
"error copying \"%s\": the target of this link doesn't" |
| 636 |
" exist", TclGetString(source))); |
| 637 |
goto done; |
| 638 |
} else { |
| 639 |
int counter = 0; |
| 640 |
|
| 641 |
while (1) { |
| 642 |
Tcl_Obj *path = Tcl_FSLink(actualSource, NULL, 0); |
| 643 |
if (path == NULL) { |
| 644 |
break; |
| 645 |
} |
| 646 |
|
| 647 |
/* |
| 648 |
* Now we want to check if this is a relative path, and if so, |
| 649 |
* to make it absolute. |
| 650 |
*/ |
| 651 |
|
| 652 |
if (Tcl_FSGetPathType(path) == TCL_PATH_RELATIVE) { |
| 653 |
Tcl_Obj *abs = Tcl_FSJoinToPath(actualSource, 1, &path); |
| 654 |
|
| 655 |
if (abs == NULL) { |
| 656 |
break; |
| 657 |
} |
| 658 |
Tcl_IncrRefCount(abs); |
| 659 |
Tcl_DecrRefCount(path); |
| 660 |
path = abs; |
| 661 |
} |
| 662 |
Tcl_DecrRefCount(actualSource); |
| 663 |
actualSource = path; |
| 664 |
counter++; |
| 665 |
|
| 666 |
/* |
| 667 |
* Arbitrary limit of 20 links to follow. |
| 668 |
*/ |
| 669 |
|
| 670 |
if (counter > 20) { |
| 671 |
/* |
| 672 |
* Too many links. |
| 673 |
*/ |
| 674 |
|
| 675 |
Tcl_SetErrno(EMLINK); |
| 676 |
errfile = source; |
| 677 |
goto done; |
| 678 |
} |
| 679 |
} |
| 680 |
/* Now 'actualSource' is the correct file */ |
| 681 |
} |
| 682 |
} |
| 683 |
#endif /* S_ISLNK */ |
| 684 |
#endif |
| 685 |
|
| 686 |
if (S_ISDIR(sourceStatBuf.st_mode)) { |
| 687 |
result = Tcl_FSCopyDirectory(actualSource, target, &errorBuffer); |
| 688 |
if (result != TCL_OK) { |
| 689 |
if (errno == EXDEV) { |
| 690 |
/* |
| 691 |
* The copy failed because we're trying to do a |
| 692 |
* cross-filesystem copy. We do this through our Tcl library. |
| 693 |
*/ |
| 694 |
|
| 695 |
Tcl_Obj *copyCommand, *cmdObj, *opObj; |
| 696 |
|
| 697 |
TclNewObj(copyCommand); |
| 698 |
TclNewLiteralStringObj(cmdObj, "::tcl::CopyDirectory"); |
| 699 |
Tcl_ListObjAppendElement(interp, copyCommand, cmdObj); |
| 700 |
if (copyFlag) { |
| 701 |
TclNewLiteralStringObj(opObj, "copying"); |
| 702 |
} else { |
| 703 |
TclNewLiteralStringObj(opObj, "renaming"); |
| 704 |
} |
| 705 |
Tcl_ListObjAppendElement(interp, copyCommand, opObj); |
| 706 |
Tcl_ListObjAppendElement(interp, copyCommand, source); |
| 707 |
Tcl_ListObjAppendElement(interp, copyCommand, target); |
| 708 |
Tcl_IncrRefCount(copyCommand); |
| 709 |
result = Tcl_EvalObjEx(interp, copyCommand, |
| 710 |
TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT); |
| 711 |
Tcl_DecrRefCount(copyCommand); |
| 712 |
if (result != TCL_OK) { |
| 713 |
/* |
| 714 |
* There was an error in the Tcl-level copy. We will pass |
| 715 |
* on the Tcl error message and can ensure this by setting |
| 716 |
* errfile to NULL |
| 717 |
*/ |
| 718 |
|
| 719 |
errfile = NULL; |
| 720 |
} |
| 721 |
} else { |
| 722 |
errfile = errorBuffer; |
| 723 |
if (Tcl_FSEqualPaths(errfile, source)) { |
| 724 |
errfile = source; |
| 725 |
} else if (Tcl_FSEqualPaths(errfile, target)) { |
| 726 |
errfile = target; |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
} else { |
| 731 |
result = Tcl_FSCopyFile(actualSource, target); |
| 732 |
if ((result != TCL_OK) && (errno == EXDEV)) { |
| 733 |
result = TclCrossFilesystemCopy(interp, source, target); |
| 734 |
} |
| 735 |
if (result != TCL_OK) { |
| 736 |
/* |
| 737 |
* We could examine 'errno' to double-check if the problem was |
| 738 |
* with the target, but we checked the source above, so it should |
| 739 |
* be quite clear |
| 740 |
*/ |
| 741 |
|
| 742 |
errfile = target; |
| 743 |
} |
| 744 |
/* |
| 745 |
* We now need to reset the result, because the above call, |
| 746 |
* may have left set it. (Ideally we would prefer not to pass |
| 747 |
* an interpreter in above, but the channel IO code used by |
| 748 |
* TclCrossFilesystemCopy currently requires one) |
| 749 |
*/ |
| 750 |
Tcl_ResetResult(interp); |
| 751 |
} |
| 752 |
if ((copyFlag == 0) && (result == TCL_OK)) { |
| 753 |
if (S_ISDIR(sourceStatBuf.st_mode)) { |
| 754 |
result = Tcl_FSRemoveDirectory(source, 1, &errorBuffer); |
| 755 |
if (result != TCL_OK) { |
| 756 |
errfile = errorBuffer; |
| 757 |
if (Tcl_FSEqualPaths(errfile, source) == 0) { |
| 758 |
errfile = source; |
| 759 |
} |
| 760 |
} |
| 761 |
} else { |
| 762 |
result = Tcl_FSDeleteFile(source); |
| 763 |
if (result != TCL_OK) { |
| 764 |
errfile = source; |
| 765 |
} |
| 766 |
} |
| 767 |
if (result != TCL_OK) { |
| 768 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf("can't unlink \"%s\": %s", |
| 769 |
TclGetString(errfile), Tcl_PosixError(interp))); |
| 770 |
errfile = NULL; |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
done: |
| 775 |
if (errfile != NULL) { |
| 776 |
Tcl_Obj *errorMsg = Tcl_ObjPrintf("error %s \"%s\"", |
| 777 |
(copyFlag ? "copying" : "renaming"), TclGetString(source)); |
| 778 |
|
| 779 |
if (errfile != source) { |
| 780 |
Tcl_AppendPrintfToObj(errorMsg, " to \"%s\"", |
| 781 |
TclGetString(target)); |
| 782 |
if (errfile != target) { |
| 783 |
Tcl_AppendPrintfToObj(errorMsg, ": \"%s\"", |
| 784 |
TclGetString(errfile)); |
| 785 |
} |
| 786 |
} |
| 787 |
Tcl_AppendPrintfToObj(errorMsg, ": %s", Tcl_PosixError(interp)); |
| 788 |
Tcl_SetObjResult(interp, errorMsg); |
| 789 |
} |
| 790 |
if (errorBuffer != NULL) { |
| 791 |
Tcl_DecrRefCount(errorBuffer); |
| 792 |
} |
| 793 |
if (actualSource != NULL) { |
| 794 |
Tcl_DecrRefCount(actualSource); |
| 795 |
} |
| 796 |
return result; |
| 797 |
} |
| 798 |
|
| 799 |
/* |
| 800 |
*--------------------------------------------------------------------------- |
| 801 |
* |
| 802 |
* FileForceOption -- |
| 803 |
* |
| 804 |
* Helps parse command line options for file commands that take the |
| 805 |
* "-force" and "--" options. |
| 806 |
* |
| 807 |
* Results: |
| 808 |
* The return value is how many arguments from argv were consumed by this |
| 809 |
* function, or -1 if there was an error parsing the options. If an error |
| 810 |
* occurred, an error message is left in the interp's result. |
| 811 |
* |
| 812 |
* Side effects: |
| 813 |
* None. |
| 814 |
* |
| 815 |
*--------------------------------------------------------------------------- |
| 816 |
*/ |
| 817 |
|
| 818 |
static int |
| 819 |
FileForceOption( |
| 820 |
Tcl_Interp *interp, /* Interp, for error return. */ |
| 821 |
int objc, /* Number of arguments. */ |
| 822 |
Tcl_Obj *const objv[], /* Argument strings. First command line |
| 823 |
* option, if it exists, begins at 0. */ |
| 824 |
int *forcePtr) /* If the "-force" was specified, *forcePtr is |
| 825 |
* filled with 1, otherwise with 0. */ |
| 826 |
{ |
| 827 |
int force, i, idx; |
| 828 |
static const char *const options[] = { |
| 829 |
"-force", "--", NULL |
| 830 |
}; |
| 831 |
|
| 832 |
force = 0; |
| 833 |
for (i = 0; i < objc; i++) { |
| 834 |
if (TclGetString(objv[i])[0] != '-') { |
| 835 |
break; |
| 836 |
} |
| 837 |
if (Tcl_GetIndexFromObj(interp, objv[i], options, "option", TCL_EXACT, |
| 838 |
&idx) != TCL_OK) { |
| 839 |
return -1; |
| 840 |
} |
| 841 |
if (idx == 0 /* -force */) { |
| 842 |
force = 1; |
| 843 |
} else { /* -- */ |
| 844 |
i++; |
| 845 |
break; |
| 846 |
} |
| 847 |
} |
| 848 |
*forcePtr = force; |
| 849 |
return i; |
| 850 |
} |
| 851 |
/* |
| 852 |
*--------------------------------------------------------------------------- |
| 853 |
* |
| 854 |
* FileBasename -- |
| 855 |
* |
| 856 |
* Given a path in either tcl format (with / separators), or in the |
| 857 |
* platform-specific format for the current platform, return all the |
| 858 |
* characters in the path after the last directory separator. But, if |
| 859 |
* path is the root directory, returns no characters. |
| 860 |
* |
| 861 |
* Results: |
| 862 |
* Returns the string object that represents the basename. If there is an |
| 863 |
* error, an error message is left in interp, and NULL is returned. |
| 864 |
* |
| 865 |
* Side effects: |
| 866 |
* None. |
| 867 |
* |
| 868 |
*--------------------------------------------------------------------------- |
| 869 |
*/ |
| 870 |
|
| 871 |
static Tcl_Obj * |
| 872 |
FileBasename( |
| 873 |
Tcl_Interp *interp, /* Interp, for error return. */ |
| 874 |
Tcl_Obj *pathPtr) /* Path whose basename to extract. */ |
| 875 |
{ |
| 876 |
int objc; |
| 877 |
Tcl_Obj *splitPtr; |
| 878 |
Tcl_Obj *resultPtr = NULL; |
| 879 |
|
| 880 |
splitPtr = Tcl_FSSplitPath(pathPtr, &objc); |
| 881 |
Tcl_IncrRefCount(splitPtr); |
| 882 |
|
| 883 |
if (objc != 0) { |
| 884 |
if ((objc == 1) && (*TclGetString(pathPtr) == '~')) { |
| 885 |
Tcl_DecrRefCount(splitPtr); |
| 886 |
if (Tcl_FSConvertToPathType(interp, pathPtr) != TCL_OK) { |
| 887 |
return NULL; |
| 888 |
} |
| 889 |
splitPtr = Tcl_FSSplitPath(pathPtr, &objc); |
| 890 |
Tcl_IncrRefCount(splitPtr); |
| 891 |
} |
| 892 |
|
| 893 |
/* |
| 894 |
* Return the last component, unless it is the only component, and it |
| 895 |
* is the root of an absolute path. |
| 896 |
*/ |
| 897 |
|
| 898 |
if (objc > 0) { |
| 899 |
Tcl_ListObjIndex(NULL, splitPtr, objc-1, &resultPtr); |
| 900 |
if ((objc == 1) && |
| 901 |
(Tcl_FSGetPathType(resultPtr) != TCL_PATH_RELATIVE)) { |
| 902 |
resultPtr = NULL; |
| 903 |
} |
| 904 |
} |
| 905 |
} |
| 906 |
if (resultPtr == NULL) { |
| 907 |
TclNewObj(resultPtr); |
| 908 |
} |
| 909 |
Tcl_IncrRefCount(resultPtr); |
| 910 |
Tcl_DecrRefCount(splitPtr); |
| 911 |
return resultPtr; |
| 912 |
} |
| 913 |
|
| 914 |
/* |
| 915 |
*---------------------------------------------------------------------- |
| 916 |
* |
| 917 |
* TclFileAttrsCmd -- |
| 918 |
* |
| 919 |
* Sets or gets the platform-specific attributes of a file. The objc-objv |
| 920 |
* points to the file name with the rest of the command line following. |
| 921 |
* This routine uses platform-specific tables of option strings and |
| 922 |
* callbacks. The callback to get the attributes take three parameters: |
| 923 |
* Tcl_Interp *interp; The interp to report errors with. Since |
| 924 |
* this is an object-based API, the object |
| 925 |
* form of the result should be used. |
| 926 |
* const char *fileName; This is extracted using |
| 927 |
* Tcl_TranslateFileName. |
| 928 |
* TclObj **attrObjPtrPtr; A new object to hold the attribute is |
| 929 |
* allocated and put here. |
| 930 |
* The first two parameters of the callback used to write out the |
| 931 |
* attributes are the same. The third parameter is: |
| 932 |
* const *attrObjPtr; A pointer to the object that has the new |
| 933 |
* attribute. |
| 934 |
* They both return standard TCL errors; if the routine to get an |
| 935 |
* attribute fails, no object is allocated and *attrObjPtrPtr is |
| 936 |
* unchanged. |
| 937 |
* |
| 938 |
* Results: |
| 939 |
* Standard TCL error. |
| 940 |
* |
| 941 |
* Side effects: |
| 942 |
* May set file attributes for the file name. |
| 943 |
* |
| 944 |
*---------------------------------------------------------------------- |
| 945 |
*/ |
| 946 |
|
| 947 |
int |
| 948 |
TclFileAttrsCmd( |
| 949 |
ClientData clientData, /* Unused */ |
| 950 |
Tcl_Interp *interp, /* The interpreter for error reporting. */ |
| 951 |
int objc, /* Number of command line arguments. */ |
| 952 |
Tcl_Obj *const objv[]) /* The command line objects. */ |
| 953 |
{ |
| 954 |
int result; |
| 955 |
const char *const *attributeStrings; |
| 956 |
const char **attributeStringsAllocated = NULL; |
| 957 |
Tcl_Obj *objStrings = NULL; |
| 958 |
int numObjStrings = -1; |
| 959 |
Tcl_Obj *filePtr; |
| 960 |
|
| 961 |
if (objc < 2) { |
| 962 |
Tcl_WrongNumArgs(interp, 1, objv, "name ?-option value ...?"); |
| 963 |
return TCL_ERROR; |
| 964 |
} |
| 965 |
|
| 966 |
filePtr = objv[1]; |
| 967 |
if (Tcl_FSConvertToPathType(interp, filePtr) != TCL_OK) { |
| 968 |
return TCL_ERROR; |
| 969 |
} |
| 970 |
|
| 971 |
objc -= 2; |
| 972 |
objv += 2; |
| 973 |
result = TCL_ERROR; |
| 974 |
Tcl_SetErrno(0); |
| 975 |
|
| 976 |
/* |
| 977 |
* Get the set of attribute names from the filesystem. |
| 978 |
*/ |
| 979 |
|
| 980 |
attributeStrings = Tcl_FSFileAttrStrings(filePtr, &objStrings); |
| 981 |
if (attributeStrings == NULL) { |
| 982 |
int index; |
| 983 |
Tcl_Obj *objPtr; |
| 984 |
|
| 985 |
if (objStrings == NULL) { |
| 986 |
if (Tcl_GetErrno() != 0) { |
| 987 |
/* |
| 988 |
* There was an error, probably that the filePtr is not |
| 989 |
* accepted by any filesystem |
| 990 |
*/ |
| 991 |
|
| 992 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 993 |
"could not read \"%s\": %s", |
| 994 |
TclGetString(filePtr), Tcl_PosixError(interp))); |
| 995 |
} |
| 996 |
return TCL_ERROR; |
| 997 |
} |
| 998 |
|
| 999 |
/* |
| 1000 |
* We own the object now. |
| 1001 |
*/ |
| 1002 |
|
| 1003 |
Tcl_IncrRefCount(objStrings); |
| 1004 |
|
| 1005 |
/* |
| 1006 |
* Use objStrings as a list object. |
| 1007 |
*/ |
| 1008 |
|
| 1009 |
if (TclListObjLength(interp, objStrings, &numObjStrings) != TCL_OK) { |
| 1010 |
goto end; |
| 1011 |
} |
| 1012 |
attributeStringsAllocated = (const char **) |
| 1013 |
TclStackAlloc(interp, (1+numObjStrings) * sizeof(char *)); |
| 1014 |
for (index = 0; index < numObjStrings; index++) { |
| 1015 |
Tcl_ListObjIndex(interp, objStrings, index, &objPtr); |
| 1016 |
attributeStringsAllocated[index] = TclGetString(objPtr); |
| 1017 |
} |
| 1018 |
attributeStringsAllocated[index] = NULL; |
| 1019 |
attributeStrings = attributeStringsAllocated; |
| 1020 |
} else if (objStrings != NULL) { |
| 1021 |
Tcl_Panic("must not update objPtrRef's variable and return non-NULL"); |
| 1022 |
} |
| 1023 |
|
| 1024 |
/* |
| 1025 |
* Process the attributes to produce a list of all of them, the value of a |
| 1026 |
* particular attribute, or to set one or more attributes (depending on |
| 1027 |
* the number of arguments). |
| 1028 |
*/ |
| 1029 |
|
| 1030 |
if (objc == 0) { |
| 1031 |
/* |
| 1032 |
* Get all attributes. |
| 1033 |
*/ |
| 1034 |
|
| 1035 |
int index, res = TCL_OK, nbAtts = 0; |
| 1036 |
Tcl_Obj *listPtr; |
| 1037 |
|
| 1038 |
listPtr = Tcl_NewListObj(0, NULL); |
| 1039 |
for (index = 0; attributeStrings[index] != NULL; index++) { |
| 1040 |
Tcl_Obj *objPtrAttr; |
| 1041 |
|
| 1042 |
if (res != TCL_OK) { |
| 1043 |
/* |
| 1044 |
* Clear the error from the last iteration. |
| 1045 |
*/ |
| 1046 |
|
| 1047 |
Tcl_ResetResult(interp); |
| 1048 |
} |
| 1049 |
|
| 1050 |
res = Tcl_FSFileAttrsGet(interp, index, filePtr, &objPtrAttr); |
| 1051 |
if (res == TCL_OK) { |
| 1052 |
Tcl_Obj *objPtr = |
| 1053 |
Tcl_NewStringObj(attributeStrings[index], -1); |
| 1054 |
|
| 1055 |
Tcl_ListObjAppendElement(interp, listPtr, objPtr); |
| 1056 |
Tcl_ListObjAppendElement(interp, listPtr, objPtrAttr); |
| 1057 |
nbAtts++; |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
if (index > 0 && nbAtts == 0) { |
| 1062 |
/* |
| 1063 |
* Error: no valid attributes found. |
| 1064 |
*/ |
| 1065 |
|
| 1066 |
Tcl_DecrRefCount(listPtr); |
| 1067 |
goto end; |
| 1068 |
} |
| 1069 |
|
| 1070 |
Tcl_SetObjResult(interp, listPtr); |
| 1071 |
} else if (objc == 1) { |
| 1072 |
/* |
| 1073 |
* Get one attribute. |
| 1074 |
*/ |
| 1075 |
|
| 1076 |
int index; |
| 1077 |
Tcl_Obj *objPtr = NULL; |
| 1078 |
|
| 1079 |
if (numObjStrings == 0) { |
| 1080 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1081 |
"bad option \"%s\", there are no file attributes in this" |
| 1082 |
" filesystem", TclGetString(objv[0]))); |
| 1083 |
Tcl_SetErrorCode(interp, "TCL","OPERATION","FATTR","NONE", (char *)NULL); |
| 1084 |
goto end; |
| 1085 |
} |
| 1086 |
|
| 1087 |
if (Tcl_GetIndexFromObj(interp, objv[0], attributeStrings, |
| 1088 |
"option", 0, &index) != TCL_OK) { |
| 1089 |
goto end; |
| 1090 |
} |
| 1091 |
if (attributeStringsAllocated != NULL) { |
| 1092 |
TclFreeIntRep(objv[0]); |
| 1093 |
} |
| 1094 |
if (Tcl_FSFileAttrsGet(interp, index, filePtr, |
| 1095 |
&objPtr) != TCL_OK) { |
| 1096 |
goto end; |
| 1097 |
} |
| 1098 |
Tcl_SetObjResult(interp, objPtr); |
| 1099 |
} else { |
| 1100 |
/* |
| 1101 |
* Set option/value pairs. |
| 1102 |
*/ |
| 1103 |
|
| 1104 |
int i, index; |
| 1105 |
|
| 1106 |
if (numObjStrings == 0) { |
| 1107 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1108 |
"bad option \"%s\", there are no file attributes in this" |
| 1109 |
" filesystem", TclGetString(objv[0]))); |
| 1110 |
Tcl_SetErrorCode(interp, "TCL","OPERATION","FATTR","NONE", (char *)NULL); |
| 1111 |
goto end; |
| 1112 |
} |
| 1113 |
|
| 1114 |
for (i = 0; i < objc ; i += 2) { |
| 1115 |
if (Tcl_GetIndexFromObj(interp, objv[i], attributeStrings, |
| 1116 |
"option", 0, &index) != TCL_OK) { |
| 1117 |
goto end; |
| 1118 |
} |
| 1119 |
if (attributeStringsAllocated != NULL) { |
| 1120 |
TclFreeIntRep(objv[i]); |
| 1121 |
} |
| 1122 |
if (i + 1 == objc) { |
| 1123 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1124 |
"value for \"%s\" missing", TclGetString(objv[i]))); |
| 1125 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "FATTR", |
| 1126 |
"NOVALUE", (char *)NULL); |
| 1127 |
goto end; |
| 1128 |
} |
| 1129 |
if (Tcl_FSFileAttrsSet(interp, index, filePtr, |
| 1130 |
objv[i + 1]) != TCL_OK) { |
| 1131 |
goto end; |
| 1132 |
} |
| 1133 |
} |
| 1134 |
} |
| 1135 |
result = TCL_OK; |
| 1136 |
|
| 1137 |
/* |
| 1138 |
* Free up the array we allocated and drop our reference to any list of |
| 1139 |
* attribute names issued by the filesystem. |
| 1140 |
*/ |
| 1141 |
|
| 1142 |
end: |
| 1143 |
if (attributeStringsAllocated != NULL) { |
| 1144 |
TclStackFree(interp, (void *) attributeStringsAllocated); |
| 1145 |
} |
| 1146 |
if (objStrings != NULL) { |
| 1147 |
Tcl_DecrRefCount(objStrings); |
| 1148 |
} |
| 1149 |
return result; |
| 1150 |
} |
| 1151 |
|
| 1152 |
/* |
| 1153 |
*---------------------------------------------------------------------- |
| 1154 |
* |
| 1155 |
* TclFileLinkCmd -- |
| 1156 |
* |
| 1157 |
* This function is invoked to process the "file link" Tcl command. See |
| 1158 |
* the user documentation for details on what it does. |
| 1159 |
* |
| 1160 |
* Results: |
| 1161 |
* A standard Tcl result. |
| 1162 |
* |
| 1163 |
* Side effects: |
| 1164 |
* May create a new link. |
| 1165 |
* |
| 1166 |
*---------------------------------------------------------------------- |
| 1167 |
*/ |
| 1168 |
|
| 1169 |
int |
| 1170 |
TclFileLinkCmd( |
| 1171 |
ClientData clientData, |
| 1172 |
Tcl_Interp *interp, |
| 1173 |
int objc, |
| 1174 |
Tcl_Obj *const objv[]) |
| 1175 |
{ |
| 1176 |
Tcl_Obj *contents; |
| 1177 |
int index; |
| 1178 |
|
| 1179 |
if (objc < 2 || objc > 4) { |
| 1180 |
Tcl_WrongNumArgs(interp, 1, objv, "?-linktype? linkname ?target?"); |
| 1181 |
return TCL_ERROR; |
| 1182 |
} |
| 1183 |
|
| 1184 |
/* |
| 1185 |
* Index of the 'source' argument. |
| 1186 |
*/ |
| 1187 |
|
| 1188 |
if (objc == 4) { |
| 1189 |
index = 2; |
| 1190 |
} else { |
| 1191 |
index = 1; |
| 1192 |
} |
| 1193 |
|
| 1194 |
if (objc > 2) { |
| 1195 |
int linkAction; |
| 1196 |
|
| 1197 |
if (objc == 4) { |
| 1198 |
/* |
| 1199 |
* We have a '-linktype' argument. |
| 1200 |
*/ |
| 1201 |
|
| 1202 |
static const char *const linkTypes[] = { |
| 1203 |
"-symbolic", "-hard", NULL |
| 1204 |
}; |
| 1205 |
if (Tcl_GetIndexFromObj(interp, objv[1], linkTypes, "option", 0, |
| 1206 |
&linkAction) != TCL_OK) { |
| 1207 |
return TCL_ERROR; |
| 1208 |
} |
| 1209 |
if (linkAction == 0) { |
| 1210 |
linkAction = TCL_CREATE_SYMBOLIC_LINK; |
| 1211 |
} else { |
| 1212 |
linkAction = TCL_CREATE_HARD_LINK; |
| 1213 |
} |
| 1214 |
} else { |
| 1215 |
linkAction = TCL_CREATE_SYMBOLIC_LINK | TCL_CREATE_HARD_LINK; |
| 1216 |
} |
| 1217 |
if (Tcl_FSConvertToPathType(interp, objv[index]) != TCL_OK) { |
| 1218 |
return TCL_ERROR; |
| 1219 |
} |
| 1220 |
|
| 1221 |
/* |
| 1222 |
* Create link from source to target. |
| 1223 |
*/ |
| 1224 |
|
| 1225 |
contents = Tcl_FSLink(objv[index], objv[index+1], linkAction); |
| 1226 |
if (contents == NULL) { |
| 1227 |
/* |
| 1228 |
* We handle three common error cases specially, and for all other |
| 1229 |
* errors, we use the standard Posix error message. |
| 1230 |
*/ |
| 1231 |
|
| 1232 |
if (errno == EEXIST) { |
| 1233 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1234 |
"could not create new link \"%s\": that path already" |
| 1235 |
" exists", TclGetString(objv[index]))); |
| 1236 |
Tcl_PosixError(interp); |
| 1237 |
} else if (errno == ENOENT) { |
| 1238 |
/* |
| 1239 |
* There are two cases here: either the target doesn't exist, |
| 1240 |
* or the directory of the src doesn't exist. |
| 1241 |
*/ |
| 1242 |
|
| 1243 |
int access; |
| 1244 |
Tcl_Obj *dirPtr = TclPathPart(interp, objv[index], |
| 1245 |
TCL_PATH_DIRNAME); |
| 1246 |
|
| 1247 |
if (dirPtr == NULL) { |
| 1248 |
return TCL_ERROR; |
| 1249 |
} |
| 1250 |
access = Tcl_FSAccess(dirPtr, F_OK); |
| 1251 |
Tcl_DecrRefCount(dirPtr); |
| 1252 |
if (access != 0) { |
| 1253 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1254 |
"could not create new link \"%s\": no such file" |
| 1255 |
" or directory", TclGetString(objv[index]))); |
| 1256 |
Tcl_PosixError(interp); |
| 1257 |
} else { |
| 1258 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1259 |
"could not create new link \"%s\": target \"%s\" " |
| 1260 |
"doesn't exist", TclGetString(objv[index]), |
| 1261 |
TclGetString(objv[index+1]))); |
| 1262 |
errno = ENOENT; |
| 1263 |
Tcl_PosixError(interp); |
| 1264 |
} |
| 1265 |
} else { |
| 1266 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1267 |
"could not create new link \"%s\" pointing to \"%s\": %s", |
| 1268 |
TclGetString(objv[index]), |
| 1269 |
TclGetString(objv[index+1]), Tcl_PosixError(interp))); |
| 1270 |
} |
| 1271 |
return TCL_ERROR; |
| 1272 |
} |
| 1273 |
} else { |
| 1274 |
if (Tcl_FSConvertToPathType(interp, objv[index]) != TCL_OK) { |
| 1275 |
return TCL_ERROR; |
| 1276 |
} |
| 1277 |
|
| 1278 |
/* |
| 1279 |
* Read link |
| 1280 |
*/ |
| 1281 |
|
| 1282 |
contents = Tcl_FSLink(objv[index], NULL, 0); |
| 1283 |
if (contents == NULL) { |
| 1284 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1285 |
"could not read link \"%s\": %s", |
| 1286 |
TclGetString(objv[index]), Tcl_PosixError(interp))); |
| 1287 |
return TCL_ERROR; |
| 1288 |
} |
| 1289 |
} |
| 1290 |
Tcl_SetObjResult(interp, contents); |
| 1291 |
if (objc == 2) { |
| 1292 |
/* |
| 1293 |
* If we are reading a link, we need to free this result refCount. If |
| 1294 |
* we are creating a link, this will just be objv[index+1], and so we |
| 1295 |
* don't own it. |
| 1296 |
*/ |
| 1297 |
|
| 1298 |
Tcl_DecrRefCount(contents); |
| 1299 |
} |
| 1300 |
return TCL_OK; |
| 1301 |
} |
| 1302 |
|
| 1303 |
/* |
| 1304 |
*---------------------------------------------------------------------- |
| 1305 |
* |
| 1306 |
* TclFileReadLinkCmd -- |
| 1307 |
* |
| 1308 |
* This function is invoked to process the "file readlink" Tcl command. |
| 1309 |
* See the user documentation for details on what it does. |
| 1310 |
* |
| 1311 |
* Results: |
| 1312 |
* A standard Tcl result. |
| 1313 |
* |
| 1314 |
* Side effects: |
| 1315 |
* None. |
| 1316 |
* |
| 1317 |
*---------------------------------------------------------------------- |
| 1318 |
*/ |
| 1319 |
|
| 1320 |
int |
| 1321 |
TclFileReadLinkCmd( |
| 1322 |
ClientData clientData, |
| 1323 |
Tcl_Interp *interp, |
| 1324 |
int objc, |
| 1325 |
Tcl_Obj *const objv[]) |
| 1326 |
{ |
| 1327 |
Tcl_Obj *contents; |
| 1328 |
|
| 1329 |
if (objc != 2) { |
| 1330 |
Tcl_WrongNumArgs(interp, 1, objv, "name"); |
| 1331 |
return TCL_ERROR; |
| 1332 |
} |
| 1333 |
|
| 1334 |
if (Tcl_FSConvertToPathType(interp, objv[1]) != TCL_OK) { |
| 1335 |
return TCL_ERROR; |
| 1336 |
} |
| 1337 |
|
| 1338 |
contents = Tcl_FSLink(objv[1], NULL, 0); |
| 1339 |
|
| 1340 |
if (contents == NULL) { |
| 1341 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1342 |
"could not read link \"%s\": %s", |
| 1343 |
TclGetString(objv[1]), Tcl_PosixError(interp))); |
| 1344 |
return TCL_ERROR; |
| 1345 |
} |
| 1346 |
Tcl_SetObjResult(interp, contents); |
| 1347 |
Tcl_DecrRefCount(contents); |
| 1348 |
return TCL_OK; |
| 1349 |
} |
| 1350 |
|
| 1351 |
/* |
| 1352 |
*--------------------------------------------------------------------------- |
| 1353 |
* |
| 1354 |
* TclFileTemporaryCmd |
| 1355 |
* |
| 1356 |
* This function implements the "tempfile" subcommand of the "file" |
| 1357 |
* command. |
| 1358 |
* |
| 1359 |
* Results: |
| 1360 |
* Returns a standard Tcl result. |
| 1361 |
* |
| 1362 |
* Side effects: |
| 1363 |
* Creates a temporary file. Opens a channel to that file and puts the |
| 1364 |
* name of that channel in the result. *Might* register suitable exit |
| 1365 |
* handlers to ensure that the temporary file gets deleted. Might write |
| 1366 |
* to a variable, so reentrancy is a potential issue. |
| 1367 |
* |
| 1368 |
*--------------------------------------------------------------------------- |
| 1369 |
*/ |
| 1370 |
|
| 1371 |
int |
| 1372 |
TclFileTemporaryCmd( |
| 1373 |
ClientData clientData, |
| 1374 |
Tcl_Interp *interp, |
| 1375 |
int objc, |
| 1376 |
Tcl_Obj *const objv[]) |
| 1377 |
{ |
| 1378 |
Tcl_Obj *nameVarObj = NULL; /* Variable to store the name of the temporary |
| 1379 |
* file in. */ |
| 1380 |
Tcl_Obj *nameObj = NULL; /* Object that will contain the filename. */ |
| 1381 |
Tcl_Channel chan; /* The channel opened (RDWR) on the temporary |
| 1382 |
* file, or NULL if there's an error. */ |
| 1383 |
Tcl_Obj *tempDirObj = NULL, *tempBaseObj = NULL, *tempExtObj = NULL; |
| 1384 |
/* Pieces of template. Each piece is NULL if |
| 1385 |
* it is omitted. The platform temporary file |
| 1386 |
* engine might ignore some pieces. */ |
| 1387 |
|
| 1388 |
if (objc < 1 || objc > 3) { |
| 1389 |
Tcl_WrongNumArgs(interp, 1, objv, "?nameVar? ?template?"); |
| 1390 |
return TCL_ERROR; |
| 1391 |
} |
| 1392 |
|
| 1393 |
if (objc > 1) { |
| 1394 |
nameVarObj = objv[1]; |
| 1395 |
TclNewObj(nameObj); |
| 1396 |
} |
| 1397 |
if (objc > 2) { |
| 1398 |
int length; |
| 1399 |
Tcl_Obj *templateObj = objv[2]; |
| 1400 |
const char *string = TclGetStringFromObj(templateObj, &length); |
| 1401 |
|
| 1402 |
/* |
| 1403 |
* Treat an empty string as if it wasn't there. |
| 1404 |
*/ |
| 1405 |
|
| 1406 |
if (length == 0) { |
| 1407 |
goto makeTemporary; |
| 1408 |
} |
| 1409 |
|
| 1410 |
/* |
| 1411 |
* The template only gives a directory if there is a directory |
| 1412 |
* separator in it. |
| 1413 |
*/ |
| 1414 |
|
| 1415 |
if (strchr(string, '/') != NULL |
| 1416 |
|| (tclPlatform == TCL_PLATFORM_WINDOWS |
| 1417 |
&& strchr(string, '\\') != NULL)) { |
| 1418 |
tempDirObj = TclPathPart(interp, templateObj, TCL_PATH_DIRNAME); |
| 1419 |
|
| 1420 |
/* |
| 1421 |
* Only allow creation of temporary files in the native filesystem |
| 1422 |
* since they are frequently used for integration with external |
| 1423 |
* tools or system libraries. [Bug 2388866] |
| 1424 |
*/ |
| 1425 |
|
| 1426 |
if (tempDirObj != NULL && Tcl_FSGetFileSystemForPath(tempDirObj) |
| 1427 |
!= &tclNativeFilesystem) { |
| 1428 |
TclDecrRefCount(tempDirObj); |
| 1429 |
tempDirObj = NULL; |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
/* |
| 1434 |
* The template only gives the filename if the last character isn't a |
| 1435 |
* directory separator. |
| 1436 |
*/ |
| 1437 |
|
| 1438 |
if (string[length-1] != '/' && (tclPlatform != TCL_PLATFORM_WINDOWS |
| 1439 |
|| string[length-1] != '\\')) { |
| 1440 |
Tcl_Obj *tailObj = TclPathPart(interp, templateObj, |
| 1441 |
TCL_PATH_TAIL); |
| 1442 |
|
| 1443 |
if (tailObj != NULL) { |
| 1444 |
tempBaseObj = TclPathPart(interp, tailObj, TCL_PATH_ROOT); |
| 1445 |
tempExtObj = TclPathPart(interp, tailObj, TCL_PATH_EXTENSION); |
| 1446 |
TclDecrRefCount(tailObj); |
| 1447 |
} |
| 1448 |
} |
| 1449 |
} |
| 1450 |
|
| 1451 |
/* |
| 1452 |
* Convert empty parts of the template into unspecified parts. |
| 1453 |
*/ |
| 1454 |
|
| 1455 |
if (tempDirObj && !TclGetString(tempDirObj)[0]) { |
| 1456 |
TclDecrRefCount(tempDirObj); |
| 1457 |
tempDirObj = NULL; |
| 1458 |
} |
| 1459 |
if (tempBaseObj && !TclGetString(tempBaseObj)[0]) { |
| 1460 |
TclDecrRefCount(tempBaseObj); |
| 1461 |
tempBaseObj = NULL; |
| 1462 |
} |
| 1463 |
if (tempExtObj && !TclGetString(tempExtObj)[0]) { |
| 1464 |
TclDecrRefCount(tempExtObj); |
| 1465 |
tempExtObj = NULL; |
| 1466 |
} |
| 1467 |
|
| 1468 |
/* |
| 1469 |
* Create and open the temporary file. |
| 1470 |
*/ |
| 1471 |
|
| 1472 |
makeTemporary: |
| 1473 |
chan = TclpOpenTemporaryFile(tempDirObj,tempBaseObj,tempExtObj, nameObj); |
| 1474 |
|
| 1475 |
/* |
| 1476 |
* If we created pieces of template, get rid of them now. |
| 1477 |
*/ |
| 1478 |
|
| 1479 |
if (tempDirObj) { |
| 1480 |
TclDecrRefCount(tempDirObj); |
| 1481 |
} |
| 1482 |
if (tempBaseObj) { |
| 1483 |
TclDecrRefCount(tempBaseObj); |
| 1484 |
} |
| 1485 |
if (tempExtObj) { |
| 1486 |
TclDecrRefCount(tempExtObj); |
| 1487 |
} |
| 1488 |
|
| 1489 |
/* |
| 1490 |
* Deal with results. |
| 1491 |
*/ |
| 1492 |
|
| 1493 |
if (chan == NULL) { |
| 1494 |
if (nameVarObj) { |
| 1495 |
TclDecrRefCount(nameObj); |
| 1496 |
} |
| 1497 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1498 |
"can't create temporary file: %s", Tcl_PosixError(interp))); |
| 1499 |
return TCL_ERROR; |
| 1500 |
} |
| 1501 |
Tcl_RegisterChannel(interp, chan); |
| 1502 |
if (nameVarObj != NULL) { |
| 1503 |
if (Tcl_ObjSetVar2(interp, nameVarObj, NULL, nameObj, |
| 1504 |
TCL_LEAVE_ERR_MSG) == NULL) { |
| 1505 |
Tcl_UnregisterChannel(interp, chan); |
| 1506 |
return TCL_ERROR; |
| 1507 |
} |
| 1508 |
} |
| 1509 |
Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_GetChannelName(chan), -1)); |
| 1510 |
return TCL_OK; |
| 1511 |
} |
| 1512 |
|
| 1513 |
/* |
| 1514 |
* Local Variables: |
| 1515 |
* mode: c |
| 1516 |
* c-basic-offset: 4 |
| 1517 |
* fill-column: 78 |
| 1518 |
* End: |
| 1519 |
*/ |