Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Move hardlinks detection to utils.c to avoid code duplication
Baptiste Daroussin committed 14 years ago
commit 8643a07d9bdaca4e7969b57e7df6100f04d597a7
parent b6be460
5 files changed +32 -44
modified libpkg/pkg.c
@@ -1082,7 +1082,6 @@ pkg_recompute_flatsize(struct pkg *pkg)
	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);
@@ -1092,24 +1091,8 @@ pkg_recompute_flatsize(struct pkg *pkg)
				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) {
-
					/* Maybe reallocate p->hardlinks. */
-
					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;
-
				}
-
			}
+
			if (st.st_nlink > 1)
+
				regular = is_hardlink(&hl, &st);

			if (regular)
				flatsize += st.st_size;
modified libpkg/pkg_ports.c
@@ -166,7 +166,7 @@ dirrmtry(struct plist *p, char *line)
static int
file(struct plist *p, char *line)
{
-
	size_t len, i;
+
	size_t len;
	char path[MAXPATHLEN];
	struct stat st;
	char *buf;
@@ -191,24 +191,8 @@ file(struct plist *p, char *line)
			regular = false;

		/* special case for hardlinks */
-
		if (st.st_nlink > 1) {
-
			for (i = 0; i < p->hardlinks->len; i++) {
-
				if (p->hardlinks->inodes[i] == st.st_ino) {
-
					regular = false;
-
					break;
-
				}
-
			}
-
			if (regular) {
-
				/* Maybe (re)allocate p->hardlinks. */
-
				if (p->hardlinks->cap <= p->hardlinks->len) {
-
					p->hardlinks->cap |= 1;
-
					p->hardlinks->cap *= 2;
-
					p->hardlinks->inodes = reallocf(p->hardlinks->inodes,
-
					    p->hardlinks->cap * sizeof(ino_t));
-
				}
-
				p->hardlinks->inodes[p->hardlinks->len++] = st.st_ino;
-
			}
-
		}
+
		if (st.st_nlink > 1)
+
			regular = is_hardlink(p->hardlinks, &st);

		if (regular) {
			p->flatsize += st.st_size;
modified libpkg/private/pkg.h
@@ -153,12 +153,6 @@ 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 libpkg/private/utils.h
@@ -39,6 +39,12 @@
#define ERROR_SQLITE(db) \
	pkg_emit_error("sqlite: %s", sqlite3_errmsg(db))

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

void sbuf_init(struct sbuf **);
int sbuf_set(struct sbuf **, const char *);
char * sbuf_get(struct sbuf *);
@@ -55,4 +61,6 @@ int is_conf_file(const char *path, char *newpath, size_t len);

int sha256_file(const char *, char[SHA256_DIGEST_LENGTH * 2 +1]);
void sha256_str(const char *, char[SHA256_DIGEST_LENGTH * 2 +1]);
+

+
bool is_hardlink(struct hardlinks *hl, struct stat *st);
#endif
modified libpkg/utils.c
@@ -362,3 +362,22 @@ is_conf_file(const char *path, char *newpath, size_t len)

	return (0);
}
+

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

+
	for (i = 0; i < hl->len; i++) {
+
		if (hl->inodes[i] == st->st_ino) {
+
			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;
+
			return false;
+
		}
+
	}
+
	return true;
+
}