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