>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. 

Here is a slightly modifed diff, it returns FALSE from writeout() when
writeout is unable to write a file, arguably this is a second bug fixed
with this diff.
 
-lum

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      8 May 2012 11:40: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);
        }
@@ -651,8 +653,8 @@ writeout(struct buffer *bp, char *fn)
                /* print a message indicating write error */
                (void)ffclose(bp);
                ewprintf("Unable to write %s", fn);
+               return (FALSE);
        }
-       (void)fupdstat(bp);
        return (s == FIOSUC);
 }

Reply via email to