{
int gb;
- cxt = fdisk_new_context_from_filename(device);
+ cxt = fdisk_new_context_from_filename(device, 1); /* read-only */
if (!cxt)
err(EXIT_FAILURE, _("unable to open %s"), device);
else if (!gb)
list_table(0);
fdisk_free_context(cxt);
+ cxt = NULL;
}
/*
}
if (argc-optind == 1) {
- cxt = fdisk_new_context_from_filename(argv[optind]);
+ cxt = fdisk_new_context_from_filename(argv[optind], 0);
if (!cxt)
err(EXIT_FAILURE, _("unable to open %s"), argv[optind]);
}
};
extern struct fdisk_context *cxt;
-extern struct fdisk_context *fdisk_new_context_from_filename(const char *fname);
+
+extern struct fdisk_context *fdisk_new_context_from_filename(const char *fname, int readonly);
extern void fdisk_free_context(struct fdisk_context *cxt);
/* prototypes for fdisk.c */
*
* Returns: newly allocated fdisk context
*/
-struct fdisk_context *fdisk_new_context_from_filename(const char *fname)
+struct fdisk_context *fdisk_new_context_from_filename(const char *fname, int readonly)
{
int fd, errsv = 0;
struct fdisk_context *cxt = NULL;
* Attempt to open the device with r-w permissions
* by default, otherwise try read-only.
*/
- if ((fd = open(fname, O_RDWR)) < 0) {
+ if (readonly == 1 || (fd = open(fname, O_RDWR)) < 0) {
if ((fd = open(fname, O_RDONLY)) < 0)
return NULL;
- DBG(CONTEXT, printf("opened %s as read-only\n", fname));
+ readonly = 1;
}
cxt = calloc(1, sizeof(*cxt));
if (!cxt->dev_path)
goto fail;
+ DBG(CONTEXT, dbgprint("context initialized for %s [%s]",
+ fname, readonly ? "READ-ONLY" : "READ-WRITE"));
return cxt;
fail:
errsv = errno;
fdisk_free_context(cxt);
errno = errsv;
+
+ DBG(CONTEXT, dbgprint("failed to initialize context for %s: %m", fname));
return NULL;
}
if (!cxt)
return;
+ DBG(CONTEXT, dbgprint("freeing context for %s", cxt->dev_path));
close(cxt->dev_fd);
free(cxt->dev_path);
free(cxt);