[Rd] file.copy(from=Directory, to=File) oddity

Martin Maechler maechler at stat.math.ethz.ch
Mon Sep 11 16:01:12 CEST 2017


>>>>> William Dunlap via R-devel <r-devel at r-project.org>
>>>>>     on Fri, 8 Sep 2017 09:54:58 -0700 writes:

    > When I mistakenly use file.copy() with a directory for the 'from' argument
    > and a non-directory for the 'to' and overwrite=TRUE, file.copy returns
    > FALSE, meaning it could not do the copying.  However, it also replaces the
    > 'to' file with a zero-length file.

    > dir.create( fromDir <- tempfile() )
    > cat(file = toFile <- tempfile(), "existing file\n")
    > readLines(toFile)
    > #[1] "existing file"
    > file.copy(fromDir, toFile, recursive=FALSE, overwrite=TRUE)
    > #[1] FALSE

I get TRUE here, on Fedora Linux F24 and F26,
for R 3.3.3, 3.4.1 and R-devel

    > readLines(toFile)
    > #character(0)

(and I get the same here)

    > or, with recursive=TRUE,

    > dir.create( fromDir <- tempfile() )
    > cat(file = toFile <- tempfile(), "existing file\n")
    > readLines(toFile)
    > #[1] "existing file"
    > file.copy(fromDir, toFile, recursive=TRUE, overwrite=TRUE)
    > #[1] FALSE
again I get TRUE  instead,
otherwise the same bahavior.

    > #Warning message:
    > #In file.copy(fromDir, toFile, recursive = TRUE, overwrite = TRUE) :
    > #  'recursive' will be ignored as 'to' is not a single existing directory
    > readLines(toFile)
    > #character(0)

    > Is this behavior intended?

I don't think so (but I had not been involved in writing these).

Effectively, 
	      file.copy(from, to, overwrite=TRUE)
in the case where 'to' is not a directory and from, to are both of length 1,
is basically the following

   ok <- file.create(to)
   if(ok) ok <- file.append(to, from)
   return(ok)

Since you get FALSE when I get TRUE, it is not quite sure if in
your case file.append() is called at all... but I'd guess so.

I think the bug is that file.append(to, from) does not give an
error in our case where 'from' is a directory.

   > dir.exists(fromDir) && file.exists(toFile)
   [1] TRUE
   > file.append(toFile, fromDir) # should signal a warning and give FALSE 
   [1] TRUE
   > 

I'd be grateful if you'd file a bug report.
Martin

    > Bill Dunlap
    > TIBCO Software
    > wdunlap tibco.com



More information about the R-devel mailing list