writeout() writes the data in a buffer to a file. Currently, it also uses the
fupdstat() function to update the buffer statistics (fi_mode, fi_mtime..etc),
once the writing has been done. However, one of the two uses of writeout
introduces a bug where the fupdstat function is called using the wrong file
name! This is using the C-c C-w (write-file) function where the old file name
is used instead of the new one to gather stats. 

To see the bug in action....

$ mg somefile

C-c C-w

Write file: somefile1

Make a change to the new file (but don't save it), then...

C-x c (to exit mg)

Save file somefile1? (y or n) y <enter>

You will receive a message:

File has changed on disk since last save. Save anyway? (yes or no)

The message is received in error since the stats for somefile1 are holding
the stats for somefile. 

comments/ok?

-lum
PS this bug was spotted by Sunil Nimmagadda testing a different diff.

Index: file.c
===================================================================
RCS file: /cvs/src/usr.bin/mg/file.c,v
retrieving revision 1.77
diff -u -p -r1.77 file.c
--- file.c      11 Apr 2012 14:16:57 -0000      1.77
+++ file.c      7 May 2012 16:30:55 -0000
@@ -530,6 +530,7 @@ filewrite(int f, int n)
                free(curbp->b_bname);
                if ((curbp->b_bname = strdup(bn)) == NULL)
                        return (FALSE);
+               (void)fupdstat(curbp);
                curbp->b_flag &= ~(BFBAK | BFCHG);
                upmodes(curbp);
        }
@@ -595,6 +596,7 @@ buffsave(struct buffer *bp)
                        return (s);
        }
        if ((s = writeout(bp, bp->b_fname)) == TRUE) {
+               (void)fupdstat(bp);
                bp->b_flag &= ~(BFCHG | BFBAK);
                upmodes(bp);
        }
@@ -632,6 +634,7 @@ makebkfile(int f, int n)
  * This function performs the details of file writing; writing the file
  * in buffer bp to file fn. Uses the file management routines in the
  * "fileio.c" package. Most of the grief is checking of some sort.
+ * You may want to call fupdstat() after using this function.
  */
 int
 writeout(struct buffer *bp, char *fn)
@@ -652,7 +655,6 @@ writeout(struct buffer *bp, char *fn)
                (void)ffclose(bp);
                ewprintf("Unable to write %s", fn);
        }
-       (void)fupdstat(bp);
        return (s == FIOSUC);
 }

Reply via email to