Bug 15631 - WISH(?): file.symlink("path/target.txt", ...) does not translate forward slashes to backward ones on Windows
Summary: WISH(?): file.symlink("path/target.txt", ...) does not translate forward slas...
Alias: None
Product: R
Classification: Unclassified
Component: I/O (show other bugs)
Version: R-devel (trunk)
Hardware: x86_64/x64/amd64 (64-bit) Windows 64-bit
: P5 enhancement
Assignee: R-core
Depends on:
Reported: 2014-01-07 03:17 UTC by Henrik Bengtsson
Modified: 2015-12-14 13:48 UTC (History)
0 users

See Also:

Makes file.symlink(from, to) translate forward slashes in 'from' to backward slashes (783 bytes, patch)
2014-01-09 01:32 UTC, Henrik Bengtsson
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Henrik Bengtsson 2014-01-07 03:17:19 UTC
file.symlink("path/target.txt", "link.txt") does not translate the forward slash to a backslash causing the link to point to a non-existing target.

In help("file.symlink") there is a sentence that may cover this use case:

"User error in supplying invalid file names (and note that ‘foo/’ and ‘foo\’ are invalid on Windows) has undefined consequences."

but it would be helpful if file.symlink() also handles forward slashes like most other R I/O function.


> cat("Hello", file="target.txt")
> file.symlink("target.txt", "link1.txt")
[1] TRUE
> file.symlink(normalizePath("./target.txt"), "link2.txt")
[1] TRUE
> file.symlink("./target.txt", "link3.txt")
[1] TRUE

> bfr <- readChar("target.txt", n=9)
> bfr1 <- readChar("link1.txt", n=9)
> bfr2 <- readChar("link2.txt", n=9)
> bfr3 <- readChar("link3.txt", n=9)
Error in file(con, "rb") : cannot open the connection
In addition: Warning message:
In file(con, "rb") : cannot open file 'link3.txt': Invalid argument

> grep("link", shell('dir link*.txt', intern=TRUE), value=TRUE)
[1] "01/06/2014  07:07 PM    <SYMLINK>      link1.txt [target.txt]"
[2] "01/06/2014  07:07 PM    <SYMLINK>      link2.txt [C:\\test\target.txt]"
[3] "01/06/2014  07:07 PM    <SYMLINK>      link3.txt [./target.txt]"

Note the *forward* slash in the last target.

> sessionInfo()
R Under development (unstable) (2014-01-05 r64665)
Platform: x86_64-w64-mingw32/x64 (64-bit)

[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_3.1.0
Comment 1 Brian Ripley 2014-01-07 10:34:05 UTC
R does not translate such calls, with a single exception (mkdir fails on some versions of Windows).

So this is an inconsistency in the Windows system calls: please report to Microsoft.

Patches would most likely be considered, although few of us have systems on which Windows symlinks actually work.
Comment 2 Henrik Bengtsson 2014-01-09 01:32:50 UTC
Created attachment 1549 [details]
Makes file.symlink(from, to) translate forward slashes in 'from' to backward slashes
Comment 3 Henrik Bengtsson 2014-01-09 01:35:24 UTC
Agree, it's a Windows "feature".  The following from the Windows command prompt will give the same problem:

  mklink ./target.txt link3.txt

See previous comment for a minimal patch.  I've rebuild R from source and verified this patch on Windows 7 Ultimate with NTFS.
Comment 4 Brian Ripley 2014-01-09 11:25:39 UTC
The command-line tools do work differently as /A is a flag.  But MSDN documents that API calls accept slashes, so the rare exceptions are Windows bugs.

Patches should have LF line endings: CRLF fails (even on the Windows patch.exe I have).
Comment 5 Henrik Bengtsson 2014-01-09 16:34:49 UTC
Sorry, that example should have been

  mklink link3.txt "./target.txt"

and nothing else.

I never noticed that 'svn diff' (SlikSVN) on Windows outputs CRLF in the diff/patch header (=I've learned something today).  For the record, I found the following workaround using only Rtools (and SlikSVN):

  svn diff src\main\platform.c | grep "" > platform.c.patch