From af60b6b5f89cff32d722f1d6fed2f3b51e359812 Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Tue, 25 Aug 2020 18:18:07 +0000
Subject: [PATCH] service can have @version field but user updated it again.
 Handle this case

---
 src/lib66/ss_environ.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/lib66/ss_environ.c b/src/lib66/ss_environ.c
index db5492f8..05f2697c 100644
--- a/src/lib66/ss_environ.c
+++ b/src/lib66/ss_environ.c
@@ -200,12 +200,35 @@ int env_make_symlink(stralloc *dst,stralloc *old_dst,sv_alltype *sv,uint8_t conf
 				log_warn_return(LOG_EXIT_ZERO,"get basename of: ",saversion.s) ;
 			dname[strlen(dname) - 1] = 0 ;
 			saversion.len = 0 ;
+			/** user may have updated the service which contain the @version field
+			 * without enable it again. In that case we have the format
+			 * /etc/66/conf/service and version directory doesn't exist.
+			 * So, dst->s and dname are equal*/
+			if (!strcmp(dst->s,dname)) {
+
+				char tmp[dst->len + 1 + strlen(version) + 1 + strlen(name) + 1] ;
+				auto_strings(tmp,dst->s,"/",version) ;
+
+				char dtmp[dst->len + 1 + strlen(name) + 1] ;
+				auto_strings(dtmp,dst->s,"/",name) ;
+
+				if (!dir_create_parent(tmp,0755))
+					log_warnsys_return(LOG_EXIT_ZERO,"create directory: ",tmp) ;
+
+				auto_strings(tmp,dst->s,"/",version,"/",name) ;
+
+				if (rename(dtmp,tmp) == -1)
+					log_warnusys_return(LOG_EXIT_ZERO,"rename: ",dtmp," to: ",tmp) ;
+
+				auto_strings(dname,dst->s,"/",version) ;
+			}
+
 			if (!auto_stra(&saversion,dname))
 				log_warn_return(LOG_EXIT_ZERO,"stralloc") ;
 
 			if (!ob_basename(current_version,dname))
 				log_warn_return(LOG_EXIT_ZERO,"get basename of: ",dname) ;
-			
+
 			{
 				char tmp[dst->len + strlen(current_version) + 2] ;
 				auto_strings(tmp,dst->s,"/",current_version) ;
-- 
GitLab