Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Fix upgrade.
Vsevolod Stakhov committed 11 years ago
commit 8c7d61f538fcc977e0a4a7062705cc331e6a6beb
parent 27c67ad
3 files changed +122 -56
modified libpkg/pkg_add.c
@@ -318,6 +318,47 @@ pkg_add_check_pkg_archive(struct pkgdb *db, struct pkg *pkg,
}

static int
+
pkg_add_cleanup_old(struct pkg *old, struct pkg *new, int flags)
+
{
+
	struct pkg_file *f, *cf;
+
	struct pkg_dir *d, *cd;
+
	int ret;
+
	bool handle_rc;
+

+
	handle_rc = pkg_object_bool(pkg_config_get("HANDLE_RC_SCRIPTS"));
+
	if (handle_rc)
+
		pkg_start_stop_rc_scripts(old, PKG_RC_START);
+

+
	/* Execute pre-deinstall scripts */
+
	if ((flags & PKG_ADD_NOSCRIPT) == 0) {
+
		ret = pkg_script_run(old, PKG_SCRIPT_PRE_DEINSTALL);
+
		if (ret != EPKG_OK)
+
			return (ret);
+
	}
+

+
	/* Now remove files that no longer exist in the new package */
+
	if (new != NULL) {
+
		f = NULL;
+
		while (pkg_files(old, &f) == EPKG_OK) {
+
			HASH_FIND_STR(new->files, f->path, cf);
+

+
			if (cf == NULL)
+
				pkg_delete_file(old, f, flags & PKG_DELETE_FORCE ? 1 : 0);
+
		}
+

+
		d = NULL;
+
		while (pkg_dirs(old, &d) == EPKG_OK) {
+
			HASH_FIND_STR(new->dirs, d->path, cd);
+

+
			if (cd == NULL)
+
				pkg_delete_dir(old, d, flags & PKG_DELETE_FORCE ? 1 : 0);
+
		}
+
	}
+

+
	return (ret);
+
}
+

+
static int
pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
    struct pkg_manifest_key *keys, const char *location, struct pkg *remote,
    struct pkg *local)
@@ -336,6 +377,9 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,

	assert(path != NULL);

+
	if (local != NULL)
+
		flags |= PKG_ADD_UPGRADE;
+

	/*
	 * Open the package archive file, read all the meta files and set the
	 * current archive_entry to the first non-meta file.
@@ -408,6 +452,12 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
			goto cleanup_reg;
	}

+
	if (local != NULL)
+
		if (pkg_add_cleanup_old(local, remote, flags) != EPKG_OK) {
+
			retcode = EPKG_FATAL;
+
			goto cleanup;
+
		}
+

	/*
	 * Execute pre-install scripts
	 */
modified libpkg/pkg_delete.c
@@ -106,6 +106,51 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags)
	return (pkgdb_unregister_pkg(db, id));
}

+
void
+
pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force)
+
{
+
	const char *sum = pkg_file_cksum(file);
+
	const ucl_object_t *obj, *an;
+
	const char *path;
+
	char fpath[MAXPATHLEN];
+
	struct stat st;
+
	char sha256[SHA256_DIGEST_LENGTH * 2 + 1];
+

+
	path = pkg_file_path(file);
+
	pkg_get(pkg, PKG_ANNOTATIONS, &an);
+
	obj = pkg_object_find(an, "relocated");
+
	snprintf(fpath, sizeof(fpath), "%s%s",
+
		obj ? pkg_object_string(obj) : "" , path );
+

+
	/* Regular files and links */
+
	/* check sha256 */
+
	if (!force && sum[0] != '\0') {
+
		if (lstat(fpath, &st) == -1) {
+
			pkg_emit_error("cannot stat %s: %s", fpath, strerror(errno));
+
			return;
+
		}
+
		if (S_ISLNK(st.st_mode)) {
+
			if (pkg_symlink_cksum(fpath, NULL, sha256) != EPKG_OK)
+
				return;
+
		}
+
		else {
+
			if (sha256_file(fpath, sha256) != EPKG_OK)
+
				return;
+
		}
+
		if (strcmp(sha256, sum)) {
+
			pkg_emit_error("%s fails original SHA256 "
+
				"checksum, not removing", path);
+
			return;
+
		}
+
	}
+

+
	if (unlink(fpath) == -1) {
+
		if (force < 2)
+
			pkg_emit_errno("unlink", fpath);
+
		return;
+
	}
+
}
+

