xref: /freebsd/lib/libbe/be_error.c (revision b179da0111bf5734c60d07c110e8abae9cea300f)
1*b179da01SKyle Evans /*-
2*b179da01SKyle Evans  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
328f16a0fSKyle Evans  *
428f16a0fSKyle Evans  * Copyright (c) 2017 Kyle J. Kneitinger <kyle@kneit.in>
528f16a0fSKyle Evans  * All rights reserved.
628f16a0fSKyle Evans  *
728f16a0fSKyle Evans  * Redistribution and use in source and binary forms, with or without
828f16a0fSKyle Evans  * modification, are permitted provided that the following conditions
928f16a0fSKyle Evans  * are met:
1028f16a0fSKyle Evans  * 1. Redistributions of source code must retain the above copyright
1128f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer.
1228f16a0fSKyle Evans  * 2. Redistributions in binary form must reproduce the above copyright
1328f16a0fSKyle Evans  *    notice, this list of conditions and the following disclaimer in the
1428f16a0fSKyle Evans  *    documentation and/or other materials provided with the distribution.
1528f16a0fSKyle Evans  *
1628f16a0fSKyle Evans  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1728f16a0fSKyle Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1828f16a0fSKyle Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1928f16a0fSKyle Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2028f16a0fSKyle Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2128f16a0fSKyle Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2228f16a0fSKyle Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2328f16a0fSKyle Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2428f16a0fSKyle Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2528f16a0fSKyle Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2628f16a0fSKyle Evans  * SUCH DAMAGE.
2728f16a0fSKyle Evans  */
2828f16a0fSKyle Evans 
2928f16a0fSKyle Evans #include "be.h"
3028f16a0fSKyle Evans #include "be_impl.h"
3128f16a0fSKyle Evans 
3228f16a0fSKyle Evans /*
3328f16a0fSKyle Evans  * Usage
3428f16a0fSKyle Evans  */
3528f16a0fSKyle Evans int
3628f16a0fSKyle Evans libbe_errno(libbe_handle_t *lbh)
3728f16a0fSKyle Evans {
38bfe0869cSKyle Evans 
3928f16a0fSKyle Evans 	return (lbh->error);
4028f16a0fSKyle Evans }
4128f16a0fSKyle Evans 
4228f16a0fSKyle Evans 
4328f16a0fSKyle Evans const char *
4428f16a0fSKyle Evans libbe_error_description(libbe_handle_t *lbh)
4528f16a0fSKyle Evans {
46bfe0869cSKyle Evans 
4728f16a0fSKyle Evans 	switch (lbh->error) {
4828f16a0fSKyle Evans 	case BE_ERR_INVALIDNAME:
4928f16a0fSKyle Evans 		return ("invalid boot environment name");
5028f16a0fSKyle Evans 
5128f16a0fSKyle Evans 	case BE_ERR_EXISTS:
5228f16a0fSKyle Evans 		return ("boot environment name already taken");
5328f16a0fSKyle Evans 
5428f16a0fSKyle Evans 	case BE_ERR_NOENT:
5528f16a0fSKyle Evans 		return ("specified boot environment does not exist");
5628f16a0fSKyle Evans 
5728f16a0fSKyle Evans 	case BE_ERR_PERMS:
5828f16a0fSKyle Evans 		return ("insufficient permissions");
5928f16a0fSKyle Evans 
6028f16a0fSKyle Evans 	case BE_ERR_DESTROYACT:
6128f16a0fSKyle Evans 		return ("cannot destroy active boot environment");
6228f16a0fSKyle Evans 
6328f16a0fSKyle Evans 	case BE_ERR_DESTROYMNT:
6428f16a0fSKyle Evans 		return ("cannot destroy mounted boot env unless forced");
6528f16a0fSKyle Evans 
6628f16a0fSKyle Evans 	case BE_ERR_PATHLEN:
6728f16a0fSKyle Evans 		return ("provided path name exceeds maximum length limit");
6828f16a0fSKyle Evans 
6928f16a0fSKyle Evans 	case BE_ERR_INVORIGIN:
7028f16a0fSKyle Evans 		return ("snapshot origin's mountpoint is not \"/\"");
7128f16a0fSKyle Evans 
7228f16a0fSKyle Evans 	case BE_ERR_NOORIGIN:
7328f16a0fSKyle Evans 		return ("could not open snapshot's origin");
7428f16a0fSKyle Evans 
7528f16a0fSKyle Evans 	case BE_ERR_MOUNTED:
7628f16a0fSKyle Evans 		return ("boot environment is already mounted");
7728f16a0fSKyle Evans 
7828f16a0fSKyle Evans 	case BE_ERR_NOMOUNT:
7928f16a0fSKyle Evans 		return ("boot environment is not mounted");
8028f16a0fSKyle Evans 
8128f16a0fSKyle Evans 	case BE_ERR_ZFSOPEN:
8228f16a0fSKyle Evans 		return ("calling zfs_open() failed");
8328f16a0fSKyle Evans 
8428f16a0fSKyle Evans 	case BE_ERR_ZFSCLONE:
8528f16a0fSKyle Evans 		return ("error when calling zfs_clone() to create boot env");
8628f16a0fSKyle Evans 
8728f16a0fSKyle Evans 	case BE_ERR_UNKNOWN:
8828f16a0fSKyle Evans 		return ("unknown error");
8928f16a0fSKyle Evans 
9028f16a0fSKyle Evans 	default:
9128f16a0fSKyle Evans 		assert(lbh->error == BE_ERR_SUCCESS);
9228f16a0fSKyle Evans 		return ("no error");
9328f16a0fSKyle Evans 	}
9428f16a0fSKyle Evans }
9528f16a0fSKyle Evans 
9628f16a0fSKyle Evans 
9728f16a0fSKyle Evans void
9828f16a0fSKyle Evans libbe_print_on_error(libbe_handle_t *lbh, bool val)
9928f16a0fSKyle Evans {
100bfe0869cSKyle Evans 
10128f16a0fSKyle Evans 	lbh->print_on_err = val;
10228f16a0fSKyle Evans 	libzfs_print_on_error(lbh->lzh, val);
10328f16a0fSKyle Evans }
10428f16a0fSKyle Evans 
10528f16a0fSKyle Evans 
10628f16a0fSKyle Evans int
10728f16a0fSKyle Evans set_error(libbe_handle_t *lbh, be_error_t err)
10828f16a0fSKyle Evans {
10928f16a0fSKyle Evans 
11028f16a0fSKyle Evans 	lbh->error = err;
111bfe0869cSKyle Evans 	if (lbh->print_on_err && (err != BE_ERR_SUCCESS))
11228f16a0fSKyle Evans 		fprintf(stderr, "%s\n", libbe_error_description(lbh));
11328f16a0fSKyle Evans 
11428f16a0fSKyle Evans 	return (err);
11528f16a0fSKyle Evans }
116