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 | |
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>
-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 | ||||
-rw-r--r-- | libfdisk/src/context.c | 15 | ||||
-rw-r--r-- | libfdisk/src/fdiskP.h | 3 | ||||
-rw-r--r-- | libfdisk/src/label.c | 3 | ||||
-rw-r--r-- | libfdisk/src/libfdisk.h | 1 |
7 files changed, 45 insertions, 12 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); diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c index 7d7b0c668..ec0b6d84f 100644 --- a/libfdisk/src/context.c +++ b/libfdisk/src/context.c @@ -260,12 +260,11 @@ int fdisk_context_assign_device(struct fdisk_context *cxt, reset_context(cxt); - if (readonly == 1 || (fd = open(fname, O_RDWR|O_CLOEXEC)) < 0) { - if ((fd = open(fname, O_RDONLY|O_CLOEXEC)) < 0) - return -errno; - readonly = 1; - } + fd = open(fname, (readonly ? O_RDONLY : O_RDWR ) | O_CLOEXEC); + if (fd < 0) + return -errno; + cxt->readonly = readonly; cxt->dev_fd = fd; cxt->dev_path = strdup(fname); if (!cxt->dev_path) @@ -315,6 +314,12 @@ int fdisk_context_deassign_device(struct fdisk_context *cxt) return 0; } +int fdisk_context_is_readonly(struct fdisk_context *cxt) +{ + assert(cxt); + return cxt->readonly; +} + /** * fdisk_free_context: * @cxt: fdisk context diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h index b5880df70..cb3c2b27c 100644 --- a/libfdisk/src/fdiskP.h +++ b/libfdisk/src/fdiskP.h @@ -346,7 +346,8 @@ struct fdisk_context { unsigned long sector_size; /* logical size */ unsigned long alignment_offset; - unsigned int display_in_cyl_units : 1, /* for obscure labels */ + unsigned int readonly : 1, /* don't write to the device */ + display_in_cyl_units : 1, /* for obscure labels */ display_details : 1, /* expert display mode */ listonly : 1; /* list partition, nothing else */ diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c index 360089972..9c6fcc5b3 100644 --- a/libfdisk/src/label.c +++ b/libfdisk/src/label.c @@ -75,11 +75,10 @@ int fdisk_dev_is_disklabel(struct fdisk_context *cxt, enum fdisk_labeltype l) */ int fdisk_write_disklabel(struct fdisk_context *cxt) { - if (!cxt || !cxt->label) + if (!cxt || !cxt->label || cxt->readonly) return -EINVAL; if (!cxt->label->op->write) return -ENOSYS; - return cxt->label->op->write(cxt); } diff --git a/libfdisk/src/libfdisk.h b/libfdisk/src/libfdisk.h index 3a173e390..ed48da793 100644 --- a/libfdisk/src/libfdisk.h +++ b/libfdisk/src/libfdisk.h @@ -78,6 +78,7 @@ extern int fdisk_context_set_ask(struct fdisk_context *cxt, int (*ask_cb)(struct fdisk_context *, struct fdisk_ask *, void *), void *data); +extern int fdisk_context_is_readonly(struct fdisk_context *cxt); extern int fdisk_context_assign_device(struct fdisk_context *cxt, const char *fname, int readonly); extern int fdisk_context_deassign_device(struct fdisk_context *cxt); |