]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cfdisk: Implemented cfdisk's opening in read-only mode
authorDmitriy Chestnykh <dm.chestnykh@gmail.com>
Mon, 4 Jan 2021 11:17:57 +0000 (12:17 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 4 Jan 2021 11:17:57 +0000 (12:17 +0100)
[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>
bash-completion/cfdisk
disk-utils/cfdisk.8
disk-utils/cfdisk.c

index 4da6fb8b5b783c9daa01e26eaeb36ffc2d546c4d..3772ba4307ab7f3951582d8a3ca22416f9b4c2f6 100644 (file)
@@ -19,6 +19,7 @@ _cfdisk_module()
                                --zero
                                --lock
                                --help
+                               --read-only
                                --version"
                        COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
                        return 0
index a61aad71f31df03a473ddb23e16d9c76dfcfd7dc..dcceef13a605b4c25941d2a5290d13ce56b44243 100644 (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
index d96b6e9e03633334129f4ae5e67dc5ee503ed002..b549fe95ba18003fb0c93c468b1a5e1842b93f20 100644 (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);