infodb-format.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * dpkg - main program for package management
  3. * infodb-format.c - package control information database format
  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 <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. return db_format;
  71. }
  72. enum pkg_infodb_format
  73. pkg_infodb_get_format(void)
  74. {
  75. if (db_format > pkg_infodb_format_unknown)
  76. return db_format;
  77. else
  78. return pkg_infodb_read_format();
  79. }
  80. void
  81. pkg_infodb_set_format(enum pkg_infodb_format version)
  82. {
  83. db_format = version;
  84. }
  85. bool
  86. pkg_infodb_is_upgrading(void)
  87. {
  88. if (db_format < 0)
  89. pkg_infodb_read_format();
  90. return db_upgrading;
  91. }
  92. const char *
  93. pkg_infodb_get_dir(void)
  94. {
  95. static char *infodir;
  96. if (infodir == NULL)
  97. infodir = dpkg_db_get_path(INFODIR);
  98. return infodir;
  99. }
  100. const char *
  101. pkg_infodb_get_file(struct pkginfo *pkg, struct pkgbin *pkgbin,
  102. const char *filetype)
  103. {
  104. static struct varbuf vb;
  105. varbuf_reset(&vb);
  106. varbuf_add_str(&vb, pkg_infodb_get_dir());
  107. varbuf_add_char(&vb, '/');
  108. varbuf_add_str(&vb, pkg->set->name);
  109. if (pkgbin->multiarch == multiarch_same &&
  110. pkg_infodb_get_format() == pkg_infodb_format_multiarch)
  111. varbuf_add_archqual(&vb, pkgbin->arch);
  112. varbuf_add_char(&vb, '.');
  113. varbuf_add_str(&vb, filetype);
  114. varbuf_end_str(&vb);
  115. return vb.buf;
  116. }