infodb-upgrade.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * dpkg - main program for package management
  3. * infodb-upgrade.c - package control information database format upgrade
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@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. size_t db_path_len;
  70. varbuf_add_str(&oldname, pkg_infodb_get_dir());
  71. varbuf_add_char(&oldname, '/');
  72. db_path_len = oldname.used;
  73. varbuf_end_str(&oldname);
  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 != 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 != multiarch_same)
  108. continue;
  109. /* Skip past the full stop. */
  110. filetype = dot + 1;
  111. varbuf_trunc(&oldname, db_path_len);
  112. varbuf_add_str(&oldname, db_de->d_name);
  113. varbuf_end_str(&oldname);
  114. varbuf_trunc(&newname, db_path_len);
  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(&newname);
  126. varbuf_destroy(&oldname);
  127. }
  128. static void
  129. cu_abort_db_upgrade(int argc, void **argv)
  130. {
  131. struct atomic_file *file = argv[0];
  132. struct rename_node *next;
  133. /* Restore the old files if needed and drop the newly created files. */
  134. while (rename_head) {
  135. next = rename_head->next;
  136. if (link(rename_head->new, rename_head->old) && errno != EEXIST)
  137. ohshite(_("error creating hard link `%.255s'"),
  138. rename_head->old);
  139. if (unlink(rename_head->new))
  140. ohshite(_("cannot remove `%.250s'"), rename_head->new);
  141. rename_node_free(rename_head);
  142. rename_head = next;
  143. }
  144. if (unlink(file->name_new) && errno != ENOENT)
  145. ohshite(_("cannot remove `%.250s'"), file->name_new);
  146. atomic_file_free(file);
  147. }
  148. static void
  149. pkg_infodb_write_format(struct atomic_file *file, int version)
  150. {
  151. if (fprintf(file->fp, "%d\n", version) < 0)
  152. ohshite(_("error while writing '%s'"), file->name_new);
  153. atomic_file_sync(file);
  154. atomic_file_close(file);
  155. dir_sync_path_parent(file->name);
  156. pkg_infodb_set_format(version);
  157. }
  158. static void
  159. pkg_infodb_unlink_monoarch_files(void)
  160. {
  161. struct rename_node *next;
  162. while (rename_head) {
  163. next = rename_head->next;
  164. if (unlink(rename_head->old))
  165. ohshite(_("cannot remove `%.250s'"), rename_head->old);
  166. rename_node_free(rename_head);
  167. rename_head = next;
  168. }
  169. }
  170. static void
  171. pkg_infodb_upgrade_to_multiarch(void)
  172. {
  173. struct atomic_file *db_file;
  174. char *db_format_file;
  175. db_format_file = dpkg_db_get_path(INFODIR "/format");
  176. db_file = atomic_file_new(db_format_file, 0);
  177. atomic_file_open(db_file);
  178. push_cleanup(cu_abort_db_upgrade, ehflag_bombout, NULL, 0, 1, db_file);
  179. pkg_infodb_link_multiarch_files();
  180. pkg_infodb_write_format(db_file, 1);
  181. pkg_infodb_unlink_monoarch_files();
  182. atomic_file_commit(db_file);
  183. dir_sync_path(pkg_infodb_get_dir());
  184. pop_cleanup(ehflag_normaltidy);
  185. atomic_file_free(db_file);
  186. free(db_format_file);
  187. }
  188. /**
  189. * Upgrade the infodb if there's the need and possibility.
  190. *
  191. * Currently this implies, that the modstatdb was opened for writing and:
  192. * - previous upgrade has not been completed; or
  193. * - current format is not the latest one.
  194. */
  195. void
  196. pkg_infodb_upgrade(void)
  197. {
  198. enum pkg_infodb_format db_format;
  199. /* Make sure to always read and verify the format version. */
  200. db_format = pkg_infodb_get_format();
  201. if (modstatdb_get_status() < msdbrw_write)
  202. return;
  203. if (db_format < PKG_INFODB_FORMAT_MULTIARCH ||
  204. pkg_infodb_is_upgrading())
  205. pkg_infodb_upgrade_to_multiarch();
  206. }