Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
add a new event: PKG_EVENT_FILE_MISSING
Baptiste Daroussin committed 10 years ago
commit 1ecbb20d36e18b6e4fef3f15e4491d2e522989e5
parent 752fd48
10 files changed +57 -23
modified libpkg/pkg.c
@@ -1583,13 +1583,18 @@ pkg_test_filesum(struct pkg *pkg)
{
	struct pkg_file *f = NULL;
	int rc = EPKG_OK;
+
	int ret;

	assert(pkg != NULL);

	while (pkg_files(pkg, &f) == EPKG_OK) {
		if (f->sum != NULL) {
-
			if (!pkg_checksum_validate_file(f->path, f->sum)) {
-
				pkg_emit_file_mismatch(pkg, f, f->sum);
+
			ret = pkg_checksum_validate_file(f->path, f->sum);
+
			if (ret != 0) {
+
				if (ret == ENOENT)
+
					pkg_emit_file_missing(pkg, f);
+
				else
+
					pkg_emit_file_mismatch(pkg, f, f->sum);
				rc = EPKG_FATAL;
			}
		}
modified libpkg/pkg.h.in
@@ -1285,6 +1285,7 @@ typedef enum {
	PKG_EVENT_NOT_FOUND,
	PKG_EVENT_NEW_ACTION,
	PKG_EVENT_MESSAGE,
+
	PKG_EVENT_FILE_MISSING,
} pkg_event_t;

struct pkg_event {
@@ -1437,6 +1438,10 @@ struct pkg_event {
		struct {
			const char *msg;
		} e_pkg_message;
+
		struct {
+
			struct pkg *pkg;
+
			struct pkg_file *file;
+
		} e_file_missing;
	};
};

modified libpkg/pkg_checksum.c
@@ -27,6 +27,7 @@
#include <sys/stat.h>

#include <fcntl.h>
+
#include <errno.h>
#include "pkg.h"
#include "private/pkg.h"
#include "private/event.h"
@@ -724,7 +725,7 @@ pkg_checksum_symlinkat(int fd, const char *path, const char *root, pkg_checksum_
	return (pkg_checksum_symlink_readlink(linkbuf, linklen, root, type));
}

-
bool
+
int
pkg_checksum_validate_file(const char *path, const char *sum)
{
	struct stat st;
@@ -740,8 +741,7 @@ pkg_checksum_validate_file(const char *path, const char *sum)
	}

	if (lstat(path, &st) == -1) {
-
		pkg_emit_errno("pkg_checksum_validate_file", "lstat");
-
		return (false);
+
		return (errno);
	}

	if (S_ISLNK(st.st_mode))
@@ -750,16 +750,16 @@ pkg_checksum_validate_file(const char *path, const char *sum)
		newsum = pkg_checksum_file(path, type);

	if (newsum == NULL)
-
		return (false);
+
		return (-1);

	if (strcmp(sum, newsum) != 0) {
		free(newsum);
-
		return (false);
+
		return (-1);
	}

	free(newsum);

-
	return (true);
+
	return (0);
}

char *
@@ -788,7 +788,7 @@ pkg_checksum_generate_file(const char *path, pkg_checksum_type_t type)
	return (cksum);
}

-
bool
+
int
pkg_checksum_validate_fileat(int rootfd, const char *path, const char *sum)
{
	struct stat st;
@@ -804,8 +804,7 @@ pkg_checksum_validate_fileat(int rootfd, const char *path, const char *sum)
	}

	if (fstatat(rootfd, path, &st, AT_SYMLINK_NOFOLLOW) == -1) {
-
		pkg_emit_errno("pkg_checksum_validate_file", "lstat");
-
		return (false);
+
		return (errno);
	}

	if (S_ISLNK(st.st_mode))
@@ -814,16 +813,16 @@ pkg_checksum_validate_fileat(int rootfd, const char *path, const char *sum)
		newsum = pkg_checksum_fileat(rootfd, path, type);

	if (newsum == NULL)
-
		return (false);
+
		return (-1);

	if (strcmp(sum, newsum) != 0) {
		free(newsum);
-
		return (false);
+
		return (-1);
	}

	free(newsum);

-
	return (true);
+
	return (0);
}

char *
modified libpkg/pkg_delete.c
@@ -291,6 +291,7 @@ pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force)
	int fd;
#endif
#endif
+
	int ret;

	pkg_open_root_fd(pkg);

