dmapi: fix build failure if libdm is provided via -ldm
[xfstests-dev.git] / dmapi / src / suite1 / cmd / downgrade_right.c
1 /*
2  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <lib/hsm.h>
20
21 #include <string.h>
22 #include <getopt.h>
23
24 /*---------------------------------------------------------------------------
25
26 Test program used to test the DMAPI function dm_downgrade_right().  The
27 command line is:
28
29         downgrade_right {-F} [-s sid] token {pathname|handle}
30
31 where:
32 -F
33         when a pathname is specified, -F indicates that its filesystem handle
34         should be used rather than its file object handle.
35 sid
36         is the dm_sessid_t to use rather than the default test session.
37 token
38         is the dm_token_t to use.
39 {pathname|handle}
40         is either a handle, or is the pathname of a file whose handle is
41         to be used.
42
43 ----------------------------------------------------------------------------*/
44
45 #ifndef linux
46 extern  char    *sys_errlist[];
47 #endif
48 extern  int     optind;
49 extern  char    *optarg;
50
51
52 char    *Progname;
53
54
55 static void
56 usage(void)
57 {
58         fprintf(stderr, "usage:\t%s [-F] [-s sid] token {pathname|handle}\n",
59                 Progname);
60         exit(1);
61 }
62
63
64 int
65 main(
66         int     argc,
67         char    **argv)
68 {
69         dm_sessid_t     sid = DM_NO_SESSION;
70         dm_token_t      token;
71         char            *object;
72         void            *hanp;
73         size_t          hlen;
74         int             Fflag = 0;
75         char            *name;
76         int             opt;
77
78         Progname = strrchr(argv[0], '/');
79         if (Progname) {
80                 Progname++;
81         } else {
82                 Progname = argv[0];
83         }
84
85         /* Crack and validate the command line options. */
86
87         while ((opt = getopt(argc, argv, "Fs:")) != EOF) {
88                 switch (opt) {
89                 case 'F':
90                         Fflag++;
91                         break;
92                 case 's':
93                         sid = atol(optarg);
94                         break;
95                 case '?':
96                         usage();
97                 }
98         }
99         if (optind + 2 != argc)
100                 usage();
101         token = atol(argv[optind++]);
102         object = argv[optind];
103
104         if (dm_init_service(&name) == -1)  {
105                 fprintf(stderr, "Can't initialize the DMAPI\n");
106                 exit(1);
107         }
108         if (sid == DM_NO_SESSION)
109                 find_test_session(&sid);
110
111         /* Get the file or filesystem's handle. */
112
113         if (opaque_to_handle(object, &hanp, &hlen)) {
114                 fprintf(stderr, "can't get handle from %s\n", object);
115                 exit(1);
116         }
117         if (Fflag) {
118                 void    *fshanp;
119                 size_t  fshlen;
120
121                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
122                         fprintf(stderr, "can't get filesystem handle from %s\n",
123                                 object);
124                         exit(1);
125                 }
126                 dm_handle_free(hanp, hlen);
127                 hanp = fshanp;
128                 hlen = fshlen;
129         }
130
131         if (dm_downgrade_right(sid, hanp, hlen, token)) {
132                 fprintf(stderr, "dm_downgrade_right failed, %s\n",
133                         strerror(errno));
134                 return(1);
135         }
136
137         dm_handle_free(hanp, hlen);
138         exit(0);
139 }