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