Subject: bug#61394: 30.0.50; [PATCH] Image-dired thumb name based on content

From: Manuel Giraud <[email-masked]>
Date: Fri, 10 Feb 2023 19:46:02 +0100

Basil Contovounesios <[email-masked]> writes:

> Manuel Giraud via "Bug reports for GNU Emacs, the Swiss army knife of
> text editors" [2023-02-09 20:06 +0100] wrote:
>
>> +(defun image-dired-content-sha1 (filename)
>> +  "Compute the SHA-1 of a part of FILENAME."
>> +  (with-temp-buffer
>> +    (let ((file-size (file-attribute-size (file-attributes filename)))
>> +	  (chunk-size 4096))
>> +      (insert-file-contents filename nil 0 (min chunk-size file-size))
>
> Can't we unconditionally pass END=chunk-size to insert-file-contents,
> even for smaller files?

>From fileio.c:4076, it seems that you are right:

--8<---------------cut here---------------start------------->8---
      /* The likely offset where we will stop reading.  We could read
	 more (or less), if the file grows (or shrinks) as we read it.  */
      off_t likely_end = min (end_offset, st.st_size);
--8<---------------cut here---------------end--------------->8---

So here is an update version of this patch.  I've tested it on small 400
bytes icons and it works also.
>From f568717f7492c57fbf248ac4dd8d16cd22a24443 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Thu, 9 Feb 2023 19:56:37 +0100
Subject: [PATCH] Image-dired thumb name based on content

* lisp/image/image-dired-util.el (image-dired-content-sha1): New
utility that compute the SHA-1 of a part of the content of a file.
(image-dired-thumb-name): Use it.
---
 lisp/image/image-dired-util.el | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/lisp/image/image-dired-util.el b/lisp/image/image-dired-util.el
index c03f9d2e3d3..e54879ce033 100644
--- a/lisp/image/image-dired-util.el
+++ b/lisp/image/image-dired-util.el
@@ -57,13 +57,17 @@ image-dired-dir
       (message "Thumbnail directory created: %s" image-dired-dir))
     image-dired-dir))
 
+(defun image-dired-content-sha1 (filename)
+  "Compute the SHA-1 of a part of FILENAME."
+  (with-temp-buffer
+    (insert-file-contents filename nil 0 4096)
+    (sha1 (current-buffer))))
+
 (defun image-dired-thumb-name (file)
   "Return absolute file name for thumbnail FILE.
 Depending on the value of `image-dired-thumbnail-storage', the
 file name of the thumbnail will vary:
-- For `use-image-dired-dir', make a SHA1-hash of the image file's
-  directory name and add that to make the thumbnail file name
-  unique.
+- For `image-dired', make a SHA1-hash of some of the image file.
 - For `per-directory' storage, just add a subdirectory.
 - For `standard' storage, produce the file name according to the
   Thumbnail Managing Standard.  Among other things, an MD5-hash
@@ -85,7 +89,7 @@ image-dired-thumb-name
           ((or (eq 'image-dired image-dired-thumbnail-storage)
                ;; Maintained for backwards compatibility:
                (eq 'use-image-dired-dir image-dired-thumbnail-storage))
-           (expand-file-name (format "%s.jpg" (sha1 file))
+           (expand-file-name (format "%s.jpg" (image-dired-content-sha1 file))
                              (image-dired-dir)))
           ((eq 'per-directory image-dired-thumbnail-storage)
            (expand-file-name (format "%s.thumb.jpg"
-- 
2.39.1

Thanks,
-- 
Manuel Giraud