Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Convert UTHASH to khash for hardlinks checking
Baptiste Daroussin committed 11 years ago
commit c68e10531abc2f6a67c3620af71f428988b11984
parent 5255863
6 files changed +22 -23
modified libpkg/pkg.c
@@ -1746,13 +1746,14 @@ int
pkg_recompute(struct pkgdb *db, struct pkg *pkg)
{
	struct pkg_file *f = NULL;
-
	struct hardlinks *hl = NULL;
+
	hardlinks_t *hl = NULL;
	int64_t flatsize = 0;
	struct stat st;
	bool regular = false;
	char sha256[SHA256_DIGEST_LENGTH * 2 + 1];
	int rc = EPKG_OK;

+
	hl = kh_init_hardlinks();
	while (pkg_files(pkg, &f) == EPKG_OK) {
		if (lstat(f->path, &st) == 0) {
			regular = true;
@@ -1771,7 +1772,7 @@ pkg_recompute(struct pkgdb *db, struct pkg *pkg)
			}

			if (st.st_nlink > 1)
-
				regular = !check_for_hardlink(&hl, &st);
+
				regular = !check_for_hardlink(hl, &st);

			if (regular)
				flatsize += st.st_size;
@@ -1779,7 +1780,7 @@ pkg_recompute(struct pkgdb *db, struct pkg *pkg)
		if (strcmp(sha256, f->sum) != 0)
			pkgdb_file_set_cksum(db, f, sha256);
	}
-
	HASH_FREE(hl, free);
+
	kh_destroy_hardlinks(hl);

	if (flatsize != pkg->flatsize)
		pkg->flatsize = flatsize;
modified libpkg/pkg_create.c
@@ -59,7 +59,7 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,
	int64_t		 flatsize = 0;
	int64_t		 nfiles;
	const char	*relocation;
-
	struct hardlinks *hardlinks = NULL;
+
	hardlinks_t	*hardlinks;

	if (pkg_is_valid(pkg) != EPKG_OK) {
		pkg_emit_error("the package is not valid");
@@ -77,6 +77,7 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,
	nfiles = HASH_COUNT(pkg->files);
	counter_init("file sizes/checksums", nfiles);

+
	hardlinks = kh_init_hardlinks();
	while (pkg_files(pkg, &file) == EPKG_OK) {

		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
@@ -90,7 +91,7 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,
		if (file->size == 0)
			file->size = (int64_t)st.st_size;

-
		if (st.st_nlink == 1 || !check_for_hardlink(&hardlinks, &st)) {
+
		if (st.st_nlink == 1 || !check_for_hardlink(hardlinks, &st)) {
			flatsize += file->size;
		}

@@ -113,11 +114,11 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,

		counter_count();
	}
+
	kh_destroy_hardlinks(hardlinks);

	counter_end();

	pkg->flatsize = flatsize;
-
	HASH_FREE(hardlinks, free);

	if (pkg->type == PKG_OLD_FILE) {
		pkg_emit_error("Cannot create an old format package");
modified libpkg/pkg_ports.c
@@ -352,7 +352,7 @@ meta_file(struct plist *p, char *line, struct file_attr *a, bool is_config)

	if (S_ISREG(st.st_mode)) {
		if (st.st_nlink > 1)
-
			regular = !check_for_hardlink(&(p->hardlinks), &st);
+
			regular = !check_for_hardlink(p->hardlinks, &st);
		else
			regular = true;
	} else if (S_ISLNK(st.st_mode)) {
@@ -1197,6 +1197,7 @@ plist_new(struct pkg *pkg, const char *stage)
	p->post_deinstall_buf = sbuf_new_auto();
	p->pre_upgrade_buf = sbuf_new_auto();
	p->post_upgrade_buf = sbuf_new_auto();
+
	p->hardlinks = kh_init_hardlinks();

	populate_keywords(p);

@@ -1209,8 +1210,6 @@ plist_free(struct plist *p)
	if (p == NULL)
		return;

-
	HASH_FREE(p->hardlinks, free);
-

	HASH_FREE(p->keywords, keyword_free);

	free(p->pkgdep);
@@ -1218,6 +1217,7 @@ plist_free(struct plist *p)
	free(p->gname);
	free(p->post_patterns.buf);
	free(p->post_patterns.patterns);
+
	kh_destroy_hardlinks(p->hardlinks);

	sbuf_delete(p->post_deinstall_buf);
	sbuf_delete(p->post_install_buf);
modified libpkg/private/pkg.h
@@ -429,7 +429,7 @@ struct plist {
	char *pkgdep;
	bool ignore_next;
	int64_t flatsize;
-
	struct hardlinks *hardlinks;
+
	hardlinks_t *hardlinks;
	mode_t perm;
	struct {
		char *buf;
modified libpkg/private/utils.h
@@ -34,6 +34,7 @@
#include <sys/param.h>
#include <uthash.h>
#include <ucl.h>
+
#include <khash.h>

#include <openssl/pem.h>
#include <openssl/rsa.h>
@@ -51,10 +52,8 @@
#define HASH_ADD_INO(head,ino,add)                                          \
	HASH_ADD(hh,head,ino,sizeof(ino_t),add)

-
struct hardlinks {
-
	ino_t inode;
-
	UT_hash_handle hh;
-
};
+
KHASH_MAP_INIT_INT(hardlinks, int)
+
typedef khash_t(hardlinks) hardlinks_t;

struct dns_srvinfo {
	unsigned int type;
@@ -103,7 +102,7 @@ int rsa_verify(const char *path, const char *key,
int rsa_verify_cert(const char *path, unsigned char *cert,
    int certlen, unsigned char *sig, int sig_len, int fd);

-
bool check_for_hardlink(struct hardlinks **hl, struct stat *st);
+
bool check_for_hardlink(hardlinks_t *hl, struct stat *st);
bool is_valid_abi(const char *arch, bool emit_error);

struct dns_srvinfo *
modified libpkg/utils.c
@@ -474,18 +474,16 @@ string_end_with(const char *path, const char *str)
}

bool
-
check_for_hardlink(struct hardlinks **hl, struct stat *st)
+
check_for_hardlink(hardlinks_t *hl, struct stat *st)
{
-
	struct hardlinks *h;
+
	khint_t k;
+
	int absent;

-
	HASH_FIND_INO(*hl, &st->st_ino, h);
-
	if (h != NULL)
+
	kh_put_hardlinks(hl, st->st_ino, &absent);
+
	k = kh_get_hardlinks(hl, st->st_ino);
+
	if (absent == 0)
		return (true);

-
	h = malloc(sizeof(struct hardlinks));
-
	h->inode = st->st_ino;
-
	HASH_ADD_INO(*hl, inode, h);
-

	return (false);
}