Author: avg
Date: Tue Sep 13 14:01:35 2011
New Revision: 225529
URL: http://svn.freebsd.org/changeset/base/225529

Log:
  zfstest: cleanup the code, improve functionality and diagnostics
  
  The utility is not connected to the build, so it should be safe
  to update it.
  To do: move the utility to tools/.
  Some code is provided by Peter Jeremy <peterjer...@acm.org>
  
  Tested by:    Sebastian Chmielewski <chmielss...@gmail.com>,
                Peter Jeremy <peterjer...@acm.org> (earlier versions)
  Approved by:  re (kib)
  MFC after:    4 days

Modified:
  head/sys/boot/zfs/zfstest.c

Modified: head/sys/boot/zfs/zfstest.c
==============================================================================
--- head/sys/boot/zfs/zfstest.c Tue Sep 13 13:56:18 2011        (r225528)
+++ head/sys/boot/zfs/zfstest.c Tue Sep 13 14:01:35 2011        (r225529)
@@ -30,6 +30,7 @@
 
 #include <sys/param.h>
 #include <sys/queue.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -37,14 +38,14 @@
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdlib.h>
-#include <errno.h>
+#include <unistd.h>
 
 #define NBBY 8
 
 void
 pager_output(const char *line)
 {
-       printf("%s", line);
+       fprintf(stderr, "%s", line);
 }
 
 #include "zfsimpl.c"
@@ -55,8 +56,8 @@ vdev_read(vdev_t *vdev, void *priv, off_
        int fd = *(int *) priv;
 
        if (pread(fd, buf, bytes, off) != bytes)
-               return -1;
-       return 0;
+               return (-1);
+       return (0);
 }
 
 static int
@@ -69,10 +70,10 @@ zfs_read(spa_t *spa, dnode_phys_t *dn, v
        n = size;
        if (off + n > zp->zp_size)
                n = zp->zp_size - off;
-       
+
        rc = dnode_read(spa, dn, off, buf, n);
        if (rc)
-               return (rc);
+               return (-rc);
 
        return (n);
 }
@@ -80,22 +81,24 @@ zfs_read(spa_t *spa, dnode_phys_t *dn, v
 int
 main(int argc, char** argv)
 {
-       int i, n, off;
-       int fd[99];
-       spa_t *spa;
-       dnode_phys_t dn;
        char buf[512];
+       int fd[100];
+       struct stat sb;
+       dnode_phys_t dn;
+       spa_t *spa;
+       int i, n, off;
 
        zfs_init();
        if (argc == 1) {
                static char *av[] = {
-                       "zfstest", "/dev/da0p2", "/dev/da1p2", "/dev/da2p2",
+                       "zfstest", "COPYRIGHT",
+                       "/dev/da0p2", "/dev/da1p2", "/dev/da2p2",
                        NULL,
                };
-               argc = 4;
+               argc = 5;
                argv = av;
        }
-       for (i = 1; i < argc; i++) {
+       for (i = 2; i < argc; i++) {
                fd[i] = open(argv[i], O_RDONLY);
                if (fd[i] < 0)
                        continue;
@@ -105,16 +108,37 @@ main(int argc, char** argv)
        spa_all_status();
 
        spa = STAILQ_FIRST(&zfs_pools);
-       if (!spa || zfs_mount_pool(spa))
+       if (spa == NULL) {
+               fprintf(stderr, "no pools\n");
                exit(1);
+       }
 
-       if (zfs_lookup(spa, "zfs.c", &dn))
+       if (zfs_mount_pool(spa)) {
+               fprintf(stderr, "can't mount pool\n");
                exit(1);
+       }
+
+       if (zfs_lookup(spa, argv[1], &dn)) {
+               fprintf(stderr, "can't lookup\n");
+               exit(1);
+       }
+
+       if (zfs_dnode_stat(spa, &dn, &sb)) {
+               fprintf(stderr, "can't stat\n");
+               exit(1);
+       }
+
 
        off = 0;
        do {
                n = zfs_read(spa, &dn, buf, 512, off);
+               if (n < 0) {
+                       fprintf(stderr, "zfs_read failed\n");
+                       exit(1);
+               }
                write(1, buf, n);
                off += n;
-       } while (n == 512);
+       } while (off < sb.st_size);
+
+       return (0);
 }
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to