!fdisk_is_disklabel(cf->cxt, SGI))
ignore[i++] = 'b';
}
+
+
+ if (fdisk_context_is_readonly(cf->cxt))
+ ignore[i++] = 'W';
return i;
}
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."),
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();
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 */
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"));
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;
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);
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)
return 0;
}
+int fdisk_context_is_readonly(struct fdisk_context *cxt)
+{
+ assert(cxt);
+ return cxt->readonly;
+}
+
/**
* fdisk_free_context:
* @cxt: 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 */
*/
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);
}
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);