Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Convert hardlinks handling to use uthash
Baptiste Daroussin committed 13 years ago
commit 8d66be851e265697f269a2ef87e754cbd319cde5
parent d42ae05
4 files changed +22 -21
modified libpkg/pkg.c
@@ -1112,7 +1112,7 @@ pkg_recompute(struct pkgdb *db, struct pkg *pkg)
{
	struct pkg_file *f = NULL;
	const char *path;
-
	struct hardlinks hl = { NULL, 0, 0 };
+
	struct hardlinks *hl = NULL;
	int64_t flatsize = 0;
	int64_t oldflatsize;
	struct stat st;
@@ -1133,7 +1133,7 @@ pkg_recompute(struct pkgdb *db, struct pkg *pkg)

			/* special case for hardlinks */
			if (st.st_nlink > 1)
-
				regular = is_hardlink(&hl, &st);
+
				regular = is_hardlink(hl, &st);

			if (regular)
				flatsize += st.st_size;
modified libpkg/pkg_ports.c
@@ -915,7 +915,6 @@ ports_parse_plist(struct pkg *pkg, char *plist, const char *stage)
	char *plist_buf, *walk, *buf, *token;
	int ret = EPKG_OK;
	off_t sz = 0;
-
	struct hardlinks hardlinks = {NULL, 0, 0};
	struct plist pplist;

	assert(pkg != NULL);
@@ -937,7 +936,7 @@ ports_parse_plist(struct pkg *pkg, char *plist, const char *stage)
	pplist.pkg = pkg;
	pplist.slash = "";
	pplist.ignore_next = false;
-
	pplist.hardlinks = &hardlinks;
+
	pplist.hardlinks = NULL;
	pplist.flatsize = 0;
	pplist.keywords = NULL;

@@ -1023,7 +1022,7 @@ ports_parse_plist(struct pkg *pkg, char *plist, const char *stage)
	flush_script_buffer(pplist.post_upgrade_buf, pkg,
	    PKG_SCRIPT_POST_UPGRADE);

-
	free(hardlinks.inodes);
+
	HASH_FREE(pplist.hardlinks, hardlinks, free);

	free(plist_buf);
	HASH_FREE(pplist.keywords, keyword, keyword_free);
modified libpkg/private/utils.h
@@ -31,6 +31,7 @@
#include <sys/types.h>
#include <sys/sbuf.h>
#include <sys/param.h>
+
#include <uthash.h>

#include <openssl/pem.h>
#include <openssl/sha.h>
@@ -41,10 +42,14 @@
#define ERROR_SQLITE(db) \
	pkg_emit_error("sqlite: %s (%s:%d)", sqlite3_errmsg(db), __FILE__, __LINE__)

+
#define HASH_FIND_INO(head,ino,out)                                          \
+
	HASH_FIND(hh,head,ino,sizeof(ino_t),out)
+
#define HASH_ADD_INO(head,ino,add)                                          \
+
	HASH_ADD(hh,head,ino,sizeof(ino_t),add)
+

struct hardlinks {
-
	ino_t *inodes;
-
	size_t len;
-
	size_t cap;
+
	ino_t inode;
+
	UT_hash_handle hh;
};

struct dns_srvinfo {
modified libpkg/utils.c
@@ -390,21 +390,18 @@ is_conf_file(const char *path, char *newpath, size_t len)
	return (0);
}

-
bool is_hardlink(struct hardlinks *hl, struct stat *st)
+
bool
+
is_hardlink(struct hardlinks *hl, struct stat *st)
{
-
	size_t i;
+
	struct hardlinks *h;

-
	for (i = 0; i < hl->len; i++) {
-
		if (hl->inodes[i] == st->st_ino)
-
			return (false);
-
	}
-
	if (hl->cap <= hl->len) {
-
		hl->cap |= 1;
-
		hl->cap *= 2;
-
		hl->inodes = reallocf(hl->inodes,
-
				hl->cap * sizeof(ino_t));
-
	}
-
	hl->inodes[hl->len++] = st->st_ino;
+
	HASH_FIND_INO(hl, &st->st_ino, h);
+
	if (h != NULL)
+
		return false;
+

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

	return (true);
}