machine-image: add 'discoverable' flag for images
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Apr 2018 09:14:57 +0000 (11:14 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 24 May 2018 15:01:57 +0000 (17:01 +0200)
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
src/shared/machine-image.h

index 36861d9..41e9513 100644 (file)
@@ -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;
index 899268d..7e09e80 100644 (file)
@@ -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;