merge irix dmapi test changes
[xfstests-dev.git] / dmapi / src / suite1 / cmd / query_right.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include <lib/hsm.h>
34
35 #include <getopt.h>
36 #include <string.h>
37
38
39 /*---------------------------------------------------------------------------
40
41 Test program used to test the DMAPI function dm_query_right().  The
42 command line is:
43
44         query_right [-F] [-s sid] token {pathname|handle}
45
46 where
47 -F
48         when a pathname is specified, -F indicates that its filesystem handle
49         should be used rather than its file object handle.
50 sid
51         is the dm_sessid_t to use rather than the default test session.
52 token
53         is the dm_token_t to use.
54 {pathname|handle}
55         is either a handle, or is the pathname of a file whose handle is
56         to be used.
57
58 ----------------------------------------------------------------------------*/
59
60 #ifndef linux
61 extern  char    *sys_errlist[];
62 #endif
63 extern  int     optind;
64 extern  char    *optarg;
65
66
67 char    *Progname;
68
69
70 static void
71 usage(void)
72 {
73         fprintf(stderr, "usage:\t%s [-F] [-s sid] token "
74                 "{pathname|handle}\n", Progname);
75         exit(1);
76 }
77
78
79 int
80 main(
81         int     argc,
82         char    **argv)
83 {
84         dm_sessid_t     sid = DM_NO_SESSION;
85         dm_token_t      token;
86         dm_right_t      right;
87         char            *object;
88         void            *hanp;
89         size_t          hlen;
90         int             Fflag = 0;
91         char            *name;
92         int             opt;
93
94         if (Progname = strrchr(argv[0], '/')) {
95                 Progname++;
96         } else {
97                 Progname = argv[0];
98         }
99
100         /* Crack and validate the command line options. */
101
102         while ((opt = getopt(argc, argv, "Fs:")) != EOF) {
103                 switch (opt) {
104                 case 'F':
105                         Fflag++;
106                         break;
107                 case 's':
108                         sid = atol(optarg);
109                         break;
110                 case '?':
111                         usage();
112                 }
113         }
114         if (optind + 2 != argc)
115                 usage();
116         token = atol(argv[optind++]);
117         object = argv[optind++];
118
119         if (dm_init_service(&name) == -1)  {
120                 fprintf(stderr, "Can't inititalize the DMAPI\n");
121                 exit(1);
122         }
123         if (sid == DM_NO_SESSION)
124                 find_test_session(&sid);
125
126         /* Get the file or filesystem's handle. */
127
128         if (opaque_to_handle(object, &hanp, &hlen)) {
129                 fprintf(stderr, "can't get handle from %s\n", object);
130                 exit(1);
131         }
132         if (Fflag) {
133                 void    *fshanp;
134                 size_t  fshlen;
135
136                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
137                         fprintf(stderr, "can't get filesystem handle from %s\n",
138                                 object);
139                         exit(1);
140                 }
141                 dm_handle_free(hanp, hlen);
142                 hanp = fshanp;
143                 hlen = fshlen;
144         }
145
146         if (dm_query_right(sid, hanp, hlen, token, &right)) {
147                 fprintf(stderr, "dm_query_right failed, %s\n",
148                         strerror(errno));
149                 return(1);
150         }
151
152         fprintf(stderr, "right is %s\n", rt_value_to_name(right));
153
154         dm_handle_free(hanp, hlen);
155         exit(0);
156 }