46a1cd757eeb0bc21f364fad65c36c606f1b29a6
[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|dirhandle} {objpath|objhandle}
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|dirhandle} {objpath|objhandle}\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;
94         size_t          hlen1, hlen2;
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 (opaque_to_handle(dirpath, &hanp1, &hlen1)) {
129                 fprintf(stderr, "can't get handle for dir %s\n", dirpath);
130                 exit(1);
131         }
132
133         if (opaque_to_handle(objpath, &hanp2, &hlen2)) {
134                 fprintf(stderr, "can't get handle for obj %s\n", objpath);
135                 exit(1);
136         }
137
138         if ((pathbufp = malloc(buflen == 0 ? 1 : buflen)) == NULL) {
139                 fprintf(stderr, "malloc failed\n");
140                 return(1);
141         }
142
143         if (dm_handle_to_path(hanp1, hlen1, hanp2, hlen2,
144             buflen, pathbufp, &rlenp)) {
145                 if (errno == E2BIG) {
146                         fprintf(stderr, "dm_handle_to_path buffer too small, "
147                                 "should be %d bytes\n", rlenp);
148                 } else {
149                         fprintf(stderr, "dm_handle_to_path failed, (%d) %s\n",
150                                 errno, strerror(errno));
151                 }
152                 return(1);
153         }
154         fprintf(stderr, "rlenp is %d, pathbufp is %s\n", rlenp, (char*)pathbufp);
155         if (strlen(pathbufp) + 1 != rlenp) {
156                 fprintf(stderr, "rlenp is %d, should be %d\n", rlenp,
157                         strlen(pathbufp) + 1);
158                 return(1);
159         }
160         exit(0);
161 }