From 4af1f221456c6ff80edb045dc5b68b87c69f1da4 Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Mon, 5 Jun 2023 21:21:10 +1100
Subject: [PATCH] add env_get_destination()

---
 src/include/66/environ.h                  |  1 +
 src/lib66/environ/deps-lib/deps           |  1 +
 src/lib66/environ/env_get_destination.c   | 44 +++++++++++++++++++++++
 src/lib66/environ/env_prepare_for_write.c | 13 ++-----
 4 files changed, 48 insertions(+), 11 deletions(-)
 create mode 100644 src/lib66/environ/env_get_destination.c

diff --git a/src/include/66/environ.h b/src/include/66/environ.h
index bb0739a7..2a56a943 100644
--- a/src/include/66/environ.h
+++ b/src/include/66/environ.h
@@ -28,6 +28,7 @@ extern int env_make_symlink(resolve_service_t *res) ;
 extern int env_compute(stralloc *result, resolve_service_t *res) ;
 extern int env_clean_with_comment(stralloc *sa) ;
 extern int env_prepare_for_write(stralloc *dst, stralloc *contents, resolve_service_t *res) ;
+extern int env_get_destination(stralloc *sa, resolve_service_t *res) ;
 
 /** version function */
 extern int env_find_current_version(stralloc *sa,char const *svconf) ;
diff --git a/src/lib66/environ/deps-lib/deps b/src/lib66/environ/deps-lib/deps
index 45660af6..8dc6a9bd 100644
--- a/src/lib66/environ/deps-lib/deps
+++ b/src/lib66/environ/deps-lib/deps
@@ -3,6 +3,7 @@ env_check_version.o
 env_clean_with_comment.o
 env_compute.o
 env_find_current_version.o
+env_get_destination.o
 env_import_version_file.o
 env_make_symlink.o
 env_prepare_for_write.o
diff --git a/src/lib66/environ/env_get_destination.c b/src/lib66/environ/env_get_destination.c
new file mode 100644
index 00000000..0614de77
--- /dev/null
+++ b/src/lib66/environ/env_get_destination.c
@@ -0,0 +1,44 @@
+/*
+ * env_resolve_conf.c
+ *
+ * Copyright (c) 2018-2022 Eric Vidal <eric@obarun.org>
+ *
+ * All rights reserved.
+ *
+ * This file is part of Obarun. It is subject to the license terms in
+ * the LICENSE file found in the top-level directory of this
+ * distribution.
+ * This file may not be copied, modified, propagated, or distributed
+ * except according to the terms contained in the LICENSE file./
+ */
+
+#include <string.h>
+
+#include <oblibs/log.h>
+#include <oblibs/string.h>
+
+#include <skalibs/stralloc.h>
+#include <skalibs/djbunix.h>
+
+#include <66/environ.h>
+#include <66/constants.h>
+#include <66/service.h>
+
+int env_get_destination(stralloc *sa, resolve_service_t *res)
+{
+    log_flow() ;
+
+    char *conf = res->sa.s + res->environ.envdir ;
+    size_t conflen = strlen(conf) ;
+    char sym[conflen + SS_SYM_VERSION_LEN + 1] ;
+
+    auto_strings(sym, conf, SS_SYM_VERSION) ;
+
+    if (sareadlink(sa, sym) == -1)
+        log_warnusys_return(LOG_EXIT_ZERO, "read link of: ", sym) ;
+
+    if (!stralloc_0(sa))
+        log_warnusys_return(LOG_EXIT_ZERO, "stralloc") ;
+
+    return 1 ;
+}
diff --git a/src/lib66/environ/env_prepare_for_write.c b/src/lib66/environ/env_prepare_for_write.c
index b688aea9..b4de56d0 100644
--- a/src/lib66/environ/env_prepare_for_write.c
+++ b/src/lib66/environ/env_prepare_for_write.c
@@ -30,20 +30,11 @@ int env_prepare_for_write(stralloc *dst, stralloc *contents, resolve_service_t *
 {
     log_flow() ;
 
-    char *conf = res->sa.s + res->environ.envdir ;
-    size_t conflen = strlen(conf) ;
-    char sym[conflen + SS_SYM_VERSION_LEN + 1] ;
-
-    auto_strings(sym, conf, SS_SYM_VERSION) ;
-
     if (!env_compute(contents, res))
         log_warnu_return(LOG_EXIT_ZERO, "compute environment") ;
 
-    if (sareadlink(dst, sym) == -1)
-        log_warnusys_return(LOG_EXIT_ZERO, "read link of: ", sym) ;
-
-    if (!stralloc_0(dst))
-        log_warnusys_return(LOG_EXIT_ZERO, "stralloc") ;
+    if (!env_get_destination(dst, res))
+        log_warnu_return(LOG_EXIT_ZERO, "get directory destination for environment") ;
 
     return 1 ;
 }
-- 
GitLab