Tcl Source Code

View Ticket
Login
Ticket UUID: 2406596
Title: file delete reports an error on non-existent file
Type: Bug Created on: 2008-12-08 14:16:46
Submitter: ralfixx Assigned to: vincentdarley
Subsystem: 37. File System Severity:
Priority: 5 Medium Last modified: 2026-10-22 17:19:31
Status: Closed Closed by: serhiy.storchaka
Resolution: Fixed Closed on: 2026-10-21 10:03:39
Version: obsolete: 8.5.5
Description:
[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 ?
User Comments:
dkf added on 2008-12-08 21:22:35:
Macs *are* Unix in this area.

ferrieux added on 2008-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.storchaka added on 2026-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.