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