From 1bf36bf9efafea1ee637e77dd9646c981a325aac Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 5 Apr 2018 17:56:22 +0200 Subject: [PATCH] machine-image: fix duplicate detection when discovering images We need to chop off the .raw suffix from the files we find before we can test it against the hashmap. Hence do that. And while we are at it, we can pass the pretty name into image_make(), since we already have it properly formatted. --- src/shared/machine-image.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c index 40fbccd596b..d73719e1ddb 100644 --- a/src/shared/machine-image.c +++ b/src/shared/machine-image.c @@ -410,14 +410,29 @@ int image_discover(ImageClass class, Hashmap *h) { FOREACH_DIRENT_ALL(de, d, return -errno) { _cleanup_(image_unrefp) Image *image = NULL; + _cleanup_free_ char *truncated = NULL; + const char *pretty, *e; - if (!image_name_is_valid(de->d_name)) + if (dot_or_dot_dot(de->d_name)) continue; - if (hashmap_contains(h, de->d_name)) + e = endswith(de->d_name, ".raw"); + if (e) { + truncated = strndup(de->d_name, e - de->d_name); + if (!truncated) + return -ENOMEM; + + pretty = truncated; + } else + pretty = de->d_name; + + if (!image_name_is_valid(pretty)) + continue; + + if (hashmap_contains(h, pretty)) continue; - r = image_make(NULL, dirfd(d), path, de->d_name, &image); + r = image_make(pretty, dirfd(d), path, de->d_name, &image); if (IN_SET(r, 0, -ENOENT)) continue; if (r < 0) -- 2.47.3