Exemple #1
0
void
pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force)
{
	const char *sum = pkg_file_cksum(file);
	const char *path;
	const char *prefix_rel;
	struct stat st;
	size_t len;
	char sha256[SHA256_DIGEST_LENGTH * 2 + 1];

	pkg_open_root_fd(pkg);

	path = pkg_file_path(file);
	path++;

	pkg_get(pkg, PKG_PREFIX, &prefix_rel);
	prefix_rel++;
	len = strlen(prefix_rel);

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

	if (unlinkat(pkg->rootfd, path, 0) == -1) {
		if (force < 2)
			pkg_emit_errno("unlinkat", path);
		return;
	}

	/* do not bother about directories not in prefix */
	if ((strncmp(prefix_rel, path, len) == 0) && path[len] == '/')
		pkg_add_dir_to_del(pkg, path, NULL);
}
Exemple #2
0
void
pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force)
{
	const char *sum = pkg_file_cksum(file);
	const char *path;
	struct stat st;
	char sha256[SHA256_DIGEST_LENGTH * 2 + 1];

	pkg_open_root_fd(pkg);

	path = pkg_file_path(file);
	path++;

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

	if (unlinkat(pkg->rootfd, path, 0) == -1) {
		if (force < 2)
			pkg_emit_errno("unlinkat", path);
		return;
	}
}