No Message Supplied
[xfstests-dev.git] / dmapi / src / suite2 / src / region_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
33 #include <sys/types.h>
34
35 #include <lib/hsm.h>
36 #include <lib/errtest.h>
37
38 #include <getopt.h>
39 #include <string.h>
40
41
42 /*---------------------------------------------------------------------------
43
44 Manual test for the DMAPI functions dm_set_region() and dm_get_region().
45
46 The command line is:
47
48         region_test [-Rv] [-s sid] [-l len] [-o offset] ls_path directory 
49
50 where 
51    -R
52         reuse the existing test file
53    pathname
54       is the name of a file
55    ls_path
56       is the path to a specific copy of ls, important only for its size
57    sid
58       is the session ID whose events you you are interested in.
59 ----------------------------------------------------------------------------*/
60
61 #define NELEM   1
62
63 #ifndef linux
64 extern  char    *sys_errlist[];
65 #endif
66 extern  int     optind;
67 extern  char    *optarg;
68
69
70 char    *Progname;
71
72
73 static void
74 usage(void)
75 {
76         fprintf(stderr, "usage:\t%s [-Rv] [-s sid] [-l len] [-o offset] ls_path pathname\n", Progname);
77         exit(1);
78 }
79
80
81 int
82 main(
83         int     argc, 
84         char    **argv)
85 {
86         dm_sessid_t     sid = DM_NO_SESSION;
87         char            *dir_name = NULL;
88         char            *ls_path = NULL;
89         char            command[1024];
90         char            test_file[128];
91         int             opt;
92         int             Vflag = 0;
93         dm_region_t     region = { 0, 0, 0 };
94         char            *name;
95         int             reuse_file = 0;
96         void            *hanp;
97         size_t           hlen;
98         dm_boolean_t    exactflag;
99
100         if (Progname = strrchr(argv[0], '/')) {
101                 Progname++;
102         } else {
103                 Progname = argv[0];
104         }
105
106         /* Crack and validate the command line options. */
107
108         while ((opt = getopt(argc, argv, "vo:l:s:R")) != EOF) {
109                 switch (opt) {
110                 case 'v':
111                         Vflag++;
112                         break;
113                 case 'R':
114                         reuse_file++;
115                         break;
116                 case 'o':
117                         region.rg_offset = atol(optarg);
118                         break;
119                 case 'l':
120                         region.rg_size = atol(optarg);
121                         break;
122                 case 's':
123                         sid = atol(optarg);
124                         break;
125                 case '?':
126                         usage();
127                 }
128         }
129         if (optind + 2 > argc)
130                 usage();
131         ls_path = argv[optind];
132         dir_name = argv[optind+1];
133
134         if (dm_init_service(&name) == -1)  {
135                 fprintf(stderr, "Can't initialize the DMAPI\n");
136                 exit(1);
137         }
138         if (sid == DM_NO_SESSION)
139                 find_test_session(&sid);
140
141         sprintf(test_file, "%s/DMAPI_test_file", dir_name);
142         if( !reuse_file ){
143                 sprintf(command, "cp %s %s\n", ls_path, test_file); 
144                 system(command);
145         }
146
147         if (dm_path_to_handle(test_file, &hanp, &hlen)) {
148                 fprintf(stderr, "can't get handle for file %s\n", test_file);
149                 exit(1);
150         }
151
152         region.rg_flags = DM_REGION_READ | DM_REGION_WRITE | DM_REGION_TRUNCATE;
153         if( dm_set_region( sid, hanp, hlen, DM_NO_TOKEN, NELEM,
154                           &region, &exactflag ) ){
155                 fprintf(stderr, "dm_set_region failed, err=%d\n", errno);
156                 dm_handle_free(hanp,hlen);
157                 exit(1);
158         }
159         if( exactflag == DM_FALSE )
160                 fprintf(stderr, "exact flag was false\n");
161
162         dm_handle_free(hanp, hlen);
163         exit(0);
164 }