From wdunlap at tibco.com  Mon Sep 11 20:50:48 2017
From: wdunlap at tibco.com (William Dunlap)
Date: Mon, 11 Sep 2017 11:50:48 -0700
Subject: [Rd] file.copy(from=Directory, to=File) oddity
In-Reply-To: <22966.38696.682122.399296@stat.math.ethz.ch>
References: <CAF8bMcaP2dHS3KAZVyXifuigZ3B2LmoR6N+=vdzBaCF5svGrhg@mail.gmail.com>
 <22966.38696.682122.399296@stat.math.ethz.ch>
Message-ID: <CAF8bMcbMYxfHyQrGTm4tR+BMVV557RJxsJ2USBEYByaMvKn6yw@mail.gmail.com>

Bug 17337.  Note that I get R making the zero-length file on both Windows
and Linux, but the return values are different.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Sep 11, 2017 at 7:01 AM, Martin Maechler <maechler at stat.math.ethz.ch
> wrote:

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

	[[alternative HTML version deleted]]



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