Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
new -r to pkg check to allow recomputing the flatsize
Baptiste Daroussin committed 14 years ago
commit e19886662d58149aec505e16fb86f6e937426329
parent 0d8a6f3
6 files changed +124 -22
modified libpkg/pkg.c
@@ -1051,3 +1051,45 @@ pkg_test_filesum(struct pkg *pkg)
		}
	}
}
+

+
int64_t
+
pkg_recompute_flatsize(struct pkg *pkg)
+
{
+
	struct pkg_file *f = NULL;
+
	const char *path;
+
	struct hardlinks hl = { NULL, 0, 0 };
+
	int64_t flatsize = 0;
+
	struct stat st;
+
	bool regular = false;
+
	size_t i;
+

+
	while (pkg_files(pkg, &f) == EPKG_OK) {
+
		path = pkg_file_get(f, PKG_FILE_PATH);
+
		if (lstat(path, &st) == 0) {
+
			regular = true;
+
			if (S_ISLNK(st.st_mode))
+
				regular = false;
+

+
			/* special case for hardlinks */
+
			if (st.st_nlink > 1) {
+
				for (i = 0; i < hl.len; i++) {
+
					if (hl.inodes[i] == st.st_ino) {
+
						regular = false;
+
						break;
+
					}
+
				}
+
				if (regular) {
+
					if (hl.cap <= hl.len) {
+
						hl.inodes = reallocf(hl.inodes,
+
						    hl.cap * sizeof(ino_t));
+
					}
+
					hl.inodes[hl.len++] = st.st_ino;
+
				}
+
			}
+

+
			if (regular)
+
				flatsize += st.st_size;
+
		}
+
	}
+
	return (flatsize);
+
}
modified libpkg/pkg.h
@@ -370,7 +370,8 @@ int pkg_analyse_files(struct pkgdb *, struct pkg *);
int pkg_set2(struct pkg *pkg, ...);
#define pkg_set(pkg, ...) pkg_set2(pkg, __VA_ARGS__, -1)

-
/*int pkg_set(struct pkg *pkg, pkg_attr attr, const char *value);*/
+
int pkgdb_set2(struct pkgdb *db, struct pkg *pkg, ...);
+
#define pkgdb_set(db, pkg, ...) pkgdb_set2(db, pkg, __VA_ARGS__, -1)

