2bb884b9f7d491aa45a880cb32b318d25dbf46d0
[xfstests-dev.git] / dmapi / src / common / cmd / set_region.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 <lib/hsm.h>
34
35 #include <string.h>
36 #include <getopt.h>
37
38 /*---------------------------------------------------------------------------
39
40 Test program used to test the DMAPI function dm_set_region().  The
41 command line is:
42
43         set_region [-n nelem] [-o offset] [-l length] [-s sid] {pathname|handle} [event...]
44
45 where pathname is the name of a file, nelem is the number of regions to pass
46 in the call, offset is the offset of the start of
47 the managed region, and length is the length.  sid is the session ID whose
48 events you you are interested in, and event is zero or more managed region
49 events to set.
50
51 ----------------------------------------------------------------------------*/
52
53 #ifndef linux
54 extern  char    *sys_errlist[];
55 #endif
56 extern  int     optind;
57 extern  char    *optarg;
58
59
60 char    *Progname;
61
62 static  struct  {
63         char    *name;
64         int     value;
65 } rg_events[3] = {
66         { "DM_REGION_READ", DM_REGION_READ },
67         { "DM_REGION_WRITE", DM_REGION_WRITE },
68         { "DM_REGION_TRUNCATE", DM_REGION_TRUNCATE }
69 };
70 static  int     nevents = sizeof(rg_events)/sizeof(rg_events[0]);
71
72
73 static void
74 usage(void)
75 {
76         int     i;
77
78         fprintf(stderr, "usage:\t%s [-n nelem] [-o offset] [-l length] "
79                 "[-s sid] {pathname|handle} [event...]\n", Progname);
80         fprintf(stderr, "possible events are:\n");
81         for (i = 0; i < nevents; i++)
82                 fprintf(stderr, "%s (0x%x)\n", rg_events[i].name, rg_events[i].value);
83         exit(1);
84 }
85
86
87 int
88 main(
89         int     argc, 
90         char    **argv)
91 {
92         dm_region_t     region = { 0, 0, 0 };
93         dm_sessid_t     sid = DM_NO_SESSION;
94         char            *object = NULL;
95         u_int           exactflag;
96         u_int           nelem = 1;
97         void            *hanp;
98         size_t           hlen;
99         char            *name;
100         int             opt;
101         int             i;
102
103         if (Progname = strrchr(argv[0], '/')) {
104                 Progname++;
105         } else {
106                 Progname = argv[0];
107         }
108
109         /* Crack and validate the command line options. */
110
111         while ((opt = getopt(argc, argv, "n:o:l:s:")) != EOF) {
112                 switch (opt) {
113                 case 'n':
114                         nelem = atol(optarg);
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 + 1 > argc)
130                 usage();
131         object = argv[optind++];
132
133         if (dm_init_service(&name) == -1)  {
134                 fprintf(stderr, "Can't initialize the DMAPI\n");
135                 exit(1);
136         }
137         if (sid == DM_NO_SESSION)
138                 find_test_session(&sid);
139
140         /* Get the file's handle. */
141
142         if (opaque_to_handle(object, &hanp, &hlen)) {
143                 fprintf(stderr, "can't get handle for %s\n", object);
144                 exit(1);
145         }
146
147         for (; optind < argc; optind++) {
148                 if (strspn(argv[optind], "0123456789") == strlen(argv[optind])){
149                         region.rg_flags |= atol(argv[optind]);
150                         continue;
151                 }
152                 for (i = 0; i < nevents; i++) {
153                         if (!strcmp(argv[optind], rg_events[i].name))
154                                 break;
155                 }
156                 if (i == nevents) {
157                         fprintf(stderr, "invalid event %s\n", argv[optind]);
158                         exit(1);
159                 }
160                 region.rg_flags |= rg_events[i].value;
161         }
162
163         if (dm_set_region(sid, hanp, hlen, DM_NO_TOKEN, nelem, &region,
164             &exactflag)) {
165                 fprintf(stderr, "dm_set_region failed, %s\n",
166                         strerror(errno));
167                 exit(1);
168         }
169         fprintf(stdout, "exactflag is %s\n",
170                 exactflag == DM_TRUE ? "True" : "False");
171         dm_handle_free(hanp, hlen);
172         exit(0);
173 }