Преглед изворни кода

dpkg: Update on-disk database to use a multiarch compliant layout

The usage of the new layout is conditional to a prior database
upgrade that should write a version number greater than 0 in
<admindir>/info/format.

The file is parsed when needed from pkg_infodb_get_format().

Based-on-patch-by: Raphaël Hertzog <hertzog@debian.org>
Patch-sponsored-by: Linaro Limited

Signed-off-by: Guillem Jover <guillem@debian.org>
Guillem Jover пре 14 година
родитељ
комит
69a5e4d5cb
6 измењених фајлова са 139 додато и 2 уклоњено
  1. 1 0
      po/POTFILES.in
  2. 4 0
      src/Makefile.am
  3. 4 0
      src/filesdb.c
  4. 9 2
      src/infodb-access.c
  5. 111 0
      src/infodb-format.c
  6. 10 0
      src/infodb.h

+ 1 - 0
po/POTFILES.in

@@ -52,6 +52,7 @@ src/errors.c
 src/filesdb.c
 src/filesdb.c
 src/help.c
 src/help.c
 src/infodb-access.c
 src/infodb-access.c
+src/infodb-format.c
 src/main.c
 src/main.c
 src/packages.c
 src/packages.c
 src/processarc.c
 src/processarc.c

+ 4 - 0
src/Makefile.am

@@ -40,6 +40,7 @@ dpkg_SOURCES = \
 	file-match.c file-match.h \
 	file-match.c file-match.h \
 	filters.c filters.h \
 	filters.c filters.h \
 	infodb-access.c \
 	infodb-access.c \
+	infodb-format.c \
 	divertdb.c \
 	divertdb.c \
 	statdb.c \
 	statdb.c \
 	help.c \
 	help.c \
@@ -57,17 +58,20 @@ dpkg_LDADD = \
 
 
 dpkg_divert_SOURCES = \
 dpkg_divert_SOURCES = \
 	filesdb.c \
 	filesdb.c \
+	infodb-format.c \
 	divertdb.c \
 	divertdb.c \
 	divertcmd.c
 	divertcmd.c
 
 
 dpkg_query_SOURCES = \
 dpkg_query_SOURCES = \
 	filesdb.c \
 	filesdb.c \
 	infodb-access.c \
 	infodb-access.c \
+	infodb-format.c \
 	divertdb.c \
 	divertdb.c \
 	querycmd.c
 	querycmd.c
 
 
 dpkg_statoverride_SOURCES = \
 dpkg_statoverride_SOURCES = \
 	filesdb.c \
 	filesdb.c \
+	infodb-format.c \
 	statdb.c \
 	statdb.c \
 	statcmd.c
 	statcmd.c
 
 

+ 4 - 0
src/filesdb.c

@@ -50,6 +50,7 @@
 #include <dpkg/progress.h>
 #include <dpkg/progress.h>
 
 
 #include "filesdb.h"
 #include "filesdb.h"
+#include "infodb.h"
 #include "main.h"
 #include "main.h"
 
 
 /*** Package control information database directory routines. ***/
 /*** Package control information database directory routines. ***/
@@ -85,6 +86,9 @@ pkgadminfile(struct pkginfo *pkg, struct pkgbin *pkgbin, const char *filetype)
   varbuf_add_str(&vb, infodir);
   varbuf_add_str(&vb, infodir);
   varbuf_add_char(&vb, '/');
   varbuf_add_char(&vb, '/');
   varbuf_add_str(&vb, pkg->set->name);
   varbuf_add_str(&vb, pkg->set->name);
+  if (pkgbin->multiarch == multiarch_same &&
+      pkg_infodb_get_format() == pkg_infodb_format_multiarch)
+    varbuf_add_archqual(&vb, pkgbin->arch);
   varbuf_add_char(&vb, '.');
   varbuf_add_char(&vb, '.');
   varbuf_add_str(&vb, filetype);
   varbuf_add_str(&vb, filetype);
   varbuf_end_str(&vb);
   varbuf_end_str(&vb);

+ 9 - 2
src/infodb-access.c

