From: Lennart Poettering Date: Thu, 5 Apr 2018 15:56:22 +0000 (+0200) Subject: machine-image: fix duplicate detection when discovering images X-Git-Tag: v239~208^2~15 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1bf36bf9efafea1ee637e77dd9646c981a325aac;p=thirdparty%2Fsystemd.git 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. --- 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)