Skip to content
Snippets Groups Projects
database.c 26.7 KiB
Newer Older
	} else {
		if (name.ptr[name.len-1] == '/')
			name.len--;

		if (ctx->diri_node == NULL)
			ctx->diri_node = hlist_tail_ptr(&pkg->owned_dirs);
		ctx->diri = diri = apk_db_diri_new(db, pkg, name,
						   ctx->diri_node);
		ctx->diri_node = &diri->pkg_dirs_list.next;
		ctx->file_diri_node = NULL;

		apk_db_diri_set(diri, ae->mode & 0777, ae->uid, ae->gid);
		apk_db_diri_mkdir(diri);
	ctx->installed_size += ctx->current_file_size;

	return r;
}

static void apk_db_purge_pkg(struct apk_database *db,
			     struct apk_package *pkg)
{
	struct apk_db_dir_instance *diri;
	struct hlist_node *dc, *dn, *fc, *fn;
	char name[1024];

	hlist_for_each_entry_safe(diri, dc, dn, &pkg->owned_dirs, pkg_dirs_list) {
		hlist_for_each_entry_safe(file, fc, fn, &diri->owned_files, diri_files_list) {
			file->diri = NULL;
			snprintf(name, sizeof(name), "%s/%s",
				 diri->dir->dirname,
				 file->filename);
			unlink(name);
			__hlist_del(fc, &diri->owned_files.first);

			db->installed.stats.files--;
		}
		apk_db_diri_rmdir(diri);
		apk_db_dir_unref(db, diri->dir);
		__hlist_del(dc, &pkg->owned_dirs.first);
	apk_pkg_set_state(db, pkg, APK_STATE_NO_INSTALL);
}

int apk_db_install_pkg(struct apk_database *db,
		       struct apk_package *oldpkg,
		       struct apk_package *newpkg,
		       apk_progress_cb cb, void *cb_ctx)
	struct apk_bstream *bs;
	struct install_ctx ctx;
	csum_t csum;
	char file[256];
	if (fchdir(db->root_fd) < 0)
		return errno;

	/* Purge the old package if there */
	if (oldpkg != NULL) {
		if (newpkg == NULL) {
			r = apk_pkg_run_script(oldpkg, db->root_fd,
					       APK_SCRIPT_PRE_DEINSTALL);
			if (r != 0)
				return r;
		}
		apk_db_purge_pkg(db, oldpkg);
		if (newpkg == NULL) {
			apk_pkg_run_script(oldpkg, db->root_fd,
					   APK_SCRIPT_POST_DEINSTALL);
			return 0;
		}
	}

	/* Install the new stuff */
	if (newpkg->filename == NULL) {
		snprintf(file, sizeof(file),
			 "%s/%s-%s.apk",
			 db->repos[0].url, newpkg->name->name, newpkg->version);
		bs = apk_bstream_from_url(file);
		bs = apk_bstream_from_file(newpkg->filename);
	if (bs == NULL) {
		apk_error("%s: %s", file, strerror(errno));

	ctx = (struct install_ctx) {
		.db = db,
		.pkg = newpkg,
		.script = (oldpkg == NULL) ?
			APK_SCRIPT_PRE_INSTALL : APK_SCRIPT_PRE_UPGRADE,
		.cb = cb,
		.cb_ctx = cb_ctx,
	if (apk_parse_tar_gz(bs, apk_db_install_archive_entry, &ctx) != 0)
Timo Teräs's avatar
Timo Teräs committed
	bs->close(bs, csum, NULL);
	apk_pkg_set_state(db, newpkg, APK_STATE_INSTALL);

	if (memcmp(csum, newpkg->csum, sizeof(csum)) != 0)
		apk_warning("%s-%s: checksum does not match",
			    newpkg->name->name, newpkg->version);

	r = apk_pkg_run_script(newpkg, db->root_fd,
			       (oldpkg == NULL) ?
			       APK_SCRIPT_POST_INSTALL : APK_SCRIPT_POST_UPGRADE);
		apk_error("%s-%s: Failed to execute post-install/upgrade script",
			  newpkg->name->name, newpkg->version);
Timo Teräs's avatar
Timo Teräs committed
	bs->close(bs, NULL, NULL);