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