2 * dpkg - main program for package management
3 * infodb-upgrade.c - package control information database format upgrade
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>
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.
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.
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/>.
27 #include <sys/types.h>
34 #include <dpkg/i18n.h>
35 #include <dpkg/dpkg.h>
36 #include <dpkg/dpkg-db.h>
37 #include <dpkg/path.h>
44 struct rename_node
*next
;
49 /* Global variables. */
50 static struct rename_node
*rename_head
= NULL
;
52 static struct rename_node
*
53 rename_node_new(const char *old
, const char *new, struct rename_node
*next
)
55 struct rename_node
*node
;
57 node
= m_malloc(sizeof(*node
));
59 node
->old
= m_strdup(old
);
60 node
->new = m_strdup(new);
66 rename_node_free(struct rename_node
*node
)
74 pkg_infodb_link_multiarch_files(void)
78 struct varbuf pkgname
= VARBUF_INIT
;
79 struct varbuf oldname
= VARBUF_INIT
;
80 struct varbuf newname
= VARBUF_INIT
;
81 struct varbuf_state db_path_state
;
83 varbuf_add_str(&oldname
, pkg_infodb_get_dir());
84 varbuf_add_char(&oldname
, '/');
85 varbuf_end_str(&oldname
);
86 varbuf_snapshot(&oldname
, &db_path_state
);
88 varbuf_add_buf(&newname
, oldname
.buf
, oldname
.used
);
89 varbuf_end_str(&newname
);
91 db_dir
= opendir(oldname
.buf
);
93 ohshite(_("cannot read info directory"));
95 push_cleanup(cu_closedir
, ~0, NULL
, 0, 1, (void *)db_dir
);
96 while ((db_de
= readdir(db_dir
)) != NULL
) {
97 const char *filetype
, *dot
;
101 /* Ignore dotfiles, including ‘.’ and ‘..’. */
102 if (db_de
->d_name
[0] == '.')
105 /* Ignore anything odd. */
106 dot
= strrchr(db_de
->d_name
, '.');
110 varbuf_reset(&pkgname
);
111 varbuf_add_buf(&pkgname
, db_de
->d_name
, dot
- db_de
->d_name
);
112 varbuf_end_str(&pkgname
);
114 /* Skip files already converted. */
115 if (strchr(pkgname
.buf
, ':'))
118 set
= pkg_db_find_set(pkgname
.buf
);
119 for (pkg
= &set
->pkg
; pkg
; pkg
= pkg
->arch_next
)
120 if (pkg
->status
!= PKG_STAT_NOTINSTALLED
)
123 warning(_("info file %s/%s not associated to any package"),
124 pkg_infodb_get_dir(), db_de
->d_name
);
128 /* Does it need to be upgraded? */
129 if (pkg
->installed
.multiarch
!= PKG_MULTIARCH_SAME
)
132 /* Skip past the full stop. */
135 varbuf_rollback(&oldname
, &db_path_state
);
136 varbuf_add_str(&oldname
, db_de
->d_name
);
137 varbuf_end_str(&oldname
);
139 varbuf_rollback(&newname
, &db_path_state
);
140 varbuf_add_pkgbin_name(&newname
, pkg
, &pkg
->installed
, pnaw_always
);
141 varbuf_add_char(&newname
, '.');
142 varbuf_add_str(&newname
, filetype
);
143 varbuf_end_str(&newname
);
145 if (link(oldname
.buf
, newname
.buf
) && errno
!= EEXIST
)
146 ohshite(_("error creating hard link '%.255s'"),
148 rename_head
= rename_node_new(oldname
.buf
, newname
.buf
, rename_head
);
150 pop_cleanup(ehflag_normaltidy
); /* closedir */
152 varbuf_destroy(&pkgname
);
153 varbuf_destroy(&newname
);
154 varbuf_destroy(&oldname
);
158 cu_abort_db_upgrade(int argc
, void **argv
)
160 struct atomic_file
*file
= argv
[0];
161 struct rename_node
*next
;
163 /* Restore the old files if needed and drop the newly created files. */
164 while (rename_head
) {
165 next
= rename_head
->next
;
166 if (link(rename_head
->new, rename_head
->old
) && errno
!= EEXIST
)
167 ohshite(_("error creating hard link '%.255s'"),
169 if (unlink(rename_head
->new))
170 ohshite(_("cannot remove '%.250s'"), rename_head
->new);
171 rename_node_free(rename_head
);
174 if (unlink(file
->name_new
) && errno
!= ENOENT
)
175 ohshite(_("cannot remove '%.250s'"), file
->name_new
);
177 atomic_file_free(file
);
181 pkg_infodb_write_format(struct atomic_file
*file
, int version
)
183 if (fprintf(file
->fp
, "%d\n", version
) < 0)
184 ohshite(_("error while writing '%s'"), file
->name_new
);
186 atomic_file_sync(file
);
187 atomic_file_close(file
);
188 dir_sync_path_parent(file
->name
);
190 pkg_infodb_set_format(version
);
194 pkg_infodb_unlink_monoarch_files(void)
196 struct rename_node
*next
;
198 while (rename_head
) {
199 next
= rename_head
->next
;
200 if (unlink(rename_head
->old
))
201 ohshite(_("cannot remove '%.250s'"), rename_head
->old
);
202 rename_node_free(rename_head
);
208 pkg_infodb_upgrade_to_multiarch(void)
210 struct atomic_file
*db_file
;
211 char *db_format_file
;
213 db_format_file
= dpkg_db_get_path(INFODIR
"/format");
214 db_file
= atomic_file_new(db_format_file
, 0);
215 atomic_file_open(db_file
);
217 push_cleanup(cu_abort_db_upgrade
, ehflag_bombout
, NULL
, 0, 1, db_file
);
219 pkg_infodb_link_multiarch_files();
220 pkg_infodb_write_format(db_file
, 1);
222 pkg_infodb_unlink_monoarch_files();
223 atomic_file_commit(db_file
);
224 dir_sync_path(pkg_infodb_get_dir());
226 pop_cleanup(ehflag_normaltidy
);
228 atomic_file_free(db_file
);
229 free(db_format_file
);
233 * Upgrade the infodb if there's the need and possibility.
235 * Currently this implies, that the modstatdb was opened for writing and:
236 * - previous upgrade has not been completed; or
237 * - current format is not the latest one.
240 pkg_infodb_upgrade(void)
242 enum pkg_infodb_format db_format
;
244 /* Make sure to always read and verify the format version. */
245 db_format
= pkg_infodb_get_format();
247 if (modstatdb_get_status() < msdbrw_write
)
250 if (db_format
< PKG_INFODB_FORMAT_MULTIARCH
||
251 pkg_infodb_is_upgrading())
252 pkg_infodb_upgrade_to_multiarch();