emacs-devel
[Top][All Lists]
Advanced

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

Re: save-buffer: avoid data loss on interrupt


From: Jim Meyering
Subject: Re: save-buffer: avoid data loss on interrupt
Date: Tue, 13 Dec 2011 21:52:49 +0100

Paul Eggert wrote:
> On 12/13/11 09:13, Jim Meyering wrote:
>
>> -      (if (or (and file-precious-flag dir-writable)
>> +      (if (or (and dir-writable
>> +               (or file-precious-flag
>> +                   (= (file-nlinks buffer-file-name) 1)))
>
> I like the general idea, but this solution seems a bit aggressive,
> as it will cause the file's ownership to change
> if the file's link count is 1, and often that isn't what is wanted.
> Instead, how about extending the semantics of break-hardlink-on-save,
> with something like the following (untested) patch?
>
> --- lisp/files.el     2011-12-04 08:02:42 +0000
> +++ lisp/files.el     2011-12-13 19:46:33 +0000
> @@ -4469,8 +4469,11 @@ Before and after saving the buffer, this
>             (dir-writable (file-writable-p dir)))
>        (if (or (and file-precious-flag dir-writable)
>                (and break-hardlink-on-save
> -                   (file-exists-p buffer-file-name)
> -                   (> (file-nlinks buffer-file-name) 1)
> +                (let ((nlinks (file-nlinks buffer-file-name)))
> +                  (and nlinks
> +                       (> nlinks (if (numberp break-hardlink-on-save)
> +                                     break-hardlink-on-save
> +                                   1))))
>                     (or dir-writable
>                         (error (concat (format
>                                         "Directory %s write-protected; " dir)
>
> That way, you can set break-hardlink-on-save to -1 to get
> the behavior that you want.
>
> While we're on the subject, break-hardlink-on-save is not
> documented; I wonder why not?  Also, the current code
> does not work if the directory is sticky and writable
> and the file is owned by someone else (a common situation
> in /tmp); this should get fixed too.

That'd be ok, but doesn't this deserve to be enabled more often
than when someone tweaks the break-hardlink-on-save variable?

How about this instead, assuming a file-owner-uid function?
(or if the two users of file-attributes is an issue,
we could combine file-nlinks and file-owner-uid into
a function that calls file-attributes just once)

Sure, this might still change the group, but if that's an issue
we could compare it to the default group.

diff --git a/lisp/files.el b/lisp/files.el
index 535715c..b0f01f2 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4469,7 +4469,8 @@ Before and after saving the buffer, this function runs
            (dir-writable (file-writable-p dir)))
       (if (or (and dir-writable
                   (or file-precious-flag
-                      (= (file-nlinks buffer-file-name) 1)))
+                      (and (= (file-nlinks buffer-file-name) 1)
+                           (= (file-owner-uid buffer-file-name) (user-uid)))))
               (and break-hardlink-on-save
                    (file-exists-p buffer-file-name)
                    (> (file-nlinks buffer-file-name) 1)



reply via email to

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