]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: add fdisk_partname()
authorKarel Zak <kzak@redhat.com>
Thu, 2 May 2013 11:39:01 +0000 (13:39 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Sep 2013 14:46:54 +0000 (16:46 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/Makemodule.am
libfdisk/src/fdiskP.h
libfdisk/src/utils.c

index fbfb1b4f21d93fddb10468e4a546f05b26b3a066..3858e7b0964bdbc51980d548cc5ce1d4f2677a16 100644 (file)
@@ -42,7 +42,8 @@ libfdisk_la_DEPENDENCIES = $(libfdisk_la_LIBADD)
 
 
 check_PROGRAMS += \
-       test_fdisk_ask
+       test_fdisk_ask \
+       test_fdisk_utils
 
 libfdisk_tests_cflags  = -DTEST_PROGRAM $(libfdisk_la_CFLAGS)
 libfdisk_tests_ldflags = -static
@@ -60,3 +61,8 @@ test_fdisk_ask_SOURCES = libfdisk/src/ask.c
 test_fdisk_ask_CFLAGS = $(libfdisk_tests_cflags)
 test_fdisk_ask_LDFLAGS = $(libfdisk_tests_ldflags)
 test_fdisk_ask_LDADD = $(libfdisk_tests_ldadd)
+
+test_fdisk_utils_SOURCES = libfdisk/src/utils.c
+test_fdisk_utils_CFLAGS = $(libfdisk_tests_cflags)
+test_fdisk_utils_LDFLAGS = $(libfdisk_tests_ldflags)
+test_fdisk_utils_LDADD = $(libfdisk_tests_ldadd)
index ff4c31bd2f09275579c1039ce200121cef21fecc..ac490be0f15de2717ac2ad7317b6d5cf8cd786e3 100644 (file)
@@ -318,6 +318,7 @@ extern int fdisk_discover_topology(struct fdisk_context *cxt);
 /* utils.c */
 extern void fdisk_zeroize_firstsector(struct fdisk_context *cxt);
 extern int fdisk_read_firstsector(struct fdisk_context *cxt);
+extern char *fdisk_partname(const char *dev, size_t partno);
 
 /* label.c */
 extern int fdisk_probe_labels(struct fdisk_context *cxt);
index 1360e84677ba9ba01c2146ae7f2ab01827e583fc..9195b2b194b9cb75dd868229ab6dee73ee1efe69 100644 (file)
@@ -1,5 +1,9 @@
 
 #include "fdiskP.h"
+#include "pathnames.h"
+
+#include <ctype.h>
+
 
 /*
  * Zeros in-memory first sector buffer
@@ -41,3 +45,77 @@ int fdisk_read_firstsector(struct fdisk_context *cxt)
 
        return 0;
 }
+
+
+/*
+ * Return allocated buffer with partition name
+ */
+char *fdisk_partname(const char *dev, size_t partno)
+{
+       char *res = NULL;
+       const char *p = "";
+       int w = 0;
+
+       if (!dev || !*dev) {
+               if (asprintf(&res, "%zd", partno) > 0)
+                       return res;
+               return NULL;
+       }
+
+       w = strlen(dev);
+       if (isdigit(dev[w - 1]))
+               p = "p";
+
+       /* devfs kludge - note: fdisk partition names are not supposed
+          to equal kernel names, so there is no reason to do this */
+       if (strcmp(dev + w - 4, "disc") == 0) {
+               w -= 4;
+               p = "part";
+       }
+
+       /* udev names partitions by appending -partN
+          e.g. ata-SAMSUNG_SV8004H_0357J1FT712448-part1 */
+       if ((strncmp(dev, _PATH_DEV_BYID, sizeof(_PATH_DEV_BYID) - 1) == 0) ||
+            strncmp(dev, _PATH_DEV_BYPATH, sizeof(_PATH_DEV_BYPATH) - 1) == 0) {
+              p = "-part";
+       }
+
+       if (asprintf(&res, "%.*s%s%zu", w, dev, p, partno) > 0)
+               return res;
+
+       return NULL;
+}
+
+#ifdef TEST_PROGRAM
+struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt) { return NULL; }
+struct fdisk_label *fdisk_new_bsd_label(struct fdisk_context *cxt) { return NULL; }
+struct fdisk_label *fdisk_new_mac_label(struct fdisk_context *cxt) { return NULL; }
+struct fdisk_label *fdisk_new_sgi_label(struct fdisk_context *cxt) { return NULL; }
+struct fdisk_label *fdisk_new_sun_label(struct fdisk_context *cxt) { return NULL; }
+
+int test_partnames(struct fdisk_test *ts, int argc, char *argv[])
+{
+       size_t i;
+       const char *disk = argv[1];
+
+       for (i = 0; i < 5; i++) {
+               char *p = fdisk_partname(disk, i + 1);
+               if (p)
+                       printf("%zu: '%s'\n", i + 1, p);
+               free(p);
+       }
+
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       struct fdisk_test tss[] = {
+               { "--partnames",  test_partnames,  "<diskname>" },
+               { NULL }
+       };
+
+       return fdisk_run_test(tss, argc, argv);
+}
+
+#endif