generic/60[78]: ensure the initial DAX file flag state before test
[xfstests-dev.git] / dmapi / src / suite1 / cmd / obj_ref_query.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 <getopt.h>
10 #include <string.h>
11
12
13 /*---------------------------------------------------------------------------
14
15 Test program used to test the DMAPI function dm_obj_ref_query().  The
16 command line is:
17
18         obj_ref_query {-F} [-s sid] token {pathname|handle}
19
20 where:
21 -F
22         when a pathname is specified, -F indicates that its filesystem handle
23         should be used rather than its file object handle.
24 sid
25         is the dm_sessid_t to use rather than the default test session.
26 token
27         is the dm_token_t to use.
28 {pathname|handle}
29         is either a handle, or is the pathname of a file whose handle is
30         to be used.
31
32 ----------------------------------------------------------------------------*/
33
34 #ifndef linux
35 extern  char    *sys_errlist[];
36 #endif
37 extern  int     optind;
38 extern  char    *optarg;
39
40
41 char    *Progname;
42
43
44 static void
45 usage(void)
46 {
47         fprintf(stderr, "usage:\t%s [-F] [-s sid] token {pathname|handle}\n",
48                 Progname);
49         exit(1);
50 }
51
52
53 int
54 main(
55         int     argc,
56         char    **argv)
57 {
58         dm_sessid_t     sid = DM_NO_SESSION;
59         dm_token_t      token;
60         char            *object;
61         void            *hanp;
62         size_t          hlen;
63         int             Fflag = 0;
64         char            *name;
65         int             error;
66         int             opt;
67
68         Progname = strrchr(argv[0], '/');
69         if (Progname) {
70                 Progname++;
71         } else {
72                 Progname = argv[0];
73         }
74
75         /* Crack and validate the command line options. */
76
77         while ((opt = getopt(argc, argv, "Fs:")) != EOF) {
78                 switch (opt) {
79                 case 'F':
80                         Fflag++;
81                         break;
82                 case 's':
83                         sid = atol(optarg);
84                         break;
85                 case '?':
86                         usage();
87                 }
88         }
89         if (optind + 2 != argc)
90                 usage();
91         token = atol(argv[optind++]);
92         object = argv[optind];
93
94         if (dm_init_service(&name) == -1)  {
95                 fprintf(stderr, "Can't initialize the DMAPI\n");
96                 exit(1);
97         }
98         if (sid == DM_NO_SESSION)
99                 find_test_session(&sid);
100
101         /* Get the file or filesystem's handle. */
102
103         if (opaque_to_handle(object, &hanp, &hlen)) {
104                 fprintf(stderr, "can't get handle from %s\n", object);
105                 exit(1);
106         }
107         if (Fflag) {
108                 void    *fshanp;
109                 size_t  fshlen;
110
111                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
112                         fprintf(stderr, "can't get filesystem handle from %s\n",
113                                 object);
114                         exit(1);
115                 }
116                 dm_handle_free(hanp, hlen);
117                 hanp = fshanp;
118                 hlen = fshlen;
119         }
120
121         if ((error = dm_obj_ref_query(sid, token, hanp, hlen)) < 0) {
122                 fprintf(stderr, "dm_obj_ref_query failed, %s\n",
123                         strerror(errno));
124                 return(1);
125         }
126
127         if (error == 1) {
128                 fprintf(stdout, "there is a hold\n");
129         } else {
130                 fprintf(stdout, "there is no hold\n");
131         }
132         dm_handle_free(hanp, hlen);
133         exit(0);
134 }