]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: refactor /proc/partitions usage
authorKarel Zak <kzak@redhat.com>
Fri, 5 Sep 2014 08:35:29 +0000 (10:35 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 7 Oct 2014 12:55:31 +0000 (14:55 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/fdisk-list.c
disk-utils/fdisk-list.h

index e3bd113a9db72d4ac30b651b0589ce81c0a7b2ae..3d26f8b7a6196af317f7b1636412ec7449ed11f3 100644 (file)
@@ -177,36 +177,19 @@ done:
        fdisk_free_iter(itr);
 }
 
-int print_device_pt(struct fdisk_context *cxt, char *device, int warnme)
+char *next_proc_partition(FILE **f)
 {
-       if (fdisk_assign_device(cxt, device, 1) != 0) { /* read-only */
-               if (warnme || errno == EACCES)
-                       warn(_("cannot open %s"), device);
-               return -1;
-       }
-
-       list_disk_geometry(cxt);
-
-       if (fdisk_has_label(cxt))
-               list_disklabel(cxt);
-
-       fdisk_deassign_device(cxt, 1);
-       return 0;
-}
-
-void print_all_devices_pt(struct fdisk_context *cxt)
-{
-       FILE *f;
        char line[128 + 1];
-       int ct = 0;
 
-       f = fopen(_PATH_PROC_PARTITIONS, "r");
-       if (!f) {
-               warn(_("cannot open %s"), _PATH_PROC_PARTITIONS);
-               return;
+       if (!*f) {
+               *f = fopen(_PATH_PROC_PARTITIONS, "r");
+               if (!*f) {
+                       warn(_("cannot open %s"), _PATH_PROC_PARTITIONS);
+                       return NULL;
+               }
        }
 
-       while (fgets(line, sizeof(line), f)) {
+       while (fgets(line, sizeof(line), *f)) {
                char buf[PATH_MAX], *cn;
                dev_t devno;
 
@@ -228,14 +211,44 @@ void print_all_devices_pt(struct fdisk_context *cxt)
                if (!cn)
                        continue;
 
-               if (!is_ide_cdrom_or_tape(cn)) {
-                       if (ct)
-                               fputs("\n\n", stdout);
-                       if (print_device_pt(cxt, cn, 0) == 0)
-                               ct++;
-               }
-               free(cn);
+               if (!is_ide_cdrom_or_tape(cn))
+                       return cn;
+       }
+       fclose(*f);
+       *f = NULL;
+
+       return NULL;
+}
+
+int print_device_pt(struct fdisk_context *cxt, char *device, int warnme)
+{
+       if (fdisk_assign_device(cxt, device, 1) != 0) { /* read-only */
+               if (warnme || errno == EACCES)
+                       warn(_("cannot open %s"), device);
+               return -1;
+       }
+
+       list_disk_geometry(cxt);
+
+       if (fdisk_has_label(cxt))
+               list_disklabel(cxt);
+
+       fdisk_deassign_device(cxt, 1);
+       return 0;
+}
+
+void print_all_devices_pt(struct fdisk_context *cxt)
+{
+       FILE *f = NULL;
+       int ct = 0;
+       char *dev;
+
+       while ((dev = next_proc_partition(&f))) {
+               if (ct)
+                       fputs("\n\n", stdout);
+               if (print_device_pt(cxt, dev, 0) == 0)
+                       ct++;
+               free(dev);
        }
-       fclose(f);
 }
 
index 125a7df34c1cc6a8aefda39872838a1df604383c..dc61c587106dc45717d4edd69aac9ab9d985919c 100644 (file)
@@ -4,6 +4,7 @@
 extern void list_disklabel(struct fdisk_context *cxt);
 extern void list_disk_geometry(struct fdisk_context *cxt);
 
+extern char *next_proc_partition(FILE **f);
 extern int print_device_pt(struct fdisk_context *cxt, char *device, int warnme);
 extern void print_all_devices_pt(struct fdisk_context *cxt);