Branch data Line data Source code
1 : : /* 2 : : * libdpkg - Debian packaging suite library routines 3 : : * fsys-dir.c - filesystem root directory functions 4 : : * 5 : : * Copyright © 2011, 2018 Guillem Jover <guillem@debian.org> 6 : : * 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. 11 : : * 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. 16 : : * 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/>. 19 : : */ 20 : : 21 : : #include <config.h> 22 : : #include <compat.h> 23 : : 24 : : #include <stdlib.h> 25 : : 26 : : #include <dpkg/dpkg.h> 27 : : #include <dpkg/string.h> 28 : : #include <dpkg/path.h> 29 : : #include <dpkg/fsys.h> 30 : : 31 : : static char *fsys_dir; 32 : : 33 : : /** 34 : : * Allocate the default on-disk filesystem root directory. 35 : : * 36 : : * The directory defaults to the value from environment variable 37 : : * DPKG_ROOT, and if not set the built-in default "". 38 : : * 39 : : * @return The filesystem root directory. 40 : : */ 41 : : static char * 42 : 102 : dpkg_fsys_new_dir(void) 43 : : { 44 : : const char *env; 45 : : char *dir; 46 : : 47 : 102 : env = getenv("DPKG_ROOT"); 48 [ + + ]: 102 : if (env == NULL) { 49 : 58 : dir = m_strdup(""); 50 : : } else { 51 : 44 : dir = m_strdup(env); 52 : 44 : path_trim_slash_slashdot(dir); 53 : : } 54 : : 55 : 102 : return dir; 56 : : } 57 : : 58 : : /** 59 : : * Set current on-disk filesystem root directory. 60 : : * 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. 63 : : * 64 : : * @param dir The new filesystem root directory, or NULL to set to default. 65 : : * 66 : : * @return The new filesystem root directory. 67 : : */ 68 : : const char * 69 : 304 : dpkg_fsys_set_dir(const char *dir) 70 : : { 71 : : char *dir_new; 72 : : 73 [ + + ]: 304 : if (dir == NULL) { 74 : 3 : dir_new = dpkg_fsys_new_dir(); 75 : : } else { 76 : 301 : dir_new = m_strdup(dir); 77 : 301 : path_trim_slash_slashdot(dir_new); 78 : : } 79 : : 80 : 304 : free(fsys_dir); 81 : 304 : fsys_dir = dir_new; 82 : : 83 : 304 : return fsys_dir; 84 : : } 85 : : 86 : : /** 87 : : * Get current on-disk filesystem root directory. 88 : : * 89 : : * This function will take care of initializing the directory if it has not 90 : : * been initialized before. 91 : : * 92 : : * @return The current filesystem root directory. 93 : : */ 94 : : const char * 95 : 792 : dpkg_fsys_get_dir(void) 96 : : { 97 [ + + ]: 792 : if (fsys_dir == NULL) 98 : 99 : fsys_dir = dpkg_fsys_new_dir(); 99 : : 100 : 792 : return fsys_dir; 101 : : } 102 : : 103 : : /** 104 : : * Get a pathname to the current on-disk filesystem root directory. 105 : : * 106 : : * This function returns an allocated string, which should be freed with 107 : : * free(2). 108 : : * 109 : : * @param pathpart The pathpart to append to the new pathname. 110 : : * 111 : : * @return The newly allocated pathname. 112 : : */ 113 : : char * 114 : 171 : dpkg_fsys_get_path(const char *pathpart) 115 : : { 116 : 171 : pathpart = path_skip_slash_dotslash(pathpart); 117 : : 118 : 171 : return str_fmt("%s/%s", dpkg_fsys_get_dir(), pathpart); 119 : : }