A bug in file.copy(), deletes file contents

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

A bug in file.copy(), deletes file contents

Pasi Haapakorva
Hi,

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:

dir.create("foo")
cat(file = "foo/bar.txt", "baz\n")
readLines("foo/bar.txt")
#> [1] "baz"

file.copy("foo", ".", recursive = TRUE)
#> [1] TRUE
readLines("foo/bar.txt")
#> character(0)

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
though normalizePath().

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.

Pasi Haapakorva

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel