c0995470d9eda0f5900425abba3aede136288b12
[xfstests-dev.git] / dmapi / src / suite1 / cmd / obj_ref_hold.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 <getopt.h>
22 #include <string.h>
23
24
25 /*---------------------------------------------------------------------------
26
27 Test program used to test the DMAPI function dm_obj_ref_hold().  The
28 command line is:
29
30         obj_ref_hold {-F} [-s sid] token {pathname|handle}
31
32 where:
33 -F
34         when a pathname is specified, -F indicates that its filesystem handle
35         should be used rather than its file object handle.
36 sid
37         is the dm_sessid_t to use rather than the default test session.
38 token
39         is the dm_token_t to use.
40 {pathname|handle}
41         is either a handle, or is the pathname of a file whose handle is
42         to be used.
43
44 ----------------------------------------------------------------------------*/
45
46 #ifndef linux
47 extern  char    *sys_errlist[];
48 #endif
49 extern  int     optind;
50 extern  char    *optarg;
51
52
53 char    *Progname;
54
55
56 static void
57 usage(void)
58 {
59         fprintf(stderr, "usage:\t%s [-F] [-s sid] token {pathname|handle}\n",
60                 Progname);
61         exit(1);
62 }
63
64
65 int
66 main(
67         int     argc,
68         char    **argv)
69 {
70         dm_sessid_t     sid = DM_NO_SESSION;
71         dm_token_t      token;
72         char            *object;
73         void            *hanp;
74         size_t          hlen;
75         int             Fflag = 0;
76         char            *name;
77         int             opt;
78
79         if (Progname = strrchr(argv[0], '/')) {
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_obj_ref_hold(sid, token, hanp, hlen)) {
132                 fprintf(stderr, "dm_obj_ref_hold failed, %s\n",
133                         strerror(errno));
134                 return(1);
135         }
136
137         dm_handle_free(hanp, hlen);
138         exit(0);
139 }