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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

R devel mailing list
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
readLines(toFile)
#character(0)

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

Bill Dunlap
TIBCO Software
wdunlap tibco.com

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: file.copy(from=Directory, to=File) oddity

Martin Maechler
>>>>> William Dunlap via R-devel <[hidden email]>
>>>>>     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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: file.copy(from=Directory, to=File) oddity

R devel mailing list
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 <[hidden email]
> wrote:

> >>>>> William Dunlap via R-devel <[hidden email]>
> >>>>>     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]]

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