infodb-access.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * dpkg - main program for package management
  3. * infodb.c - package control information database
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2011 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 <http://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 db_path = VARBUF_INIT;
  55. const char *pkgname;
  56. size_t db_path_len;
  57. if (pkgbin->multiarch == multiarch_same &&
  58. pkg_infodb_get_format() == pkg_infodb_format_multiarch)
  59. pkgname = pkgbin_name(pkg, pkgbin, pnaw_always);
  60. else
  61. pkgname = pkgbin_name(pkg, pkgbin, pnaw_never);
  62. varbuf_add_str(&db_path, pkg_infodb_get_dir());
  63. varbuf_add_char(&db_path, '/');
  64. db_path_len = db_path.used;
  65. varbuf_add_char(&db_path, '\0');
  66. db_dir = opendir(db_path.buf);
  67. if (!db_dir)
  68. ohshite(_("cannot read info directory"));
  69. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)db_dir);
  70. while ((db_de = readdir(db_dir)) != NULL) {
  71. const char *filename, *filetype, *dot;
  72. debug(dbg_veryverbose, "infodb foreach info file '%s'",
  73. db_de->d_name);
  74. /* Ignore dotfiles, including ‘.’ and ‘..’. */
  75. if (db_de->d_name[0] == '.')
  76. continue;
  77. /* Ignore anything odd. */
  78. dot = strrchr(db_de->d_name, '.');
  79. if (dot == NULL)
  80. continue;
  81. /* Ignore files from other packages. */
  82. if (strlen(pkgname) != (size_t)(dot - db_de->d_name) ||
  83. strncmp(db_de->d_name, pkgname, dot - db_de->d_name))
  84. continue;
  85. debug(dbg_stupidlyverbose, "infodb foreach file this pkg");
  86. /* Skip past the full stop. */
  87. filetype = dot + 1;
  88. varbuf_trunc(&db_path, db_path_len);
  89. varbuf_add_str(&db_path, db_de->d_name);
  90. varbuf_end_str(&db_path);
  91. filename = db_path.buf;
  92. func(filename, filetype);
  93. }
  94. pop_cleanup(ehflag_normaltidy); /* closedir */
  95. varbuf_destroy(&db_path);
  96. }