xfstests: eliminate some warnings under dmapi/src
[xfstests-dev.git] / dmapi / src / common / cmd / write_invis.c
1 /*
2  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <lib/hsm.h>
20
21 #include <string.h>
22 #include <malloc.h>
23 #include <getopt.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 /*---------------------------------------------------------------------------
29
30 Test program used to test the DMAPI function dm_write_invis().  The
31 command line is:
32
33         write_invis [-c char] [-o offset] [-l length] [-s sid] \
34                 [-S storefile] {pathname|handle}
35
36 where:
37 'char' is the character to use as a repeated pattern ('X' is the default),
38 'offset' is the offset of the start of the write (0 is the default),
39 'length' is the length of the write in bytes (1 is the default),
40 'sid' is the session ID whose events you you are interested in.
41 'pathname' is the name of the file to be written.
42
43 DM_WRITE_SYNC is is not supported.
44
45 ----------------------------------------------------------------------------*/
46
47 #ifndef linux
48 extern  char    *sys_errlist[];
49 #endif
50 extern  int     optind;
51 extern  char    *optarg;
52
53
54 char    *Progname;
55
56
57 static void
58 usage(void)
59 {
60         fprintf(stderr, "usage:\t%s [-c char] [-o offset] [-l length] "
61                 "[-s sid] [-S storefile] {pathname|handle}\n", Progname);
62         exit(1);
63 }
64
65
66 int
67 main(
68         int     argc, 
69         char    **argv)
70 {
71         dm_sessid_t     sid = DM_NO_SESSION;
72         char            *object = NULL;
73         dm_off_t        offset = 0;
74         long long       lltemp;
75         dm_size_t       length = 1;
76         unsigned long long      ulltemp;
77         u_char          ch = 'X';
78         void            *bufp = NULL;
79         void            *hanp;
80         size_t          hlen;
81         dm_ssize_t      rc;
82         char            *name;
83         int             opt;
84         char            *storefile = NULL;
85         int             storefd;
86         int             exit_status = 0;
87
88         Progname = strrchr(argv[0], '/');
89         if (Progname) {
90                 Progname++;
91         } else {
92                 Progname = argv[0];
93         }
94
95         /* Crack and validate the command line options. */
96
97         while ((opt = getopt(argc, argv, "c:o:l:s:S:")) != EOF) {
98                 switch (opt) {
99                 case 'c':
100                         ch = *optarg;
101                         break;
102                 case 'o':
103                         sscanf(optarg, "%lld", &lltemp);
104                         offset = (dm_off_t) offset;
105                         break;
106                 case 'l':
107                         sscanf(optarg, "%llu", &ulltemp);
108                         length = (dm_size_t) ulltemp;
109                         break;
110                 case 's':
111                         sid = atol(optarg);
112                         break;
113                 case 'S':
114                         storefile = optarg;
115                         break;
116                 case '?':
117                         usage();
118                 }
119         }
120         if (optind + 1 != argc)
121                 usage();
122         object = argv[optind];
123
124         if (dm_init_service(&name) == -1)  {
125                 fprintf(stderr, "Can't initialize the DMAPI\n");
126                 exit(1);
127         }
128         if (sid == DM_NO_SESSION)
129                 find_test_session(&sid);
130
131         /* Get the file's handle. */
132
133         if (opaque_to_handle(object, &hanp, &hlen)) {
134                 fprintf(stderr, "can't get handle for %s\n", object);
135                 exit(1);
136         }
137
138         if (length > 0) {
139                 /* In case it is a realtime file, align the buffer on a
140                    sufficiently big boundary.
141                 */
142                 if ((bufp = memalign(4096, length)) == NULL) {
143                         fprintf(stderr, "malloc of %llu bytes failed\n",
144                                 (unsigned long long) length);
145                         exit(1);
146                 }
147                 memset(bufp, ch, length);
148         }
149
150         if (storefile) {
151                 ssize_t sret;
152                 size_t len;
153
154                 if ((storefd = open(storefile, O_RDONLY)) == -1) {
155                         fprintf(stderr, "unable to open store file for read (%s), errno = %d\n", storefile, errno);
156                         exit(1);
157                 }
158
159                 len = length;
160                 sret = read(storefd, bufp, len);
161                 if (sret < 0) {
162                         fprintf(stderr, "unable to read store file (%s), errno = %d\n", storefile, errno);
163                         exit(1);
164                 }
165                 else if (sret != length) {
166                         fprintf(stderr, "read(%s) returned %lld, expected %lld\n",
167                                 storefile, (long long)sret, (long long)length);
168                         exit(1);
169                 }
170                 close(storefd);
171         }
172
173         rc = dm_write_invis(sid, hanp, hlen, DM_NO_TOKEN, 0, offset, length, bufp);
174
175         if (rc < 0) {
176                 fprintf(stderr, "dm_write_invis failed, %s\n", strerror(errno));
177                 exit_status++;
178         } else if (rc != length) {
179                 fprintf(stderr, "dm_write_invis expected to write %llu bytes, "
180                         "actually wrote %lld\n",
181                         (unsigned long long) length, (long long)rc);
182                 exit_status++;
183         }
184         dm_handle_free(hanp, hlen);
185         exit(exit_status);
186 }