fix write_invis, workaround irix compiler bug
[xfstests-dev.git] / dmapi / src / common / cmd / write_invis.c
index e253fd8fe293eaa3e9e7968d7bee0ea0ab477d4b..a28bdeee6e1e80e4fda8a87ccf16f7ce3b9a14d6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
+ * Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
  * 
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of version 2 of the GNU General Public License as
 #include <string.h>
 #include <malloc.h>
 #include <getopt.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 /*---------------------------------------------------------------------------
 
 Test program used to test the DMAPI function dm_write_invis().  The
 command line is:
 
-       write_invis [-c char] [-o offset] [-l length] [-s sid] pathname
+       write_invis [-c char] [-o offset] [-l length] [-s sid] \
+               [-S storefile] {pathname|handle}
 
 where:
 'char' is the character to use as a repeated pattern ('X' is the default),
@@ -68,7 +72,7 @@ static void
 usage(void)
 {
        fprintf(stderr, "usage:\t%s [-c char] [-o offset] [-l length] "
-               "[-s sid] pathname\n", Progname);
+               "[-s sid] [-S storefile] {pathname|handle}\n", Progname);
        exit(1);
 }
 
@@ -79,7 +83,7 @@ main(
        char    **argv)
 {
        dm_sessid_t     sid = DM_NO_SESSION;
-       char            *pathname = NULL;
+       char            *object = NULL;
        dm_off_t        offset = 0;
        dm_size_t       length = 1;
        u_char          ch = 'X';
@@ -89,6 +93,9 @@ main(
        dm_ssize_t      rc;
        char            *name;
        int             opt;
+       char            *storefile = NULL;
+       int             storefd;
+       int             exit_status = 0;
 
        if (Progname = strrchr(argv[0], '/')) {
                Progname++;
@@ -98,27 +105,30 @@ main(
 
        /* Crack and validate the command line options. */
 
-       while ((opt = getopt(argc, argv, "c:o:l:s:")) != EOF) {
+       while ((opt = getopt(argc, argv, "c:o:l:s:S:")) != EOF) {
                switch (opt) {
                case 'c':
                        ch = *optarg;
                        break;
                case 'o':
-                       offset = atol(optarg);
+                       sscanf(optarg, "%lld", &offset);
                        break;
                case 'l':
-                       length = atol(optarg);
+                       sscanf(optarg, "%llu", &length);
                        break;
                case 's':
                        sid = atol(optarg);
                        break;
+               case 'S':
+                       storefile = optarg;
+                       break;
                case '?':
                        usage();
                }
        }
        if (optind + 1 != argc)
                usage();
-       pathname = argv[optind];
+       object = argv[optind];
 
        if (dm_init_service(&name) == -1)  {
                fprintf(stderr, "Can't initialize the DMAPI\n");
@@ -129,8 +139,8 @@ main(
 
        /* Get the file's handle. */
 
-       if (dm_path_to_handle(pathname, &hanp, &hlen)) {
-               fprintf(stderr, "can't get handle for file %s\n", pathname);
+       if (opaque_to_handle(object, &hanp, &hlen)) {
+               fprintf(stderr, "can't get handle for %s\n", object);
                exit(1);
        }
 
@@ -145,16 +155,39 @@ main(
                memset(bufp, ch, length);
        }
 
+       if (storefile) {
+               ssize_t sret;
+               size_t len;
+
+               if ((storefd = open(storefile, O_RDONLY)) == -1) {
+                       fprintf(stderr, "unable to open store file for read (%s), errno = %d\n", storefile, errno);
+                       exit(1);
+               }
+
+               len = length;
+               sret = read(storefd, bufp, len);
+               if (sret < 0) {
+                       fprintf(stderr, "unable to read store file (%s), errno = %d\n", storefile, errno);
+                       exit(1);
+               }
+               else if (sret != length) {
+                       fprintf(stderr, "read(%s) returned %lld, expected %lld\n",
+                               storefile, (long long)sret, (long long)length);
+                       exit(1);
+               }
+               close(storefd);
+       }
+
        rc = dm_write_invis(sid, hanp, hlen, DM_NO_TOKEN, 0, offset, length, bufp);
 
        if (rc < 0) {
                fprintf(stderr, "dm_write_invis failed, %s\n", strerror(errno));
-               exit(1);
+               exit_status++;
        } else if (rc != length) {
-               fprintf(stderr, "expected to write %lld bytes, actually "
+               fprintf(stderr, "dm_write_invis expected to write %lld bytes, actually "
                        "wrote %lld\n", length, rc);
-               exit(1);
+               exit_status++;
        }
        dm_handle_free(hanp, hlen);
-       exit(0);
+       exit(exit_status);
 }