HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/portable/portabled-image-bus.c

44,670 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 698e75db9e4ed71d95194a7da27704c3b2698c3d

Lines 1001–1198 of 1,198

 
int bus_image_acquire(
                Manager *m,
                sd_bus_message *message,
                const char *name_or_path,
                Image *image,
                ImageAcquireMode mode,
                const char *polkit_action,
                Image **ret,
                sd_bus_error *error) {
 
        _cleanup_(image_unrefp) Image *loaded = NULL;
        Image *cached;
        int r;
 
        assert(m);
        assert(message);
        assert(name_or_path || image);
        assert(mode >= 0);
        assert(mode < _BUS_IMAGE_ACQUIRE_MODE_MAX);
        assert(polkit_action || mode == BUS_IMAGE_REFUSE_BY_PATH);
        assert(ret);
 
        /* Acquires an 'Image' object if not acquired yet, and enforces necessary authentication while doing so. */
 
        if (mode == BUS_IMAGE_AUTHENTICATE_ALL && m->runtime_scope != RUNTIME_SCOPE_USER) {
                r = bus_verify_polkit_async(
                                message,
                                polkit_action,
                                /* details= */ NULL,
                                &m->polkit_registry,
                                error);
                if (r < 0)
                        return r;
                if (r == 0) { /* Will call us back */
                        *ret = NULL;
                        return 0;
                }
        }
 
        /* Already passed in? */
        if (image) {
                *ret = image;
                return 1;
        }
 
        /* Let's see if this image is already cached? */
        cached = manager_image_cache_get(m, name_or_path);
        if (cached) {
                *ret = cached;
                return 1;
        }
 
        if (image_name_is_valid(name_or_path)) {
 
                /* If it's a short name, let's search for it */
                r = image_find(m->runtime_scope, IMAGE_PORTABLE, name_or_path, NULL, &loaded);
                if (r == -ENOENT)
                        return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_PORTABLE_IMAGE,
                                                 "No image '%s' found.", name_or_path);
 
                /* other errors are handled below… */
        } else {
                /* Don't accept path if this is always forbidden */
                if (mode == BUS_IMAGE_REFUSE_BY_PATH)
                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                                 "Expected image name, not path in place of '%s'.", name_or_path);
 
                if (!path_is_absolute(name_or_path))
                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                                 "Image name '%s' is not valid or not a valid path.", name_or_path);
 
                if (!path_is_normalized(name_or_path))
                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                                 "Image path '%s' is not normalized.", name_or_path);
 
                if (mode == BUS_IMAGE_AUTHENTICATE_BY_PATH && m->runtime_scope != RUNTIME_SCOPE_USER) {
                        r = bus_verify_polkit_async(
                                        message,
                                        polkit_action,
                                        /* details= */ NULL,
                                        &m->polkit_registry,
                                        error);
                        if (r < 0)
                                return r;
                        if (r == 0) { /* Will call us back */
                                *ret = NULL;
                                return 0;
                        }
                }
 
                r = image_from_path(name_or_path, &loaded);
        }
        if (r == -EMEDIUMTYPE) {
                sd_bus_error_setf(error, BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE,
                                  "Type of image '%s' not recognized; supported image types are directories/btrfs subvolumes, block devices, and raw disk image files with suffix '.raw'.",
                                  name_or_path);
                return r;
        }
        if (r < 0)
                return r;
 
        /* Add what we just loaded to the cache. This has as side-effect that the object stays in memory until the
         * cache is purged again, i.e. at least for the current event loop iteration, which is all we need, and which
         * means we don't actually need to ref the return object. */
        r = manager_image_cache_add(m, loaded);
        if (r < 0)
                return r;
 
        *ret = loaded;
        return 1;
}
 
int bus_image_object_find(
                sd_bus *bus,
                const char *path,
                const char *interface,
                void *userdata,
                void **found,
                sd_bus_error *error) {
 
        _cleanup_free_ char *e = NULL;
        Manager *m = userdata;
        Image *image = NULL;
        int r;
 
        assert(bus);
        assert(path);
        assert(interface);
        assert(found);
 
        r = sd_bus_path_decode(path, "/org/freedesktop/portable1/image", &e);
        if (r < 0)
                return 0;
        if (r == 0)
                goto not_found;
        if (isempty(e))
                /* The path is "/org/freedesktop/portable1/image" itself */
                goto not_found;
 
        r = bus_image_acquire(m, sd_bus_get_current_message(bus), e, NULL, BUS_IMAGE_REFUSE_BY_PATH, NULL, &image, error);
        if (r == -ENOENT)
                goto not_found;
        if (r < 0)
                return r;
 
        *found = image;
        return 1;
 
not_found:
        *found = NULL;
        return 0;
}
 
int bus_image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
        _cleanup_hashmap_free_ Hashmap *images = NULL;
        _cleanup_strv_free_ char **l = NULL;
        Manager *m = userdata;
        size_t n = 0;
        Image *image;
        int r;
 
        assert(bus);
        assert(path);
        assert(nodes);
 
        r = manager_image_cache_discover(m, &images, error);
        if (r < 0)
                return r;
 
        HASHMAP_FOREACH(image, images) {
                char *p;
 
                r = bus_image_path(image, &p);
                if (r < 0)
                        return r;
 
                if (!GREEDY_REALLOC(l, n+2)) {
                        free(p);
                        return -ENOMEM;
                }
 
                l[n++] = p;
                l[n] = NULL;
        }
 
        *nodes = TAKE_PTR(l);
 
        return 1;
}
 
const BusObjectImplementation image_object = {
        "/org/freedesktop/portable1/image",
        "org.freedesktop.portable1.Image",
        .fallback_vtables = BUS_FALLBACK_VTABLES({image_vtable, bus_image_object_find}),
        .node_enumerator = bus_image_node_enumerator,
};