Path: blob/main/sys/contrib/openzfs/cmd/zfs_ids_to_path.c
48259 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/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>3233libzfs_handle_t *g_zfs;3435static void36usage(int err)37{38fprintf(stderr, "Usage: zfs_ids_to_path [-v] <pool> <objset id> "39"<object id>\n");40exit(err);41}4243int44main(int argc, char **argv)45{46boolean_t verbose = B_FALSE;47int c;48while ((c = getopt(argc, argv, "v")) != -1) {49switch (c) {50case 'v':51verbose = B_TRUE;52break;53}54}55argc -= optind;56argv += optind;5758if (argc != 3) {59(void) fprintf(stderr, "Incorrect number of arguments: %d\n",60argc);61usage(1);62}6364uint64_t objset, object;65if (sscanf(argv[1], "%llu", (u_longlong_t *)&objset) != 1) {66(void) fprintf(stderr, "Invalid objset id: %s\n", argv[1]);67usage(2);68}69if (sscanf(argv[2], "%llu", (u_longlong_t *)&object) != 1) {70(void) fprintf(stderr, "Invalid object id: %s\n", argv[2]);71usage(3);72}73if ((g_zfs = libzfs_init()) == NULL) {74(void) fprintf(stderr, "%s\n", libzfs_error_init(errno));75return (4);76}77zpool_handle_t *pool = zpool_open(g_zfs, argv[0]);78if (pool == NULL) {79fprintf(stderr, "Could not open pool %s\n", argv[0]);80libzfs_fini(g_zfs);81return (5);82}8384char pathname[PATH_MAX * 2];85if (verbose) {86zpool_obj_to_path_ds(pool, objset, object, pathname,87sizeof (pathname));88} else {89zpool_obj_to_path(pool, objset, object, pathname,90sizeof (pathname));91}92printf("%s\n", pathname);93zpool_close(pool);94libzfs_fini(g_zfs);95return (0);96}979899