infodb-upgrade.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * dpkg - main program for package management
  3. * infodb-upgrade.c - package control information database format upgrade
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2011-2014 Guillem Jover <guillem@debian.org>
  7. * Copyright © 2011 Linaro Limited
  8. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. #include <config.h>
  24. #include <compat.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/dpkg.h>
  32. #include <dpkg/dpkg-db.h>
  33. #include <dpkg/path.h>
  34. #include <dpkg/dir.h>
  35. #include "filesdb.h"
  36. #include "infodb.h"
  37. struct rename_node {
  38. struct rename_node *next;
  39. char *old;
  40. char *new;
  41. };
  42. /* Global variables. */
  43. static struct rename_node *rename_head = NULL;
  44. static struct rename_node *
  45. rename_node_new(const char *old, const char *new, struct rename_node *next)
  46. {
  47. struct rename_node *node;
  48. node = m_malloc(sizeof(*node));
  49. node->next = next;
  50. node->old = m_strdup(old);
  51. node->new = m_strdup(new);
  52. return node;
  53. }
  54. static void
  55. rename_node_free(struct rename_node *node)
  56. {
  57. free(node->old);
  58. free(node->new);
  59. free(node);
  60. }
  61. static void
  62. pkg_infodb_link_multiarch_files(void)
  63. {
  64. DIR *db_dir;
  65. struct dirent *db_de;
  66. struct varbuf pkgname = VARBUF_INIT;
  67. struct varbuf oldname = VARBUF_INIT;
  68. struct varbuf newname = VARBUF_INIT;
  69. struct varbuf_state db_path_state;
  70. varbuf_add_str(&oldname, pkg_infodb_get_dir());
  71. varbuf_add_char(&oldname, '/');
  72. varbuf_end_str(&oldname);
  73. varbuf_snapshot(&oldname, &db_path_state);
  74. varbuf_add_buf(&newname, oldname.buf, oldname.used);
  75. varbuf_end_str(&newname);
  76. db_dir = opendir(oldname.buf);
  77. if (!db_dir)
  78. ohshite(_("cannot read info directory"));
  79. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)db_dir);
  80. while ((db_de = readdir(db_dir)) != NULL) {
  81. const char *filetype, *dot;
  82. struct pkginfo *pkg;
  83. struct pkgset *set;
  84. /* Ignore dotfiles, including ‘.’ and ‘..’. */
  85. if (db_de->d_name[0] == '.')
  86. continue;
  87. /* Ignore anything odd. */
  88. dot = strrchr(db_de->d_name, '.');
  89. if (dot == NULL)
  90. continue;
  91. varbuf_reset(&pkgname);
  92. varbuf_add_buf(&pkgname, db_de->d_name, dot - db_de->d_name);
  93. varbuf_end_str(&pkgname);
  94. /* Skip files already converted. */
  95. if (strchr(pkgname.buf, ':'))
  96. continue;
  97. set = pkg_db_find_set(pkgname.buf);
  98. for (pkg = &set->pkg; pkg; pkg = pkg->arch_next)
  99. if (pkg->status != PKG_STAT_NOTINSTALLED)
  100. break;
  101. if (!pkg) {
  102. warning(_("info file %s/%s not associated to any package"),
  103. pkg_infodb_get_dir(), db_de->d_name);
  104. continue;
  105. }
  106. /* Does it need to be upgraded? */
  107. if (pkg->installed.multiarch != PKG_MULTIARCH_SAME)
  108. continue;
  109. /* Skip past the full stop. */
  110. filetype = dot + 1;
  111. varbuf_rollback(&oldname, &db_path_state);
  112. varbuf_add_str(&oldname, db_de->d_name);
  113. varbuf_end_str(&oldname);
  114. varbuf_rollback(&newname, &db_path_state);
  115. varbuf_add_pkgbin_name(&newname, pkg, &pkg->installed, pnaw_always);
  116. varbuf_add_char(&newname, '.');
  117. varbuf_add_str(&newname, filetype);
  118. varbuf_end_str(&newname);
  119. if (link(oldname.buf, newname.buf) && errno != EEXIST)
  120. ohshite(_("error creating hard link '%.255s'"),
  121. newname.buf);
  122. rename_head = rename_node_new(oldname.buf, newname.buf, rename_head);
  123. }
  124. pop_cleanup(ehflag_normaltidy); /* closedir */
  125. varbuf_destroy(&pkgname);
  126. varbuf_destroy(&newname);
  127. varbuf_destroy(&oldname);
  128. }
  129. static void
  130. cu_abort_db_upgrade(int argc, void **argv)
  131. {
  132. struct atomic_file *file = argv[0];
  133. struct rename_node *next;
  134. /* Restore the old files if needed and drop the newly created files. */
  135. while (rename_head) {
  136. next = rename_head->next;
  137. if (link(rename_head->new, rename_head->old) && errno != EEXIST)
  138. ohshite(_("error creating hard link '%.255s'"),
  139. rename_head->old);
  140. if (unlink(rename_head->new))
  141. ohshite(_("cannot remove '%.250s'"), rename_head->new);
  142. rename_node_free(rename_head);
  143. rename_head = next;
  144. }
  145. if (unlink(file->name_new) && errno != ENOENT)
  146. ohshite(_("cannot remove '%.250s'"), file->name_new);
  147. atomic_file_free(file);
  148. }
  149. static void
  150. pkg_infodb_write_format(struct atomic_file *file, int version)
  151. {
  152. if (fprintf(file->fp, "%d\n", version) < 0)
  153. ohshite(_("error while writing '%s'"), file->name_new);
  154. atomic_file_sync(file);
  155. atomic_file_close(file);
  156. dir_sync_path_parent(file->name);
  157. pkg_infodb_set_format(version);
  158. }
  159. static void
  160. pkg_infodb_unlink_monoarch_files(void)
  161. {
  162. struct rename_node *next;
  163. while (rename_head) {
  164. next = rename_head->next;
  165. if (unlink(rename_head->old))
  166. ohshite(_("cannot remove '%.250s'"), rename_head->old);
  167. rename_node_free(rename_head);
  168. rename_head = next;
  169. }
  170. }
  171. static void
  172. pkg_infodb_upgrade_to_multiarch(void)
  173. {
  174. struct atomic_file *db_file;
  175. char *db_format_file;
  176. db_format_file = dpkg_db_get_path(INFODIR "/format");
  177. db_file = atomic_file_new(db_format_file, 0);
  178. atomic_file_open(db_file);
  179. push_cleanup(cu_abort_db_upgrade, ehflag_bombout, NULL, 0, 1, db_file);
  180. pkg_infodb_link_multiarch_files();
  181. pkg_infodb_write_format(db_file, 1);
  182. pkg_infodb_unlink_monoarch_files();
  183. atomic_file_commit(db_file);
  184. dir_sync_path(pkg_infodb_get_dir());
  185. pop_cleanup(ehflag_normaltidy);
  186. atomic_file_free(db_file);
  187. free(db_format_file);
  188. }
  189. /**
  190. * Upgrade the infodb if there's the need and possibility.
  191. *
  192. * Currently this implies, that the modstatdb was opened for writing and:
  193. * - previous upgrade has not been completed; or
  194. * - current format is not the latest one.
  195. */
  196. void
  197. pkg_infodb_upgrade(void)
  198. {
  199. enum pkg_infodb_format db_format;
  200. /* Make sure to always read and verify the format version. */
  201. db_format = pkg_infodb_get_format();
  202. if (modstatdb_get_status() < msdbrw_write)
  203. return;
  204. if (db_format < PKG_INFODB_FORMAT_MULTIARCH ||
  205. pkg_infodb_is_upgrading())
  206. pkg_infodb_upgrade_to_multiarch();
  207. }