Subject: Re: bug#61326: [DRAFT PATCH] Work around zip's filename extension limitation (was: Adding --no-add-suffix to zip patch)

From: Ruijie Yu <[email-masked]>
Date: Thu, 09 Feb 2023 00:48:15 +0800

Ruijie Yu via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <[email-masked]> writes:

> Eli Zaretskii <[email-masked]> writes:
>
>>> Cc: [email-masked]
>>> Date: Tue, 7 Feb 2023 09:31:11 +0800
>>> From:  Ruijie Yu via "Bug reports for GNU Emacs,
>>>  the Swiss army knife of text editors" <[email-masked]>
>>>
>>> Maybe, at least in the meantime, we change it such that all write operations
>>> for zip create files in temp, and move to / overwrite the original file when
>>> done? Although I don’t have a full understanding on how that would be done and
>>> whether there are problems along with it.
>>
>> How about submitting a patch to do that, but only when the original
>> file doesn't have an extension?
>
> Will do.  Will report back in 2-3 days unless someone else gets to it
> first.
>
> Best,
>
>
> RY

Here is a preliminary patch that contains some "REVIEW" comments where I
need inputs.  I have tested with the following recipe and things seem to
work correctly:

$ touch 1 2 && zip z 1 2 && mv z.zip z
$ ls -l
.... 1
.... 2
.... z
$ src/emacs -Q
RET          ; open file "1" from within "z" archive
s C-x C-s    ; insert "s" and save file "1"
C-x C-c      ; exit
$ ls -l      # notice that no "z.zip" exists, and "z" is correctly updated
.... 1
.... 2
.... z

Patch based on 907fd1f7ff402f9d226ebb3b891ea5b54fac1d1c which is ~3 days
old.  I will amend the commit (and rebase if necessary) according to ML
reviews and further progress.

>From bc086395929520a66eb928fd5d3baf6c9fa79bb5 Mon Sep 17 00:00:00 2001
From: Ruijie Yu <[email-masked]>
Date: Thu, 9 Feb 2023 00:45:19 +0800
Subject: [PATCH] lisp/arc-mode.el Work around zip's filename limitations on
 extension

[DRAFT PATCH]

Fixes 61326.  The "zip" executable requires that the named archive must have
an extension, else it attaches ".zip" to the supplied file name, causing
incorrect behaviors.  This patch looks for such scenarios and temporarily
rename extension-less archives so that "zip" would function correctly.

TODO:

1. Address all REVIEW points.
2. Make sure other write operations, in addition to zip-write-member, are
fixed.
3. Tests? (I might need some pointers as to where existing tests are and how
to write them.)
---
 lisp/arc-mode.el | 82 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 54 insertions(+), 28 deletions(-)

diff --git a/lisp/arc-mode.el b/lisp/arc-mode.el
index 6f3e922880d..ac8c7cefa89 100644
--- a/lisp/arc-mode.el
+++ b/lisp/arc-mode.el
@@ -1350,42 +1350,61 @@ archive-write-file-member
       (setq last-coding-system-used archive-member-coding-system))
   t)

