allow it to accept the text form of a file handle
[xfstests-dev.git] / dmapi / src / suite1 / cmd / handle_to_path.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 <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <lib/dmport.h>
39
40 #include <getopt.h>
41 #ifdef linux
42 #include <xfs/xfs_fs.h>
43 #include <xfs/handle.h>
44 #endif
45
46 /*---------------------------------------------------------------------------
47
48 Test program used to test the DMAPI function dm_handle_to_path().  The
49 command line is:
50
51         handle_to_path [-b buflen] dirpath objpath
52
53 There are two parameters.  The first is the pathname of a directory,
54 and the second is the pathname of a file, directory, or symbolic link
55 within that directory.  The second parameter can also be the same as
56 the first if you want to specify "." (this is how EMASS uses it).
57 Pathnames can either be relative or full.
58
59 buflen is the size of the buffer to use in the call.
60
61 This program will return the full pathname of the object which is the
62 second parameter using the dm_handle_to_path() function.
63
64 The program should work successfully for files, directories, and
65 symbolic links, and does not have to work for any other type of
66 object.  It doesn't have to work across mount points.  There shouldn't
67 be any "/." crud on the end of the returned pathname either.
68
69 ----------------------------------------------------------------------------*/
70
71 extern  int     optind;
72 extern  char    *optarg;
73
74
75 char    *Progname;
76
77
78 static void
79 usage(void)
80 {
81         fprintf(stderr, "usage:\t%s [-b buflen] dirpath objpath\n", Progname);
82         exit(1);
83 }
84
85
86 int
87 main(
88         int             argc,
89         char            **argv)
90 {
91         char            *dirpath;
92         char            *objpath;
93         void            *hanp1, *hanp2, *hanp1a;
94         size_t          hlen1, hlen2, hlen1a;
95         void            *pathbufp;
96         size_t          buflen = 1024;
97         size_t          rlenp;
98         char            *name;
99         int             opt;
100
101         if (Progname = strrchr(argv[0], '/')) {
102                 Progname++;
103         } else {
104                 Progname = argv[0];
105         }
106
107         /* Crack and validate the command line options. */
108
109         while ((opt = getopt(argc, argv, "b:")) != EOF) {
110                 switch (opt) {
111                 case 'b':
112                         buflen = atol(optarg);
113                         break;
114                 case '?':
115                         usage();
116                 }
117         }
118         if (optind + 2 != argc)
119                 usage();
120         dirpath = argv[optind++];
121         objpath = argv[optind];
122
123         if (dm_init_service(&name)) {
124                 fprintf(stderr, "Can't initialize the DMAPI\n");
125                 return(1);
126         }
127
128         if (dm_path_to_handle(dirpath, &hanp1, &hlen1)) {
129                 fprintf(stderr, "dm_path_to_handle failed for %s, (%d) %s\n",
130                         dirpath, errno, strerror(errno));
131                 return(1);
132         }
133         if (path_to_handle(dirpath, &hanp1a, &hlen1a)) {
134                 fprintf(stderr, "path_to_handle failed for %s, (%d) %s\n",
135                         dirpath, errno, strerror(errno));
136                 return(1);
137         }
138         if(hlen1 != hlen1a){
139                 fprintf(stderr, "dm_path_to_handle != path_to_handle, %d != %d\n",
140                         hlen1, hlen1a);
141         }
142         if( memcmp(hanp1, hanp1a, hlen1) != 0 ){
143                 fprintf(stderr, "dm_path_to_handle != path_to_handle, handles differ\n");
144         }
145         if (dm_path_to_handle(objpath, &hanp2, &hlen2)) {
146                 fprintf(stderr, "dm_path_to_handle failed for %s, (%d) %s\n",
147                         objpath, errno, strerror(errno));
148                 return(1);
149         }
150
151         if ((pathbufp = malloc(buflen == 0 ? 1 : buflen)) == NULL) {
152                 fprintf(stderr, "malloc failed\n");
153                 return(1);
154         }
155
156         if (dm_handle_to_path(hanp1, hlen1, hanp2, hlen2,
157             buflen, pathbufp, &rlenp)) {
158                 if (errno == E2BIG) {
159                         fprintf(stderr, "dm_handle_to_path buffer too small, "
160                                 "should be %d bytes\n", rlenp);
161                 } else {
162                         fprintf(stderr, "dm_handle_to_path failed, (%d) %s\n",
163                                 errno, strerror(errno));
164                 }
165                 return(1);
166         }
167         fprintf(stderr, "rlenp is %d, pathbufp is %s\n", rlenp, (char*)pathbufp);
168         if (strlen(pathbufp) + 1 != rlenp) {
169                 fprintf(stderr, "rlenp is %d, should be %d\n", rlenp,
170                         strlen(pathbufp) + 1);
171                 return(1);
172         }
173         exit(0);
174 }