HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/import/importctl.c

47,216 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 59bfb89c11dbc8c093ec2530753850daec986a29

Lines 1001–1231 of 1,231

         * command was introduced in v256, so changing the field name would be a breaking change. */
        (void) table_set_json_field_name(t, 8, "usage-exclusive");
        (void) table_set_json_field_name(t, 10, "limit-exclusive");
 
        for (;;) {
                uint64_t crtime, mtime, usage, usage_exclusive, limit, limit_exclusive;
                const char *class, *name, *type, *path;
                int read_only;
 
                r = sd_bus_message_read(reply, "(ssssbtttttt)", &class, &name, &type, &path, &read_only, &crtime, &mtime, &usage, &usage_exclusive, &limit, &limit_exclusive);
                if (r < 0)
                        return bus_log_parse_error(r);
                if (r == 0)
                        break;
 
                r = table_add_many(
                                t,
                                TABLE_STRING, class,
                                TABLE_STRING, name,
                                TABLE_STRING, type,
                                TABLE_PATH, path);
                if (r < 0)
                        return table_log_add_error(r);
 
                if (!sd_json_format_enabled(arg_json_format_flags))
                        r = table_add_many(
                                        t,
                                        TABLE_STRING, read_only ? "ro" : "rw",
                                        TABLE_SET_COLOR, read_only ? ANSI_HIGHLIGHT_RED : ANSI_HIGHLIGHT_GREEN);
                else
                        r = table_add_many(
                                        t,
                                        TABLE_BOOLEAN, read_only);
                if (r < 0)
                        return table_log_add_error(r);
 
                r = table_add_many(
                                t,
                                TABLE_TIMESTAMP, crtime,
                                TABLE_TIMESTAMP, mtime,
                                TABLE_SIZE, usage,
                                TABLE_SIZE, usage_exclusive,
                                TABLE_SIZE, limit,
                                TABLE_SIZE, limit_exclusive);
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        r = sd_bus_message_exit_container(reply);
        if (r < 0)
                return bus_log_parse_error(r);
 
        if (!table_isempty(t)) {
                r = table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
                if (r < 0)
                        return r;
        }
 
        if (arg_legend) {
                if (!table_isempty(t))
                        printf("\n%zu images listed.\n", table_get_rows(t) - 1);
                else
                        printf("No images.\n");
        }
 
        return 0;
}
 
VERB_COMMON_HELP_AUTO();
 
