infodb-format.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * dpkg - main program for package management
  3. * infodb-format.c - package control information database format
  4. *
  5. * Copyright © 2011-2014 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. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/dpkg.h>
  29. #include <dpkg/dpkg-db.h>
  30. #include <dpkg/varbuf.h>
  31. #include "infodb.h"
  32. static enum pkg_infodb_format db_format = PKG_INFODB_FORMAT_UNKNOWN;
  33. static bool db_upgrading;
  34. static enum pkg_infodb_format
  35. pkg_infodb_parse_format(const char *file)
  36. {
  37. FILE *fp;
  38. int format;
  39. fp = fopen(file, "r");
  40. if (fp == NULL) {
  41. /* A missing format file means legacy format (0). */
  42. if (errno == ENOENT)
  43. return PKG_INFODB_FORMAT_LEGACY;
  44. ohshite(_("error trying to open %.250s"), file);
  45. }
  46. if (fscanf(fp, "%u", &format) != 1)
  47. ohshit(_("corrupt info database format file '%s'"), file);
  48. fclose(fp);
  49. return format;
  50. }
  51. static enum pkg_infodb_format
  52. pkg_infodb_read_format(void)
  53. {
  54. struct atomic_file *file;
  55. struct stat st;
  56. char *filename;
  57. filename = dpkg_db_get_path(INFODIR "/format");
  58. file = atomic_file_new(filename, 0);
  59. db_format = pkg_infodb_parse_format(file->name);
  60. /* Check if a previous upgrade got interrupted. Because we are only
  61. * supposed to upgrade the db layout one format at a time, if the
  62. * new file exists that means the new format is just one ahead,
  63. * we don't try to read it because it contains unreliable data. */
  64. if (stat(file->name_new, &st) == 0) {
  65. db_format++;
  66. db_upgrading = true;
  67. }
  68. atomic_file_free(file);
  69. free(filename);
  70. if (db_format < 0 || db_format >= PKG_INFODB_FORMAT_LAST)
  71. ohshit(_("info database format (%d) is bogus or too new; "
  72. "try getting a newer dpkg"), db_format);
  73. return db_format;
  74. }
  75. enum pkg_infodb_format
  76. pkg_infodb_get_format(void)
  77. {
  78. if (db_format > PKG_INFODB_FORMAT_UNKNOWN)
  79. return db_format;
  80. else
  81. return pkg_infodb_read_format();
  82. }
  83. void
  84. pkg_infodb_set_format(enum pkg_infodb_format version)
  85. {
  86. db_format = version;
  87. }
  88. bool
  89. pkg_infodb_is_upgrading(void)
  90. {
  91. if (db_format < 0)
  92. pkg_infodb_read_format();
  93. return db_upgrading;
  94. }
  95. const char *
  96. pkg_infodb_get_dir(void)
  97. {
  98. static char *infodir;
  99. if (infodir == NULL)
  100. infodir = dpkg_db_get_path(INFODIR);
  101. return infodir;
  102. }
  103. const char *
  104. pkg_infodb_get_file(struct pkginfo *pkg, struct pkgbin *pkgbin,
  105. const char *filetype)
  106. {
  107. static struct varbuf vb;
  108. enum pkg_infodb_format format;
  109. /* Make sure to always read and verify the format version. */
  110. format = pkg_infodb_get_format();
  111. varbuf_reset(&vb);
  112. varbuf_add_str(&vb, pkg_infodb_get_dir());
  113. varbuf_add_char(&vb, '/');
  114. varbuf_add_str(&vb, pkg->set->name);
  115. if (pkgbin->multiarch == PKG_MULTIARCH_SAME &&
  116. format == PKG_INFODB_FORMAT_MULTIARCH)
  117. varbuf_add_archqual(&vb, pkgbin->arch);
  118. varbuf_add_char(&vb, '.');
  119. varbuf_add_str(&vb, filetype);
  120. varbuf_end_str(&vb);
  121. return vb.buf;
  122. }