• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * erofs-utils/lib/config.c
4  *
5  * Copyright (C) 2018-2019 HUAWEI, Inc.
6  *             http://www.huawei.com/
7  * Created by Li Guifu <bluce.liguifu@huawei.com>
8  */
9 #include <string.h>
10 #include "erofs/print.h"
11 #include "erofs/internal.h"
12 
13 struct erofs_configure cfg;
14 struct erofs_sb_info sbi;
15 
erofs_init_configure(void)16 void erofs_init_configure(void)
17 {
18 	memset(&cfg, 0, sizeof(cfg));
19 
20 	cfg.c_dbg_lvl  = 0;
21 	cfg.c_version  = PACKAGE_VERSION;
22 	cfg.c_dry_run  = false;
23 	cfg.c_compr_level_master = -1;
24 	cfg.c_force_inodeversion = 0;
25 	cfg.c_inline_xattr_tolerance = 2;
26 	cfg.c_unix_timestamp = -1;
27 }
28 
erofs_show_config(void)29 void erofs_show_config(void)
30 {
31 	const struct erofs_configure *c = &cfg;
32 
33 	erofs_dump("\tc_version:           [%8s]\n", c->c_version);
34 	erofs_dump("\tc_dbg_lvl:           [%8d]\n", c->c_dbg_lvl);
35 	erofs_dump("\tc_dry_run:           [%8d]\n", c->c_dry_run);
36 }
37 
erofs_exit_configure(void)38 void erofs_exit_configure(void)
39 {
40 #ifdef HAVE_LIBSELINUX
41 	if (cfg.sehnd)
42 		selabel_close(cfg.sehnd);
43 #endif
44 }
45 
46 static unsigned int fullpath_prefix;	/* root directory prefix length */
47 
erofs_set_fs_root(const char * rootdir)48 void erofs_set_fs_root(const char *rootdir)
49 {
50 	fullpath_prefix = strlen(rootdir);
51 }
52 
erofs_fspath(const char * fullpath)53 const char *erofs_fspath(const char *fullpath)
54 {
55 	const char *s = fullpath + fullpath_prefix;
56 
57 	while (*s == '/')
58 		s++;
59 	return s;
60 }
61 
62 #ifdef HAVE_LIBSELINUX
erofs_selabel_open(const char * file_contexts)63 int erofs_selabel_open(const char *file_contexts)
64 {
65 	struct selinux_opt seopts[] = {
66 		{ .type = SELABEL_OPT_PATH, .value = file_contexts }
67 	};
68 
69 	if (cfg.sehnd) {
70 		erofs_info("ignore duplicated file contexts \"%s\"",
71 			   file_contexts);
72 		return -EBUSY;
73 	}
74 
75 	cfg.sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
76 	if (!cfg.sehnd) {
77 		erofs_err("failed to open file contexts \"%s\"",
78 			  file_contexts);
79 		return -EINVAL;
80 	}
81 	return 0;
82 }
83 #endif
84 
85