On Sun, 23 Aug 2015 19:45:17 +1000
Steven McDonald <ste...@steven-mcdonald.id.au> wrote:

>         fd = opendev(name, flags, 0, devicep);
>         if (fd == -1 && errno == ENOENT)
> -               devicep = &name;
> +               *devicep = name;

I'm actually wondering if opendev(3) itself should set realpath to path
in the ENOENT case, rather than the last path it tried to open. It
seems more correct to say "no such device for any of the possible
interpretations of this path".

The diff below implements this idea, and adapts tunefs(8) to suit. I'm
not sure if it's a better or worse solution than my first.

Index: lib/libutil/opendev.3
===================================================================
RCS file: /cvs/src/lib/libutil/opendev.3,v
retrieving revision 1.22
diff -u -p -r1.22 opendev.3
--- lib/libutil/opendev.3       15 Jan 2015 19:06:32 -0000      1.22
+++ lib/libutil/opendev.3       24 Aug 2015 06:33:45 -0000
@@ -88,7 +88,8 @@ If
 .Fa realpath
 is not
 .Dv NULL ,
-it is modified to point at the fully expanded device name.
+it is modified to point at the fully expanded device name, or to the
+unmodified device name if no device was found.
 .Sh RETURN VALUES
 The
 .Fn opendev
Index: lib/libutil/opendev.c
===================================================================
RCS file: /cvs/src/lib/libutil/opendev.c,v
retrieving revision 1.15
diff -u -p -r1.15 opendev.c
--- lib/libutil/opendev.c       30 Jun 2011 15:04:58 -0000      1.15
+++ lib/libutil/opendev.c       24 Aug 2015 06:33:45 -0000
@@ -103,8 +103,11 @@ opendev(const char *path, int oflags, in
                                errno = ENAMETOOLONG;
                }
        }
-       if (realpath)
+       if (realpath) {
+               if (fd == -1 && errno == ENOENT)
+                       strlcpy(namebuf, path, sizeof(namebuf));
                *realpath = namebuf;
+       }
 
        return (fd);
 }
Index: sbin/tunefs/tunefs.c
===================================================================
RCS file: /cvs/src/sbin/tunefs/tunefs.c,v
retrieving revision 1.37
diff -u -p -r1.37 tunefs.c
--- sbin/tunefs/tunefs.c        7 Feb 2015 02:09:14 -0000       1.37
+++ sbin/tunefs/tunefs.c        24 Aug 2015 06:33:45 -0000
@@ -309,21 +309,14 @@ bread(daddr_t blk, char *buffer, int cnt
 static int
 openpartition(char *name, int flags, char **devicep)
 {
-       char            rawspec[PATH_MAX], *p;
+       char            *p;
        struct fstab    *fs;
-       int             fd;
 
        fs = getfsfile(name);
        if (fs) {
-               if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
-                       snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
-                           (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
-                       name = rawspec;
-               } else
-                       name = fs->fs_spec;
+               name = fs->fs_spec;
+               if ((p = strrchr(name, '/')) != NULL)
+                       name = p + 1;
        }
-       fd = opendev(name, flags, 0, devicep);
-       if (fd == -1 && errno == ENOENT)
-               devicep = &name;
-       return (fd);
+       return opendev(name, flags, 0, devicep);
 }

Reply via email to