Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-22
17:19 • Ticket [2406596fff] file delete reports an error on non-existent file status... artifact: 7c07226ca1 user: serhiy.storchaka
2026-10-21
10:03 • Closed ticket [2406596fff]. artifact: 83e45c63fb user: serhiy.storchaka
2008-12-08
21:42 • Ticket [2406596fff]: 4 changes artifact: 13bf146148 user: ferrieux
21:22 • Ticket [2406596fff]: 4 changes artifact: 12929f2cae user: dkf
14:16 • New ticket [2406596fff]. artifact: 9cd2f84cd9 user: ralfixx

file delete reports an error on non-existent file

Closed / Fixed2406596fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 41 days ago
Descriptionplain2008-12-08 14:16:46
[file delete] is documented as:
   Trying to delete a non-existent file is not considered an error.

However, the following code reports an error when run in two
simultaneous processes on Linux:

    #!/bin/sh --
    set testfile /tmp/testfile
    while {1} {
        # create the testfile, ignore any errors
catch {close [open $testfile w]}
# call file delete: should not err out
file delete $testfile
# second call: with a single process this is no problem
file delete $testfile
    }
    # End of file

    tclsh8.5 t.tcl
    => runs ok

    tclsh8.5 t.tcl & tclsh8.5 t.tcl
    => immediate error from one of the processes
    error deleting "/tmp/testfile": no such file or directory
while executing
    "file delete $testfile"
("while" body line 3)
invoked from within
    "while {1} {
catch {close [open $testfile w]}
file delete $testfile
file delete $testfile
    }"
(file "t.tcl" line 3)

According to the documentation this error should never occur.

Citing a message on comp.lang.tcl:

    From: Ralf Fassel <[email protected]>
    Newsgroups: comp.lang.tcl
    Subject: Re: catch and trap
    Date: Thu, 23 Oct 2008 22:08:48 +0200
    Message-ID: <[email protected]>

    * "Donal K. Fellows" <[email protected]>
    | In order to know which syscall to use, Tcl has to lstat() it first
    | (lstat because we want to just remove the link if it is a symlink to
    | a directory); if we get ENOENT from the lstat(), we don't bother
    | doing anything else as the file is (probably) gone;
    --<snip-snip>--

    But if the file is there at lstat-time, and it is a file, then in
    tcl-8.5.2 the result of
       result = Tcl_FSDeleteFile(objv[i]);
    is returned, which boils down to calling unlink(2) on Unix, which
    returns an ENOENT error for non-existing files, and *this* is then
    translated into TCL_ERROR (unix/tclUnixFCmd.c, TclpDeleteFile()).

   

Proposed patch for Unix:

*** tcl8.5.5/unix/tclUnixFCmd.c~Thu Dec 13 16:28:42 2007
--- tcl8.5.5/unix/tclUnixFCmd.cMon Dec  8 15:11:58 2008
***************
*** 633,639 ****
  TclpDeleteFile(
      CONST char *path)/* Pathname of file to be removed (native). */
  {
!     if (unlink(path) != 0) {/* INTL: Native. */
  return TCL_ERROR;
      }
      return TCL_OK;
--- 633,639 ----
  TclpDeleteFile(
      CONST char *path)/* Pathname of file to be removed (native). */
  {
!     if (unlink(path) != 0 && errno != ENOENT) {/* INTL: Native. */
  return TCL_ERROR;
      }
      return TCL_OK;

Diff finished.  Mon Dec  8 15:12:04 2008

patches for windows & mac ?
dkfplain2008-12-08 21:22:35
Macs *are* Unix in this area.
ferrieuxplain2008-12-08 21:42:29
First, please use 'diff -u' format ;-)
Second, the proposed patch hides the truth one level too low. Indeed, it prevents any caller of TclpDeleteFile from learning about the absence of the file.
I think this hiding should just happen in TclFileDeleteCmd (the caller) rather than in TclpDeleteFile (the callee). This way you won't even need a per-platform patch since it is in the generic layer.
serhiy.storchakawiki2026-10-21 10:03:39

Fixed in 8.6.9 by [5acb57c7ae] (ticket [27b6822849]): file delete now ignores ENOENT when the file vanishes between lstat() and unlink(), in the generic layer as suggested here. The two-process script fails with 8.6.8 and passes with 8.6.9 and later.

serhiy.storchaka Ticket [2406596fff]: 3 changes [History diff]2026-10-22 17:19:31
Log in (anonymous is fine) to add a comment.