xfstests: eliminate warnings under dmapi/src/suite1/cmd (1)
[xfstests-dev.git] / dmapi / src / suite1 / cmd / get_dirattrs.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 /*---------------------------------------------------------------------------
22
23 Test program used to test the DMAPI function dm_get_dirattrs().  The
24 command line is:
25
26         get_dirattrs [-b buflen] [-l loc] [-s sid] dirpath
27
28 where dirpath is the name of a directory, buflen is the size of the buffer
29 to use in the call, loc is a starting location, and sid is the session ID
30 whose attributes you are interested in.
31
32 ----------------------------------------------------------------------------*/
33
34 #ifndef linux
35 extern  char    *sys_errlist[];
36 #endif
37 extern  int     optind;
38 extern  char    *optarg;
39
40
41 char    *Progname;
42
43 static void
44 usage(void)
45 {
46         fprintf(stderr, "usage:\t%s [-b buflen] [-l loc] [-s sid] [-1] [-q] dirpath\n",
47                 Progname);
48         exit(1);
49 }
50
51
52 int
53 main(
54         int     argc, 
55         char    **argv)
56 {
57         dm_sessid_t     sid = DM_NO_SESSION;
58         dm_attrloc_t    loc = 0;
59         char            *dirpath;
60         char            buffer[100];
61         void            *bufp;
62         size_t          buflen = 10000;
63         u_int           mask;
64         size_t          rlenp;
65         void            *hanp;
66         size_t          hlen;
67         char            *name;
68         int             opt;
69         int             ret;
70         int             oneline = 0;
71         int             quiet = 0;
72
73         Progname = strrchr(argv[0], '/');
74         if (Progname) {
75                 Progname++;
76         } else {
77                 Progname = argv[0];
78         }
79
80         /* Crack and validate the command line options. */
81
82         while ((opt = getopt(argc, argv, "b:l:s:1q")) != EOF) {
83                 switch (opt) {
84                 case 'b':
85                         buflen = atol(optarg);
86                         break;
87                 case 'l':
88                         loc = atol(optarg);
89                         break;
90                 case 's':
91                         sid = atol(optarg);
92                         break;
93                 case '1':
94                         oneline = 1;
95                         break;
96                 case 'q':
97                         quiet = 1;
98                         break;
99                 case '?':
100                         usage();
101                 }
102         }
103         if (optind + 1 != argc)
104                 usage();
105         dirpath = argv[optind++];
106
107         if (dm_init_service(&name) == -1)  {
108                 fprintf(stderr, "Can't initialize the DMAPI\n");
109                 exit(1);
110         }
111         if (sid == DM_NO_SESSION)
112                 find_test_session(&sid);
113
114         /* Get the directory's handle. */
115
116         if (dm_path_to_handle(dirpath, &hanp, &hlen)) {
117                 fprintf(stderr, "can't get handle for file %s, %s\n",
118                         dirpath, strerror(errno));
119                 exit(1);
120         }
121
122         if ((bufp = malloc(buflen == 0 ? 1 : buflen)) == NULL) {
123                 fprintf(stderr, "malloc failed, %s\n", strerror(errno));
124                 exit(1);
125         }
126
127         mask = DM_AT_HANDLE|DM_AT_EMASK|DM_AT_PMANR|DM_AT_PATTR|DM_AT_DTIME|DM_AT_CFLAG|DM_AT_STAT;
128
129         do {
130                 memset(bufp, 0, buflen);
131                 if ((ret = dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN, mask,
132                                 &loc, buflen, bufp, &rlenp)) < 0) {
133                         fprintf(stderr, "dm_get_dirattrs failed, %s\n",
134                                 strerror(errno));
135                         exit(1);
136                 }
137                 if (!quiet) {
138                         fprintf(stdout, "ret = %d, rlenp is %zd, loc is %lld\n",
139                                 ret, rlenp, (long long) loc);
140                 }
141                 if (rlenp > 0) {
142                         dm_stat_t       *statp;
143
144                         statp = (dm_stat_t *)bufp;
145                         while (statp != NULL) {
146
147                                 hantoa((char *)statp + statp->dt_handle.vd_offset,
148                                         statp->dt_handle.vd_length, buffer);
149                                 if (oneline) {
150                                         fprintf(stdout, "%s %s\n",
151                                                 (char *)statp + statp->dt_compname.vd_offset,
152                                                 buffer);
153                                 }
154                                 else {
155                                         fprintf(stdout, "handle %s\n", buffer);
156                                         fprintf(stdout, "name %s\n",
157                                                 (char *)statp + statp->dt_compname.vd_offset);
158                                         print_line(statp);
159                                 }
160
161                                 statp = DM_STEP_TO_NEXT(statp, dm_stat_t *);
162                         }
163                 }
164                 else if ((ret == 1) && (rlenp == 0) && (!quiet)) {
165                         fprintf(stderr, "buflen is too short to hold anything\n");
166                         exit(1);
167                 }
168         } while (ret != 0);
169
170         dm_handle_free(hanp, hlen);
171         exit(0);
172 }