@@ -306,7 +307,12 @@ pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force)
	/* Regular files and links */
	/* check checksum */
	if (!force && file->sum != NULL) {
-
		if (!pkg_checksum_validate_fileat(pkg->rootfd, path, file->sum)) {
+
		ret = pkg_checksum_validate_fileat(pkg->rootfd, path, file->sum);
+
		if (ret == ENOENT) {
+
			pkg_emit_file_missing(pkg, file);
+
			return;
+
		}
+
		if (ret != 0) {
			pkg_emit_error("%s%s%s different from original "
			    "checksum, not removing", pkg->rootpath,
			    pkg->rootpath[strlen(pkg->rootpath) - 1] == '/' ? "" : "/",
modified libpkg/pkg_event.c
@@ -791,7 +791,8 @@ pkg_emit_newpkgversion(void)
}

void
-
pkg_emit_file_mismatch(struct pkg *pkg, struct pkg_file *f, const char *newsum) {
+
pkg_emit_file_mismatch(struct pkg *pkg, struct pkg_file *f, const char *newsum)
+
{
	struct pkg_event ev;
	ev.type = PKG_EVENT_FILE_MISMATCH;

@@ -803,6 +804,18 @@ pkg_emit_file_mismatch(struct pkg *pkg, struct pkg_file *f, const char *newsum)
}

void
+
pkg_emit_file_missing(struct pkg *pkg, struct pkg_file *f)
+
{
+
	struct pkg_event ev;
+
	ev.type = PKG_EVENT_FILE_MISSING;
+

+
	ev.e_file_missing.pkg = pkg;
+
	ev.e_file_missing.file = f;
+

+
	pkg_emit_event(&ev);
+
}
+

+
void
pkg_plugin_errno(struct pkg_plugin *p, const char *func, const char *arg)
{
	struct pkg_event ev;
modified libpkg/private/event.h
@@ -78,5 +78,6 @@ void pkg_emit_delete_files_begin(struct pkg *p);
void pkg_emit_delete_files_finished(struct pkg *p);
void pkg_emit_new_action(void);
void pkg_emit_message(const char *msg);
+
void pkg_emit_file_missing(struct pkg *p, struct pkg_file *f);

#endif
modified libpkg/private/pkg.h
@@ -697,8 +697,8 @@ unsigned char *pkg_checksum_symlink(const char *path, const char *root,
    pkg_checksum_type_t type);
unsigned char *pkg_checksum_symlinkat(int fd, const char *path,
    const char *root, pkg_checksum_type_t type);
-
bool pkg_checksum_validate_file(const char *path, const  char *sum);
-
bool pkg_checksum_validate_fileat(int fd, const char *path, const  char *sum);
+
int pkg_checksum_validate_file(const char *path, const  char *sum);
+
int pkg_checksum_validate_fileat(int fd, const char *path, const  char *sum);

bool pkg_checksum_is_valid(const char *cksum, size_t clen);
pkg_checksum_type_t pkg_checksum_get_type(const char *cksum, size_t clen);
modified libpkg/repo/binary/fetch.c
@@ -225,7 +225,7 @@ checksum:
		    pkg->name, pkg->version);
		return (pkg_repo_binary_try_fetch(repo, pkg, true, mirror, destdir));
	}
-
	if (!pkg_checksum_validate_file(dest, pkg->sum)) {
+
	if (pkg_checksum_validate_file(dest, pkg->sum) != 0) {
		if (already_tried || fetched) {
			pkg_emit_error("%s-%s failed checksum "
			    "from repository", pkg->name, pkg->version);
modified src/event.c
@@ -774,6 +774,11 @@ event_callback(void *data, struct pkg_event *ev)
		pkg_fprintf(stderr, "%n-%v: checksum mismatch for %Fn\n", pkg,
		    pkg, ev->e_file_mismatch.file);
		break;
+
	case PKG_EVENT_FILE_MISSING:
+
		pkg = ev->e_file_missing.pkg;
+
		pkg_fprintf(stderr, "%n-%v: missing file %Fn\n", pkg, pkg,
+
		    ev->e_file_missing.file);
+
		break;
	case PKG_EVENT_PLUGIN_ERRNO:
		warnx("%s: %s(%s): %s",
		    pkg_plugin_get(ev->e_plugin_errno.plugin, PKG_PLUGIN_NAME),
modified tests/lib/checksum.c
@@ -46,7 +46,7 @@ ATF_TC_BODY(check_symlinks, tc)
	sum = pkg_checksum_symlink("bar", NULL, PKG_HASH_TYPE_SHA256_HEX);
	ATF_REQUIRE_STREQ(sum, "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae");

-
	ATF_CHECK(pkg_checksum_validate_file("bar", "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"));
+
	ATF_CHECK(pkg_checksum_validate_file("bar", "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae") == 0);
	free(sum);

	sum = pkg_checksum_generate_file("bar", PKG_HASH_TYPE_SHA256_HEX);
@@ -57,8 +57,8 @@ ATF_TC_BODY(check_symlinks, tc)
	ATF_REQUIRE_STREQ(sum, "2$kgygnaah7wxsgn1wkuic4j78zq8dicmx53picmma99ogmkbd7k5nhuxr5xxemz6yzjab15oor3tjt7nupj8mh764y7kddbne7qw9agn");
	free(sum);

-
	ATF_CHECK(pkg_checksum_validate_file("bar", "2$kgygnaah7wxsgn1wkuic4j78zq8dicmx53picmma99ogmkbd7k5nhuxr5xxemz6yzjab15oor3tjt7nupj8mh764y7kddbne7qw9agn"));
-
	ATF_CHECK(pkg_checksum_validate_file("bar", "1$2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"));
+
	ATF_CHECK(pkg_checksum_validate_file("bar", "2$kgygnaah7wxsgn1wkuic4j78zq8dicmx53picmma99ogmkbd7k5nhuxr5xxemz6yzjab15oor3tjt7nupj8mh764y7kddbne7qw9agn") == 0);
+
	ATF_CHECK(pkg_checksum_validate_file("bar", "1$2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae") == 0);
}

ATF_TC(check_files);
@@ -80,7 +80,7 @@ ATF_TC_BODY(check_files, tc)
	sum = pkg_checksum_file("foo", PKG_HASH_TYPE_SHA256_HEX);
	ATF_REQUIRE_STREQ(sum, "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730");

-
	ATF_CHECK(pkg_checksum_validate_file("foo", "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730"));
+
	ATF_CHECK(pkg_checksum_validate_file("foo", "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730") == 0);
	free(sum);

	sum=pkg_checksum_generate_file("foo", PKG_HASH_TYPE_SHA256_HEX);