summaryrefslogtreecommitdiffstats
path: root/fdisk/utils.c
diff options
context:
space:
mode:
authorDavidlohr Bueso2012-05-21 22:28:03 +0200
committerKarel Zak2012-05-23 10:53:05 +0200
commit823f0fd107415ced8edde12306b9134058aafdc0 (patch)
treee5f7edcc875cd7013e23c2320232d1943990e5be /fdisk/utils.c
parentfdisk: refactor -s option (diff)
downloadkernel-qcow2-util-linux-823f0fd107415ced8edde12306b9134058aafdc0.tar.gz
kernel-qcow2-util-linux-823f0fd107415ced8edde12306b9134058aafdc0.tar.xz
kernel-qcow2-util-linux-823f0fd107415ced8edde12306b9134058aafdc0.zip
fdisk: introduce fdisk context
This is the first patch that adds the initial parts of the new fdisk internal API. Two functions are created to both init and deinit the fdisk context. Only the device's descriptor and path are added as a start and these are replaced throughout fdisk.c and label specific code. All logic that opens the file does it with fdisk_new_context_from_filename(), and this enforces always opening the device with rw, then, if unsuccesfull, with read-only. This changes the current logic that opens the device with certain permissions depending on the user given options. For example, -l opens the device read-only. This patch passed regression tests and local modifications for sgi/dos/sun disk labels. Signed-off-by: Davidlohr Bueso <dave@gnu.org>
Diffstat (limited to 'fdisk/utils.c')
-rw-r--r--fdisk/utils.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/fdisk/utils.c b/fdisk/utils.c
new file mode 100644
index 000000000..5138c043a
--- /dev/null
+++ b/fdisk/utils.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "fdisk.h"
+
+/**
+ * fdisk_new_context:
+ *
+ * Returns: newly allocated fdisk context
+ */
+struct fdisk_context *fdisk_new_context_from_filename(const char *fname)
+{
+ 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 ((fd = open(fname, O_RDONLY)) < 0)
+ return NULL;
+
+ cxt = calloc(1, sizeof(*cxt));
+ if (!cxt)
+ goto fail;
+
+ cxt->dev_fd = fd;
+ cxt->dev_path = strdup(fname);
+ if (!cxt->dev_path)
+ goto fail;
+
+ return cxt;
+fail:
+ errsv = errno;
+ fdisk_free_context(cxt);
+ errno = errsv;
+ return NULL;
+}
+
+/**
+ * fdisk_free_context:
+ * @cxt: fdisk context
+ *
+ * Deallocates context struct.
+ */
+void fdisk_free_context(struct fdisk_context *cxt)
+{
+ if (!cxt)
+ return;
+
+ close(cxt->dev_fd);
+ free(cxt->dev_path);
+ free(cxt);
+}