Bug 15302 - PATCH: file.info("C:/") returns valid information (instead of all NAs)
PATCH: file.info("C:/") returns valid information (instead of all NAs)
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Windows GUI / Window specific
R-devel (trunk)
ix86 (32-bit) Windows 32-bit
: P5 normal
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-05-03 04:22 UTC by Henrik Bengtsson
Modified: 2013-05-12 14:34 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Henrik Bengtsson 2013-05-03 04:22:41 UTC
PROBLEM:
file.info() fails to return information for Windows "root" directories, e.g. C:/, D:/ etc.

> file.info("C:/")
    size isdir mode mtime ctime atime  exe
C:/   NA    NA <NA>  <NA>  <NA>  <NA> <NA>
> file.info("C:\\")
     size isdir mode mtime ctime atime  exe
C:\\   NA    NA <NA>  <NA>  <NA>  <NA> <NA>


Unpleasant workaround:

> file.info("C:/.")
     size isdir mode      mtime      ctime      atime exe
C:/.    0  TRUE  777 1980-01-01 1980-01-01 1980-01-01  no


Also, note that R itself reports 'C:/' for this directory:

> setwd("C:")
> getwd()
[1] "c:/R/R-devel_svn/trunk/src/gnuwin32/"
> setwd("C:/")
> getwd()
[1] "C:/"

See also R-devel thread 'file.info(path) "inconsistent" with file.exists(path) when path == "C:/"' on 2011-09-19.



TROUBLESHOOTING:
In src/main/platform.c:do_fileinfo() trailing slashes are dropped turning 'C:/' into 'C:';

#ifdef Win32
	wchar_t *wfn = filenameToWchar(STRING_ELT(fn, i), TRUE);
	/* 'Sharpie' and fellow ignorami use trailing / on Windows,
	   where it is not valid */
	wchar_t *p = wfn + (wcslen(wfn) - 1);
	if (*p == L'/' || *p == L'\\') *p = 0;
#else 



PATCH:

c:\R\R-devel_svn\trunk\src\main>svn diff platform.c
Index: platform.c
===================================================================
--- platform.c  (revision 62711)
+++ platform.c  (working copy)
@@ -832,8 +832,11 @@
        wchar_t *wfn = filenameToWchar(STRING_ELT(fn, i), TRUE);
        /* 'Sharpie' and fellow ignorami use trailing / on Windows,
           where it is not valid */
+        /* Ghost of Ignoramus says "Above fellow, please join us!"
+           NB: Don't drop trailing / iff root directory, e.g. 'C:/' */
        wchar_t *p = wfn + (wcslen(wfn) - 1);
-       if (*p == L'/' || *p == L'\\') *p = 0;
+       if ((*p == L'/' || *p == L'\\') &&
+           (wcslen(wfn) != 3 || *(p-1) != L':')) *p = 0;
 #else
        const char *efn = R_ExpandFileName(translateChar(STRING_ELT(fn, i)));
 #endif



TEST OF PATCH:

# "ROOT" DIRECTORY:
> file.info("C:/")
    size isdir mode      mtime      ctime      atime exe
C:/    0  TRUE  777 1980-01-01 1980-01-01 1980-01-01  no

> file.info("C:/.")
     size isdir mode      mtime      ctime      atime exe
C:/.    0  TRUE  777 1980-01-01 1980-01-01 1980-01-01  no


# NO PATH, JUST THE DRIVE VOLUME:
> file.info("C:")
   size isdir mode mtime ctime atime  exe
C:   NA    NA <NA>  <NA>  <NA>  <NA> <NA>


# DIRECTORY WITH AND WITHOUT TRAILING SLASH:
> file.info("C:/Windows/")
            size isdir mode               mtime               ctime
C:/Windows/    0  TRUE  777 2013-05-02 18:11:11 2009-07-13 20:20:08
                          atime exe
C:/Windows/ 2013-05-02 18:11:11  no

> file.info("C:/Windows")
           size isdir mode               mtime               ctime
C:/Windows    0  TRUE  777 2013-05-02 18:11:11 2009-07-13 20:20:08
                         atime exe
C:/Windows 2013-05-02 18:11:11  no



# NON-EXISTING FILE/DIRECTORY:
> file.info("C:/JohnDoe")
           size isdir mode mtime ctime atime  exe
C:/JohnDoe   NA    NA <NA>  <NA>  <NA>  <NA> <NA>


# EXISTING FILE:
> file.info("C:/bootmgr")
             size isdir mode               mtime               ctime
C:/bootmgr 383562 FALSE  444 2009-07-13 18:38:58 2009-07-24 10:28:56
                         atime exe
C:/bootmgr 2009-07-24 10:28:56  no
Comment 1 Duncan Murdoch 2013-05-12 14:34:58 UTC
There are many other ways to specify a root directory, and they all failed:  "/", "\\\\?\\C:\\", etc.  I will put in place a patch that tries not to mess up the path in any of these cases.  It will go into R-devel today, R-patched after the 3.0.1 release.