Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
add: only create pkgsave if the files are actually differents
Baptiste Daroussin committed 2 years ago
commit f169036f542d012c341a3a1b15aec0349178241d
parent bc1137c
2 files changed +42 -3
modified libpkg/pkg_add.c
@@ -858,6 +858,39 @@ cleanup:
	return (retcode);
}

+
static void
+
backup_file_if_needed(struct pkg *p, struct pkg_file *f)
+
{
+
	char path[MAXPATHLEN];
+
	struct stat st;
+
	char *sum;
+
	pkg_checksum_type_t t;
+

+
	if (fstatat(p->rootfd, RELATIVE_PATH(f->path), &st,
+
	    AT_SYMLINK_NOFOLLOW) == -1)
+
		return;
+

+
	if (S_ISLNK(st.st_mode))
+
		return;
+

+
	if (S_ISREG(st.st_mode)) {
+
		t = pkg_checksum_file_get_type(f->sum, -1);
+
		sum = pkg_checksum_generate_fileat(p->rootfd,
+
		    RELATIVE_PATH(f->path), t);
+
		if (sum == NULL)
+
			return;
+

+
		if (strcmp(sum, f->sum) == 0) {
+
			free(sum);
+
			return;
+
		}
+
	}
+

+
	snprintf(path, sizeof(path), "%s.pkgsave", f->path);
+
	renameat(p->rootfd, RELATIVE_PATH(f->path),
+
	    p->rootfd, RELATIVE_PATH(path));
+
}
+

static int
pkg_extract_finalize(struct pkg *pkg, tempdirs_t *tempdirs)
{
@@ -924,9 +957,7 @@ pkg_extract_finalize(struct pkg *pkg, tempdirs_t *tempdirs)
#endif
			/* if the files does not belong to any package, we do save it */
			if (f->previous == PKG_FILE_SAVE) {
-
				snprintf(path, sizeof(path), "%s.pkgsave", f->path);
-
				renameat(pkg->rootfd, RELATIVE_PATH(fto),
-
				    pkg->rootfd, RELATIVE_PATH(path));
+
				backup_file_if_needed(pkg, f);
			}
			unlinkat(pkg->rootfd, RELATIVE_PATH(fto), 0);
		}
modified tests/frontend/conflicts.sh
@@ -380,6 +380,14 @@ fileexists_notinpkg_body()
	atf_check \
		pkg -o REPOS_DIR=${TMPDIR}/reposconf -r ${TMPDIR}/target install -qyf test
	test -f ${TMPDIR}/target/${TMPDIR}/a.pkgsave && atf_fail "file saved when it should not have"
+

+
	# Test if both files are identical
+
	atf_check \
+
		pkg -o REPOS_DIR=${TMPDIR}/reposconf -r ${TMPDIR}/target delete -qyf test
+
	echo "entry 2" > ${TMPDIR}/target/${TMPDIR}/a
+
	atf_check \
+
		pkg -o REPOS_DIR=${TMPDIR}/reposconf -r ${TMPDIR}/target install -qyf test
+
	test -f ${TMPDIR}/target/${TMPDIR}/a.pkgsave && atf_fail "file saved when it should not have"
	return 0
}