Esempio n. 1
0
void
pkg_delete_dir(struct pkg *pkg, struct pkg_dir *dir)
{
	const char *path;
	const char *prefix_rel;
	size_t len;

	pkg_open_root_fd(pkg);

	path = pkg_dir_path(dir);
	/* remove the first / */
	path++;

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

	if ((strncmp(prefix_rel, path, len) == 0) && path[len] == '/') {
		pkg_add_dir_to_del(pkg, NULL, path);
	} else {
		if (pkg->dir_to_del_len + 1 > pkg->dir_to_del_cap) {
			pkg->dir_to_del_cap += 64;
			pkg->dir_to_del = reallocf(pkg->dir_to_del,
			    pkg->dir_to_del_cap * sizeof(char *));
		}
		pkg->dir_to_del[pkg->dir_to_del_len++] = strdup(path);
	}
}
Esempio n. 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;
	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);
}
Esempio n. 3
0
void
pkg_delete_dir(struct pkg *pkg, struct pkg_dir *dir)
{
	const char *path;
	pkg_open_root_fd(pkg);

	path = pkg_dir_path(dir);
	/* remove the first / */
	path++;

	if (unlinkat(pkg->rootfd, path, AT_REMOVEDIR) == -1 &&
	    errno != ENOTEMPTY && errno != EBUSY)
		pkg_emit_errno("rmdir", pkg_dir_path(dir));
}
Esempio n. 4
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;
	}
}