xfstests: eliminate warnings under dmapi/src/suite2/src
[xfstests-dev.git] / dmapi / src / suite2 / src / invis_test.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 #include <sys/types.h>
19 #include <sys/stat.h>
20
21 #include <limits.h>
22
23 #include <lib/hsm.h>
24 #include <lib/errtest.h>
25
26 #include <getopt.h>
27 #include <string.h>
28
29
30 /*---------------------------------------------------------------------------
31
32 For manually testing DMAPI functions dm_write_invis() and dm_read_invis()
33
34 The command line is:
35
36         invis_test [-Rrv] [-l len] [-o offset] [-s sid] ls_path pathname
37
38 where:
39    -R
40         reuse existing test file
41    -r
42         use dm_invis_read, default is dm_invis_write.
43    len
44         length of read/write
45    offset
46         offset in file for read/write
47    sid
48       is the session ID whose events you you are interested in.
49    ls_path
50       is the path to a specific copy of ls, important only for its size
51    pathname
52       is the filesystem to use for the test.
53
54 DM_WRITE_SYNC is is not supported.
55 ----------------------------------------------------------------------------*/
56
57 #ifndef linux
58 extern  char    *sys_errlist[];
59 #endif
60 extern  int     optind;
61 extern  char    *optarg;
62
63 char    *Progname;
64
65
66 static void
67 usage(void)
68 {
69         fprintf(stderr, "usage:\t%s [-Rrv] [-l len] [-o offset] [-s sid] ls_path pathname\n", 
70                 Progname);
71         exit(1);
72 }
73
74 #define BUFSZ 100
75
76 int
77 main(
78         int     argc, 
79         char    **argv)
80 {
81         int             Vflag=0;
82         dm_sessid_t     sid = DM_NO_SESSION;
83         char            *dir_name = NULL;
84         char            *ls_path = NULL;
85         char            *name;
86         char            ch = 'A';
87         char            test_file[128];
88         char            command[1024];
89         void            *hanp;
90         size_t          hlen;
91         char            buf[BUFSZ];
92         dm_ssize_t      rc;
93         dm_off_t        offset = 0;
94         dm_size_t       length = BUFSZ;
95         int             opt;
96         int             reading = 0; /* writing is the default */
97         int             exitstat=0;
98         dm_size_t       errblockstart, errblockend;
99         int             in_err_block;
100         int             i;
101         int             reuse_file = 0;
102
103         Progname = strrchr(argv[0], '/');
104         if (Progname) {
105                 Progname++;
106         } else {
107                 Progname = argv[0];
108         }
109
110         /* Crack and validate the command line options. */
111
112         while ((opt = getopt(argc, argv, "Rvs:rl:o:")) != EOF) {
113                 switch (opt) {
114                 case 'v':
115                         Vflag++;
116                         break;
117                 case 'r':
118                         reading++;
119                         break;
120                 case 'R':
121                         reuse_file++;
122                         break;
123                 case 'l':
124                         length = atoi(optarg);
125                         break;
126                 case 'o':
127                         offset = atoi(optarg);
128                         break;
129                 case 's':
130                         sid = atol(optarg);
131                         break;
132                 case '?':
133                         usage();
134                 }
135         }
136         if (optind + 2 != argc)
137                 usage();
138         ls_path = argv[optind];
139         dir_name = argv[optind+1];
140
141         if (dm_init_service(&name) == -1)  {
142                 fprintf(stderr, "Can't initialize the DMAPI\n");
143                 exit(1);
144         }
145         if (sid == DM_NO_SESSION)
146                 find_test_session(&sid);
147         
148         sprintf(test_file, "%s/DMAPI_test_file", dir_name);
149         if( (!reading) && (!reuse_file) ){
150                 sprintf(command, "cp %s %s\n", ls_path, test_file); 
151                 system(command);
152         }
153
154         if (dm_path_to_handle(test_file, &hanp, &hlen)) {
155                 fprintf(stderr, "can't get handle for %s; bypassing test\n",
156                       test_file);
157                 exit(1);
158         }
159
160         if( Vflag )
161                 printf("using length = %llu\n", (unsigned long long) length);
162         if( length > BUFSZ ){
163                 fprintf(stderr, "length(%llu) > BUFSZ(%d)\n",
164                         (unsigned long long) length, BUFSZ);
165                 exit(1);
166         }
167
168         if( reading ){
169                 memset(buf, '\0', BUFSZ);
170
171                 rc = dm_read_invis(sid, hanp, hlen, DM_NO_TOKEN,
172                                    offset, length, buf);
173                 if( rc < 0 ){
174                         fprintf(stderr, "dm_read_invis failed, (err=%d)\n", errno);
175                         dm_handle_free(hanp, hlen);
176                         exit(1);
177                 }
178                 if( rc != length ){
179                         fprintf(stderr, "dm_read_invis read %llu bytes, "
180                                 "wanted to write %lld bytes\n",
181                                 (long long) rc, (unsigned long long) length);
182                         dm_handle_free(hanp, hlen);
183                         exitstat++;
184                 }
185                 else {
186                         printf("dm_read_invis read %lld bytes\n",
187                                 (long long) rc);
188                 }
189                 
190                 in_err_block = 0;
191                 errblockstart = errblockend = 0;
192                 for( i=0; i < length; ++i ){
193                         if( in_err_block ){
194                                 if( buf[i] != ch ){
195                                         /* still in the err block */
196                                         errblockend = i;
197                                 }
198                                 else {
199                                         /* end of bad block */
200                                         fprintf(stderr, "read err block: "
201                                                 "byte %lld to %lld\n",
202                                                 (long long) errblockstart,
203                                                 (long long) errblockend);
204                                         in_err_block = 0;
205                                 }
206                         }
207                         else if( buf[i] != ch ){
208                                 /* enter err block */
209                                 errblockstart = i;
210                                 in_err_block = 1;
211                         }
212                 }
213                 if( in_err_block ){
214                         /* end of bad block */
215                         fprintf(stderr, "read err block: byte %lld to %lld\n",
216                                 (long long) errblockstart,
217                                 (long long) errblockend);
218                         in_err_block = 0;
219                 }
220         }
221         else {
222
223                 memset(buf, ch, BUFSZ);
224
225                 rc = dm_write_invis(sid, hanp, hlen, DM_NO_TOKEN,
226                                     0, offset, length, buf);
227                 if( rc < 0 ){
228                         fprintf(stderr, "dm_write_invis failed, (err=%d)\n", errno);
229                         dm_handle_free(hanp, hlen);
230                         exit(1);
231                 }
232                 if( rc != length ){
233                         fprintf(stderr, "dm_write_invis wrote %lld bytes, "
234                                 "wanted to write %lld bytes\n",
235                                 (long long) rc, (long long) length );
236                         dm_handle_free(hanp, hlen);
237                         exit(1);
238                 }
239                 printf("dm_write_invis wrote %lld bytes\n", (long long) rc);
240         }
241
242         dm_handle_free(hanp, hlen);
243         exit(exitstat);
244 }