c0ac28cca24c56a27f0a53377a17cef052dd59d7
[xfstests-dev.git] / dmapi / src / common / cmd / set_region.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <lib/hsm.h>
8
9 #include <string.h>
10 #include <getopt.h>
11
12 /*---------------------------------------------------------------------------
13
14 Test program used to test the DMAPI function dm_set_region().  The
15 command line is:
16
17         set_region [-n nelem] [-o offset] [-l length] [-s sid] {pathname|handle} [event...]
18
19 where pathname is the name of a file, nelem is the number of regions to pass
20 in the call, offset is the offset of the start of
21 the managed region, and length is the length.  sid is the session ID whose
22 events you you are interested in, and event is zero or more managed region
23 events to set.
24
25 ----------------------------------------------------------------------------*/
26
27 #ifndef linux
28 extern  char    *sys_errlist[];
29 #endif
30 extern  int     optind;
31 extern  char    *optarg;
32
33
34 char    *Progname;
35
36 static  struct  {
37         char    *name;
38         int     value;
39 } rg_events[3] = {
40         { "DM_REGION_READ", DM_REGION_READ },
41         { "DM_REGION_WRITE", DM_REGION_WRITE },
42         { "DM_REGION_TRUNCATE", DM_REGION_TRUNCATE }
43 };
44 static  int     nevents = sizeof(rg_events)/sizeof(rg_events[0]);
45
46
47 static void
48 usage(void)
49 {
50         int     i;
51
52         fprintf(stderr, "usage:\t%s [-n nelem] [-o offset] [-l length] "
53                 "[-s sid] {pathname|handle} [event...]\n", Progname);
54         fprintf(stderr, "possible events are:\n");
55         for (i = 0; i < nevents; i++)
56                 fprintf(stderr, "%s (0x%x)\n", rg_events[i].name, rg_events[i].value);
57         exit(1);
58 }
59
60
61 int
62 main(
63         int     argc, 
64         char    **argv)
65 {
66         dm_region_t     region = { 0, 0, 0 };
67         dm_sessid_t     sid = DM_NO_SESSION;
68         char            *object = NULL;
69         u_int           exactflag;
70         u_int           nelem = 1;
71         void            *hanp;
72         size_t           hlen;
73         char            *name;
74         int             opt;
75         int             i;
76
77         Progname = strrchr(argv[0], '/');
78         if (Progname) {
79                 Progname++;
80         } else {
81                 Progname = argv[0];
82         }
83
84         /* Crack and validate the command line options. */
85
86         while ((opt = getopt(argc, argv, "n:o:l:s:")) != EOF) {
87                 switch (opt) {
88                 case 'n':
89                         nelem = atol(optarg);
90                         break;
91                 case 'o':
92                         region.rg_offset = atol(optarg);
93                         break;
94                 case 'l':
95                         region.rg_size = atol(optarg);
96                         break;
97                 case 's':
98                         sid = atol(optarg);
99                         break;
100                 case '?':
101                         usage();
102                 }
103         }
104         if (optind + 1 > argc)
105                 usage();
106         object = argv[optind++];
107
108         if (dm_init_service(&name) == -1)  {
109                 fprintf(stderr, "Can't initialize the DMAPI\n");
110                 exit(1);
111         }
112         if (sid == DM_NO_SESSION)
113                 find_test_session(&sid);
114
115         /* Get the file's handle. */
116
117         if (opaque_to_handle(object, &hanp, &hlen)) {
118                 fprintf(stderr, "can't get handle for %s\n", object);
119                 exit(1);
120         }
121
122         for (; optind < argc; optind++) {
123                 if (strspn(argv[optind], "0123456789") == strlen(argv[optind])){
124                         region.rg_flags |= atol(argv[optind]);
125                         continue;
126                 }
127                 for (i = 0; i < nevents; i++) {
128                         if (!strcmp(argv[optind], rg_events[i].name))
129                                 break;
130                 }
131                 if (i == nevents) {
132                         fprintf(stderr, "invalid event %s\n", argv[optind]);
133                         exit(1);
134                 }
135                 region.rg_flags |= rg_events[i].value;
136         }
137
138         if (dm_set_region(sid, hanp, hlen, DM_NO_TOKEN, nelem, &region,
139             &exactflag)) {
140                 fprintf(stderr, "dm_set_region failed, %s\n",
141                         strerror(errno));
142                 exit(1);
143         }
144         fprintf(stdout, "exactflag is %s\n",
145                 exactflag == DM_TRUE ? "True" : "False");
146         dm_handle_free(hanp, hlen);
147         exit(0);
148 }