Dpkg::Vendor::Debian: Add support for new hardening branch feature
[dpkg.git] / lib / dpkg / fsys-dir.c
blob76d5f39ded765b91e685fc298f0f1a5c4a068a8b
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * fsys-dir.c - filesystem root directory functions
5 * Copyright © 2011, 2018 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <compat.h>
24 #include <stdlib.h>
26 #include <dpkg/dpkg.h>
27 #include <dpkg/string.h>
28 #include <dpkg/path.h>
29 #include <dpkg/fsys.h>
31 static char *fsys_dir;
33 /**
34 * Allocate the default on-disk filesystem root directory.
36 * The directory defaults to the value from environment variable
37 * DPKG_ROOT, and if not set the built-in default "".
39 * @return The filesystem root directory.
41 static char *
42 dpkg_fsys_new_dir(void)
44 const char *env;
45 char *dir;
47 env = getenv("DPKG_ROOT");
48 if (env == NULL) {
49 dir = m_strdup("");
50 } else {
51 dir = m_strdup(env);
52 path_trim_slash_slashdot(dir);
55 return dir;
58 /**
59 * Set current on-disk filesystem root directory.
61 * This function can be used to set the directory to a new value, or to
62 * reset it to a default value if dir is NULL.
64 * @param dir The new filesystem root directory, or NULL to set to default.
66 * @return The new filesystem root directory.
68 const char *
69 dpkg_fsys_set_dir(const char *dir)
71 char *dir_new;
73 if (dir == NULL) {
74 dir_new = dpkg_fsys_new_dir();
75 } else {
76 dir_new = m_strdup(dir);
77 path_trim_slash_slashdot(dir_new);
80 free(fsys_dir);
81 fsys_dir = dir_new;
83 return fsys_dir;
86 /**
87 * Get current on-disk filesystem root directory.
89 * This function will take care of initializing the directory if it has not
90 * been initialized before.
92 * @return The current filesystem root directory.
94 const char *
95 dpkg_fsys_get_dir(void)
97 if (fsys_dir == NULL)
98 fsys_dir = dpkg_fsys_new_dir();
100 return fsys_dir;
104 * Get a pathname to the current on-disk filesystem root directory.
106 * This function returns an allocated string, which should be freed with
107 * free(2).
109 * @param pathpart The pathpart to append to the new pathname.
111 * @return The newly allocated pathname.
113 char *
114 dpkg_fsys_get_path(const char *pathpart)
116 pathpart = path_skip_slash_dotslash(pathpart);
118 return str_fmt("%s/%s", dpkg_fsys_get_dir(), pathpart);