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