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