Branch data Line data Source code
1 : : /* 2 : : * libdpkg - Debian packaging suite library routines 3 : : * dbdir.c - on-disk database directory functions 4 : : * 5 : : * Copyright © 2011 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 <sys/types.h> 25 : : 26 : : #include <stdlib.h> 27 : : 28 : : #include <dpkg/dpkg.h> 29 : : #include <dpkg/dpkg-db.h> 30 : : #include <dpkg/fsys.h> 31 : : 32 : : static char *db_dir; 33 : : 34 : : /** 35 : : * Allocate the default on-disk database directory. 36 : : * 37 : : * The directory defaults to the value from environment variable 38 : : * DPKG_ADMINDIR, and if not set the built-in default ADMINDIR. 39 : : * 40 : : * @return The database directory. 41 : : */ 42 : : static char * 43 : 76 : dpkg_db_new_dir(void) 44 : : { 45 : : const char *env; 46 : : char *dir; 47 : : 48 : : /* Make sure the filesystem root directory is initialized. */ 49 : 76 : dpkg_fsys_get_dir(); 50 : : 51 : 76 : env = getenv("DPKG_ADMINDIR"); 52 [ + + ]: 76 : if (env) 53 : 31 : dir = m_strdup(env); 54 : : else 55 : 45 : dir = dpkg_fsys_get_path(ADMINDIR); 56 : : 57 : 76 : return dir; 58 : : } 59 : : 60 : : /** 61 : : * Set current on-disk database directory. 62 : : * 63 : : * This function can be used to set the directory to a new value, or to 64 : : * reset it to a default value if dir is NULL. 65 : : * 66 : : * @param dir The new database directory, or NULL to set to default. 67 : : * 68 : : * @return The new database directory. 69 : : */ 70 : : const char * 71 : 344 : dpkg_db_set_dir(const char *dir) 72 : : { 73 : : char *dir_new; 74 : : 75 [ + + ]: 344 : if (dir == NULL) 76 : 2 : dir_new = dpkg_db_new_dir(); 77 : : else 78 : 342 : dir_new = m_strdup(dir); 79 : : 80 : 344 : free(db_dir); 81 : 344 : db_dir = dir_new; 82 : : 83 : 344 : return db_dir; 84 : : } 85 : : 86 : : /** 87 : : * Get current on-disk database 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 database directory. 93 : : */ 94 : : const char * 95 : 1350 : dpkg_db_get_dir(void) 96 : : { 97 [ + + ]: 1350 : if (db_dir == NULL) 98 : 74 : db_dir = dpkg_db_new_dir(); 99 : : 100 : 1350 : return db_dir; 101 : : } 102 : : 103 : : /** 104 : : * Get a pathname to the current on-disk database 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 : 813 : dpkg_db_get_path(const char *pathpart) 115 : : { 116 : 813 : return str_fmt("%s/%s", dpkg_db_get_dir(), pathpart); 117 : : }