c18cc5914d26337a4e880126ca21f7fdbda06123
[xfstests-dev.git] / dmapi / src / common / cmd / set_return_on_destroy.c
1 /*
2  * Copyright (c) 2000-2002 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_set_return_on_destroy().  The
27 command line is:
28
29         set_return_on_destroy [-F] [-s sid] [-t token] {pathname|fshandle} [attr]
30
31 where pathname is the name of a file which resides in the filesystem of
32 interest.  attr is the name of the DMAPI attribute; if none is specified,
33 then set-return-on-destroy will be disabled for the filesystem.
34 sid is the session ID whose attribute you are interested in setting.
35
36 Use -F if you want the program to find the fshandle based on the pathname.
37
38 ----------------------------------------------------------------------------*/
39
40 #ifndef linux
41 extern  char    *sys_errlist[];
42 #endif
43 extern  int     optind;
44 extern  char    *optarg;
45
46
47 char    *Progname;
48
49 static void
50 usage(void)
51 {
52         fprintf(stderr, "usage:\t%s [-F] [-s sid] [-t token] {pathname|fshandle} [attr]\n", Progname);
53         exit(1);
54 }
55
56
57 int
58 main(
59         int     argc, 
60         char    **argv)
61 {
62         dm_sessid_t     sid = DM_NO_SESSION;
63         dm_token_t      token = DM_NO_TOKEN;
64         char            *pathname;
65         dm_attrname_t   *attrnamep = NULL;
66         dm_boolean_t    enable = DM_FALSE;
67         void            *hanp;
68         size_t          hlen;
69         char            *name;
70         int             opt;
71         int             Fflag = 0;
72
73         if (Progname = strrchr(argv[0], '/')) {
74                 Progname++;
75         } else {
76                 Progname = argv[0];
77         }
78
79         /* Crack and validate the command line options. */
80
81         while ((opt = getopt(argc, argv, "Fs:t:")) != EOF) {
82                 switch (opt) {
83                 case 's':
84                         sid = atol(optarg);
85                         break;
86                 case 't':
87                         token = atol(optarg);
88                         break;
89                 case 'F':
90                         Fflag++;
91                         break;
92                 case '?':
93                         usage();
94                 }
95         }
96         if (optind == argc || optind + 2 < argc)
97                 usage();
98         pathname = argv[optind++];
99         if (optind < argc) {
100                 enable = DM_TRUE;
101                 attrnamep = (dm_attrname_t *)argv[optind++];
102         }
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         if (opaque_to_handle(pathname, &hanp, &hlen)) {
112                 fprintf(stderr, "can't get handle for %s\n", pathname);
113                 exit(1);
114         }
115
116         /* Get the file's handle. */
117
118         if (Fflag) {
119                 void *fshanp;
120                 size_t fshlen;
121
122                 if (dm_handle_to_fshandle(hanp, hlen, &fshanp, &fshlen)) {
123                         fprintf(stderr, "can't get filesystem handle for file %s, %s\n",
124                                 pathname, strerror(errno));
125                         exit(1);
126                 }
127                 dm_handle_free(hanp, hlen);
128                 hanp = fshanp;
129                 hlen = fshlen;
130         }
131
132         if (dm_set_return_on_destroy(sid, hanp, hlen, token,
133             attrnamep, enable)) {
134                 fprintf(stderr, "dm_set_return_on_destroy failed, %s\n",
135                         strerror(errno));
136                 exit(1);
137         }
138
139         dm_handle_free(hanp, hlen);
140         exit(0);
141 }