This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch master
in repository efl.
View the commit online.
commit d2512632b6698f15ad45a83e58e680f7b4339f14
Author: Cedric BAIL <[email protected]>
AuthorDate: Wed Sep 23 08:14:59 2026 -0600
ethumb: write Thumb::URI and Thumb::MTime into fdo thumbnails
The Thumbnail Managing Standard has every thumbnail carry the URI of
its source and that source's modification time, as PNG text chunks.
That is how everything else decides whether a thumbnail on disk still
describes the file it is named after. Ours carried neither, so a file
manager reading one of ours could not tell it was still good and made
its own, which defeats sharing the directory.
Thumb::URI, Thumb::MTime and Thumb::Size are spliced after IHDR once
Evas has written the file, because Evas's PNG saver has no way to be
told about text chunks. The file is still the temporary one at that
point and is renamed into place afterwards, so nothing ever reads it
half-written, and a failure leaves the thumbnail as Evas wrote it.
https://specifications.freedesktop.org/thumbnail-spec/latest/
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
src/lib/ethumb/ethumb.c | 184 ++++++++++++++++++++++++++++++----
src/tests/ethumb/ethumb_suite.c | 1 +
src/tests/ethumb/ethumb_suite.h | 4 +
src/tests/ethumb/ethumb_test_ethumb.c | 168 +++++++++++++++++++++++++++++++
src/tests/ethumb/ethumb_test_slave.c | 7 +-
src/tests/ethumb/meson.build | 1 +
6 files changed, 341 insertions(+), 24 deletions(-)
diff --git a/src/lib/ethumb/ethumb.c b/src/lib/ethumb/ethumb.c
index 89ebe31b27..e3488867ca 100644
--- a/src/lib/ethumb/ethumb.c
+++ b/src/lib/ethumb/ethumb.c
@@ -904,6 +904,41 @@ static const char ACCEPTABLE_URI_CHARS[96] = {
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
};
+/* The name of a file as the thumbnail standard spells it: a file:// URI with
+ * everything outside a small set of characters percent-encoded.
+ *
+ * The buffer must hold three bytes per byte of $file plus room for "file://"
+ * and the terminator.
+ */
+static void
+_ethumb_uri_build(char *uri, const char *file)
+{
+ static const char hex[] = "0123456789abcdef";
+ const unsigned char *c;
+ char *t;
+
+#define _check_uri_char(c) \
+ ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
+
+ memcpy(uri, "file://", sizeof("file://"));
+ t = uri + sizeof("file://") - 1;
+
+ for (c = (const unsigned char *)file; *c != '\0'; c++)
+ {
+ if (!_check_uri_char(*c))
+ {
+ *t++ = '%';
+ *t++ = hex[*c >> 4];
+ *t++ = hex[*c & 15];
+ }
+ else
+ *t++ = *c;
+ }
+ *t = '\0';
+
+#undef _check_uri_char
+}
+
static const char *
_ethumb_generate_hash(const char *file)
{
@@ -914,8 +949,6 @@ _ethumb_generate_hash(const char *file)
static const char hex[] = "0123456789abcdef";
char *uri;
- char *t;
- const unsigned char *c;
#ifdef HAVE_XATTR
ssize_t length;
@@ -938,29 +971,10 @@ _ethumb_generate_hash(const char *file)
}
#endif
-#define _check_uri_char(c) \
- ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
-
EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
uri = alloca(3 * strlen(file) + 9);
- memcpy(uri, "file://", sizeof("file://"));
- t = uri + sizeof("file://") - 1;
-
- for (c = (const unsigned char *)file; *c != '\0'; c++)
- {
- if (!_check_uri_char(*c))
- {
- *t++ = '%';
- *t++ = hex[*c >> 4];
- *t++ = hex[*c & 15];
- }
- else
- *t++ = *c;
- }
- *t = '\0';
-
-#undef _check_uri_char
+ _ethumb_uri_build(uri, file);
MD5Init (&ctx);
MD5Update (&ctx, (unsigned char const*)uri, (unsigned)strlen (uri));
@@ -1317,6 +1331,128 @@ ethumb_plugin_image_resize(Ethumb *e, int w, int h)
return EINA_TRUE;
}
+/* One PNG chunk: a length, a four-letter type, the bytes, and a CRC over the
+ * type and the bytes together. */
+static unsigned char *
+_ethumb_png_chunk_put(unsigned char *p, const char *type,
+ const char *data, unsigned int len)
+{
+ unsigned int crc;
+
+ p[0] = (len >> 24) & 0xff;
+ p[1] = (len >> 16) & 0xff;
+ p[2] = (len >> 8) & 0xff;
+ p[3] = len & 0xff;
+ memcpy(p + 4, type, 4);
+ if (len) memcpy(p + 8, data, len);
+
+ /* eina_crc is the one PNG asks for -- the 0xEDB88320 polynomial, seeded
+ with all ones and returned inverted -- so what comes out goes in. */
+ crc = eina_crc((const char *) (p + 4), 4 + len, 0xffffffff, EINA_TRUE);
+ p[8 + len + 0] = (crc >> 24) & 0xff;
+ p[8 + len + 1] = (crc >> 16) & 0xff;
+ p[8 + len + 2] = (crc >> 8) & 0xff;
+ p[8 + len + 3] = crc & 0xff;
+
+ return p + 12 + len;
+}
+
+/* Add the Thumb::URI, Thumb::MTime and Thumb::Size text chunks of the
+ * thumbnail standard to the PNG at "path", which is a thumbnail of "src".
+ *
+ * They are spliced in after IHDR because Evas's PNG saver cannot be told about
+ * text chunks. The result is written beside the PNG and renamed over it, so a
+ * failure anywhere leaves the thumbnail as Evas wrote it.
+ */
+static void
+_ethumb_png_meta_add(const char *path, const char *src)
+{
+ struct stat st;
+ Eina_File *f;
+ unsigned char *out = NULL, *p;
+ const unsigned char *map;
+ char *uri, mtime[64], size[64], tmp[PATH_MAX];
+ unsigned int ihdr_len, head;
+ size_t len, rest, room;
+ FILE *w;
+
+ if (stat(src, &st) != 0) return;
+
+ f = eina_file_open(path, EINA_FALSE);
+ if (!f) return;
+
+ len = eina_file_size_get(f);
+ map = eina_file_map_all(f, EINA_FILE_SEQUENTIAL);
+ if (!map) goto done;
+ if (len < 8 + 12) goto done;
+
+ /* A signature and an IHDR, or it is not a PNG and not ours to edit. */
+ if (memcmp(map, "\211PNG\r\n\032\n", 8)) goto done;
+ if (memcmp(map + 12, "IHDR", 4)) goto done;
+
+ ihdr_len = (map[8] << 24) | (map[9] << 16) | (map[10] << 8) | map[11];
+ head = 8 + 12 + ihdr_len;
+ if (head > len) goto done;
+ rest = len - head;
+
+ uri = alloca(3 * strlen(src) + 9);
+ _ethumb_uri_build(uri, src);
+ snprintf(mtime, sizeof(mtime), "%llu", (unsigned long long) st.st_mtime);
+ snprintf(size, sizeof(size), "%llu", (unsigned long long) st.st_size);
+
+ /* Three chunks: twelve bytes of frame each, plus a keyword, a separator
+ and a value. */
+ room = len + 3 * (12 + 32) + strlen(uri) + strlen(mtime) + strlen(size);
+ out = malloc(room);
+ if (!out) goto done;
+
+ memcpy(out, map, head);
+ p = out + head;
+
+#define _TEXT(k, v) \
+ do { \
+ size_t _kl = strlen(k), _vl = strlen(v); \
+ char *_b = alloca(_kl + 1 + _vl); \
+ \
+ memcpy(_b, k, _kl); \
+ _b[_kl] = '\0'; \
+ memcpy(_b + _kl + 1, v, _vl); \
+ p = _ethumb_png_chunk_put(p, "tEXt", _b, _kl + 1 + _vl); \
+ } while (0)
+
+ _TEXT("Thumb::URI", uri);
+ _TEXT("Thumb::MTime", mtime);
+ _TEXT("Thumb::Size", size);
+
+#undef _TEXT
+
+ memcpy(p, map + head, rest);
+ p += rest;
+
+ eina_file_map_free(f, (void *) map);
+ eina_file_close(f);
+
+ snprintf(tmp, sizeof(tmp), "%s.meta", path);
+ w = fopen(tmp, "wb");
+ if (w)
+ {
+ Eina_Bool ok = (fwrite(out, 1, p - out, w) == (size_t) (p - out));
+
+ if ((fclose(w) != 0) || !ok || (rename(tmp, path) != 0))
+ {
+ ERR("could not write the metadata of the thumbnail of '%s'", src);
+ unlink(tmp);
+ }
+ }
+ free(out);
+ return;
+
+ done:
+ if (map) eina_file_map_free(f, (void *) map);
+ eina_file_close(f);
+ free(out);
+}
+
EAPI Eina_Bool
ethumb_image_save(Ethumb *e)
{
@@ -1362,6 +1498,10 @@ ethumb_image_save(Ethumb *e)
e->thumb_key);
return EINA_FALSE;
}
+
+ /* Only the FDO format is a PNG read by other programs. */
+ if (e->format == ETHUMB_THUMB_FDO) _ethumb_png_meta_add(buf, e->src_path);
+
if (rename(buf, e->thumb_path) < 0)
{
ERR("could not rename image: path=%s, key=%s to path=%s",
diff --git a/src/tests/ethumb/ethumb_suite.c b/src/tests/ethumb/ethumb_suite.c
index 88588621f4..6be81f0ab5 100644
--- a/src/tests/ethumb/ethumb_suite.c
+++ b/src/tests/ethumb/ethumb_suite.c
@@ -6,6 +6,7 @@
static const Efl_Test_Case etc[] = {
{ "ethumb_slave", ethumb_test_slave },
+ { "ethumb_ethumb", ethumb_test_ethumb },
{ NULL, NULL }
};
diff --git a/src/tests/ethumb/ethumb_suite.h b/src/tests/ethumb/ethumb_suite.h
index 0495e91885..30b51e5620 100644
--- a/src/tests/ethumb/ethumb_suite.h
+++ b/src/tests/ethumb/ethumb_suite.h
@@ -4,6 +4,10 @@
#include <check.h>
#include "../efl_check.h"
+extern const unsigned char ethumb_test_png[];
+extern const size_t ethumb_test_png_size;
+
void ethumb_test_slave(TCase *tc);
+void ethumb_test_ethumb(TCase *tc);
#endif
diff --git a/src/tests/ethumb/ethumb_test_ethumb.c b/src/tests/ethumb/ethumb_test_ethumb.c
new file mode 100644
index 0000000000..e0f7f9ce2c
--- /dev/null
+++ b/src/tests/ethumb/ethumb_test_ethumb.c
@@ -0,0 +1,168 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <Eina.h>
+#include <Ecore.h>
+#include <Ethumb.h>
+
+#include "ethumb_suite.h"
+
+static char tmpdir[PATH_MAX];
+static char srcfile[PATH_MAX];
+static char thumbfile[PATH_MAX];
+
+static void
+_tree_make(void)
+{
+ FILE *f;
+
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/ethumb_meta_XXXXXX");
+ ck_assert_ptr_ne(mkdtemp(tmpdir), NULL);
+
+ ck_assert_int_lt(snprintf(srcfile, sizeof(srcfile), "%s/pic.png", tmpdir),
+ sizeof(srcfile));
+ ck_assert_int_lt(snprintf(thumbfile, sizeof(thumbfile), "%s/thumb.png", tmpdir),
+ sizeof(thumbfile));
+ f = fopen(srcfile, "wb");
+ ck_assert_ptr_ne(f, NULL);
+ ck_assert_int_eq(fwrite(ethumb_test_png, 1, ethumb_test_png_size, f),
+ ethumb_test_png_size);
+ fclose(f);
+}
+
+static void
+_tree_remove(void)
+{
+ char cmd[PATH_MAX + 16];
+
+ if (!tmpdir[0]) return;
+ snprintf(cmd, sizeof(cmd), "rm -rf %s", tmpdir);
+ if (system(cmd) != 0) fprintf(stderr, "could not remove %s\n", tmpdir);
+ tmpdir[0] = '\0';
+}
+
+static void
+_generated_cb(void *data, Ethumb *e EINA_UNUSED, Eina_Bool success)
+{
+ Eina_Bool *done = data;
+
+ ck_assert(success);
+ *done = EINA_TRUE;
+ ecore_main_loop_quit();
+}
+
+static unsigned int
+_be32(const unsigned char *p)
+{
+ return ((unsigned int) p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
+}
+
+/* Value of the tEXt chunk of PNG "png" whose keyword is "keyword", or NULL.
+ * The CRC of the text chunks crossed on the way is checked. */
+static char *
+_png_text_get(const unsigned char *png, size_t len, const char *keyword)
+{
+ size_t pos = 8;
+ char *found = NULL;
+
+ ck_assert_int_ge(len, 8);
+ ck_assert(!memcmp(png, "\211PNG\r\n\032\n", 8));
+
+ while (pos + 12 <= len)
+ {
+ unsigned int clen = _be32(png + pos);
+ const unsigned char *type = png + pos + 4;
+
+ ck_assert_int_le(pos + 12 + clen, len);
+
+ if (!memcmp(type, "tEXt", 4))
+ {
+ const char *text = (const char *) png + pos + 8;
+ size_t klen = strnlen(text, clen);
+
+ ck_assert_uint_eq(_be32(png + pos + 8 + clen),
+ eina_crc((const char *) type, 4 + clen,
+ 0xffffffff, EINA_TRUE));
+ if ((klen < clen) && !strcmp(text, keyword))
+ found = strndup(text + klen + 1, clen - klen - 1);
+ }
+ pos += 12 + clen;
+ }
+ return found;
+}
+
+EFL_START_TEST(ethumb_fdo_thumbnail_names_its_source)
+{
+ Ethumb *e;
+ Eina_Bool done = EINA_FALSE;
+ struct stat src, thumb;
+ FILE *f;
+ unsigned char *png;
+ char expected[PATH_MAX + 16], *text;
+ char leftovers[PATH_MAX + 16];
+
+ ck_assert_int_eq(ethumb_init(), 1);
+ e = ethumb_new();
+ ck_assert_ptr_ne(e, NULL);
+
+ ethumb_thumb_format_set(e, ETHUMB_THUMB_FDO);
+ ck_assert(ethumb_file_set(e, srcfile, NULL));
+ ethumb_thumb_path_set(e, thumbfile, NULL);
+ ck_assert(ethumb_generate(e, _generated_cb, &done, NULL));
+ if (!done) ecore_main_loop_begin();
+ ck_assert(done);
+
+ ck_assert_int_eq(stat(srcfile, &src), 0);
+ ck_assert_int_eq(stat(thumbfile, &thumb), 0);
+ png = malloc(thumb.st_size);
+ ck_assert_ptr_ne(png, NULL);
+ f = fopen(thumbfile, "rb");
+ ck_assert_ptr_ne(f, NULL);
+ ck_assert_int_eq(fread(png, 1, thumb.st_size, f), thumb.st_size);
+ fclose(f);
+
+ snprintf(expected, sizeof(expected), "file://%s", srcfile);
+ text = _png_text_get(png, thumb.st_size, "Thumb::URI");
+ ck_assert_ptr_ne(text, NULL);
+ ck_assert_str_eq(text, expected);
+ free(text);
+
+ snprintf(expected, sizeof(expected), "%llu", (unsigned long long) src.st_mtime);
+ text = _png_text_get(png, thumb.st_size, "Thumb::MTime");
+ ck_assert_ptr_ne(text, NULL);
+ ck_assert_str_eq(text, expected);
+ free(text);
+
+ snprintf(expected, sizeof(expected), "%llu", (unsigned long long) src.st_size);
+ text = _png_text_get(png, thumb.st_size, "Thumb::Size");
+ ck_assert_ptr_ne(text, NULL);
+ ck_assert_str_eq(text, expected);
+ free(text);
+
+ free(png);
+
+ snprintf(leftovers, sizeof(leftovers), "%s/.tmp.thumb.png", tmpdir);
+ ck_assert_int_ne(stat(leftovers, &thumb), 0);
+ snprintf(leftovers, sizeof(leftovers), "%s.meta", thumbfile);
+ ck_assert_int_ne(stat(leftovers, &thumb), 0);
+
+ ethumb_free(e);
+ ethumb_shutdown();
+}
+EFL_END_TEST
+
+void
+ethumb_test_ethumb(TCase *tc)
+{
+ tcase_add_checked_fixture(tc, _tree_make, _tree_remove);
+ tcase_add_test(tc, ethumb_fdo_thumbnail_names_its_source);
+}
diff --git a/src/tests/ethumb/ethumb_test_slave.c b/src/tests/ethumb/ethumb_test_slave.c
index b7826f5b0a..cb24f57482 100644
--- a/src/tests/ethumb/ethumb_test_slave.c
+++ b/src/tests/ethumb/ethumb_test_slave.c
@@ -28,7 +28,7 @@
/* A real PNG, seven across and five down, so the slave has something it can
actually open and scale. */
-static const unsigned char real_png[] = {
+const unsigned char ethumb_test_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x05,
0x08, 0x02, 0x00, 0x00, 0x00, 0x06, 0xf8, 0x61, 0x8f, 0x00, 0x00, 0x00,
@@ -43,6 +43,8 @@ static const unsigned char real_png[] = {
0x42, 0x60, 0x82
};
+const size_t ethumb_test_png_size = sizeof(ethumb_test_png);
+
/* The wire is native ints and length-prefixed strings, the way the daemon
writes them. Built up in one buffer so the whole conversation can be
handed over in a single write. */
@@ -149,7 +151,8 @@ _tree_make(void)
sizeof(srcfile));
f = fopen(srcfile, "wb");
ck_assert_ptr_ne(f, NULL);
- ck_assert_int_eq(fwrite(real_png, 1, sizeof(real_png), f), sizeof(real_png));
+ ck_assert_int_eq(fwrite(ethumb_test_png, 1, ethumb_test_png_size, f),
+ ethumb_test_png_size);
fclose(f);
}
diff --git a/src/tests/ethumb/meson.build b/src/tests/ethumb/meson.build
index 5905588fa0..827ac51385 100644
--- a/src/tests/ethumb/meson.build
+++ b/src/tests/ethumb/meson.build
@@ -7,6 +7,7 @@ if is_variable('ethumbd_slave')
'ethumb_suite.c',
'ethumb_suite.h',
'ethumb_test_slave.c',
+ 'ethumb_test_ethumb.c',
]
ethumb_suite = executable('ethumb_suite',
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.