From cf604fd40f75e046838c095a8b343eca24f65079 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 17 Apr 2018 11:14:57 +0200 Subject: [PATCH] machine-image: add 'discoverable' flag for images This new flag indicates whether the image object was found in the search paths using the usual algorithm, or was instantiated by path. This is useful for code that wants to know whether an image may be referenced by its shortened name or must be specified by its full name. --- src/shared/machine-image.c | 25 ++++++++++++++++++++++--- src/shared/machine-image.h | 3 ++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c index 36861d93fcc..41e9513abc1 100644 --- a/src/shared/machine-image.c +++ b/src/shared/machine-image.c @@ -477,16 +477,32 @@ int image_find(ImageClass class, const char *name, Image **ret) { if (r < 0) return r; + if (ret) + (*ret)->discoverable = true; + return 1; } - if (class == IMAGE_MACHINE && streq(name, ".host")) - return image_make(".host", AT_FDCWD, NULL, "/", NULL, ret); + if (class == IMAGE_MACHINE && streq(name, ".host")) { + r = image_make(".host", AT_FDCWD, NULL, "/", NULL, ret); + if (r < 0) + return r; + + if (ret) + (*ret)->discoverable = true; + + return r; + } return -ENOENT; }; int image_from_path(const char *path, Image **ret) { + + /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether + * the image is in the image search path. And if it is we don't know if the path we used is actually not + * overriden by another, different image earlier in the search path */ + if (path_equal(path, "/")) return image_make(".host", AT_FDCWD, NULL, "/", NULL, ret); @@ -567,6 +583,8 @@ int image_discover(ImageClass class, Hashmap *h) { if (r < 0) return r; + image->discoverable = true; + r = hashmap_put(h, image->name, image); if (r < 0) return r; @@ -582,12 +600,13 @@ int image_discover(ImageClass class, Hashmap *h) { if (r < 0) return r; + image->discoverable = true; + r = hashmap_put(h, image->name, image); if (r < 0) return r; image = NULL; - } return 0; diff --git a/src/shared/machine-image.h b/src/shared/machine-image.h index 899268dbd1d..7e09e8002a7 100644 --- a/src/shared/machine-image.h +++ b/src/shared/machine-image.h @@ -54,7 +54,8 @@ typedef struct Image { char **machine_info; char **os_release; - bool metadata_valid; + bool metadata_valid:1; + bool discoverable:1; /* true if we know for sure that image_find() would find the image given just the short name */ void *userdata; } Image; -- 2.47.3