libfstools: query drivers by priority
[project/fstools.git] / libfstools / ubi.c
index 091ccf6e5bbf0dc561853be7873290a6df3165d9..9bd7b050f4784bd59b7ed3d6be3ee6e64ae26c1a 100644 (file)
  * GNU General Public License for more details.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <dirent.h>
-
-#include "libfstools.h"
-#include "volume.h"
+#include "common.h"
 
 /* fit for UBI_MAX_VOLUME_NAME and sysfs path lengths */
 #define BUFLEN         128
 
 /* could use libubi-tiny instead, but already had the code directly reading
  * from sysfs */
-const char *const ubi_dir_name = "/sys/devices/virtual/ubi";
+const char *const ubi_dir_name = "/sys/class/ubi";
 
 struct ubi_volume {
        struct volume v;
@@ -34,56 +28,6 @@ struct ubi_volume {
 
 static struct driver ubi_driver;
 
-static int
-read_uint_from_file(char *dirname, char *filename, unsigned int *i)
-{
-       FILE *f;
-       char fname[BUFLEN];
-       int ret = -1;
-
-       snprintf(fname, sizeof(fname), "%s/%s", dirname, filename);
-
-       f = fopen(fname, "r");
-       if (!f)
-               return ret;
-
-       if (fscanf(f, "%u", i) == 1)
-               ret = 0;
-
-       fclose(f);
-       return ret;
-}
-
-static char
-*read_string_from_file(char *dirname, char *filename)
-{
-       FILE *f;
-       char fname[BUFLEN];
-       char buf[BUFLEN];
-       int i;
-
-       snprintf(fname, sizeof(fname), "%s/%s", dirname, filename);
-
-       f = fopen(fname, "r");
-       if (!f)
-               return NULL;
-
-       if (fgets(buf, sizeof(buf), f) == NULL)
-               return NULL;
-
-       fclose(f);
-
-       /* make sure the string is \0 terminated */
-       buf[sizeof(buf) - 1] = '\0';
-
-       /* remove trailing whitespace */
-       i = strlen(buf) - 1;
-       while (i > 0 && buf[i] <= ' ')
-               buf[i--] = '\0';
-
-       return strdup(buf);
-}
-
 static unsigned int
 test_open(char *filename)
 {
@@ -100,17 +44,16 @@ test_open(char *filename)
 static int ubi_volume_init(struct volume *v)
 {
        struct ubi_volume *p = container_of(v, struct ubi_volume, v);
-       char voldir[BUFLEN], voldev[BUFLEN], *volname;
+       char voldir[BUFLEN], voldev[BUFLEN], volname[BUFLEN];
        unsigned int volsize;
 
-       snprintf(voldir, sizeof(voldir), "%s/ubi%u/ubi%u_%u",
-               ubi_dir_name, p->ubi_num, p->ubi_num, p->ubi_volid);
+       snprintf(voldir, sizeof(voldir), "%s/ubi%u_%u",
+               ubi_dir_name, p->ubi_num, p->ubi_volid);
 
        snprintf(voldev, sizeof(voldev), "/dev/ubi%u_%u",
                p->ubi_num, p->ubi_volid);
 
-       volname = read_string_from_file(voldir, "name");
-       if (!volname)
+       if (!read_string_from_file(voldir, "name", volname, sizeof(volname)))
                return -1;
 
        if (read_uint_from_file(voldir, "data_bytes", &volsize))
@@ -126,11 +69,11 @@ static int ubi_volume_init(struct volume *v)
 
 static struct volume *ubi_volume_match(char *name, int ubi_num, int volid)
 {
-       char voldir[BUFLEN], volblkdev[BUFLEN], *volname;
+       char voldir[BUFLEN], volblkdev[BUFLEN], volname[BUFLEN];
        struct ubi_volume *p;
 
-       snprintf(voldir, sizeof(voldir), "%s/ubi%u/ubi%u_%u",
-               ubi_dir_name, ubi_num, ubi_num, volid);
+       snprintf(voldir, sizeof(voldir), "%s/ubi%u_%u",
+               ubi_dir_name, ubi_num, volid);
 
        snprintf(volblkdev, sizeof(volblkdev), "/dev/ubiblock%u_%u",
                ubi_num, volid);
@@ -141,8 +84,7 @@ static struct volume *ubi_volume_match(char *name, int ubi_num, int volid)
 
        /* todo: skip existing gluebi device for legacy support */
 
-       volname = read_string_from_file(voldir, "name");
-       if (!volname) {
+       if (!read_string_from_file(voldir, "name", volname, sizeof(volname))) {
                ULOG_ERR("Couldn't read %s/name\n", voldir);
                return NULL;
        }
@@ -229,6 +171,7 @@ static int ubi_volume_identify(struct volume *v)
 
 static struct driver ubi_driver = {
        .name = "ubi",
+       .priority = 20,
        .find = ubi_volume_find,
        .init = ubi_volume_init,
        .identify = ubi_volume_identify,