+typedef struct ImageInfo {
+ const char *name;
+ const char *type;
+ bool read_only;
+ usec_t crtime;
+ usec_t mtime;
+} ImageInfo;
+
+static int compare_image_info(const void *a, const void *b) {
+ const ImageInfo *x = a, *y = b;
+
+ return strcmp(x->name, y->name);
+}
+
+static int list_images(int argc, char *argv[], void *userdata) {
+
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ size_t max_name = strlen("NAME"), max_type = strlen("TYPE"), max_crtime = strlen("CREATED"), max_mtime = strlen("MODIFIED");
+ _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
+ _cleanup_free_ ImageInfo *images = NULL;
+ size_t n_images = 0, n_allocated = 0, j;
+ const char *name, *type, *object;
+ sd_bus *bus = userdata;
+ uint64_t crtime, mtime;
+ int read_only, r;
+
+ assert(bus);
+
+ pager_open_if_enabled();
+
+ r = sd_bus_call_method(
+ bus,
+ "org.freedesktop.machine1",
+ "/org/freedesktop/machine1",
+ "org.freedesktop.machine1.Manager",
+ "ListImages",
+ &error,
+ &reply,
+ "");
+ if (r < 0) {
+ log_error("Could not get images: %s", bus_error_message(&error, -r));
+ return r;
+ }
+
+ r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbtto)");
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ while ((r = sd_bus_message_read(reply, "(ssbtto)", &name, &type, &read_only, &crtime, &mtime, &object)) > 0) {
+ char buf[FORMAT_TIMESTAMP_MAX];
+ size_t l;
+
+ if (name[0] == '.' && !arg_all)
+ continue;
+
+ if (!GREEDY_REALLOC(images, n_allocated, n_images + 1))
+ return log_oom();
+
+ images[n_images].name = name;
+ images[n_images].type = type;
+ images[n_images].read_only = read_only;
+ images[n_images].crtime = crtime;
+ images[n_images].mtime = mtime;
+
+ l = strlen(name);
+ if (l > max_name)
+ max_name = l;
+
+ l = strlen(type);
+ if (l > max_type)
+ max_type = l;
+
+ if (crtime != 0) {
+ l = strlen(format_timestamp(buf, sizeof(buf), crtime));
+ if (l > max_crtime)
+ max_crtime = l;
+ }
+
+ if (mtime != 0) {
+ l = strlen(format_timestamp(buf, sizeof(buf), mtime));
+ if (l > max_mtime)
+ max_mtime = l;
+ }
+
+ n_images++;
+ }
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ r = sd_bus_message_exit_container(reply);
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ qsort_safe(images, n_images, sizeof(ImageInfo), compare_image_info);
+
+ if (arg_legend)
+ printf("%-*s %-*s %-3s %*s %*s\n",
+ (int) max_name, "NAME",
+ (int) max_type, "TYPE",
+ "RO",
+ (int) max_crtime, "CREATED",
+ (int) max_mtime, "MODIFIED");
+
+ for (j = 0; j < n_images; j++) {
+ char crtime_buf[FORMAT_TIMESTAMP_MAX], mtime_buf[FORMAT_TIMESTAMP_MAX];
+
+ printf("%-*s %-*s %-3s %*s %*s\n",
+ (int) max_name, images[j].name,
+ (int) max_type, images[j].type,
+ yes_no(images[j].read_only),
+ (int) max_crtime, images[j].crtime != 0 ? format_timestamp(crtime_buf, sizeof(crtime_buf), images[j].crtime) : "-",
+ (int) max_mtime, images[j].mtime != 0 ? format_timestamp(mtime_buf, sizeof(mtime_buf), images[j].mtime) : "-");
+ }
+
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+
+ if (arg_legend)
+ printf("\n%zu images listed.\n", n_images);
+
+ return 0;
+}
+