infodb.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "infodb.h"
  33. bool
  34. pkg_infodb_has_file(struct pkginfo *pkg, const char *name)
  35. {
  36. const char *filename;
  37. struct stat stab;
  38. filename = pkgadminfile(pkg, name);
  39. if (lstat(filename, &stab) == 0)
  40. return true;
  41. else if (errno == ENOENT)
  42. return false;
  43. else
  44. ohshite(_("unable to check existence of `%.250s'"), filename);
  45. }
  46. void
  47. pkg_infodb_foreach(struct pkginfo *pkg, pkg_infodb_file_func *func)
  48. {
  49. DIR *db_dir;
  50. struct dirent *db_de;
  51. struct varbuf db_path = VARBUF_INIT;
  52. size_t db_path_len;
  53. varbuf_add_str(&db_path, pkgadmindir());
  54. db_path_len = db_path.used;
  55. varbuf_add_char(&db_path, '\0');
  56. db_dir = opendir(db_path.buf);
  57. if (!db_dir)
  58. ohshite(_("cannot read info directory"));
  59. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)db_dir);
  60. while ((db_de = readdir(db_dir)) != NULL) {
  61. const char *filename, *filetype, *dot;
  62. debug(dbg_veryverbose, "infodb foreach info file '%s'",
  63. db_de->d_name);
  64. /* Ignore dotfiles, including ‘.’ and ‘..’. */
  65. if (db_de->d_name[0] == '.')
  66. continue;
  67. /* Ignore anything odd. */
  68. dot = strrchr(db_de->d_name, '.');
  69. if (dot == NULL)
  70. continue;
  71. /* Ignore files from other packages. */
  72. if (strlen(pkg->name) != (size_t)(dot - db_de->d_name) ||
  73. strncmp(db_de->d_name, pkg->name, dot - db_de->d_name))
  74. continue;
  75. debug(dbg_stupidlyverbose, "infodb foreach file this pkg");
  76. /* Skip past the full stop. */
  77. filetype = dot + 1;
  78. varbuf_trunc(&db_path, db_path_len);
  79. varbuf_add_str(&db_path, db_de->d_name);
  80. varbuf_end_str(&db_path);
  81. filename = db_path.buf;
  82. func(filename, filetype);
  83. }
  84. pop_cleanup(ehflag_normaltidy); /* closedir */
  85. varbuf_destroy(&db_path);
  86. }