xref: /freebsd/lib/libbe/be_error.c (revision e17f5b1d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "be.h"
32 #include "be_impl.h"
33 
34 /*
35  * Usage
36  */
37 int
38 libbe_errno(libbe_handle_t *lbh)
39 {
40 
41 	return (lbh->error);
42 }
43 
44 
45 const char *
46 libbe_error_description(libbe_handle_t *lbh)
47 {
48 
49 	switch (lbh->error) {
50 	case BE_ERR_INVALIDNAME:
51 		return ("invalid boot environment name");
52 
53 	case BE_ERR_EXISTS:
54 		return ("boot environment name already taken");
55 
56 	case BE_ERR_NOENT:
57 		return ("specified boot environment does not exist");
58 
59 	case BE_ERR_PERMS:
60 		return ("insufficient permissions");
61 
62 	case BE_ERR_DESTROYACT:
63 		return ("cannot destroy active boot environment");
64 
65 	case BE_ERR_DESTROYMNT:
66 		return ("cannot destroy mounted boot env unless forced");
67 
68 	case BE_ERR_BADPATH:
69 		return ("path not suitable for operation");
70 
71 	case BE_ERR_PATHBUSY:
72 		return ("specified path is busy");
73 
74 	case BE_ERR_PATHLEN:
75 		return ("provided path name exceeds maximum length limit");
76 
77 	case BE_ERR_BADMOUNT:
78 		return ("mountpoint is not \"/\"");
79 
80 	case BE_ERR_NOORIGIN:
81 		return ("could not open snapshot's origin");
82 
83 	case BE_ERR_MOUNTED:
84 		return ("boot environment is already mounted");
85 
86 	case BE_ERR_NOMOUNT:
87 		return ("boot environment is not mounted");
88 
89 	case BE_ERR_ZFSOPEN:
90 		return ("calling zfs_open() failed");
91 
92 	case BE_ERR_ZFSCLONE:
93 		return ("error when calling zfs_clone() to create boot env");
94 
95 	case BE_ERR_IO:
96 		return ("input/output error");
97 
98 	case BE_ERR_NOPOOL:
99 		return ("operation not supported on this pool");
100 
101 	case BE_ERR_NOMEM:
102 		return ("insufficient memory");
103 
104 	case BE_ERR_UNKNOWN:
105 		return ("unknown error");
106 
107 	case BE_ERR_INVORIGIN:
108 		return ("invalid origin");
109 
110 	case BE_ERR_HASCLONES:
111 		return ("snapshot has clones");
112 
113 	default:
114 		assert(lbh->error == BE_ERR_SUCCESS);
115 		return ("no error");
116 	}
117 }
118 
119 
120 void
121 libbe_print_on_error(libbe_handle_t *lbh, bool val)
122 {
123 
124 	lbh->print_on_err = val;
125 	libzfs_print_on_error(lbh->lzh, val);
126 }
127 
128 
129 int
130 set_error(libbe_handle_t *lbh, be_error_t err)
131 {
132 
133 	lbh->error = err;
134 	if (lbh->print_on_err && (err != BE_ERR_SUCCESS))
135 		fprintf(stderr, "%s\n", libbe_error_description(lbh));
136 
137 	return (err);
138 }
139