Skip to content
Prev 42396 / 63424 Next

misfeature: forced file.copy() of a file over itself truncates the file ...

Ben Bolker <bbolker <at> gmail.com> writes:
Bump.  Will I be scolded if I submit this as a bug report/wishlist
item?

  Test case:
[snip]

  My proposed fix (thanks to W. Dunlap) is to use normalizePath();
as he points out, this won't catch situations where the same file
can be referred to via an NFS mount, but it should help at least.
Writing a platform-independent version a la S-PLUS's match.path()
seemed to much work at the moment.

===================================================================
--- files.R	(revision 58240)
+++ files.R	(working copy)
@@ -116,7 +116,7 @@
     if(nt > nf) from <- rep(from, length.out = nt)
     okay <- file.exists(from)
     if (!overwrite) okay[file.exists(to)] <- FALSE
-    if (any(from[okay] %in% to[okay]))
+    if (any(normalizePath(from[okay]) %in% normalizePath(to[okay])))
         stop("file can not be copied both 'from' and 'to'")
     if (any(okay)) { # care: file.create could fail but file.append work.
     	okay[okay] <- file.create(to[okay])


  thanks
    Ben Bolker