[Rd] A bug in file.copy(), deletes file contents
p@@|@h@@p@korv@ @end|ng |rom gm@||@com
Thu Apr 25 16:37:06 CEST 2019
I've discovered a way to accidentally delete all your files with
file.copy(). This involves copying a directory to itself by bypassing
the check `if (recursive && to %in% from)` by pointing to a directory
with two differing ways.
Here I'm copying the directory foo to itself:
cat(file = "foo/bar.txt", "baz\n")
#>  "baz"
file.copy("foo", ".", recursive = TRUE)
#>  TRUE
Since "foo" is not ".", the directory is copied to itself and the
function calls file.create() to delete the file contents.
A simple fix would be to use `if (recursive && (to %in% from || from
%in% list.files(to)))` instead, I think. This could be slow if there
are a lot of files, so maybe this could be faster:
if (recursive && (to %in% from ||
normalizePath(dirname(normalizePath(from))) == normalizePath(to)))
There I'm looking if the parent dir of `from` matches the dir of `to`.
At least on my machine dirname() gives different path syntax than
normalizePath() so both input and output of dirname() needs to be put
There is a related bug in file.create(): it doesn't check if the file
already exists and goes on to delete contents of existing files.
More information about the R-devel