diff options
author | Karel Zak | 2014-03-21 14:04:59 +0100 |
---|---|---|
committer | Karel Zak | 2014-03-21 14:04:59 +0100 |
commit | e146ae4edb52b0559cbb42717338665c6395b56f (patch) | |
tree | 95f86c65e0c0f4d1eed1940b92a955be838dc670 /disk-utils | |
parent | libfdisk: clean up debug output (diff) | |
download | kernel-qcow2-util-linux-e146ae4edb52b0559cbb42717338665c6395b56f.tar.gz kernel-qcow2-util-linux-e146ae4edb52b0559cbb42717338665c6395b56f.tar.xz kernel-qcow2-util-linux-e146ae4edb52b0559cbb42717338665c6395b56f.zip |
libfdisk: properly implement read-only mode
Don't use fallback to read-only mode in fdisk_context_assign_device(),
it's application responsibility open the device in the right mode.
The commands fdisk and cfdisk check (and report) read-only mode now.
Reported-by: Maciej MaĆecki <me@mmalecki.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'disk-utils')
-rw-r--r-- | disk-utils/cfdisk.c | 21 | ||||
-rw-r--r-- | disk-utils/fdisk-menu.c | 4 | ||||
-rw-r--r-- | disk-utils/fdisk.c | 10 |
3 files changed, 31 insertions, 4 deletions
diff --git a/disk-utils/cfdisk.c b/disk-utils/cfdisk.c index 6a07ced25..651c45b85 100644 --- a/disk-utils/cfdisk.c +++ b/disk-utils/cfdisk.c @@ -1525,6 +1525,10 @@ static int main_menu_ignore_keys(struct cfdisk *cf, char *ignore, !fdisk_is_disklabel(cf->cxt, SGI)) ignore[i++] = 'b'; } + + + if (fdisk_context_is_readonly(cf->cxt)) + ignore[i++] = 'W'; return i; } @@ -1643,7 +1647,14 @@ static int main_menu_action(struct cfdisk *cf, int key) case 'W': /* Write */ { char buf[64] = { 0 }; - int rc = ui_get_string(cf, + int rc; + + if (fdisk_context_is_readonly(cf->cxt)) { + ui_warnx(_("Device open in read-only mode")); + break; + } + + rc = ui_get_string(cf, _("Are you sure you want to write the partition " "table to disk? "), _("Type \"yes\" or \"no\" or press ESC to left dialog."), @@ -1709,6 +1720,9 @@ static int ui_run(struct cfdisk *cf) if (rc) return rc; + if (fdisk_context_is_readonly(cf->cxt)) + ui_warnx(_("Device open in read-only mode.")); + do { int rc = 0, key = getch(); @@ -1835,7 +1849,10 @@ int main(int argc, char *argv[]) if (optind == argc) usage(stderr); - if (fdisk_context_assign_device(cf->cxt, argv[optind], 0) != 0) + rc = fdisk_context_assign_device(cf->cxt, argv[optind], 0); + if (rc == -EACCES) + rc = fdisk_context_assign_device(cf->cxt, argv[optind], 1); + if (rc != 0) err(EXIT_FAILURE, _("cannot open %s"), argv[optind]); /* Don't use err(), warn() from this point */ diff --git a/disk-utils/fdisk-menu.c b/disk-utils/fdisk-menu.c index 822761568..bdaa73475 100644 --- a/disk-utils/fdisk-menu.c +++ b/disk-utils/fdisk-menu.c @@ -447,6 +447,10 @@ static int generic_menu_cb(struct fdisk_context **cxt0, list_disklabel(cxt); break; case 'w': + if (fdisk_context_is_readonly(cxt)) { + fdisk_warnx(cxt, _("Device open in read-only mode.")); + break; + } rc = fdisk_write_disklabel(cxt); if (rc) err(EXIT_FAILURE, _("failed to write disklabel")); diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index fa7f248cc..bfe3fe3b5 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -778,7 +778,7 @@ enum { int main(int argc, char **argv) { - int i, c, act = ACT_FDISK; + int rc, i, c, act = ACT_FDISK; int colormode = UL_COLORMODE_UNDEF; struct fdisk_context *cxt; @@ -920,7 +920,13 @@ int main(int argc, char **argv) fdisk_info(cxt, _("Changes will remain in memory only, until you decide to write them.\n" "Be careful before using the write command.\n")); - if (fdisk_context_assign_device(cxt, argv[optind], 0) != 0) + rc = fdisk_context_assign_device(cxt, argv[optind], 0); + if (rc == -EACCES) { + rc = fdisk_context_assign_device(cxt, argv[optind], 1); + if (rc == 0) + fdisk_warnx(cxt, _("Device open in read-only mode.")); + } + if (rc) err(EXIT_FAILURE, _("cannot open %s"), argv[optind]); fflush(stdout); |