1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 2019 by Delphix. All rights reserved.
24 */
25 #include <libintl.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <stdint.h>
29 #include <libzfs.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33
34 libzfs_handle_t *g_zfs;
35
36 static void
usage(int err)37 usage(int err)
38 {
39 fprintf(stderr, "Usage: zfs_ids_to_path [-v] <pool> <objset id> "
40 "<object id>\n");
41 exit(err);
42 }
43
44 int
main(int argc,char ** argv)45 main(int argc, char **argv)
46 {
47 boolean_t verbose = B_FALSE;
48 int c;
49 while ((c = getopt(argc, argv, "v")) != -1) {
50 switch (c) {
51 case 'v':
52 verbose = B_TRUE;
53 break;
54 }
55 }
56 argc -= optind;
57 argv += optind;
58
59 if (argc != 3) {
60 (void) fprintf(stderr, "Incorrect number of arguments: %d\n",
61 argc);
62 usage(1);
63 }
64
65 uint64_t objset, object;
66 if (sscanf(argv[1], "%llu", (u_longlong_t *)&objset) != 1) {
67 (void) fprintf(stderr, "Invalid objset id: %s\n", argv[1]);
68 usage(2);
69 }
70 if (sscanf(argv[2], "%llu", (u_longlong_t *)&object) != 1) {
71 (void) fprintf(stderr, "Invalid object id: %s\n", argv[2]);
72 usage(3);
73 }
74 if ((g_zfs = libzfs_init()) == NULL) {
75 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
76 return (4);
77 }
78 zpool_handle_t *pool = zpool_open(g_zfs, argv[0]);
79 if (pool == NULL) {
80 fprintf(stderr, "Could not open pool %s\n", argv[0]);
81 libzfs_fini(g_zfs);
82 return (5);
83 }
84
85 char pathname[PATH_MAX * 2];
86 if (verbose) {
87 zpool_obj_to_path_ds(pool, objset, object, pathname,
88 sizeof (pathname));
89 } else {
90 zpool_obj_to_path(pool, objset, object, pathname,
91 sizeof (pathname));
92 }
93 printf("%s\n", pathname);
94 zpool_close(pool);
95 libzfs_fini(g_zfs);
96 return (0);
97 }
98