1111188Ssos/*-
2230132Suqs * SPDX-License-Identifier: BSD-2-Clause
3111188Ssos *
4111188Ssos * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
5111188Ssos *
6111188Ssos * Redistribution and use in source and binary forms, with or without
7111188Ssos * modification, are permitted provided that the following conditions
8111188Ssos * are met:
9111188Ssos * 1. Redistributions of source code must retain the above copyright
10111188Ssos *    notice, this list of conditions and the following disclaimer.
11111188Ssos * 2. Redistributions in binary form must reproduce the above copyright
12111188Ssos *    notice, this list of conditions and the following disclaimer in the
13111188Ssos *    documentation and/or other materials provided with the distribution.
14111188Ssos *
15111188Ssos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16111188Ssos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17111188Ssos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18111188Ssos * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19111188Ssos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20111188Ssos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21111188Ssos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22111188Ssos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23111188Ssos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24111188Ssos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25111188Ssos * SUCH DAMAGE.
26111188Ssos */
27111188Ssos
28111188Ssos#include <sys/cdefs.h>
29111188Ssos#include "be.h"
30111188Ssos#include "be_impl.h"
31144330Ssos
32144330Ssos/*
33144330Ssos * Usage
34144330Ssos */
35144330Ssosint
36233282Smariuslibbe_errno(libbe_handle_t *lbh)
37111188Ssos{
38111188Ssos
39199322Smav	return (lbh->error);
40199322Smav}
41111188Ssos
42111188Ssos
43144330Ssosconst char *
44144330Ssoslibbe_error_description(libbe_handle_t *lbh)
45144330Ssos{
46144330Ssos
47144330Ssos	switch (lbh->error) {
48144330Ssos	case BE_ERR_INVALIDNAME:
49144330Ssos		return ("invalid boot environment name");
50188655Smav
51144330Ssos	case BE_ERR_EXISTS:
52144330Ssos		return ("boot environment name already taken");
53233282Smarius
54186182Smav	case BE_ERR_NOENT:
55144330Ssos		return ("specified boot environment does not exist");
56188694Smav
57144330Ssos	case BE_ERR_PERMS:
58224270Smav		return ("insufficient permissions");
59183141Ssos
60183141Ssos	case BE_ERR_DESTROYACT:
61188765Smav		return ("cannot destroy active boot environment");
62188765Smav
63190581Smav	case BE_ERR_DESTROYMNT:
64190581Smav		return ("cannot destroy mounted boot env unless forced");
65145713Ssos
66200171Smav	case BE_ERR_BADPATH:
67200171Smav		return ("path not suitable for operation");
68111188Ssos
69144330Ssos	case BE_ERR_PATHBUSY:
70144330Ssos		return ("specified path is busy");
71199322Smav
72192105Sjhb	case BE_ERR_PATHLEN:
73111188Ssos		return ("provided path name exceeds maximum length limit");
74111188Ssos
75111188Ssos	case BE_ERR_BADMOUNT:
76144330Ssos		return ("mountpoint is not \"/\"");
77144330Ssos
78144330Ssos	case BE_ERR_NOORIGIN:
79144330Ssos		return ("could not open snapshot's origin");
80144330Ssos
81144330Ssos	case BE_ERR_MOUNTED:
82144330Ssos		return ("boot environment is already mounted");
83144330Ssos
84111188Ssos	case BE_ERR_NOMOUNT:
85178375Ssos		return ("boot environment is not mounted");
86178375Ssos
87204509Smav	case BE_ERR_ZFSOPEN:
88178375Ssos		return ("calling zfs_open() failed");
89178375Ssos
90178375Ssos	case BE_ERR_ZFSCLONE:
91178375Ssos		return ("error when calling zfs_clone() to create boot env");
92178375Ssos
93178375Ssos	case BE_ERR_IO:
94144330Ssos		return ("input/output error");
95144330Ssos
96144330Ssos	case BE_ERR_NOPOOL:
97144330Ssos		return ("operation not supported on this pool");
98144330Ssos
99144330Ssos	case BE_ERR_NOMEM:
100172436Sphk		return ("insufficient memory");
101244146Smav
102244146Smav	case BE_ERR_UNKNOWN:
103244146Smav		return ("unknown error");
104244146Smav
105244146Smav	case BE_ERR_INVORIGIN:
106244146Smav		return ("invalid origin");
107111188Ssos
108178375Ssos	case BE_ERR_HASCLONES:
109178375Ssos		return ("snapshot has clones");
110198700Smav
111111188Ssos	default:
112154515Ssos		assert(lbh->error == BE_ERR_SUCCESS);
113154515Ssos		return ("no error");
114154515Ssos	}
115173734Ssos}
116154515Ssos
117154515Ssos
118154515Ssosvoid
119173734Ssoslibbe_print_on_error(libbe_handle_t *lbh, bool val)
120176891Ssos{
121173734Ssos
122176891Ssos	lbh->print_on_err = val;
123191568Sjkim	libzfs_print_on_error(lbh->lzh, val);
124191568Sjkim}
125191568Sjkim
126191568Sjkim
127191568Sjkimint
128151267Ssosset_error(libbe_handle_t *lbh, be_error_t err)
129144330Ssos{
130144330Ssos
131138555Ssos	lbh->error = err;
132144330Ssos	if (lbh->print_on_err && (err != BE_ERR_SUCCESS))
133144330Ssos		fprintf(stderr, "%s\n", libbe_error_description(lbh));
134111188Ssos
135144330Ssos	return (err);
136144330Ssos}
137111188Ssos