[issue46791] Allow os.remove to defer to rmdir

Eryk Sun report at bugs.python.org
Tue Mar 1 04:58:03 EST 2022


Eryk Sun <eryksun at gmail.com> added the comment:

glibc remove() has an optimization to skip calling rmdir() if the macro condition IS_NO_DIRECTORY_ERROR is true for the unlink() error. For Linux, this condition is `errno != EISDIR`. On other platforms (e.g. BSD systems), the condition is `errno != EPERM`. The implementation is the following, from "sysdeps/posix/remove.c":

    int
    remove (const char *file)
    {
      /* First try to unlink since this is more frequently the necessary action. */
      if (__unlink (file) != 0
          /* If it is indeed a directory...  */
          && (IS_NO_DIRECTORY_ERROR
          /* ...try to remove it.  */
          || __rmdir (file) != 0))
        /* Cannot remove the object for whatever reason.  */
        return -1;

      return 0;
    }

WinAPI DeleteFileW() doesn't support the same distinction. In this case, ERROR_ACCESS_DENIED is set for the following system status codes:

    STATUS_ACCESS_DENIED (like EACCES)
    STATUS_CANNOT_DELETE (like EPERM; readonly or image-mapped)
    STATUS_DELETE_PENDING (like EPERM)
    STATUS_FILE_IS_A_DIRECTORY (like EISDIR)

os.remove() could skip skip calling RemoveDirectoryW() for a sharing violation, i.e. ERROR_SHARING_VIOLATION. If DeleteFileW() fails with a sharing violation, the path is not a directory and, even if it were, an attempt to delete it would fail.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46791>
_______________________________________


More information about the Python-bugs-list mailing list