summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Camera <skonfig@dtnr.ch>2022-08-21 00:45:19 +0200
committerDennis Camera <skonfig@dtnr.ch>2022-08-21 00:47:31 +0200
commit02921040ade564177304b24ffbef15d45017b317 (patch)
tree773c844c71ca1d30b1c96e0b0da8bd4d6b775c79
parentb307c6010c7439191064e80da04b6e1672b2001e (diff)
downloadskonfig-c-02921040ade564177304b24ffbef15d45017b317.tar.gz
skonfig-c-02921040ade564177304b24ffbef15d45017b317.zip
[src/run.c] Implement linking SK_PATH into local_hostdir
-rw-r--r--src/run.c159
-rw-r--r--src/util/fs.h13
2 files changed, 146 insertions, 26 deletions
diff --git a/src/run.c b/src/run.c
index bddcaf9..8ff27b7 100644
--- a/src/run.c
+++ b/src/run.c
@@ -3,6 +3,7 @@
#include "log.h"
#include "util/array.h"
+#include "util/fs.h"
#include <stdio.h>
#include <stdlib.h>
@@ -13,6 +14,8 @@
#include <limits.h>
#include <ftw.h>
#include <fcntl.h>
+#include <sys/types.h>
+#include <dirent.h>
#ifndef P_tmpdir
#define P_tmpdir "/tmp"
@@ -155,27 +158,24 @@ int creatfile(const char *path, mode_t mode) {
return fd;
}
-sk_run_error sk_run_prepare_local_workdir(const char *target_host, const char *workdir) {
+sk_run_error sk_run_prepare_local_hostdir(const char *local_hostdir) {
static const char *subdirs[] = {
"bin", "conf", "explorer", "object", "stdout", "stderr"
};
- char host_dir[PATH_MAX+1], subpath[PATH_MAX+1];
+ char subpath[PATH_MAX+1];
- if (PATH_MAX < pathjoin_r(host_dir, PATH_MAX+1, workdir, target_host)) {
- goto path_overflow;
- }
-
- /* create host_dir */
- if (mkdir(host_dir, S_IRWXU)) {
+ /* create local host dir */
+ if (mkdir(local_hostdir, S_IRWXU)) {
sk_error("failed to create host directory (%s): %s",
- host_dir, strerror(errno));
+ local_hostdir, strerror(errno));
return SK_RUN_GENERIC_ERROR;
}
/* create sub directories of host_dir */
for (size_t i = 0; i < arraylen(subdirs); ++i) {
- if (PATH_MAX < pathjoin_r(subpath, PATH_MAX+1, host_dir, subdirs[i])) {
+ if (PATH_MAX < pathjoin_r(
+ subpath, PATH_MAX+1, local_hostdir, subdirs[i])) {
goto path_overflow;
}
@@ -187,7 +187,7 @@ sk_run_error sk_run_prepare_local_workdir(const char *target_host, const char *w
}
/* create messages file */
- if (PATH_MAX < pathjoin_r(subpath, PATH_MAX+1, host_dir, "messages")) {
+ if (PATH_MAX < pathjoin_r(subpath, PATH_MAX+1, local_hostdir, "messages")) {
goto path_overflow;
}
if (0 > creatfile(subpath, S_IRUSR | S_IWUSR)) {
@@ -203,12 +203,115 @@ sk_run_error sk_run_prepare_local_workdir(const char *target_host, const char *w
return SK_RUN_GENERIC_ERROR;
}
+sk_run_error sk_run_link_local_hostdir(
+ const char *local_hostdir, struct sk_config *config) {
+ static const char *conf_subdirs_linked[] = {
+ "explorer", "files", "manifest", "type"
+ };
+
+ char tmppath[PATH_MAX+1];
+
+ /* create linked host_dir/conf sub directories */
+ for (size_t i = 0; i < arraylen(conf_subdirs_linked); ++i) {
+ if (PATH_MAX < pathjoin_r(
+ tmppath, PATH_MAX+1,
+ local_hostdir, "conf", conf_subdirs_linked[i])) {
+ goto path_overflow;
+ }
+
+ if (mkdir(tmppath, S_IRWXU)) {
+ sk_error("failed to create directory (%s): %s",
+ tmppath, strerror(errno));
+ return SK_RUN_GENERIC_ERROR;
+ }
+ }
+
+ /* iterate over SK_PATH directories and link their conents to the hostdir */
+ if (NULL == config->global.conf_dir) {
+ /* nothing to do */
+ return SK_RUN_OK;
+ }
+
+ for (size_t i = 0; NULL != config->global.conf_dir[i]; ++i) {
+ const char *conf_src = config->global.conf_dir[i];
+
+ sk_debug("Checking conf_dir %s ...", conf_src);
+
+ for (size_t j = 0; j < arraylen(conf_subdirs_linked); ++j) {
+ if (PATH_MAX < pathjoin_r(
+ tmppath, PATH_MAX+1, conf_src, conf_subdirs_linked[j])) {
+ goto path_overflow;
+ }
+
+ /* check if said subdir exists in the current SK_PATH element */
+ if (!exists(tmppath)) continue;
+
+ DIR *dirp = opendir(tmppath);
+ if (NULL == dirp) {
+ sk_error("cannot open directory (%s): %s",
+ tmppath, strerror(errno));
+ continue;
+ }
+
+ struct dirent *dp;
+ char src[PATH_MAX+1], dst[PATH_MAX+1];
+ while (NULL != (dp = readdir(dirp))) {
+ if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
+ continue;
+ }
+
+ if (PATH_MAX < pathjoin_r(
+ tmppath, PATH_MAX+1,
+ conf_src, conf_subdirs_linked[j], dp->d_name)) {
+ sk_error("source path too long: %s/%s/%s",
+ conf_src, conf_subdirs_linked[j], dp->d_name);
+ continue;
+ }
+ if (NULL == realpath(tmppath, src)) {
+ sk_error("cannot resolve path %s: %s",
+ tmppath, strerror(errno));
+ continue;
+ }
+
+ if (PATH_MAX < pathjoin_r(
+ dst, PATH_MAX+1,
+ local_hostdir, "conf", conf_subdirs_linked[j], dp->d_name)) {
+ sk_error("destination path too long for conf/%s/%s",
+ conf_subdirs_linked[j], dp->d_name);
+ continue;
+ }
+
+ if (exists(dst)) {
+ /* already exists -> remove first */
+ remove(dst);
+ }
+
+ sk_trace("Linking %s -> %s", dst, src);
+
+ if (symlink(src, dst)) {
+ sk_error(
+ "Linking %s %s to %s failed: %s",
+ conf_subdirs_linked[j], src, dst, strerror(errno));
+ }
+ }
+
+ (void)closedir(dirp);
+ }
+ }
+
+ return SK_RUN_OK;
+
+ path_overflow:
+ sk_error("path is longer than PATH_MAX");
+ return SK_RUN_GENERIC_ERROR;
+}
+
sk_run_error sk_run_init_manifest(
const char *target_host, bool dry_run, struct sk_config *config) {
/* check if the initial manifest file exists */
const char *init_manifest = config->global.init_manifest;
- if (access(init_manifest, F_OK)) {
+ if (!exists(init_manifest)) {
sk_error("cannot find initial manifest (%s): %s",
init_manifest, strerror(errno));
return SK_RUN_GENERIC_ERROR;
@@ -220,17 +323,28 @@ sk_run_error sk_run_init_manifest(
}
sk_run_error sk_run_host(
- const char *target_host, bool dry_run, const char *host_workdir,
+ const char *target_host, bool dry_run, const char *local_hostdir,
struct sk_config *config) {
- if (setenv("__global", host_workdir, 1))
+ sk_run_error rv = SK_RUN_OK;
+
+ /* create the local hostdir */
+ rv = sk_run_prepare_local_hostdir(local_hostdir);
+ if (SK_RUN_OK != rv) return rv;
+
+ if (setenv("__global", local_hostdir, 1))
return SK_RUN_GENERIC_ERROR;
+ /* create conf path and link conf dirs */
+ rv = sk_run_link_local_hostdir(local_hostdir, config);
+ if (SK_RUN_OK != rv) return rv;
return SK_RUN_OK;
}
sk_run_error sk_run_start(
const char *target_host, bool dry_run, struct sk_config *config) {
sk_run_error rv = SK_RUN_OK;
+ char local_workdir[PATH_MAX+1] = "";
+ char local_hostdir[PATH_MAX+1] = "";
if (1 < config->global.jobs) {
sk_warn("using multiple jobs is currently not implemented.");
@@ -244,28 +358,25 @@ sk_run_error sk_run_start(
sk_info("%s: Starting %s run",
target_host, (!dry_run ? "configuration" : "dry"));
- /* set up local cache paths */
- char local_workdir[PATH_MAX+1];
+ /* determine local workdir */
if ((NULL == sk_run_gen_workdir_path(config, local_workdir))) {
return SK_RUN_GENERIC_ERROR;
}
sk_debug("%s: local work directory: %s", target_host, local_workdir);
- rv = sk_run_prepare_local_workdir(target_host, local_workdir);
- if (SK_RUN_OK != rv) {
- goto abort;
+ if (PATH_MAX < pathjoin_r(
+ local_hostdir, PATH_MAX+1, local_workdir, target_host)) {
+ sk_error("%s: local host directory name is longer than PATH_MAX", target_host);
+ return SK_RUN_GENERIC_ERROR;
}
- char *host_workdir = pathjoin(local_workdir, target_host);
- rv = sk_run_host(target_host, dry_run, host_workdir, config);
- free(host_workdir);
+ rv = sk_run_host(target_host, dry_run, local_hostdir, config);
- abort:
if (SK_RUN_OK != rv) {
sk_error("failed to configure the following host: %s", target_host);
} else {
/* cleanup local workdir */
- if (-1 == rmtree(local_workdir)) {
+ if (*local_workdir && -1 == rmtree(local_workdir)) {
sk_error("failed to clean up workdir (%s): %s",
local_workdir, strerror(errno));
}
diff --git a/src/util/fs.h b/src/util/fs.h
index 9701bc8..7be8527 100644
--- a/src/util/fs.h
+++ b/src/util/fs.h
@@ -13,11 +13,20 @@
#endif
/**
+ * exists:
+ * Check if a given pathname exists.
+ *
+ * @param pathname: the path to check
+ * @return true if @pathname exists, false otherwise.
+ */
+#define exists(pathname) (0 == access(pathname, F_OK))
+
+/**
* is_dir:
* Check if a given pathname is a directory.
*
- * @returns true if the given pathname exists and is a directory, false
- * otherwise.
+ * @param pathname: the path to check
+ * @return true if @pathname exists and is a directory, false otherwise.
*/
bool is_dir(const char *restrict pathname);