-(defun archive-*-write-file-member (archive descr command)
+;; REVIEW: is there a better name than AVOID-EXTLESS-P?
+(defun archive-*-write-file-member (archive descr command
+                                            &optional avoid-extless-p)
   (let* ((archive (expand-file-name archive))
          (ename (archive--file-desc-ext-file-name descr))
          (tmpfile (expand-file-name ename archive-tmpdir))
          (top (directory-file-name (file-name-as-directory archive-tmpdir)))
     (default-directory (file-name-as-directory top)))
+    ;; REVIEW: the diff here is because the previous code had TAB's
+    ;; (while assuming each TAB is 4 spaces), and my Emacs replaced
+    ;; them with spaces.  What is the status quo on this kind of diff?
+    ;; I can remove them if we consider this change excessive and/or
+    ;; intrusive.
     (unwind-protect
         (progn
           (make-directory (file-name-directory tmpfile) t)
-     ;; If the member is itself an archive, write it without
-     ;; the dired-like listing we created.
-     (if (eq major-mode 'archive-mode)
-         (archive-write-file tmpfile)
-       (write-region nil nil tmpfile nil 'nomessage))
-     ;; basic-save-buffer needs last-coding-system-used to have
-     ;; the value used to write the file, so save it before any
-     ;; further processing clobbers it (we restore it in
-     ;; archive-write-file-member, above).
-     (setq archive-member-coding-system last-coding-system-used)
-     (if (archive--file-desc-mode descr)
-         ;; Set the file modes, but make sure we can read it.
-         (set-file-modes tmpfile
-                         (logior ?\400 (archive--file-desc-mode descr))))
-     (setq ename
-       (encode-coding-string ename archive-file-name-coding-system))
+          ;; If the member is itself an archive, write it without
+          ;; the dired-like listing we created.
+          (if (eq major-mode 'archive-mode)
+              (archive-write-file tmpfile)
+            (write-region nil nil tmpfile nil 'nomessage))
+          ;; basic-save-buffer needs last-coding-system-used to have
+          ;; the value used to write the file, so save it before any
+          ;; further processing clobbers it (we restore it in
+          ;; archive-write-file-member, above).
+          (setq archive-member-coding-system last-coding-system-used)
+          (if (archive--file-desc-mode descr)
+              ;; Set the file modes, but make sure we can read it.
+              (set-file-modes tmpfile
+                              (logior ?\400 (archive--file-desc-mode descr))))
+          (setq ename
+                (encode-coding-string ename archive-file-name-coding-system))
           (let* ((coding-system-for-write 'no-conversion)
-        (default-directory (file-name-as-directory archive-tmpdir))
-        (exitcode (apply #'call-process
-                 (car command)
-                 nil
-                 nil
-                 nil
-                 (append (cdr command)
-                     (list archive ename)))))
-            (or (zerop exitcode)
-       (error "Updating was unsuccessful (%S)" exitcode))))
+                 (default-directory (file-name-as-directory archive-tmpdir))
+                 (safe-archive
+                  (if avoid-extless-p
+                      (make-temp-name
+                       (expand-file-name (concat archive "_tmp.")))
+                    archive))
+                 (maybe-rename
+                  (lambda (newname)
+                    (when avoid-extless-p
+                      (with-current-buffer archive-superior-buffer
+                        (rename-visited-file newname))))))
+            ;; REVIEW: is `unwind-protect' necessary here?
+            (prog2 (funcall maybe-rename safe-archive)
+                (let ((exitcode
+                       (apply #'call-process (car command)
+                              nil nil nil
+                              (append (cdr command)
+                                      (list safe-archive ename)))))
+                  (or (zerop exitcode)
+                      (error "Updating was unsuccessful (%S)"
+                             exitcode)))
+              (funcall maybe-rename archive))))
       (archive-delete-local tmpfile))))

 (defun archive-write-file (&optional file)
@@ -2048,12 +2067,19 @@ archive--file-desc-case-fiddled
   (not (eq (archive--file-desc-int-file-name fd)
            (archive--file-desc-ext-file-name fd))))

+(defun archive--file-name-zip-extless-p (fname)
+  ;; zip's rule: if the filename contains "." anywhere in the name
+  ;; (including obscure names like ".foo" and "bar."), then this
+  ;; filename is considered to have an extension.
+  (not (seq-contains-p (file-name-nondirectory fname) ?. #'eq)))
+
 (defun archive-zip-write-file-member (archive descr)
   (archive-*-write-file-member
    archive
    descr
    (if (archive--file-desc-case-fiddled descr)
-       archive-zip-update-case archive-zip-update)))
+       archive-zip-update-case archive-zip-update)
+   (archive--file-name-zip-extless-p archive)))

 (defun archive-zip-chmod-entry (newmode files)
   (save-restriction
--
2.39.1

Best,

RY