c2473674069c5e07f3fe5868a24a5d95194f7856
[xfstests-dev.git] / dmapi / src / suite1 / cmd / get_dirattrs.c
1 /*
2  * Copyright (c) 2000 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 /*---------------------------------------------------------------------------
36
37 Test program used to test the DMAPI function dm_get_dirattrs().  The
38 command line is:
39
40         get_dirattrs [-b buflen] [-l loc] [-s sid] dirpath
41
42 where dirpath is the name of a directory, buflen is the size of the buffer
43 to use in the call, loc is a starting location, and sid is the session ID
44 whose attributes you are interested in.
45
46 ----------------------------------------------------------------------------*/
47
48 extern  char    *sys_errlist[];
49 extern  int     optind;
50 extern  char    *optarg;
51
52
53 char    *Progname;
54
55 static void
56 usage(void)
57 {
58         int     i;
59
60         fprintf(stderr, "usage:\t%s [-b buflen] [-l loc] [-s sid] dirpath\n",
61                 Progname);
62         exit(1);
63 }
64
65
66 int
67 main(
68         int     argc, 
69         char    **argv)
70 {
71         dm_sessid_t     sid = DM_NO_SESSION;
72         dm_attrloc_t    loc = 0;
73         char            *dirpath;
74         char            buffer[100];
75         void            *bufp;
76         size_t          buflen = 10000;
77         u_int           mask;
78         size_t          rlenp;
79         void            *hanp;
80         size_t          hlen;
81         char            *name;
82         int             error;
83         int             opt;
84         int             i;
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:l:s:")) != EOF) {
95                 switch (opt) {
96                 case 'b':
97                         buflen = atol(optarg);
98                         break;
99                 case 'l':
100                         loc = atol(optarg);
101                         break;
102                 case 's':
103                         sid = atol(optarg);
104                         break;
105                 case '?':
106                         usage();
107                 }
108         }
109         if (optind + 1 != argc)
110                 usage();
111         dirpath = argv[optind++];
112
113         if (dm_init_service(&name) == -1)  {
114                 fprintf(stderr, "Can't inititalize the DMAPI\n");
115                 exit(1);
116         }
117         if (sid == DM_NO_SESSION)
118                 find_test_session(&sid);
119
120         /* Get the diretory's handle. */
121
122         if (dm_path_to_handle(dirpath, &hanp, &hlen)) {
123                 fprintf(stderr, "can't get handle for file %s, %s\n",
124                         dirpath, strerror(errno));
125                 exit(1);
126         }
127
128         if ((bufp = malloc(buflen == 0 ? 1 : buflen)) == NULL) {
129                 fprintf(stderr, "malloc failed, %s\n", strerror(errno));
130                 exit(1);
131         }
132
133         mask = DM_AT_HANDLE|DM_AT_EMASK|DM_AT_PMANR|DM_AT_PATTR|DM_AT_DTIME|DM_AT_CFLAG|DM_AT_STAT;
134
135         if ((error = dm_get_dirattrs(sid, hanp, hlen, DM_NO_TOKEN, mask,
136                         &loc, buflen, bufp, &rlenp)) < 0) {
137                 if (errno == E2BIG) {
138                         fprintf(stderr, "dm_get_dirattrs buffer too small, "
139                                 "should be %d bytes\n", rlenp);
140                 } else {
141                         fprintf(stderr, "dm_get_dirattrs failed, %s\n",
142                                 strerror(errno));
143                 }
144                 exit(1);
145         }
146         fprintf(stdout, "rc = %d, rlenp is %d, loc is %lld\n", error,
147                 rlenp, loc);
148         if (rlenp > 0) {
149                 dm_stat_t       *statp;
150
151                 statp = (dm_stat_t *)bufp;
152                 while (statp != NULL) {
153
154                         hantoa((char *)statp + statp->dt_handle.vd_offset,
155                                 statp->dt_handle.vd_length, buffer);
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                         statp = DM_STEP_TO_NEXT(statp, dm_stat_t *);
162                 }
163         }
164
165         dm_handle_free(hanp, hlen);
166         exit(0);
167 }