sfdisk: add --dump functionality
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
207de32a97
commit
8a8d204cd1
|
@ -71,7 +71,6 @@ enum {
|
||||||
struct sfdisk {
|
struct sfdisk {
|
||||||
int act; /* action */
|
int act; /* action */
|
||||||
size_t partno; /* partition number <1..N> */
|
size_t partno; /* partition number <1..N> */
|
||||||
const char *devname; /* disk */
|
|
||||||
|
|
||||||
struct fdisk_context *cxt; /* libfdisk context */
|
struct fdisk_context *cxt; /* libfdisk context */
|
||||||
};
|
};
|
||||||
|
@ -132,9 +131,6 @@ static int sfdisk_deinit(struct sfdisk *sf)
|
||||||
assert(sf);
|
assert(sf);
|
||||||
assert(sf->cxt);
|
assert(sf->cxt);
|
||||||
|
|
||||||
if (sf->devname)
|
|
||||||
rc = fdisk_deassign_device(sf->cxt, 0);
|
|
||||||
|
|
||||||
fdisk_unref_context(sf->cxt);
|
fdisk_unref_context(sf->cxt);
|
||||||
memset(sf, 0, sizeof(*sf));
|
memset(sf, 0, sizeof(*sf));
|
||||||
|
|
||||||
|
@ -149,31 +145,68 @@ static int command_list_partitions(struct sfdisk *sf, int argc, char **argv)
|
||||||
fdisk_enable_listonly(sf->cxt, 1);
|
fdisk_enable_listonly(sf->cxt, 1);
|
||||||
|
|
||||||
if (argc) {
|
if (argc) {
|
||||||
for (i = 0; i < argc; i++)
|
int ct = 0;
|
||||||
print_device_pt(sf->cxt, argv[i], 0);
|
|
||||||
|
for (i = 0; i < argc; i++) {
|
||||||
|
if (ct)
|
||||||
|
fputs("\n\n", stdout);
|
||||||
|
if (print_device_pt(sf->cxt, argv[i], 0) == 0)
|
||||||
|
ct++;
|
||||||
|
}
|
||||||
} else
|
} else
|
||||||
print_all_devices_pt(sf->cxt);
|
print_all_devices_pt(sf->cxt);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int command_dump(struct sfdisk *sf, int argc, char **argv)
|
||||||
|
{
|
||||||
|
const char *devname = NULL;
|
||||||
|
struct fdisk_script *dp;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
if (argc)
|
||||||
|
devname = argv[0];
|
||||||
|
if (!devname)
|
||||||
|
errx(EXIT_FAILURE, _("no disk device specified"));
|
||||||
|
|
||||||
|
rc = fdisk_assign_device(sf->cxt, devname, 1);
|
||||||
|
if (rc)
|
||||||
|
err(EXIT_FAILURE, _("cannot open %s"), devname);
|
||||||
|
|
||||||
|
dp = fdisk_new_script(sf->cxt);
|
||||||
|
if (!dp)
|
||||||
|
err(EXIT_FAILURE, _("failed to allocate dump struct"));
|
||||||
|
|
||||||
|
rc = fdisk_script_read_context(dp, NULL);
|
||||||
|
if (rc)
|
||||||
|
err(EXIT_FAILURE, _("failed to dump partition table"));
|
||||||
|
|
||||||
|
fdisk_script_write_file(dp, stdout);
|
||||||
|
|
||||||
|
fdisk_unref_script(dp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* default command */
|
/* default command */
|
||||||
static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
|
static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
const char *devname = NULL;
|
||||||
|
|
||||||
|
if (argc)
|
||||||
|
devname = argv[0];
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
sf->devname = argv[optind++];
|
sf->partno = strtou32_or_err(argv[1],
|
||||||
if (argc > 2)
|
|
||||||
sf->partno = strtou32_or_err(argv[optind++],
|
|
||||||
_("failed to parse partition number"));
|
_("failed to parse partition number"));
|
||||||
if (!sf->devname)
|
if (!devname)
|
||||||
errx(EXIT_FAILURE, _("no disk device specified"));
|
errx(EXIT_FAILURE, _("no disk device specified"));
|
||||||
|
|
||||||
rc = fdisk_assign_device(sf->cxt, sf->devname, 0);
|
rc = fdisk_assign_device(sf->cxt, devname, 0);
|
||||||
if (rc != 0)
|
if (rc)
|
||||||
err(EXIT_FAILURE, _("cannot open %s"), sf->devname);
|
err(EXIT_FAILURE, _("cannot open %s"), devname);
|
||||||
|
|
||||||
|
fdisk_deassign_device(sf->cxt, 1);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,6 +219,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
|
||||||
|
|
||||||
fputs(USAGE_OPTIONS, out);
|
fputs(USAGE_OPTIONS, out);
|
||||||
|
|
||||||
|
|
||||||
fputs(USAGE_SEPARATOR, out);
|
fputs(USAGE_SEPARATOR, out);
|
||||||
fputs(USAGE_HELP, out);
|
fputs(USAGE_HELP, out);
|
||||||
fputs(USAGE_VERSION, out);
|
fputs(USAGE_VERSION, out);
|
||||||
|
@ -199,10 +233,11 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct sfdisk _sf = { .partno = 0 }, *sf = &_sf;
|
struct sfdisk _sf = { .partno = 0 }, *sf = &_sf;
|
||||||
int rc, c;
|
int rc = -EINVAL, c;
|
||||||
|
|
||||||
static const struct option longopts[] = {
|
static const struct option longopts[] = {
|
||||||
{ "list", no_argument, NULL, 'l' },
|
{ "list", no_argument, NULL, 'l' },
|
||||||
|
{ "dump", no_argument, NULL, 'd' },
|
||||||
{ "help", no_argument, NULL, 'h' },
|
{ "help", no_argument, NULL, 'h' },
|
||||||
{ "version", no_argument, NULL, 'v' },
|
{ "version", no_argument, NULL, 'v' },
|
||||||
{ NULL, 0, 0, 0 },
|
{ NULL, 0, 0, 0 },
|
||||||
|
@ -213,7 +248,7 @@ int main(int argc, char *argv[])
|
||||||
textdomain(PACKAGE);
|
textdomain(PACKAGE);
|
||||||
atexit(close_stdout);
|
atexit(close_stdout);
|
||||||
|
|
||||||
while((c = getopt_long(argc, argv, "hlv", longopts, NULL)) != -1) {
|
while((c = getopt_long(argc, argv, "dhlv", longopts, NULL)) != -1) {
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case 'h':
|
case 'h':
|
||||||
usage(stdout);
|
usage(stdout);
|
||||||
|
@ -221,6 +256,9 @@ int main(int argc, char *argv[])
|
||||||
case 'l':
|
case 'l':
|
||||||
sf->act = ACT_LIST;
|
sf->act = ACT_LIST;
|
||||||
break;
|
break;
|
||||||
|
case 'd':
|
||||||
|
sf->act = ACT_DUMP;
|
||||||
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
printf(_("%s from %s\n"), program_invocation_short_name,
|
printf(_("%s from %s\n"), program_invocation_short_name,
|
||||||
PACKAGE_STRING);
|
PACKAGE_STRING);
|
||||||
|
@ -238,6 +276,10 @@ int main(int argc, char *argv[])
|
||||||
case ACT_FDISK:
|
case ACT_FDISK:
|
||||||
rc = command_fdisk(sf, argc - optind, argv + optind);
|
rc = command_fdisk(sf, argc - optind, argv + optind);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case ACT_DUMP:
|
||||||
|
rc = command_dump(sf, argc - optind, argv + optind);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
sfdisk_deinit(sf);
|
sfdisk_deinit(sf);
|
||||||
|
|
Loading…
Reference in New Issue