Subject: Re: bug#13149: 24.3.50; Emacs thinks file was changed outside Emacs, but it was not

From: Dmitry Gutov <[email-masked]>
Date: Fri, 18 Jan 2013 18:45:14 +0400

On 18.01.2013 11:56, Eli Zaretskii wrote:
>> Date: Thu, 17 Jan 2013 21:25:31 -0800
>> From: Paul Eggert <[email-masked]>
>> CC: [email-masked], Eli Zaretskii <[email-masked]>
>>
>> On 01/17/2013 09:10 PM, Dmitry Gutov wrote:
>>> cifs:
>>>
>>> 1358485764.638001400 fstat
>>> 1358485764.638001400 lstat
>>> 1358485764.638001400 stat
>>> *
>>> 1358485764.638001400 fstat
>>> 1358485764.638001400 lstat
>>> 1358485764.638001400 stat
>>
>> That looks busted, since there's
>> a 10 ms sleep followed by a write at the two
>> points I marked "*" above, which means that
>> fstat, lstat, and stat are all wrong after the "*".
>
> Who said anything was actually written to the file?  You'd need
> 'fsync' to be sure, don't you?

Sticking fsync(fd) after the call to 'write' doesn't change anything.

If, however, I move the 'nanosleep' after the 'write' and set the 
interval to 1 or 2 seconds, on vboxsf the numbers often become 
different, but without guarantee. With 1 second, they usually the same, 
with 2 second, they're mostly different, but still the same sometimes.

gutov@vbx:~/docs/Ruby$ ~/emacs-bzr/fs-test2
1358520180.757726300 fstat
1358520180.757726300 lstat
1358520180.757726300 stat

1358520181.689844700 fstat
1358520181.689844700 lstat
1358520181.689844700 stat

gutov@vbx:~/docs/Ruby$ ~/emacs-bzr/fs-test2
1358520183.414063600 fstat
1358520183.414063600 lstat
1358520183.414063600 stat

1358520183.414063600 fstat
1358520183.414063600 lstat
1358520183.414063600 stat

Adding another 'nanosleep' before the 'write' doesn't change that.

On cifs, the numbers are never different.

#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>

static int
report_times (int fd, char const *file)
{
   struct stat fst, lst, st;
   if (fstat (fd, &fst) != 0)
     return perror ("fstat"), -1;
   if (lstat (file, &lst) != 0)
     return perror ("lstat"), -1;
   if (stat (file, &st) != 0)
     return perror ("stat"), -1;
   printf ("%ld.%09ld fstat\n", (long) fst.st_mtim.tv_sec, 
fst.st_mtim.tv_nsec);
   printf ("%ld.%09ld lstat\n", (long) lst.st_mtim.tv_sec, 
lst.st_mtim.tv_nsec);
   printf ("%ld.%09ld stat\n", (long) st.st_mtim.tv_sec, 
st.st_mtim.tv_nsec);
   printf ("\n");
   return 0;
}

int
main (void)
{
   static char const file[] = "foo";
   struct timespec interval, bef_interval;
   int fd;
   unlink (file);
   fd = open (file, O_CREAT | O_WRONLY, -1);
   if (fd < 0)
     return perror ("open"), 1;
   if (report_times (fd, file) != 0)
     return 1;
   bef_interval.tv_sec = 0;
   bef_interval.tv_nsec = 100000000;
   interval.tv_sec = 2;
   interval.tv_nsec = 0;
   //  if (nanosleep (&bef_interval, 0) != 0)
   //  return perror ("nanosleep"), 1;
   if (write (fd, file, sizeof file - 1) != sizeof file - 1)
     return perror ("write"), 1;
   // fsync(fd);
   if (nanosleep (&interval, 0) != 0)
     return perror ("nanosleep"), 1;
   if (report_times (fd, file) != 0)
     return 1;
   return 0;
}