generic/60[78]: ensure the initial DAX file flag state before test
[xfstests-dev.git] / dmapi / src / suite1 / cmd / obj_ref_hold.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_hold().  The
16 command line is:
17
18         obj_ref_hold {-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             opt;
66
67         Progname = strrchr(argv[0], '/');
68         if (Progname) {
69                 Progname++;
70         } else {
71                 Progname = argv[0];
72         }
73
74         /* Crack and validate the command line options. */
75
76         while ((opt = getopt(argc, argv, "Fs:")) != EOF) {
77                 switch (opt) {
78                 case 'F':
79                         Fflag++;
80                         break;
81                 case 's':
82                         sid = atol(optarg);
83                         break;
84                 case '?':
85                         usage();
86                 }
87         }
88         if (optind + 2 != argc)
89                 usage();
90         token = atol(argv[optind++]);
91         object = argv[optind];
92
93         if (dm_init_service(&name) == -1)  {
94                 fprintf(stderr, "Can't initialize the DMAPI\n");
95                 exit(1);
96         }
97         if (sid == DM_NO_SESSION)
98                 find_test_session(&sid);
99
100         /* Get the file or filesystem's handle. */
101
102         if (opaque_to_handle(object, &hanp, &hlen)) {
103                 fprintf(stderr, "can't get handle from %s\n", object);
104                 exit(1);
105         }
106         if (Fflag) {
107                 void    *fshanp;
108                 size_t  fshlen;
109
110                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
111                         fprintf(stderr, "can't get filesystem handle from %s\n",
112                                 object);
113                         exit(1);
114                 }
115                 dm_handle_free(hanp, hlen);
116                 hanp = fshanp;
117                 hlen = fshlen;
118         }
119
120         if (dm_obj_ref_hold(sid, token, hanp, hlen)) {
121                 fprintf(stderr, "dm_obj_ref_hold failed, %s\n",
122                         strerror(errno));
123                 return(1);
124         }
125
126         dm_handle_free(hanp, hlen);
127         exit(0);
128 }