infodb-access.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * dpkg - main program for package management
  3. * infodb.c - package control information database
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2011-2014 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <errno.h>
  26. #include <dirent.h>
  27. #include <unistd.h>
  28. #include <dpkg/i18n.h>
  29. #include <dpkg/dpkg.h>
  30. #include <dpkg/dpkg-db.h>
  31. #include <dpkg/debug.h>
  32. #include "filesdb.h"
  33. #include "infodb.h"
  34. bool
  35. pkg_infodb_has_file(struct pkginfo *pkg, struct pkgbin *pkgbin,
  36. const char *name)
  37. {
  38. const char *filename;
  39. struct stat stab;
  40. filename = pkg_infodb_get_file(pkg, pkgbin, name);
  41. if (lstat(filename, &stab) == 0)
  42. return true;
  43. else if (errno == ENOENT)
  44. return false;
  45. else
  46. ohshite(_("unable to check existence of '%.250s'"), filename);
  47. }
  48. void
  49. pkg_infodb_foreach(struct pkginfo *pkg, struct pkgbin *pkgbin,
  50. pkg_infodb_file_func *func)
  51. {
  52. DIR *db_dir;
  53. struct dirent *db_de;
  54. struct varbuf_state db_path_state;
  55. struct varbuf db_path = VARBUF_INIT;
  56. const char *pkgname;
  57. enum pkg_infodb_format db_format;
  58. /* Make sure to always read and verify the format version. */
  59. db_format = pkg_infodb_get_format();
  60. if (pkgbin->multiarch == PKG_MULTIARCH_SAME &&
  61. db_format == PKG_INFODB_FORMAT_MULTIARCH)
  62. pkgname = pkgbin_name(pkg, pkgbin, pnaw_always);
  63. else
  64. pkgname = pkgbin_name(pkg, pkgbin, pnaw_never);
  65. varbuf_add_str(&db_path, pkg_infodb_get_dir());
  66. varbuf_add_char(&db_path, '/');
  67. varbuf_end_str(&db_path);
  68. varbuf_snapshot(&db_path, &db_path_state);
  69. db_dir = opendir(db_path.buf);
  70. if (!db_dir)
  71. ohshite(_("cannot read info directory"));
  72. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)db_dir);
  73. while ((db_de = readdir(db_dir)) != NULL) {
  74. const char *filename, *filetype, *dot;
  75. debug(dbg_veryverbose, "infodb foreach info file '%s'",
  76. db_de->d_name);
  77. /* Ignore dotfiles, including ‘.’ and ‘..’. */
  78. if (db_de->d_name[0] == '.')
  79. continue;
  80. /* Ignore anything odd. */
  81. dot = strrchr(db_de->d_name, '.');
  82. if (dot == NULL)
  83. continue;
  84. /* Ignore files from other packages. */
  85. if (strlen(pkgname) != (size_t)(dot - db_de->d_name) ||
  86. strncmp(db_de->d_name, pkgname, dot - db_de->d_name))
  87. continue;
  88. debug(dbg_stupidlyverbose, "infodb foreach file this pkg");
  89. /* Skip past the full stop. */
  90. filetype = dot + 1;
  91. varbuf_rollback(&db_path, &db_path_state);
  92. varbuf_add_str(&db_path, db_de->d_name);
  93. varbuf_end_str(&db_path);
  94. filename = db_path.buf;
  95. func(filename, filetype);
  96. }
  97. pop_cleanup(ehflag_normaltidy); /* closedir */
  98. varbuf_destroy(&db_path);
  99. }