int
pkg_delete_files(struct pkg *pkg, unsigned force)
	/* force: 0 ... be careful and vocal about it. 
@@ -114,11 +159,8 @@ pkg_delete_files(struct pkg *pkg, unsigned force)
	 */
{
	struct pkg_file	*file = NULL;
-
	char		 sha256[SHA256_DIGEST_LENGTH * 2 + 1];
-
	const char	*path;
-
	char		fpath[MAXPATHLEN];
+

	int		nfiles, cur_file = 0;
-
	struct stat st;

	nfiles = HASH_COUNT(pkg->files);

@@ -128,46 +170,11 @@ pkg_delete_files(struct pkg *pkg, unsigned force)
		pkg_emit_progress_tick(1, 1);

	while (pkg_files(pkg, &file) == EPKG_OK) {
-
		const char *sum = pkg_file_cksum(file);
-
		const ucl_object_t *obj, *an;
-

		pkg_emit_progress_tick(cur_file++, nfiles);
-
		if (file->keep == 1)
-
			continue;

-
		path = pkg_file_path(file);
-
		pkg_get(pkg, PKG_ANNOTATIONS, &an);
-
		obj = pkg_object_find(an, "relocated");
-
		snprintf(fpath, sizeof(fpath), "%s%s",
-
		    obj ? pkg_object_string(obj) : "" , path );
-

-
		/* Regular files and links */
-
		/* check sha256 */
-
		if (!force && sum[0] != '\0') {
-
			if (lstat(fpath, &st) == -1) {
-
				pkg_emit_error("cannot stat %s: %s", fpath, strerror(errno));
-
				continue;
-
			}
-
			if (S_ISLNK(st.st_mode)) {
-
				if (pkg_symlink_cksum(fpath, NULL, sha256) != EPKG_OK)
-
					continue;
-
			}
-
			else {
-
				if (sha256_file(fpath, sha256) != EPKG_OK)
-
					continue;
-
			}
-
			if (strcmp(sha256, sum)) {
-
				pkg_emit_error("%s fails original SHA256 "
-
				    "checksum, not removing", path);
-
				continue;
-
			}
-
		}
-

-
		if (unlink(fpath) == -1) {
-
			if (force < 2)
-
				pkg_emit_errno("unlink", fpath);
+
		if (file->keep == 1)
			continue;
-
		}
+
		pkg_delete_file(pkg, file, force);
	}

	pkg_emit_progress_tick(nfiles, nfiles);
@@ -175,30 +182,37 @@ pkg_delete_files(struct pkg *pkg, unsigned force)
	return (EPKG_OK);
}

+
void
+
pkg_delete_dir(struct pkg *pkg, struct pkg_dir *dir, unsigned force)
+
{
+
	const ucl_object_t 	*obj, *an;
+
	char			 fpath[MAXPATHLEN];
+

+
	pkg_get(pkg, PKG_ANNOTATIONS, &an);
+
	obj = pkg_object_find(an, "relocated");
+
	snprintf(fpath, sizeof(fpath), "%s%s",
+
		obj ? pkg_object_string(obj) : "" , pkg_dir_path(dir) );
+

+
	if (pkg_dir_try(dir)) {
+
		if (rmdir(fpath) == -1 &&
+
						errno != ENOTEMPTY && errno != EBUSY && !force)
+
			pkg_emit_errno("rmdir", fpath);
+
	} else {
+
		if (rmdir(fpath) == -1 && !force)
+
			pkg_emit_errno("rmdir", fpath);
+
	}
+
}
+

int
pkg_delete_dirs(__unused struct pkgdb *db, struct pkg *pkg, bool force)
{
	struct pkg_dir		*dir = NULL;
-
	const ucl_object_t 	*obj, *an;
-
	char			 fpath[MAXPATHLEN];

	while (pkg_dirs(pkg, &dir) == EPKG_OK) {
		if (dir->keep == 1)
			continue;

-
		pkg_get(pkg, PKG_ANNOTATIONS, &an);
-
		obj = pkg_object_find(an, "relocated");
-
		snprintf(fpath, sizeof(fpath), "%s%s",
-
		    obj ? pkg_object_string(obj) : "" , pkg_dir_path(dir) );
-

-
		if (pkg_dir_try(dir)) {
-
			if (rmdir(fpath) == -1 &&
-
			    errno != ENOTEMPTY && errno != EBUSY && !force)
-
				pkg_emit_errno("rmdir", fpath);
-
		} else {
-
			if (rmdir(fpath) == -1 && !force)
-
				pkg_emit_errno("rmdir", fpath);
-
		}
+
		pkg_delete_dir(pkg, dir, force);
	}

	return (EPKG_OK);
modified libpkg/private/pkg.h
@@ -612,5 +612,7 @@ int pkg_symlink_cksum(const char *path, const char *root, char *cksum);
int pkg_add_upgrade(struct pkgdb *db, const char *path, unsigned flags,
    struct pkg_manifest_key *keys, const char *location,
    struct pkg *rp, struct pkg *lp);
+
void pkg_delete_dir(struct pkg *pkg, struct pkg_dir *dir, unsigned force);
+
void pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force);

#endif