Add storefile option to DMAPI invis I/O tests
[xfstests-dev.git] / dmapi / src / common / cmd / read_invis.c
1 /*
2  * Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <ctype.h>
34
35 #include <lib/hsm.h>
36
37 #include <string.h>
38 #include <malloc.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43
44 /*---------------------------------------------------------------------------
45
46 Test program used to test the DMAPI function dm_read_invis().  The
47 command line is:
48
49         read_invis [-o offset] [-l length] [-s sid] [-c char] \
50                 [-S storefile] {pathname|handle}
51
52 where:
53 'offset' is the offset of the start of the write (0 is the default),
54 'length' is the length of the write in bytes (1 is the default),
55 'sid' is the session ID whose events you you are interested in.
56 'pathname' is the name of the file to be written.
57 'char' is ignored--it just allows read_invis and write_invis to have
58     interchangeable commandlines without having to fuss with the params.
59
60 ----------------------------------------------------------------------------*/
61
62 #ifndef linux
63 extern  char    *sys_errlist[];
64 #endif
65 extern  int     optind;
66 extern  char    *optarg;
67
68
69 char    *Progname;
70
71
72 static void
73 usage(void)
74 {
75         fprintf(stderr, "usage:\t%s [-o offset] [-l length] "
76                 "[-s sid] [-c char] "
77                 "[-S storefile] {pathname|handle}\n", Progname);
78         exit(1);
79 }
80
81
82 int
83 main(
84         int     argc, 
85         char    **argv)
86 {
87         dm_sessid_t     sid = DM_NO_SESSION;
88         char            *object = NULL;
89         dm_off_t        offset = 0;
90         dm_size_t       length = 1;
91         char            *bufp = NULL;
92         void            *hanp;
93         size_t          hlen;
94         dm_ssize_t      rc;
95         char            *name;
96         int             opt;
97         int             i;
98         char            *storefile = NULL;
99         int             storefd;
100         int             exit_status = 0;
101
102         if (Progname = strrchr(argv[0], '/')) {
103                 Progname++;
104         } else {
105                 Progname = argv[0];
106         }
107
108         /* Crack and validate the command line options. */
109
110         while ((opt = getopt(argc, argv, "o:l:s:c:S:")) != EOF) {
111                 switch (opt) {
112                 case 'o':
113                         sscanf(optarg, "%lld", &offset);
114                         break;
115                 case 'l':
116                         sscanf(optarg, "%llu", &length);
117                         break;
118                 case 's':
119                         sid = atol(optarg);
120                         break;
121                 case 'c':
122                         /* This is a no-op, it just allows read_invis
123                          * and write_invis to have interchangeable
124                          * commandlines, without having to fuss with
125                          * the params.
126                          */
127                         break;
128                 case 'S':
129                         storefile = optarg;
130                         break;
131                 case '?':
132                         usage();
133                 }
134         }
135         if (optind + 1 != argc)
136                 usage();
137         object = argv[optind];
138
139         if (dm_init_service(&name) == -1)  {
140                 fprintf(stderr, "Can't initialize the DMAPI\n");
141                 exit(1);
142         }
143         if (sid == DM_NO_SESSION)
144                 find_test_session(&sid);
145
146         /* Get the file's handle. */
147
148         if (opaque_to_handle(object, &hanp, &hlen)) {
149                 fprintf(stderr, "can't get handle for %s\n", object);
150                 exit(1);
151         }
152
153         if (length > 0) {
154                 /* In case it is a realtime file, align the buffer on a
155                    sufficiently big boundary.
156                 */
157                 if ((bufp = memalign(4096, length)) == NULL) {
158                         fprintf(stderr, "malloc of %llu bytes failed\n", length);
159                         exit(1);
160                 }
161                 memset(bufp, '\0', length);
162         }
163
164         if (storefile) {
165                 off_t   lret;
166
167                 if ((storefd = open(storefile, O_WRONLY|O_CREAT, 0777)) == -1) {
168                         fprintf(stderr, "unable to open store file for write (%s), errno = %d\n", storefile, errno);
169                         exit(1);
170                 }
171                 lret = lseek(storefd, offset, SEEK_SET);
172                 if (lret < 0) {
173                         fprintf(stderr, "unable to lseek(%s) to offset %lld, errno = %d\n",
174                                 storefile, (long long)lret, errno);
175                         exit(1);
176                 }
177         }
178
179         rc = dm_read_invis(sid, hanp, hlen, DM_NO_TOKEN, offset, length, bufp);
180
181         if (rc < 0) {
182                 fprintf(stderr, "dm_read_invis failed, %s\n", strerror(errno));
183                 exit_status++;
184         } else if (rc != length) {
185                 fprintf(stderr, "dm_read_invis expected to read %lld bytes, actually "
186                         "read %lld\n", length, rc);
187                 exit_status++;
188         }
189
190         if (storefile) {
191                 ssize_t sret;
192                 sret = write(storefd, bufp, rc);
193                 if (sret < 0) {
194                         fprintf(stderr, "unable to write to store file (%s), errno = %d\n", storefile, errno);
195                         exit_status++;
196                 }
197                 else if (sret != rc) {
198                         fprintf(stderr, "write(%s) returned %lld, expected %lld\n",
199                                 storefile, (long long)sret, (long long)rc);
200                         exit_status++;
201                 }
202                 close(storefd);
203         }
204         else {
205                 for (i = 0; i < rc; i++) {
206                         if (isprint(bufp[i])) {
207                                 fprintf(stdout, "%c", bufp[i]);
208                         } else {
209                                 fprintf(stdout, "\\%03d", bufp[i]);
210                         }
211                 }
212         }
213         dm_handle_free(hanp, hlen);
214         exit(exit_status);
215 }