allow it to accept the text form of a file handle
[xfstests-dev.git] / dmapi / src / suite1 / cmd / dm_handle.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 <sys/types.h>
34 #include <sys/stat.h>
35
36 #include <fcntl.h>
37 #include <unistd.h>
38
39 #include <lib/hsm.h>
40
41 #include <string.h>
42
43 /*---------------------------------------------------------------------------
44
45 Test program used to test all the DMAPI functions in dm_handle.c.  The
46 command line is:
47
48         dm_handle pathname
49
50 where pathname is the name of a file.  If any function fails, an error message
51 containing the work ERROR will be written to stderr.
52
53 Tested DMAPI functions are:
54         dm_fd_to_handle
55         dm_handle_cmp
56         dm_handle_free
57         dm_handle_hash
58         dm_handle_is_valid
59         dm_handle_to_fshandle
60         dm_handle_to_fsid
61         dm_handle_to_ino
62         dm_handle_to_igen
63         dm_make_handle
64         dm_make_fshandle
65         dm_path_to_handle
66         dm_path_to_fshandle
67
68 ----------------------------------------------------------------------------*/
69
70
71 char    *Progname;
72
73
74
75 int
76 main(
77         int             argc,
78         char            **argv)
79 {
80         char            *pathname;
81         char            *name;
82         void            *hanp1, *hanp2, *hanp3, *fshanp1, *fshanp2, *fshanp3;
83         size_t          hlen1, hlen2, hlen3, fshlen1, fshlen2, fshlen3;
84         u_int           hash1, hash2, hash3, fshash1, fshash2, fshash3;
85         dm_fsid_t       fsid;
86         dm_ino_t        ino;
87         dm_igen_t       igen;
88         char            buffer[100];
89         char            buffer1[100];
90         char            fsbuffer1[100];
91         char            buffer2[100];
92         char            fsbuffer2[100];
93         char            buffer3[100];
94         char            fsbuffer3[100];
95         int             fd;
96
97         if (Progname = strrchr(argv[0], '/')) {
98                 Progname++;
99         } else {
100                 Progname = argv[0];
101         }
102
103         if (argc != 2) {
104                 fprintf(stderr, "usage: %s path\n", argv[0]);
105                 exit(1);
106         }
107         pathname = argv[1];
108
109         (void)dm_init_service(&name);
110
111         if (dm_path_to_handle(pathname, &hanp1, &hlen1) != 0) {
112                 fprintf(stderr, "dm_path_to_handle failed, %s\n",
113                         strerror(errno));
114                 exit(1);
115         }
116         hash1 = dm_handle_hash(hanp1, hlen1);
117         hantoa(hanp1, hlen1, buffer1);
118         fprintf(stdout, "  han1:  hash %u  value %s  (dm_path_to_handle)\n",
119                 hash1, buffer1);
120         if (dm_handle_is_valid(hanp1, hlen1) == DM_FALSE) {
121                 fprintf(stderr, "ERROR: han1 is not valid\n");
122         }
123
124         if (dm_path_to_fshandle(pathname, &fshanp1, &fshlen1) != 0) {
125                 fprintf(stderr, "dm_path_to_fshandle failed, %s\n",
126                         strerror(errno));
127                 exit(1);
128         }
129         fshash1 = dm_handle_hash(fshanp1, fshlen1);
130         hantoa(fshanp1, fshlen1, fsbuffer1);
131         fprintf(stdout, "fshan1:  hash %u  value %s  (dm_path_to_fshandle\n",
132                 fshash1, fsbuffer1);
133         if (dm_handle_is_valid(fshanp1, fshlen1) == DM_FALSE) {
134                 fprintf(stderr, "ERROR: fshan1 is not valid\n");
135         }
136
137         if ((fd = open(pathname, O_RDONLY)) < 0) {
138                 fprintf(stderr, "open of %s failed, %s\n", pathname,
139                         strerror(errno));
140                 exit(1);
141         }
142         if (dm_fd_to_handle(fd, &hanp2, &hlen2) != 0) {
143                 fprintf(stderr, "dm_fd_to_handle failed, %s\n",
144                         strerror(errno));
145                 exit(1);
146         }
147         (void)close(fd);
148         hash2 = dm_handle_hash(hanp2, hlen2);
149         hantoa(hanp2, hlen2, buffer2);
150         fprintf(stdout, "  han2:  hash %u  value %s  (dm_fd_to_handle)\n",
151                 hash2, buffer2);
152         if (dm_handle_is_valid(hanp2, hlen2) == DM_FALSE) {
153                 fprintf(stderr, "ERROR: han2 is not valid\n");
154         }
155
156         if (dm_handle_to_fshandle(hanp2, hlen2, &fshanp2, &fshlen2) != 0) {
157                 fprintf(stderr, "dm_handle_to_fshandle failed, %s\n",
158                         strerror(errno));
159                 exit(1);
160         }
161         fshash2 = dm_handle_hash(fshanp2, fshlen2);
162         hantoa(fshanp2, fshlen2, fsbuffer2);
163         fprintf(stdout, "fshan2:  hash %u  value %s  (dm_handle_to_fshandle)\n",
164                 fshash2, fsbuffer2);
165         if (dm_handle_is_valid(fshanp2, fshlen2) == DM_FALSE) {
166                 fprintf(stderr, "ERROR: fshan2 is not valid\n");
167         }
168
169         if (dm_handle_cmp(hanp1, hlen1, hanp2, hlen2)) {
170                 fprintf(stderr, "ERROR: han1 and han2 differ in dm_handle_cmp\n");
171         }
172         if (strcmp(buffer1, buffer2)) {
173                 fprintf(stderr, "ERROR: han1 and han2 differ in strcmp\n");
174         }
175         if (hash1 != hash2) {
176                 fprintf(stderr, "ERROR: hash1 and hash2 differ\n");
177         }
178
179         if (dm_handle_cmp(fshanp1, fshlen1, fshanp2, fshlen2)) {
180                 fprintf(stderr, "ERROR: fshan1 and fshan2 differ in dm_handle_cmp\n");
181         }
182         if (strcmp(fsbuffer1, fsbuffer2)) {
183                 fprintf(stderr, "ERROR: fshan1 and fshan2 differ in strcmp\n");
184         }
185         if (fshash1 != fshash2) {
186                 fprintf(stderr, "ERROR: fshash1 and fshash2 differ\n");
187         }
188
189         /* Break the handle into its component parts and display them.  Use
190            hantoa() instead of printing the parts directly because some are
191            32 bits on Veritas and 64 bits on SGI.
192         */
193
194         if (dm_handle_to_fsid(hanp1, hlen1, &fsid) != 0) {
195                 fprintf(stderr, "dm_handle_to_fsid failed, %s\n",
196                         strerror(errno));
197                 exit(1);
198         }
199         hantoa(&fsid, sizeof(fsid), buffer);
200         fprintf(stdout, "fsid  %s       (dm_handle_to_fsid)\n", buffer);
201
202         if (dm_handle_to_ino(hanp1, hlen1, &ino) != 0) {
203                 fprintf(stderr, "dm_handle_to_ino failed, %s\n",
204                         strerror(errno));
205                 exit(1);
206         }
207         hantoa(&ino, sizeof(ino), buffer);
208         fprintf(stdout, "ino   %s       (dm_handle_to_ino)\n", buffer);
209
210         if (dm_handle_to_igen(hanp1, hlen1, &igen) != 0) {
211                 fprintf(stderr, "dm_handle_to_igen failed, %s\n",
212                         strerror(errno));
213                 exit(1);
214         }
215         hantoa(&igen, sizeof(igen), buffer);
216         fprintf(stdout, "igen  %s       (dm_handle_to_igen)\n", buffer);
217
218         /* Now use the parts to remake the handle and verify we get the same
219            answer.
220         */
221
222         if (dm_make_handle(&fsid, &ino, &igen, &hanp3, &hlen3) != 0) {
223                 fprintf(stderr, "dm_make_handle failed, %s\n",
224                         strerror(errno));
225                 exit(1);
226         }
227         hash3 = dm_handle_hash(hanp3, hlen3);
228         hantoa(hanp3, hlen3, buffer3);
229         fprintf(stdout, "  han3:  hash %u  value %s  (dm_make_handle)\n",
230                 hash3, buffer3);
231         if (dm_handle_is_valid(hanp3, hlen3) == DM_FALSE) {
232                 fprintf(stderr, "ERROR: han3 is not valid\n");
233         }
234
235         if (dm_handle_cmp(hanp1, hlen1, hanp3, hlen3)) {
236                 fprintf(stderr, "ERROR: hanp1 and hanp3 differ in dm_handle_cmp\n");
237         }
238         if (strcmp(buffer1, buffer3)) {
239                 fprintf(stderr, "ERROR: hanp1 and hanp3 differ in strcmp\n");
240         }
241         if (hash1 != hash3) {
242                 fprintf(stderr, "ERROR: hash1 and hash3 differ\n");
243         }
244
245         if (dm_make_fshandle(&fsid, &fshanp3, &fshlen3) != 0) {
246                 fprintf(stderr, "dm_make_fshandle failed, %s\n",
247                         strerror(errno));
248                 exit(1);
249         }
250         fshash3 = dm_handle_hash(fshanp3, fshlen3);
251         hantoa(fshanp3, fshlen3, fsbuffer3);
252         fprintf(stdout, "fshan3:  hash %u  value %s  (dm_make_fshandle)\n",
253                 fshash3, fsbuffer3);
254         if (dm_handle_is_valid(fshanp3, fshlen3) == DM_FALSE) {
255                 fprintf(stderr, "ERROR: fshan3 is not valid\n");
256         }
257
258         if (dm_handle_cmp(fshanp1, fshlen1, fshanp3, fshlen3)) {
259                 fprintf(stderr, "ERROR: fshan1 and fshan3 differ in dm_handle_cmp\n");
260         }
261         if (strcmp(fsbuffer1, fsbuffer3)) {
262                 fprintf(stderr, "ERROR: fshan1 and fshan3 differ in strcmp\n");
263         }
264         if (fshash1 != fshash3) {
265                 fprintf(stderr, "ERROR: fshash1 and fshash3 differ\n");
266         }
267
268         dm_handle_free(hanp1, hlen1);
269         dm_handle_free(hanp2, hlen2);
270         dm_handle_free(hanp3, hlen3);
271         dm_handle_free(fshanp1, fshlen1);
272         dm_handle_free(fshanp2, fshlen2);
273         dm_handle_free(fshanp3, fshlen3);
274         exit(0);
275 }