]> git.ipfire.org Git - people/ms/pakfire.git/blame - src/cli/lib/image_create.c
cli: Check for root privileges when needed
[people/ms/pakfire.git] / src / cli / lib / image_create.c
CommitLineData
0a333c90
MT
1/*#############################################################################
2# #
3# Pakfire - The IPFire package management system #
4# Copyright (C) 2023 Pakfire development team #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19#############################################################################*/
20
ab91ce3f 21#include <argp.h>
0a333c90
MT
22
23#include <pakfire/build.h>
24#include <pakfire/pakfire.h>
25
ab91ce3f 26#include "command.h"
0a333c90 27#include "image_create.h"
ab91ce3f 28#include "pakfire.h"
0a333c90 29
ab91ce3f
MT
30struct config {
31 const char* type;
32 const char* path;
33};
0a333c90 34
ab91ce3f 35static const char* args_doc = "image create TYPE PATH";
0a333c90 36
ab91ce3f 37static const char* doc = "Creates images";
0a333c90 38
d673ca6c 39static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
ab91ce3f 40 struct config* config = data;
0a333c90 41
ab91ce3f
MT
42 switch (key) {
43 case ARGP_KEY_ARG:
44 if (!config->type)
45 config->type = arg;
0a333c90 46
ab91ce3f
MT
47 else if (!config->path)
48 config->path = arg;
0a333c90 49
ab91ce3f
MT
50 break;
51
52 default:
53 return ARGP_ERR_UNKNOWN;
0a333c90
MT
54 }
55
56 return 0;
57}
58
ab91ce3f
MT
59int cli_image_create(void* data, int argc, char* argv[]) {
60 struct pakfire* pakfire = NULL;
0a333c90
MT
61 struct pakfire_build* build = NULL;
62 FILE* f = NULL;
63 int r;
64
ab91ce3f 65 struct cli_config* cli_config = data;
0a333c90 66
ab91ce3f
MT
67 struct config config = {
68 .type = NULL,
69 .path = NULL,
70 };
0a333c90 71
ab91ce3f 72 // Parse the command line
9b8d2c6e 73 r = cli_parse(NULL, NULL, args_doc, doc, parse, 0, argc, argv, &config);
ab91ce3f
MT
74 if (r)
75 goto ERROR;
76
77 f = fopen(config.path, "w");
0a333c90 78 if (!f) {
ab91ce3f 79 fprintf(stderr, "Could not open %s: %m\n", config.path);
0a333c90
MT
80 r = -errno;
81 goto ERROR;
82 }
83
ab91ce3f
MT
84 // Setup pakfire
85 r = cli_setup_pakfire(&pakfire, cli_config);
86 if (r)
87 goto ERROR;
88
0a333c90
MT
89 // Setup the build environment
90 r = pakfire_build_create(&build, pakfire, NULL, 0);
91 if (r)
92 goto ERROR;
93
94 // Create the image
ab91ce3f 95 r = pakfire_build_mkimage(build, config.type, f);
0a333c90
MT
96 if (r)
97 goto ERROR;
98
99ERROR:
100 if (build)
101 pakfire_build_unref(build);
ab91ce3f
MT
102 if (pakfire)
103 pakfire_unref(pakfire);
0a333c90
MT
104 if (f)
105 fclose(f);
106
107 return r;
108}