@@ -60,8 +60,15 @@ pkg_infodb_foreach(struct pkginfo *pkg, struct pkgbin *pkgbin,
 	DIR *db_dir;
 	DIR *db_dir;
 	struct dirent *db_de;
 	struct dirent *db_de;
 	struct varbuf db_path = VARBUF_INIT;
 	struct varbuf db_path = VARBUF_INIT;
+	const char *pkgname;
 	size_t db_path_len;
 	size_t db_path_len;
 
 
+	if (pkgbin->multiarch == multiarch_same &&
+	    pkg_infodb_get_format() == pkg_infodb_format_multiarch)
+		pkgname = pkgbin_name(pkg, pkgbin, pnaw_always);
+	else
+		pkgname = pkgbin_name(pkg, pkgbin, pnaw_never);
+
 	varbuf_add_str(&db_path, pkgadmindir());
 	varbuf_add_str(&db_path, pkgadmindir());
 	varbuf_add_char(&db_path, '/');
 	varbuf_add_char(&db_path, '/');
 	db_path_len = db_path.used;
 	db_path_len = db_path.used;
@@ -88,8 +95,8 @@ pkg_infodb_foreach(struct pkginfo *pkg, struct pkgbin *pkgbin,
 			continue;
 			continue;
 
 
 		/* Ignore files from other packages. */
 		/* Ignore files from other packages. */
-		if (strlen(pkg->set->name) != (size_t)(dot - db_de->d_name) ||
-		    strncmp(db_de->d_name, pkg->set->name, dot - db_de->d_name))
+		if (strlen(pkgname) != (size_t)(dot - db_de->d_name) ||
+		    strncmp(db_de->d_name, pkgname, dot - db_de->d_name))
 			continue;
 			continue;
 
 
 		debug(dbg_stupidlyverbose, "infodb foreach file this pkg");
 		debug(dbg_stupidlyverbose, "infodb foreach file this pkg");

+ 111 - 0
src/infodb-format.c

@@ -0,0 +1,111 @@
+/*
+ * dpkg - main program for package management
+ * infodb-format.c - package control information database format
+ *
+ * Copyright © 2011 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <dpkg/i18n.h>
+#include <dpkg/dpkg.h>
+#include <dpkg/dpkg-db.h>
+
+#include "infodb.h"
+
+static enum pkg_infodb_format db_format = pkg_infodb_format_unknown;
+static bool db_upgrading;
+
+static enum pkg_infodb_format
+pkg_infodb_parse_format(const char *file)
+{
+	FILE *fp;
+	int format;
+
+	fp = fopen(file, "r");
+	if (fp == NULL) {
+		/* A missing format file means legacy format (0). */
+		if (errno == ENOENT)
+			return pkg_infodb_format_legacy;
+		ohshite(_("error trying to open %.250s"), file);
+	}
+
+	if (fscanf(fp, "%u", &format) != 1)
+		ohshit(_("corrupt info database format file '%s'"), file);
+
+	fclose(fp);
+
+	return format;
+}
+
+static enum pkg_infodb_format
+pkg_infodb_read_format(void)
+{
+	struct atomic_file *file;
+	struct stat st;
+	char *filename;
+
+	filename = dpkg_db_get_path(INFODIR "/format");
+	file = atomic_file_new(filename, 0);
+
+	db_format = pkg_infodb_parse_format(file->name);
+
+	/* Check if a previous upgrade got interrupted. Because we are only
+	 * supposed to upgrade the db layout one format at a time, if the
+	 * new file exists that means the new format is just one ahead,
+	 * we don't try to read it because it contains unreliable data. */
+	if (stat(file->name_new, &st) == 0) {
+		db_format++;
+		db_upgrading = true;
+	}
+
+	atomic_file_free(file);
+	free(filename);
+
+	return db_format;
+}
+
+enum pkg_infodb_format
+pkg_infodb_get_format(void)
+{
+	if (db_format > pkg_infodb_format_unknown)
+		return db_format;
+	else
+		return pkg_infodb_read_format();
+}
+
+void
+pkg_infodb_set_format(enum pkg_infodb_format version)
+{
+	db_format = version;
+}
+
+bool
+pkg_infodb_is_upgrading(void)
+{
+	if (db_format < 0)
+		pkg_infodb_read_format();
+
+	return db_upgrading;
+}

+ 10 - 0
src/infodb.h

@@ -25,6 +25,16 @@
 
 
 #include <dpkg/dpkg-db.h>
 #include <dpkg/dpkg-db.h>
 
 
+enum pkg_infodb_format {
+	pkg_infodb_format_unknown = -1,
+	pkg_infodb_format_legacy = 0,
+	pkg_infodb_format_multiarch = 1,
+};
+
+enum pkg_infodb_format pkg_infodb_get_format(void);
+void pkg_infodb_set_format(enum pkg_infodb_format format);
+bool pkg_infodb_is_upgrading(void);
+
 bool pkg_infodb_has_file(struct pkginfo *pkg, struct pkgbin *pkgbin,
 bool pkg_infodb_has_file(struct pkginfo *pkg, struct pkgbin *pkgbin,
                          const char *name);
                          const char *name);