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]]
>>>>> 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
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]]
Thanks for the report, fixed in R-devel. Now we get a warning when copying a directory over a non-directory file is attempted. The target (non-directory) file is left alone. Tomas On 09/08/2017 06:54 PM, William Dunlap via R-devel wrote:> 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]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel