Skip to content

is fs::remove_file() guaranteed to remove the symlink link? #143189

Open
@midnightveil

Description

@midnightveil

Location

https://doc.rust-lang.org/stable/std/fs/fn.remove_file.html

Summary

The documentation for std::fs::remove_file() states:

Removes a file from the filesystem.
[...]
This function currently corresponds to the unlink function on Unix. On Windows, DeleteFile is used or CreateFileW and SetInformationByHandle for readonly files. Note that, this may change in the future.

As noted, the implementation details may changed in the future. It is not mentioned whether or not this removes the symlink link, or the target, or whether this is platform-specific.

At the moment, both unlink 1 and DeleteFile 2 remove the link, not the target. The popular symlink crate has been relying on this behaviour for over 8 years.

Is this behaviour part of the API contract?

Footnotes

  1. https://www.man7.org/linux/man-pages/man2/unlink.2.html#DESCRIPTION

    If the name referred to a symbolic link, the link is removed.

  2. https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-deletefile#remarks

    If the path points to a symbolic link, the symbolic link is deleted, not the target.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-filesystemArea: `std::fs`T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions