From maechler at stat.math.ethz.ch  Mon Sep 11 16:01:12 2017
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Mon, 11 Sep 2017 16:01:12 +0200
Subject: [Rd] file.copy(from=Directory, to=File) oddity
In-Reply-To: <CAF8bMcaP2dHS3KAZVyXifuigZ3B2LmoR6N+=vdzBaCF5svGrhg@mail.gmail.com>
References: <CAF8bMcaP2dHS3KAZVyXifuigZ3B2LmoR6N+=vdzBaCF5svGrhg@mail.gmail.com>
Message-ID: <22966.38696.682122.399296@stat.math.ethz.ch>

>>>>> 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



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