Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Factorise a bit rcscripts.c
Marin Atanasov Nikolov committed 14 years ago
commit 4e5447bf11a85c2b43a81d0334d08f354df49d71
parent bc049c8
6 files changed +23 -36
modified libpkg/pkg.h
@@ -280,6 +280,14 @@ typedef enum {
} pkg_error_t;

/**
+
 * rc script actions
+
 */
+
typedef enum {
+
	PKG_RC_START = 0,
+
	PKG_RC_STOP
+
} pkg_rc_attr;
+

+
/**
 * Allocate a new pkg.
 * Allocated pkg must be deallocated by pkg_free().
 */
modified libpkg/pkg_add.c
@@ -259,7 +259,7 @@ pkg_add(struct pkgdb *db, const char *path, int flags)

	pkg_config_bool(PKG_CONFIG_HANDLE_RC_SCRIPTS, &handle_rc);
	if (handle_rc)
-
		pkg_start_rc_scripts(pkg);
+
		pkg_start_stop_rc_scripts(pkg, PKG_RC_START);

	cleanup_reg:
	if ((flags & PKG_ADD_UPGRADE) == 0)
modified libpkg/pkg_delete.c
@@ -87,7 +87,7 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, int flags)
	 */
	pkg_config_bool(PKG_CONFIG_HANDLE_RC_SCRIPTS, &handle_rc);
	if (handle_rc)
-
		pkg_stop_rc_scripts(pkg);
+
		pkg_start_stop_rc_scripts(pkg, PKG_RC_STOP);

	if (flags & PKG_DELETE_UPGRADE) {
		if (( ret = pkg_script_run(pkg, PKG_SCRIPT_PRE_UPGRADE)) != EPKG_OK )
modified libpkg/pkg_jobs.c
@@ -249,7 +249,7 @@ pkg_jobs_install(struct pkg_jobs *j)
					 */
					pkg_config_bool(PKG_CONFIG_HANDLE_RC_SCRIPTS, &handle_rc);
					if (handle_rc)
-
						pkg_stop_rc_scripts(pkg);
+
						pkg_start_stop_rc_scripts(pkg, PKG_RC_STOP);
					pkgdb_unregister_pkg(j->db, origin);
					pkg = NULL;
				}
@@ -271,7 +271,7 @@ pkg_jobs_install(struct pkg_jobs *j)
				 */
				pkg_config_bool(PKG_CONFIG_HANDLE_RC_SCRIPTS, &handle_rc);
				if (handle_rc)
-
					pkg_stop_rc_scripts(pkg);
+
					pkg_start_stop_rc_scripts(pkg, PKG_RC_STOP);
				pkgdb_unregister_pkg(j->db, origin);
				pkg = NULL;
			}
modified libpkg/private/pkg.h
@@ -174,8 +174,7 @@ int pkg_delete(struct pkg *pkg, struct pkgdb *db, int flags);

int pkg_repo_fetch(struct pkg *pkg);

-
int pkg_stop_rc_scripts(struct pkg *);
-
int pkg_start_rc_scripts(struct pkg *);
+
int pkg_start_stop_rc_scripts(struct pkg *, pkg_rc_attr attr);

int pkg_script_run(struct pkg *, pkg_script_t type);

modified libpkg/rcscripts.c
@@ -38,7 +38,7 @@ static int rc_stop(const char *);
static int rc_start(const char *);

int
-
pkg_stop_rc_scripts(struct pkg *pkg)
+
pkg_start_stop_rc_scripts(struct pkg *pkg, pkg_rc_attr attr)
{
	struct pkg_file *file = NULL;
	char rc_d_path[PATH_MAX + 1];
@@ -59,40 +59,20 @@ pkg_stop_rc_scripts(struct pkg *pkg)
			rcfile += len;
			rc = strrchr(rcfile, '/');
			rc++;
-
			ret += rc_stop(rcfile);
+
			switch (attr) {
+
			case PKG_RC_START:
+
				ret += rc_start(rcfile);
+
				break;
+
			case PKG_RC_STOP:
+
				ret += rc_stop(rcfile);
+
				break;
+
			}
		}
	}

	return (ret);
}

-
int
-
pkg_start_rc_scripts(struct pkg *pkg)
-
{
-
	struct pkg_file *file = NULL;
-
	char rc_d_path[PATH_MAX + 1];
-
	const char *rcfile;
-
	const char *rc;
-
	size_t len = 0;
-
	int ret = 0;
-
	const char *prefix;
-

-
	pkg_get(pkg, PKG_PREFIX, &prefix);
-
	snprintf(rc_d_path, PATH_MAX, "%s/etc/rc.d/", prefix);
-
	len = strlen(rc_d_path);
-

-
	while (pkg_files(pkg, &file) == EPKG_OK) {
-
		if (strncmp(rc_d_path, pkg_file_get(file, PKG_FILE_PATH), len) == 0) {
-
			rcfile = pkg_file_get(file, PKG_FILE_PATH);
-
			rcfile += len;
-
			rc = strrchr(rcfile, '/');
-
			rc++;
-
			ret += rc_start(rcfile);
-
		}
-
	}
-
	return (ret);
-
}
-

static int
rc_stop(const char *rc_file)
{
@@ -109,7 +89,7 @@ rc_stop(const char *rc_file)
		case 0:
			/* child */
			/*
-
			 * We don't need to see the out put
+
			 * We don't need to see the output
			 */
			fd = open("/dev/null", O_WRONLY);
			dup2(fd, STDERR_FILENO);