Allow user to specify handles, rather than paths which the tool will
[xfstests-dev.git] / dmapi / src / suite1 / cmd / obj_ref_rele.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_obj_ref_rele().  The
42 command line is:
43
44         obj_ref_rele {-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 {pathname|handle}\n",
74                 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         char            *object;
87         void            *hanp;
88         size_t          hlen;
89         int             Fflag = 0;
90         char            *name;
91         int             opt;
92
93         if (Progname = strrchr(argv[0], '/')) {
94                 Progname++;
95         } else {
96                 Progname = argv[0];
97         }
98
99         /* Crack and validate the command line options. */
100
101         while ((opt = getopt(argc, argv, "Fs:")) != EOF) {
102                 switch (opt) {
103                 case 'F':
104                         Fflag++;
105                         break;
106                 case 's':
107                         sid = atol(optarg);
108                         break;
109                 case '?':
110                         usage();
111                 }
112         }
113         if (optind + 2 != argc)
114                 usage();
115         token = atol(argv[optind++]);
116         object = argv[optind];
117
118         if (dm_init_service(&name) == -1)  {
119                 fprintf(stderr, "Can't initialize the DMAPI\n");
120                 exit(1);
121         }
122         if (sid == DM_NO_SESSION)
123                 find_test_session(&sid);
124
125         /* Get the file or filesystem's handle. */
126
127         if (opaque_to_handle(object, &hanp, &hlen)) {
128                 fprintf(stderr, "can't get handle from %s\n", object);
129                 exit(1);
130         }
131         if (Fflag) {
132                 void    *fshanp;
133                 size_t  fshlen;
134
135                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
136                         fprintf(stderr, "can't get filesystem handle from %s\n",
137                                 object);
138                         exit(1);
139                 }
140                 dm_handle_free(hanp, hlen);
141                 hanp = fshanp;
142                 hlen = fshlen;
143         }
144
145         if (dm_obj_ref_rele(sid, token, hanp, hlen)) {
146                 fprintf(stderr, "dm_obj_ref_rele failed, %s\n",
147                         strerror(errno));
148                 return(1);
149         }
150
151         dm_handle_free(hanp, hlen);
152         exit(0);
153 }