a03180da21c7cdaaa503e80375c6e686e9e5970f
[xfstests-dev.git] / dmapi / src / suite1 / cmd / get_dmattr.c
1 /*
2  * Copyright (c) 2000-2001 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 <string.h>
36 #include <getopt.h>
37
38 /*---------------------------------------------------------------------------
39
40 Test program used to test the DMAPI function dm_get_dmattr().  The
41 command line is:
42
43         get_dmattr [-b buflen] [-s sid] [-t token] {pathname|handle} attr
44
45 where pathname is the name of a file, buflen is the size of the buffer to use
46 in the call, attr is the name of the DMAPI attribute, and sid is the session ID
47 whose attributes you you are interested in.
48
49 ----------------------------------------------------------------------------*/
50
51 #ifndef linux
52 extern  char    *sys_errlist[];
53 #endif
54 extern  int     optind;
55 extern  char    *optarg;
56
57
58 char    *Progname;
59
60 static void
61 usage(void)
62 {
63         fprintf(stderr, "usage:\t%s [-b buflen] [-s sid] [-t token] "
64                 "{pathname|handle} attr\n", Progname);
65         exit(1);
66 }
67
68
69 int
70 main(
71         int     argc, 
72         char    **argv)
73 {
74         dm_sessid_t     sid = DM_NO_SESSION;
75         dm_token_t      token = DM_NO_TOKEN;
76         char            *object;
77         dm_attrname_t   *attrnamep;
78         void            *bufp;
79         size_t          buflen = 10000;
80         size_t          rlenp;
81         void            *hanp;
82         size_t           hlen;
83         char            *name;
84         int             opt;
85
86         if (Progname = strrchr(argv[0], '/')) {
87                 Progname++;
88         } else {
89                 Progname = argv[0];
90         }
91
92         /* Crack and validate the command line options. */
93
94         while ((opt = getopt(argc, argv, "b:s:t:")) != EOF) {
95                 switch (opt) {
96                 case 'b':
97                         buflen = atol(optarg);
98                         break;
99                 case 's':
100                         sid = atol(optarg);
101                         break;
102                 case 't':
103                         token = atol(optarg);
104                         break;
105                 case '?':
106                         usage();
107                 }
108         }
109         if (optind + 2 != argc)
110                 usage();
111         object = argv[optind++];
112         attrnamep = (dm_attrname_t *)argv[optind];
113
114         if (dm_init_service(&name) == -1)  {
115                 fprintf(stderr, "Can't initialize the DMAPI\n");
116                 exit(1);
117         }
118         if (sid == DM_NO_SESSION)
119                 find_test_session(&sid);
120
121         /* Get the file's handle. */
122
123         if (opaque_to_handle(object, &hanp, &hlen)) {
124                 fprintf(stderr, "can't get handle for %s\n", object);
125                 exit(1);
126         }
127
128         if (buflen > 0) {
129                 if ((bufp = malloc(buflen)) == NULL) {
130                         fprintf(stderr, "malloc failed, %s\n", strerror(errno));
131                         exit(1);
132                 }
133         }
134
135         if (dm_get_dmattr(sid, hanp, hlen, token, attrnamep, buflen,
136             bufp, &rlenp)) {
137                 if (errno == E2BIG) {
138                         fprintf(stderr, "dm_get_dmattr buffer too small, "
139                                 "should be %d bytes\n", rlenp);
140                 } else {
141                         fprintf(stderr, "dm_get_dmattr failed, %s\n",
142                                 strerror(errno));
143                 }
144                 exit(1);
145         }
146         fprintf(stdout, "rlenp is %d, value is '%s'\n", rlenp, (char*)bufp);
147
148         dm_handle_free(hanp, hlen);
149         exit(0);
150 }