dbdir.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <stdlib.h>
  24. #include <dpkg/dpkg.h>
  25. #include <dpkg/dpkg-db.h>
  26. static const char *db_dir = ADMINDIR;
  27. /**
  28. * Set current on-disk database directory.
  29. *
  30. * The directory is initially set to ADMINDIR, this function can be used to
  31. * set the directory to a new value, or to set it to a default value if dir
  32. * is NULL. For the latter the order is, value from environment variable
  33. * DPKG_ADMINDIR, and then the built-in default ADMINDIR,
  34. *
  35. * @param dir The new databse directory, or NULL to set to default.
  36. *
  37. * @return The new database directory.
  38. */
  39. const char *
  40. dpkg_db_set_dir(const char *dir)
  41. {
  42. if (dir == NULL) {
  43. const char *env;
  44. env = getenv("DPKG_ADMINDIR");
  45. if (env)
  46. db_dir = env;
  47. else
  48. db_dir = ADMINDIR;
  49. } else {
  50. db_dir = dir;
  51. }
  52. return db_dir;
  53. }
  54. /**
  55. * Get current on-disk database directory.
  56. *
  57. * @return The current database directory.
  58. */
  59. const char *
  60. dpkg_db_get_dir(void)
  61. {
  62. return db_dir;
  63. }
  64. /**
  65. * Get a pathname to the current on-disk database directory.
  66. *
  67. * This function returns an allocated string, which should be freed with
  68. * free(2).
  69. *
  70. * @param pathpart The pathpart to append to the new pathnme.
  71. *
  72. * @return The newly allocated pathname.
  73. */
  74. char *
  75. dpkg_db_get_path(const char *pathpart)
  76. {
  77. char *pathname;
  78. m_asprintf(&pathname, "%s/%s", db_dir, pathpart);
  79. return pathname;
  80. }