generic/60[78]: ensure the initial DAX file flag state before test
[xfstests-dev.git] / dmapi / src / suite1 / cmd / get_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         get_region [-n nelem] [-s sid] {pathname|handle}
18
19 where pathname is the name of a file, nelem is the number of regions to pass
20 in the call, and sid is the session ID whose events you you are interested in.
21
22 ----------------------------------------------------------------------------*/
23
24 #ifndef linux
25 extern  char    *sys_errlist[];
26 #endif
27 extern  int     optind;
28 extern  char    *optarg;
29
30
31 char    *Progname;
32
33
34 static void
35 usage(void)
36 {
37         fprintf(stderr, "usage:\t%s [-n nelem] [-s sid] {pathname|handle}\n", Progname);
38         exit(1);
39 }
40
41
42 int
43 main(
44         int     argc, 
45         char    **argv)
46 {
47         dm_sessid_t     sid = DM_NO_SESSION;
48         dm_region_t     *regbufp = NULL;
49         char            *object = NULL;
50         u_int           nelemp;
51         u_int           nelem = 1;
52         void            *hanp;
53         size_t          hlen;
54         char            *name;
55         int             opt;
56         int             i;
57
58         Progname = strrchr(argv[0], '/');
59         if (Progname) {
60                 Progname++;
61         } else {
62                 Progname = argv[0];
63         }
64
65         /* Crack and validate the command line options. */
66
67         while ((opt = getopt(argc, argv, "n:s:")) != EOF) {
68                 switch (opt) {
69                 case 'n':
70                         nelem = atol(optarg);
71                         break;
72                 case 's':
73                         sid = atol(optarg);
74                         break;
75                 case '?':
76                         usage();
77                 }
78         }
79         if (optind + 1 != argc)
80                 usage();
81         object = argv[optind++];
82
83         if (dm_init_service(&name) == -1)  {
84                 fprintf(stderr, "Can't initialize the DMAPI\n");
85                 exit(1);
86         }
87         if (sid == DM_NO_SESSION)
88                 find_test_session(&sid);
89
90         /* Get the file's handle. */
91
92         if (opaque_to_handle(object, &hanp, &hlen)) {
93                 fprintf(stderr, "can't get handle for %s\n", object);
94                 exit(1);
95         }
96
97         if (nelem > 0) {
98                 if ((regbufp = calloc(nelem, sizeof(*regbufp))) == NULL) {
99                         fprintf(stderr, "calloc failed, %s\n", strerror(errno));
100                         exit(1);
101                 }
102         }
103
104         if (dm_get_region(sid, hanp, hlen, DM_NO_TOKEN, nelem, regbufp,
105             &nelemp)) {
106                 fprintf(stderr, "dm_get_region failed, %s\n",
107                         strerror(errno));
108                 exit(1);
109         }
110         fprintf(stdout, "%d regions\n", nelemp);
111
112         for (i = 0; i < nelemp; i++) {
113                 fprintf(stdout, "offset %lld, size %llu, flags 0x%x\n",
114                         (long long) regbufp[i].rg_offset,
115                         (unsigned long long) regbufp[i].rg_size,
116                         regbufp[i].rg_flags);
117         }
118
119         dm_handle_free(hanp, hlen);
120         exit(0);
121 }