Subject: bug#10733: 24.0.93; w32 file truncation

From: Óscar Fuentes <[email-masked]>
Date: Tue, 07 Feb 2012 06:22:12 +0100

Eli Zaretskii <[email-masked]> writes:

>> Maybe it can be integrated in the
>> 
>> if (!(NILP(Vw32_get_true_file_attributes) ...
>> 
>> hence reusing the calls to CreateFile and GetFileInformationByHandle and
>> shortening the patch, but as I don't know what
>> Vw32_get_true_file_attributes does, preferread to follow the safe way.
>
> You did right: w32-get-true-file-attributes can be set by the user to
> nil, if she wants her file ops faster.

I was thinking on something like

diff --git a/src/w32.c b/src/w32.c
index 3d3d334..418be63 100644
--- a/src/w32.c
+++ b/src/w32.c
@@ -3447,8 +3447,12 @@ stat (const char * path, struct stat * buf)
 	}
     }
 
-  if (!(NILP (Vw32_get_true_file_attributes)
-	|| (EQ (Vw32_get_true_file_attributes, Qlocal) && is_slow_fs (name)))
+  buf->st_size = 0;
+
+  if ((!(NILP (Vw32_get_true_file_attributes)
+         || (EQ (Vw32_get_true_file_attributes, Qlocal) && is_slow_fs (name)))
+       || ((wfd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
+           (wfd.dwReserved0 == IO_REPARSE_TAG_SYMLINK)))
       /* No access rights required to get info.  */
       && (fh = CreateFile (name, 0, 0, NULL, OPEN_EXISTING,
 			   FILE_FLAG_BACKUP_SEMANTICS, NULL))
@@ -3461,6 +3465,9 @@ stat (const char * path, struct stat * buf)
 
       if (GetFileInformationByHandle (fh, &info))
 	{
+          buf->st_size = info.nFileSizeHigh;
+          buf->st_size <<= 32;
+          buf->st_size += info.nFileSizeLow;
 	  buf->st_nlink = info.nNumberOfLinks;
 	  /* Might as well use file index to fake inode values, but this
 	     is not guaranteed to be unique unless we keep a handle open
@@ -3535,9 +3542,12 @@ stat (const char * path, struct stat * buf)
   buf->st_dev = volume_info.serialnum;
   buf->st_rdev = volume_info.serialnum;
 
-  buf->st_size = wfd.nFileSizeHigh;
-  buf->st_size <<= 32;
-  buf->st_size += wfd.nFileSizeLow;
+  if (!buf->st_size)
+    {
+      buf->st_size = wfd.nFileSizeHigh;
+      buf->st_size <<= 32;
+      buf->st_size += wfd.nFileSizeLow;
+    }
 
   /* Convert timestamps to Unix format. */
   buf->st_mtime = convert_time (wfd.ftLastWriteTime);

That is not as robust, though, because if GetFileInformationByHandle
fails the function does not return with an error code.

>> And the fix is just for the size. Don't know if there are other
>> attributes suffer from the same problem. Possibly the right thing is to
>> do what MSVCRT does:
>> 
>> if is-symlink?
>>   use fstat
>> fi
>
> Since fstat is also reimplemented, I'd rather do what it does inline.
>
> For that, we need to know which other attributes are reported
> different.  Or maybe just test for the reparse point up front and do
> all the work for the target instead.

Since Emacs' fstat reimplementation is based on
GetFileInformationByHandle, and that the handle points to the linked
file (CreateFile follows the link unless told otherwise), we should be
safe delegating all work to `fstat' when a symlink is detected on `stat'
(the executable bit must be setted on `stat', but that's no problem.)