bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#18141: 24.4.50; saving .gz file breaks file coding


From: Eli Zaretskii
Subject: bug#18141: 24.4.50; saving .gz file breaks file coding
Date: Wed, 06 Aug 2014 17:36:00 +0300

> From: Glenn Morris <address@hidden>
> Date: Tue, 05 Aug 2014 04:34:35 -0400
> Cc: Katsumi Yamaoka <address@hidden>
> 
> Perhaps someone could bisect this to find the cause?

I looked into this bug.  It is a side effect of the following commit:

  revno: 111638
  fixes bug: http://debbugs.gnu.org/13522
  committer: Glenn Morris <address@hidden>
  branch nick: trunk
  timestamp: Wed 2013-01-30 22:35:45 -0800
  message:
    Reduce delay between backing up a file and saving new version

    * lisp/files.el (basic-save-buffer-2):
    Choose coding system for writing the file before backing it up.

    * src/fileio.c (choose_write_coding_system): Make it callable from Lisp.
    (Fwrite_region): If coding-system-for-write is set, don't call
    choose_write_coding_system.
    Move the last piece of choose_write_coding_system here.
    (syms_of_fileio): Add choose-write-coding-system.

That commit determines the encoding using the .gz file name, which of
course yields no-conversion.  Thus jka-compr is forced to use
no-conversion when it writes a temporary file to be submitted to gzip.

I can fix that with the simple changes at the end of this message.
However, I'd like first to argue that the changes in the above commit
are ill-advised and should be reverted.  Here's why:

 . Bug#13522, which these changes were trying to solve, happens when
   Emacs is killed by an external signal during the time between
   backing up the original file and writing the new one.  (This time
   can be quite large if write-region asks the user to choose a
   suitable encoding for the new content.)  That is a pretty rare
   situation, and IMO it's perfectly OK for Emacs to leave just the
   backup file if it was brutally killed during that time window.

 . The problem reported in bug#13522 exists because backing up the old
   contents and writing the new one is not a transaction.  The changes
   in r111638 didn't change that basic fact, they just made the window
   between the backup and the saving smaller, when Emacs needs to ask
   the user about the encoding.  But the window, albeit a smaller one,
   is still there, and so it is still possible to cause the same
   problem by killing Emacs at a suitably chosen opportune moment.

 . The changes effectively moved the selection of the encoding to the
   very beginning of write-region.  By contrast, the place where
   write-region calls choose-write-coding-system was carefully chosen
   through a long trial-and-error process.  As shown by this bug
   report, doing so too early is clearly wrong for "magic" and remote
   files, but it is also wrong for files whose saving needs to use
   annotations, as this comment in write-region says:

    /* Decide the coding-system to encode the data with.
       We used to make this choice before calling build_annotations, but that
       leads to problems when a write-annotate-function takes care of
       unsavable chars (as was the case with X-Symbol).  */

   So I guess X-Symbol is likely also broken now, as are potentially
   any packages that use write-annotate-functions.  It took us 1.5
   years to find this bug; who knows how much time will pass until we
   find and fix those with write-annotate-functions, a feature that is
   nowadays used very little?

   If we do want to make sure the users of write-annotate-functions
   are not broken, we should probably also refrain from calling
   choose-write-coding-system from basic-save-buffer-2 when
   write-annotate-functions are non-nil.  But that means part of
   write-region will now be dragged into basic-save-buffer-2.  Where
   does that end?

So I think simply reverting the r111638 changes and leaving bug#13522
as wontfix is an easier way out.

An even better fix would be to make the backing up and saving a
transaction-like process.  That would solve the problem entirely.  But
I'm not sure this is feasible/practical, with parts of the puzzle
implemented in C and parts in Lisp.  If it is possible, it will
probably be anything but simple, and so likely inappropriate for the
emacs-24 branch.

Here's the patch I promised that solves this particular bug without
reverting r111638:

--- lisp/files.el~0     2014-06-29 06:54:20 +0300
+++ lisp/files.el       2014-08-06 17:26:42 +0300
@@ -4835,13 +4835,17 @@
                                                   (nth 1 setmodes)))
                 (set-file-modes buffer-file-name
                                 (logior (car setmodes) 128))))))
-       (let (success)
+       (let ((filename-is-magic (find-file-name-handler buffer-file-name
+                                                        'write-region))
+             success)
          (unwind-protect
                 ;; Pass in nil&nil rather than point-min&max to indicate
                 ;; we're saving the buffer rather than just a region.
                 ;; write-region-annotate-functions may make us of it.
-             (let ((coding-system-for-write writecoding)
-                   (coding-system-require-warning nil))
+             (let ((coding-system-for-write
+                    (if filename-is-magic coding-system-for-write writecoding))
+                   (coding-system-require-warning
+                    (if filename-is-magic coding-system-require-warning)))
                (write-region nil nil
                              buffer-file-name nil t buffer-file-truename)
                (setq success t))





reply via email to

[Prev in Thread] Current Thread [Next in Thread]