]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
partx: get partition number with sysfs lib
authorDavidlohr Bueso <dave@gnu.org>
Sun, 17 Jul 2011 16:47:30 +0000 (12:47 -0400)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Jul 2011 15:49:02 +0000 (17:49 +0200)
Now that we have this feature, there's no need to manually parse sysfs in partx.

Signed-off-by: Davidlohr Bueso <dave@gnu.org>
partx/Makefile.am
partx/partx.c

index 46fc641724440f5818394536f49ee1d347be0841..6a72942fe9de20ca89b29b01990c38b2a899969a 100644 (file)
@@ -6,6 +6,7 @@ dist_man_MANS = addpart.8 delpart.8
 usrsbin_exec_PROGRAMS += partx
 partx_SOURCES = partx.c partx.h \
                $(top_srcdir)/lib/blkdev.c \
+               $(top_srcdir)/lib/sysfs.c \
                $(top_srcdir)/lib/tt.c \
                $(top_srcdir)/lib/at.c \
                $(top_srcdir)/lib/mbsalign.c \
index d8a4119c23670aaca609b8381fd48de1ce7c5b12..e29e50dcca7cab7b29c0e1c2d59678b9d4259742 100644 (file)
@@ -30,6 +30,7 @@
 #include "strutils.h"
 #include "xalloc.h"
 #include "partx.h"
+#include "sysfs.h"
 #include "at.h"
 
 /* this is the default upper limit, could be modified by --nr */
@@ -123,8 +124,6 @@ static int column_name_to_id(const char *name, size_t namesz)
  *
  * Note that this function tries to use sysfs, otherwise it assumes that the
  * last characters are always numeric (sda1, sdc20, etc).
- *
- * Returns -1 on error.
  */
 static int get_partno_from_device(char *partition, dev_t devno)
 {
@@ -135,21 +134,10 @@ static int get_partno_from_device(char *partition, dev_t devno)
        assert(partition);
 
        if (devno) {
-               /* the device exists, read the partition number from /sys
-                * TODO: move this to stuff to lib/sysfs.c */
-               char path[PATH_MAX];
-               FILE *f;
-
-               snprintf(path, sizeof(path),
-                               _PATH_SYS_DEVBLOCK "/%d:%d/partition",
-                               major(devno), minor(devno));
-               f = fopen(path, "r");
-               if (f) {
-                       if (fscanf(f, "%d", &partno) != 1)
-                               partno = 0;
-                       fclose(f);
-               }
-               if (partno)
+               struct sysfs_cxt cxt;
+
+               sysfs_init(&cxt, devno, NULL);
+               if (sysfs_read_int(&cxt, "partition", &partno) >= 0)
                        return partno;
        }