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