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