cfdisk: Implemented cfdisk's opening in read-only mode

[kzak@redhat.com: - clean up the patch
                  - add note "Changes will remain in memory only."]

Addresses: https://github.com/karelzak/util-linux/issues/1209
Addresses: https://github.com/karelzak/util-linux/pull/1213
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Dmitriy Chestnykh 2021-01-04 12:17:57 +01:00 committed by Karel Zak
parent b2db5a71b5
commit 0d182490e3
3 changed files with 14 additions and 4 deletions

View File

@ -19,6 +19,7 @@ _cfdisk_module()
--zero
--lock
--help
--read-only
--version"
COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
return 0

View File

@ -69,6 +69,9 @@ environment variable \fB$LOCK_BLOCK_DEVICE\fR. The default is not to use any
lock at all, but it's recommended to avoid collisions with udevd or other
tools.
.TP
.BR \-r , " \-\-read-only"
Forced open in read-only mode.
.TP
.BR \-V , " \-\-version"
Display version information and exit.
.TP

View File

@ -2568,7 +2568,7 @@ static int ui_run(struct cfdisk *cf)
ui_draw_extra(cf);
if (fdisk_is_readonly(cf->cxt))
ui_warnx(_("Device is open in read-only mode."));
ui_warnx(_("Device is open in read-only mode. Changes will remain in memory only."));
else if (cf->wrong_order)
ui_info(_("Note that partition table entries are not in disk order now."));
@ -2667,6 +2667,7 @@ static void __attribute__((__noreturn__)) usage(void)
fputs(_(" -z, --zero start with zeroed partition table\n"), out);
fprintf(out,
_(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
fputs(_(" -r, --read-only forced open cfdisk in read-only mode\n"), out);
fputs(USAGE_SEPARATOR, out);
printf(USAGE_HELP_OPTIONS(26));
@ -2679,6 +2680,7 @@ int main(int argc, char *argv[])
{
const char *diskpath = NULL, *lockmode = NULL;
int rc, c, colormode = UL_COLORMODE_UNDEF;
int read_only = 0;
struct cfdisk _cf = { .lines_idx = 0 },
*cf = &_cf;
enum {
@ -2690,6 +2692,7 @@ int main(int argc, char *argv[])
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ "zero", no_argument, NULL, 'z' },
{ "read-only", no_argument, NULL, 'r' },
{ NULL, 0, NULL, 0 },
};
@ -2698,7 +2701,7 @@ int main(int argc, char *argv[])
textdomain(PACKAGE);
close_stdout_atexit();
while((c = getopt_long(argc, argv, "L::hVz", longopts, NULL)) != -1) {
while((c = getopt_long(argc, argv, "L::hVzr", longopts, NULL)) != -1) {
switch(c) {
case 'h':
usage();
@ -2709,6 +2712,9 @@ int main(int argc, char *argv[])
colormode = colormode_or_err(optarg,
_("unsupported color mode"));
break;
case 'r':
read_only = 1;
break;
case 'V':
print_version(EXIT_SUCCESS);
case 'z':
@ -2752,8 +2758,8 @@ int main(int argc, char *argv[])
} else
diskpath = argv[optind];
rc = fdisk_assign_device(cf->cxt, diskpath, 0);
if (rc == -EACCES)
rc = fdisk_assign_device(cf->cxt, diskpath, read_only);
if (rc == -EACCES && read_only == 0)
rc = fdisk_assign_device(cf->cxt, diskpath, 1);
if (rc != 0)
err(EXIT_FAILURE, _("cannot open %s"), diskpath);