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