static int parse_argv(int argc, char *argv[], char ***ret_args) {
        int r;
 
        assert(argc >= 0);
        assert(argv);
 
        OptionParser opts = { argc, argv };
 
        FOREACH_OPTION_OR_RETURN(c, &opts)
                switch (c) {
 
                OPTION_COMMON_HELP:
                        return command_print_help();
 
                OPTION_COMMON_VERSION:
                        return version();
 
                OPTION_COMMON_NO_PAGER:
                        arg_pager_flags |= PAGER_DISABLE;
                        break;
 
                OPTION_COMMON_NO_LEGEND:
                        arg_legend = false;
                        break;
 
                OPTION_COMMON_NO_ASK_PASSWORD:
                        arg_ask_password = false;
                        break;
 
                OPTION_COMMON_HOST:
                        arg_transport = BUS_TRANSPORT_REMOTE;
                        arg_host = opts.arg;
                        break;
 
                OPTION_COMMON_MACHINE:
                        r = parse_machine_argument(opts.arg, &arg_host, &arg_transport);
                        if (r < 0)
                                return r;
                        break;
 
                OPTION_LONG("system", NULL, "Connect to system machine manager"):
                        arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
                        break;
 
                OPTION_LONG("user", NULL, "Connect to user machine manager"):
                        arg_runtime_scope = RUNTIME_SCOPE_USER;
                        break;
 
                OPTION_LONG("read-only", NULL, "Create read-only image"):
                        arg_import_flags |= IMPORT_READ_ONLY;
                        arg_import_flags_mask |= IMPORT_READ_ONLY;
                        break;
 
                OPTION('q', "quiet", NULL, "Suppress output"):
                        arg_quiet = true;
                        break;
 
                OPTION_COMMON_JSON:
                        r = parse_json_argument(opts.arg, &arg_json_format_flags);
                        if (r <= 0)
                                return r;
                        arg_legend = false;
                        break;
 
                OPTION_COMMON_LOWERCASE_J:
                        arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO;
                        arg_legend = false;
                        break;
 
                OPTION_LONG("verify", "MODE",
                            "Verification mode for downloaded images (no, checksum, signature)"):
                        if (streq(opts.arg, "help"))
                                return DUMP_STRING_TABLE(import_verify, ImportVerify, _IMPORT_VERIFY_MAX);
 
                        r = import_verify_from_string(opts.arg);
                        if (r < 0)
                                return log_error_errno(r, "Failed to parse --verify= setting: %s", opts.arg);
                        arg_verify = r;
                        break;
 
                OPTION_LONG("format", "FORMAT",
                            "Desired output format for export (zstd, xz, gzip, bzip2)"):
                        if (!STR_IN_SET(opts.arg, "uncompressed", "xz", "gzip", "bzip2", "zstd"))
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "Unknown format: %s", opts.arg);
                        arg_format = opts.arg;
                        break;
 
                OPTION_LONG("force", NULL, "Install image even if already exists"):
                        arg_import_flags |= IMPORT_FORCE;
                        arg_import_flags_mask |= IMPORT_FORCE;
                        break;
 
                OPTION_LONG("class", "TYPE", "Install as the specified TYPE"):
                        arg_image_class = image_class_from_string(opts.arg);
                        if (arg_image_class < 0)
                                return log_error_errno(arg_image_class, "Failed to parse --class= parameter: %s", opts.arg);
                        break;
 
                OPTION_SHORT('m', NULL, "Install as --class=machine, machine image"):
                        arg_image_class = IMAGE_MACHINE;
                        break;
 
                OPTION_SHORT('P', NULL, "Install as --class=portable, portable service image"):
                        arg_image_class = IMAGE_PORTABLE;
                        break;
 
                OPTION_SHORT('S', NULL, "Install as --class=sysext, system extension image"):
                        arg_image_class = IMAGE_SYSEXT;
                        break;
 
                OPTION_SHORT('C', NULL, "Install as --class=confext, configuration extension image"):
                        arg_image_class = IMAGE_CONFEXT;
                        break;
 
                OPTION_LONG("keep-download", "BOOL",
                            "Control whether to keep pristine copy of download"):
                        r = parse_boolean(opts.arg);
                        if (r < 0)
                                return log_error_errno(r, "Failed to parse --keep-download= value: %s", opts.arg);
 
                        SET_FLAG(arg_import_flags, IMPORT_PULL_KEEP_DOWNLOAD, r);
                        arg_import_flags_mask |= IMPORT_PULL_KEEP_DOWNLOAD;
                        break;
 
                OPTION_SHORT('N', NULL, "Same as --keep-download=no"):
                        arg_import_flags &= ~IMPORT_PULL_KEEP_DOWNLOAD;
                        arg_import_flags_mask |= IMPORT_PULL_KEEP_DOWNLOAD;
                        break;
 
                OPTION_COMMON_INTROSPECT_CLI:
                        return introspect_cli(arg_json_format_flags);
                }
 
        *ret_args = option_parser_get_args(&opts);
        return 1;
}
 
static int run(int argc, char *argv[]) {
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        int r;
 
        setlocale(LC_ALL, "");
        log_setup();
 
        char **args = NULL;
        r = parse_argv(argc, argv, &args);
        if (r <= 0)
                return r;
 
        r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, &bus);
        if (r < 0)
                return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
 
        (void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
 
        return dispatch_verb(args, bus);
}
 
DEFINE_MAIN_FUNCTION(run);