Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Merge pull request #247 from rg1/update-checksums
Baptiste Daroussin committed 13 years ago
commit 737b7f4c6c25706fa4350565c16e8c5a6fa7fb33
parent a5bc3b4
5 files changed +61 -19
modified libpkg/pkg.c
@@ -1119,22 +1119,29 @@ pkg_test_filesum(struct pkg *pkg)
	}
}

-
int64_t
-
pkg_recompute_flatsize(struct pkg *pkg)
+
void
+
pkg_recompute(struct pkgdb *db, struct pkg *pkg)
{
	struct pkg_file *f = NULL;
	const char *path;
	struct hardlinks hl = { NULL, 0, 0 };
	int64_t flatsize = 0;
+
	int64_t oldflatsize;
	struct stat st;
	bool regular = false;
+
	const char *sum;
+
	char sha256[SHA256_DIGEST_LENGTH * 2 + 1];

	while (pkg_files(pkg, &f) == EPKG_OK) {
		path = pkg_file_get(f, PKG_FILE_PATH);
+
		sum = pkg_file_get(f, PKG_FILE_SUM);
		if (lstat(path, &st) == 0) {
			regular = true;
-
			if (S_ISLNK(st.st_mode))
+
			if (S_ISLNK(st.st_mode)) {
				regular = false;
+
				*sha256 = '\0';
+
			} else
+
				sha256_file(path, sha256);

			/* special case for hardlinks */
			if (st.st_nlink > 1)
@@ -1143,8 +1150,13 @@ pkg_recompute_flatsize(struct pkg *pkg)
			if (regular)
				flatsize += st.st_size;
		}
+
		if (strcmp(sha256, sum) != 0)
+
			pkgdb_file_set_cksum(db, f, sha256);
	}
-
	return (flatsize);
+

+
	pkg_get(pkg, PKG_FLATSIZE, &oldflatsize);
+
	if (flatsize != oldflatsize)
+
		pkgdb_set(db, pkg, PKG_SET_FLATSIZE, flatsize);
}

int
modified libpkg/pkg.h
@@ -444,6 +444,16 @@ int pkgdb_set2(struct pkgdb *db, struct pkg *pkg, ...);
#define pkgdb_set(db, pkg, ...) pkgdb_set2(db, pkg, __VA_ARGS__, -1)

/**
+
 * update the checksum of a file in the database (and the object)
+
 * XXX I don't think this function should be part of a public API.
+
 * @param db A pointer to a struct pkgdb object
+
 * @param file A pointer to a struct pkg_file object
+
 * @param sha256 The new checksum
+
 * @return An error code
+
 */
+
int pkgdb_file_set_cksum(struct pkgdb *db, struct pkg_file *file, const char *sha256);
+

+
/**
 * Read the content of a file into a buffer, then call pkg_set().
 */
int pkg_set_from_file(struct pkg *pkg, pkg_attr attr, const char *file);
@@ -914,7 +924,7 @@ int pkg_init(const char *);
int pkg_shutdown(void);

void pkg_test_filesum(struct pkg *);
-
int64_t pkg_recompute_flatsize(struct pkg *);
+
void pkg_recompute(struct pkgdb *, struct pkg *);

int pkg_get_myarch(char *pkgarch, size_t sz);

modified libpkg/pkgdb.c
@@ -3111,6 +3111,31 @@ pkgdb_set2(struct pkgdb *db, struct pkg *pkg, ...)
	return (ret);
}

+
int
+
pkgdb_file_set_cksum(struct pkgdb *db, struct pkg_file *file, const char *sha256)
+
{
+
	sqlite3_stmt *stmt = NULL;
+
	const char sql_file_update[] = ""
+
		"UPDATE files SET sha256=?1 WHERE path=?2";
+

+
	if (sqlite3_prepare_v2(db->sqlite, sql_file_update, -1, &stmt, NULL) != SQLITE_OK) {
+
		ERROR_SQLITE(db->sqlite);
+
		return (EPKG_FATAL);
+
	}
+
	sqlite3_bind_text(stmt, 1, sha256, -1, SQLITE_STATIC);
+
	sqlite3_bind_text(stmt, 2, pkg_file_get(file, PKG_FILE_PATH), -1, SQLITE_STATIC);
+

+
	if (sqlite3_step(stmt) != SQLITE_DONE) {
+
		ERROR_SQLITE(db->sqlite);
+
		sqlite3_finalize(stmt);
+
		return (EPKG_FATAL);
+
	}
+
	sqlite3_finalize(stmt);
+
	strlcpy(file->sum, sha256, sizeof(file->sum));
+

+
	return (EPKG_OK);
+
}
+

struct pkgdb_it *
pkgdb_query_fetch(struct pkgdb *db, match_t match, int nbpkgs, char **pkgs, const char *repo, int flags)
{
modified pkg/check.c
@@ -239,10 +239,8 @@ exec_check(int argc, char **argv)
	bool yes = false;
	bool dcheck = false;
	bool checksums = false;
-
	bool recomputeflatsize = false;
+
	bool recompute = false;
	int nbpkgs = 0;
-
	int64_t flatsize;
-
	int64_t newflatsize;
	int i;
	int verbose = 0;

@@ -274,10 +272,10 @@ exec_check(int argc, char **argv)
				flags |= PKG_LOAD_FILES;
				break;
			case 'r':
-
				recomputeflatsize = true;
+
				recompute = true;
				flags |= PKG_LOAD_FILES;
				if (geteuid() != 0)
-
					errx(EX_USAGE, "Needs to be root to recompute the flatsize");
+
					errx(EX_USAGE, "Needs to be root to recompute the checksums and size");
				break;
			case 'v':
				verbose = 1;
@@ -291,9 +289,9 @@ exec_check(int argc, char **argv)
	argv += optind;

	/* Default to all packages if no pkg provided */
-
	if (argc == 0 && (dcheck || checksums || recomputeflatsize)) {
+
	if (argc == 0 && (dcheck || checksums || recompute)) {
		match = MATCH_ALL;
-
	} else if ((argc == 0 && match != MATCH_ALL) || !(dcheck || checksums || recomputeflatsize)) {
+
	} else if ((argc == 0 && match != MATCH_ALL) || !(dcheck || checksums || recompute)) {
		usage_check();
		return (EX_USAGE);
	}
@@ -330,13 +328,10 @@ exec_check(int argc, char **argv)
					printf("Checking checksums: %s\n", pkgname);
				pkg_test_filesum(pkg);
			}
-
			if (recomputeflatsize) {
+
			if (recompute) {
				if (verbose)
-
					printf("Recomputing size: %s\n", pkgname);
-
				newflatsize = pkg_recompute_flatsize(pkg);
-
				pkg_get(pkg, PKG_FLATSIZE, &flatsize);
-
				if (newflatsize != flatsize)
-
					pkgdb_set(db, pkg, PKG_SET_FLATSIZE, newflatsize);
+
					printf("Recomputing size and sums: %s\n", pkgname);
+
				pkg_recompute(db, pkg);
			}
		}

modified pkg/pkg-check.8
@@ -33,7 +33,7 @@ is used to check for and install missing dependencies.
.Pp
.Nm
.Fl r
-
is used to recompute sizes of installed packages.
+
is used to recompute sizes and checksums of installed packages.
.Pp
.Nm
.Fl s