]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
When finding a /dev name for a device, prefer shorter names
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index 386ec2ae4ee2e98f244ffd77bf3e4d523626279a..f3f6446219e793e92861e6aa8d29130aacfdf62a 100644 (file)
--- a/util.c
+++ b/util.c
@@ -348,12 +348,15 @@ int add_dev(const char *name, const struct stat *stb, int flag)
 
 /*
  * Find a block device with the right major/minor number.
- * Avoid /dev/mdNN and /dev/md/dNN if possible
+ * If we find multiple names, choose the shortest.
+ * If we find a non-standard name, it is probably there
+ * deliberately so prefer it over a standard name.
+ * This applies only to names for MD devices.
  */
 char *map_dev(int major, int minor)
 {
        struct devmap *p;
-       char *std = NULL;
+       char *std = NULL, *nonstd=NULL;
        if (!devlist_ready) {
 #ifndef __dietlibc__
                nftw("/dev", add_dev, 10, FTW_PHYS);
@@ -366,12 +369,17 @@ char *map_dev(int major, int minor)
        for (p=devlist; p; p=p->next)
                if (p->major == major &&
                    p->minor == minor) {
-                       if (is_standard(p->name, NULL))
-                               std = p->name;
-                       else
-                               return p->name;
+                       if (is_standard(p->name, NULL)) {
+                               if (std == NULL ||
+                                   strlen(p->name) < strlen(std))
+                                       std = p->name;
+                       } else {
+                               if (nonstd == NULL ||
+                                   strlen(p->name) < strlen(nonstd))
+                                       nonstd = p->name;
+                       }
                }
-       return std;
+       return nonstd ? nonstd : std;
 }
 
 #endif