bfc065efbfbe577dbc40ede94a874b712220693b
[xfstests-dev.git] / dmapi / src / suite2 / src / invis_test.c
1 /*
2  * Copyright (c) 2000 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 #ifdef linux
41 #include <string.h>
42 #endif
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         int     i;
84
85         fprintf(stderr, "usage:\t%s [-Rrv] [-l len] [-o offset] [-s sid] ls_path pathname\n", 
86                 Progname);
87         exit(1);
88 }
89
90 #define BUFSZ 100
91
92 int
93 main(
94         int     argc, 
95         char    **argv)
96 {
97         int             Vflag=0;
98         dm_sessid_t     sid = DM_NO_SESSION;
99         char            *dir_name = NULL;
100         char            *ls_path = NULL;
101         char            *name;
102         char            ch = 'A';
103         char            test_file[128];
104         char            command[1024];
105         void            *hanp;
106         size_t          hlen;
107         char            buf[BUFSZ];
108         dm_ssize_t      rc;
109         dm_off_t        offset = 0;
110         dm_size_t       length = BUFSZ;
111         int             opt;
112         int             reading = 0; /* writing is the default */
113         int             exitstat=0;
114         dm_size_t       errblockstart, errblockend;
115         int             in_err_block;
116         int             i;
117         int             reuse_file = 0;
118
119         if (Progname = strrchr(argv[0], '/')) {
120                 Progname++;
121         } else {
122                 Progname = argv[0];
123         }
124
125         /* Crack and validate the command line options. */
126
127         while ((opt = getopt(argc, argv, "Rvs:rl:o:")) != EOF) {
128                 switch (opt) {
129                 case 'v':
130                         Vflag++;
131                         break;
132                 case 'r':
133                         reading++;
134                         break;
135                 case 'R':
136                         reuse_file++;
137                         break;
138                 case 'l':
139                         length = atoi(optarg);
140                         break;
141                 case 'o':
142                         offset = atoi(optarg);
143                         break;
144                 case 's':
145                         sid = atol(optarg);
146                         break;
147                 case '?':
148                         usage();
149                 }
150         }
151         if (optind + 2 != argc)
152                 usage();
153         ls_path = argv[optind];
154         dir_name = argv[optind+1];
155
156         if (dm_init_service(&name) == -1)  {
157                 fprintf(stderr, "Can't inititalize the DMAPI\n");
158                 exit(1);
159         }
160         if (sid == DM_NO_SESSION)
161                 find_test_session(&sid);
162         
163         sprintf(test_file, "%s/DMAPI_test_file", dir_name);
164         if( (!reading) && (!reuse_file) ){
165                 sprintf(command, "cp %s %s\n", ls_path, test_file); 
166                 system(command);
167         }
168
169         if (dm_path_to_handle(test_file, &hanp, &hlen)) {
170                 fprintf(stderr, "can't get handle for %s; bypassing test\n",
171                       test_file);
172                 exit(1);
173         }
174
175         if( Vflag )
176                 printf("using length = %llu\n", length );
177         if( length > BUFSZ ){
178                 fprintf(stderr, "length(%llu) > BUFSZ(%d)\n", length, BUFSZ);
179                 exit(1);
180         }
181
182         if( reading ){
183                 memset(buf, '\0', BUFSZ);
184
185                 rc = dm_read_invis(sid, hanp, hlen, DM_NO_TOKEN,
186                                    offset, length, buf);
187                 if( rc < 0 ){
188                         fprintf(stderr, "dm_read_invis failed, (err=%d)\n", errno);
189                         dm_handle_free(hanp, hlen);
190                         exit(1);
191                 }
192                 if( rc != length ){
193                         fprintf(stderr, "dm_read_invis read %lld bytes, wanted to write %lld bytes\n",
194                                 rc, length );
195                         dm_handle_free(hanp, hlen);
196                         exitstat++;
197                 }
198                 else {
199                         printf("dm_read_invis read %lld bytes\n", rc);
200                 }
201                 
202                 in_err_block = 0;
203                 errblockstart = errblockend = 0;
204                 for( i=0; i < length; ++i ){
205                         if( in_err_block ){
206                                 if( buf[i] != ch ){
207                                         /* still in the err block */
208                                         errblockend = i;
209                                 }
210                                 else {
211                                         /* end of bad block */
212                                         fprintf(stderr, "read err block: byte %lld to %lld\n", errblockstart, errblockend);
213                                         in_err_block = 0;
214                                 }
215                         }
216                         else if( buf[i] != ch ){
217                                 /* enter err block */
218                                 errblockstart = i;
219                                 in_err_block = 1;
220                         }
221                 }
222                 if( in_err_block ){
223                         /* end of bad block */
224                         fprintf(stderr, "read err block: byte %lld to %lld\n", errblockstart, errblockend);
225                         in_err_block = 0;
226                 }
227         }
228         else {
229
230                 memset(buf, ch, BUFSZ);
231
232                 rc = dm_write_invis(sid, hanp, hlen, DM_NO_TOKEN,
233                                     0, offset, length, buf);
234                 if( rc < 0 ){
235                         fprintf(stderr, "dm_write_invis failed, (err=%d)\n", errno);
236                         dm_handle_free(hanp, hlen);
237                         exit(1);
238                 }
239                 if( rc != length ){
240                         fprintf(stderr, "dm_write_invis wrote %lld bytes, wanted to write %lld bytes\n",
241                                 rc, length );
242                         dm_handle_free(hanp, hlen);
243                         exit(1);
244                 }
245                 printf("dm_write_invis wrote %lld bytes\n", rc);
246         }
247
248         dm_handle_free(hanp, hlen);
249         exit(exitstat);
250 }