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/>.
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
;
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.
42 dpkg_fsys_new_dir(void)
47 env
= getenv("DPKG_ROOT");
52 path_trim_slash_slashdot(dir
);
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.
69 dpkg_fsys_set_dir(const char *dir
)
74 dir_new
= dpkg_fsys_new_dir();
76 dir_new
= m_strdup(dir
);
77 path_trim_slash_slashdot(dir_new
);
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.
95 dpkg_fsys_get_dir(void)
98 fsys_dir
= dpkg_fsys_new_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
109 * @param pathpart The pathpart to append to the new pathname.
111 * @return The newly allocated pathname.
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
);