You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This function currently corresponds to the rename function on Unix and the MoveFileEx function with the MOVEFILE_REPLACE_EXISTING flag on Windows.
Because of this, the behavior when both from and to exist differs. On Unix, if from is a directory, to must also be an (empty) directory. If from is not a directory, to must also be not a directory. In contrast, on Windows, from can be anything, but to must not be a directory.
Therefore, the simple statement
If a file or directory already exists at the target location, it will be overwritten by this operation.
does not cover the cases where it WILL return an error, which is:
Unix (and Linux):
from and to is an existing directory and a file, or a file and a directory respectively.
from and to are both directories, but to is not empty.
Windows
from is a existing directory.
This behavior can be demonstrated with the following code:
fnmain() -> io::Result<()>{
async_std::task::block_on(async{let from = Path::new("./foo");//Create some directories...
fs::create_dir_all(from).await?;
fs::create_dir_all("./bar/baz").await?;//This should work... or does it?
fs::rename(from,"./bar").await})}
(Note that this uses fs::create_dir_all() to avoid returning an error if the directories already exist, see #1051)
Running the above code prints
Error: Custom { kind: PermissionDenied, error: VerboseError { source: Os { code: 5, kind: PermissionDenied, message: "Access is denied." }, message: "could not rename `./foo` to `./bar`" } }
for Windows, and
Error: Custom { kind: DirectoryNotEmpty, error: VerboseError { source: Os { code: 39, kind: DirectoryNotEmpty, message: "Directory not empty" }, message: "could not rename `./foo` to `./bar`" } }
for Linux.
The text was updated successfully, but these errors were encountered:
Found another minor documentation error. Yay :)
Maybe I'm missing something, but
fs::rename()
's docs also have the wrong behavior documented:However, async-std's
rename()
uses std'srename()
internally, which currently MAY return an error ifto
exists:Therefore, the simple statement
does not cover the cases where it WILL return an error, which is:
Unix (and Linux):
from
andto
is an existing directory and a file, or a file and a directory respectively.from
andto
are both directories, butto
is not empty.Windows
from
is a existing directory.This behavior can be demonstrated with the following code:
(Note that this uses
fs::create_dir_all()
to avoid returning an error if the directories already exist, see #1051)Running the above code prints
for Windows, and
for Linux.
The text was updated successfully, but these errors were encountered: