8e2f4cbb75ceafecad728fe283d89e39d77afdaa
[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         int     i;
47
48         fprintf(stderr, "usage:\t%s [-b buflen] [-l loc] [-s sid] [-1] [-q] dirpath\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         dm_attrloc_t    loc = 0;
61         char            *dirpath;
62         char            buffer[100];
63         void            *bufp;
64         size_t          buflen = 10000;
65         u_int           mask;
66         size_t          rlenp;
67         void            *hanp;
68         size_t          hlen;
69         char            *name;
70         int             opt;
71         int             i;
72         int             ret;
73         int             oneline = 0;
74         int             quiet = 0;
75
76         if (Progname = strrchr(argv[0], '/')) {
77                 Progname++;
78         } else {
79                 Progname = argv[0];
80         }
81
82         /* Crack and validate the command line options. */
83
84         while ((opt = getopt(argc, argv, "b:l:s:1q")) != EOF) {
85                 switch (opt) {
86                 case 'b':
87                         buflen = atol(optarg);
88                         break;
89                 case 'l':
90                         loc = atol(optarg);
91                         break;
92                 case 's':
93                         sid = atol(optarg);
94                         break;
95                 case '1':
96                         oneline = 1;
97                         break;
98                 case 'q':
99                         quiet = 1;
100                         break;
101                 case '?':
102                         usage();
103                 }
104         }
105         if (optind + 1 != argc)
106                 usage();
107         dirpath = argv[optind++];
108
109         if (dm_init_service(&name) == -1)  {
110                 fprintf(stderr, "Can't initialize the DMAPI\n");
111                 exit(1);
112         }
113         if (sid == DM_NO_SESSION)
114                 find_test_session(&sid);
115
116         /* Get the directory's handle. */
117
118         if (dm_path_to_handle(dirpath, &hanp, &hlen)) {
119                 fprintf(stderr, "can't get handle for file %s, %s\n",
120                         dirpath, strerror(errno));
121                 exit(1);
122         }
123
124         if ((bufp = malloc(buflen == 0 ? 1 : buflen)) == NULL) {
125                 fprintf(stderr, "malloc failed, %s\n", strerror(errno));
126                 exit(1);
127         }
128
129         mask = DM_AT_HANDLE|DM_AT_EMASK|DM_AT_PMANR|DM_AT_PATTR|DM_AT_DTIME|DM_AT_CFLAG|DM_AT_STAT;
130
131         do {
132                 memset(bufp, 0, buflen);
133                 if ((ret = dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN, mask,
134                                 &loc, buflen, bufp, &rlenp)) < 0) {
135                         fprintf(stderr, "dm_get_dirattrs failed, %s\n",
136                                 strerror(errno));
137                         exit(1);
138                 }
139                 if (!quiet) {
140                         fprintf(stdout, "ret = %d, rlenp is %d, loc is %lld\n", ret,
141                                 rlenp, loc);
142                 }
143                 if (rlenp > 0) {
144                         dm_stat_t       *statp;
145
146                         statp = (dm_stat_t *)bufp;
147                         while (statp != NULL) {
148
149                                 hantoa((char *)statp + statp->dt_handle.vd_offset,
150                                         statp->dt_handle.vd_length, buffer);
151                                 if (oneline) {
152                                         fprintf(stdout, "%s %s\n",
153                                                 (char *)statp + statp->dt_compname.vd_offset,
154                                                 buffer);
155                                 }
156                                 else {
157                                         fprintf(stdout, "handle %s\n", buffer);
158                                         fprintf(stdout, "name %s\n",
159                                                 (char *)statp + statp->dt_compname.vd_offset);
160                                         print_line(statp);
161                                 }
162
163                                 statp = DM_STEP_TO_NEXT(statp, dm_stat_t *);
164                         }
165                 }
166                 else if ((ret == 1) && (rlenp == 0) && (!quiet)) {
167                         fprintf(stderr, "buflen is too short to hold anything\n");
168                         exit(1);
169                 }
170         } while (ret != 0);
171
172         dm_handle_free(hanp, hlen);
173         exit(0);
174 }