/**
 * Read the content of a file into a buffer, then call pkg_set().
@@ -814,6 +815,6 @@ int pkg_init(const char *);
int pkg_shutdown(void);

void pkg_test_filesum(struct pkg *);
-
int pkg_recompute_flatsize(struct pkg *);
+
int64_t pkg_recompute_flatsize(struct pkg *);

#endif
modified libpkg/pkg_ports.c
@@ -16,13 +16,6 @@
#include "private/event.h"
#include "private/pkg.h"

-

-
struct hardlinks {
-
	ino_t *inodes;
-
	size_t len;
-
	size_t cap;
-
};
-

struct keyword {
	const char *keyword;
	STAILQ_HEAD(actions, action) actions;
modified libpkg/pkgdb.c
@@ -2678,21 +2678,66 @@ pkgdb_integrity_check(struct pkgdb *db)
struct pkgdb_it *
pkgdb_integrity_conflict_local(struct pkgdb *db, const char *origin)
{
-
       sqlite3_stmt *stmt;
+
	sqlite3_stmt *stmt;

-
       assert(db != NULL && origin != NULL);
+
	assert(db != NULL && origin != NULL);

-
       const char sql_conflicts [] = "SELECT DISTINCT p.id as rowid, p.origin, p.name, p.version, p.prefix "
-
               "FROM packages AS p, files AS f, integritycheck AS i "
-
               "WHERE p.id = f.package_id AND f.path = i.path AND i.origin = ?1";
+
	const char sql_conflicts [] = "SELECT DISTINCT p.id as rowid, p.origin, p.name, p.version, p.prefix "
+
		"FROM packages AS p, files AS f, integritycheck AS i "
+
		"WHERE p.id = f.package_id AND f.path = i.path AND i.origin = ?1";

-
       if (sqlite3_prepare_v2(db->sqlite, sql_conflicts, -1, &stmt, NULL) != SQLITE_OK) {
-
               ERROR_SQLITE(db->sqlite);
-
               return (NULL);
-
       }
+
	if (sqlite3_prepare_v2(db->sqlite, sql_conflicts, -1, &stmt, NULL) != SQLITE_OK) {
+
		ERROR_SQLITE(db->sqlite);
+
		return (NULL);
+
	}

-
       sqlite3_bind_text(stmt, 1, origin, -1, SQLITE_TRANSIENT);
+
	sqlite3_bind_text(stmt, 1, origin, -1, SQLITE_TRANSIENT);
+

+
	return (pkgdb_it_new(db, stmt, PKG_INSTALLED));
+
}

-
       return (pkgdb_it_new(db, stmt, PKG_INSTALLED));
+
static int
+
pkgdb_vset(struct pkgdb *db, int64_t id, va_list ap)
+
{
+
	int attr;
+
	char sql[BUFSIZ];
+

+
	while ((attr = va_arg(ap, int)) > 0) {
+
		switch (attr) {
+
			case PKG_FLATSIZE:
+
				snprintf(sql, BUFSIZ, "update packages set flatsize=%"PRId64" where id=%"PRId64";",
+
				    va_arg(ap, int64_t), id);
+
				sql_exec(db->sqlite, sql);
+
				break;
+
			case PKG_AUTOMATIC:
+
				if (va_arg(ap, int) == 0) {
+
					snprintf(sql, BUFSIZ, "update packages set automatic=0 where id=%"PRId64";", id);
+
					sql_exec(db->sqlite, sql);
+
					break;
+
				} else if (va_arg(ap, int) == 1) {
+
					snprintf(sql, BUFSIZ, "update packages set automatic=1 where id=%"PRId64";", id);
+
					sql_exec(db->sqlite, sql);
+
					break;
+
				}
+
		}
+
	}
+
	return (EPKG_OK);
}

+
int
+
pkgdb_set2(struct pkgdb *db, struct pkg *pkg, ...)
+
{
+
	int ret = EPKG_OK;
+
	int64_t id;
+

+
	va_list ap;
+

+
	assert(pkg != NULL);
+

+
	va_start(ap, pkg);
+
	pkg_get(pkg, PKG_ROWID, &id);
+
	ret = pkgdb_vset(db, id, ap);
+
	va_end(ap);
+

+
	return (ret);
+
}
modified libpkg/private/pkg.h
@@ -126,6 +126,12 @@ struct pkg_group {
	STAILQ_ENTRY(pkg_group) next;
};

+
struct hardlinks {
+
	ino_t *inodes;
+
	size_t len;
+
	size_t cap;
+
};
+

/**
 * Remove and unregister the package.
 * @param pkg An installed package to delete
modified pkg/check.c
@@ -232,7 +232,7 @@ check_summary(struct pkgdb *db, struct deps_head *dh)
void
usage_check(void)
{
-
	fprintf(stderr, "usage: pkg check [-yagdxXs] <pattern>\n\n");
+
	fprintf(stderr, "usage: pkg check [-yagdxXsr] <pattern>\n\n");
	fprintf(stderr, "For more information see 'pkg help check'.\n");
}

@@ -249,12 +249,15 @@ exec_check(int argc, char **argv)
	bool yes = false;
	bool dcheck = false;
	bool checksums = true;
+
	bool recomputeflatsize = false;
	int nbpkgs = 0;
+
	int64_t flatsize;
+
	int64_t newflatsize;
	int i;

	struct deps_head dh = STAILQ_HEAD_INITIALIZER(dh);

-
	while ((ch = getopt(argc, argv, "yagdxXs")) != -1) {
+
	while ((ch = getopt(argc, argv, "yagdxXsr")) != -1) {
		switch (ch) {
			case 'a':
				match = MATCH_ALL;
@@ -277,6 +280,11 @@ exec_check(int argc, char **argv)
			case 's':
				checksums = true;
				break;
+
			case 'r':
+
				recomputeflatsize = true;
+
				if (geteuid() != 0)
+
					errx(EX_USAGE, "Needs to be root to recompute the flatsize");
+
				break;
			default:
				usage_check();
				return (EX_USAGE);
@@ -314,6 +322,13 @@ exec_check(int argc, char **argv)
				nbpkgs += check_deps(db, pkg, &dh);
			if (checksums)
				pkg_test_filesum(pkg);
+
			if (recomputeflatsize) {
+
				newflatsize = pkg_recompute_flatsize(pkg);
+
				pkg_get(pkg, PKG_FLATSIZE, &flatsize);
+
				printf(" %ld\n", newflatsize);
+
				if (newflatsize != flatsize)
+
					pkgdb_set(db, pkg, PKG_FLATSIZE, newflatsize);
+
			}
		}

		if (geteuid() == 0 && nbpkgs > 0) {