commit 661329653329949929b043f5fc45821aa314face
parent 9575ac2abacc87c4167d419e5a1fa1d650dee11d
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Wed, 14 Nov 2018 17:08:47 +0000
Add env_dump()
Diffstat:
5 files changed, 55 insertions(+), 0 deletions(-)
diff --git a/NEWS b/NEWS
@@ -7,6 +7,7 @@ In 2.8.0.0
- avltreeb removed. (It used VLAs inside of structures, which is
GNU C, not C99.) The AVLTREEN_DECLARE_AND_INIT() macro should be
used instead.
+ - New function: env_dump()
In 2.7.0.0
diff --git a/doc/upgrade.html b/doc/upgrade.html
@@ -21,6 +21,7 @@
<ul>
<li> The avltreeb macros have been removed. Instead, the
<tt>AVLTREEN_DECLARE_AND_INIT() macro has been added. </li>
+ <li> New function: <tt>env_dump()</tt> </li>
</ul>
<h2> in 2.7.0.0 </h2>
diff --git a/package/deps.mak b/package/deps.mak
@@ -288,6 +288,7 @@ src/libstddjb/deepsleepuntil.o src/libstddjb/deepsleepuntil.lo: src/libstddjb/de
src/libstddjb/dir_close.o src/libstddjb/dir_close.lo: src/libstddjb/dir_close.c src/include/skalibs/direntry.h
src/libstddjb/dir_fd.o src/libstddjb/dir_fd.lo: src/libstddjb/dir_fd.c src/include/skalibs/direntry.h src/include/skalibs/nonposix.h src/include/skalibs/sysdeps.h
src/libstddjb/env_addmodif.o src/libstddjb/env_addmodif.lo: src/libstddjb/env_addmodif.c src/include/skalibs/env.h src/include/skalibs/stralloc.h
+src/libstddjb/env_dump.o src/libstddjb/env_dump.lo: src/libstddjb/env_dump.c src/include/skalibs/bytestr.h src/include/skalibs/djbunix.h src/include/skalibs/env.h src/include/skalibs/unix-transactional.h
src/libstddjb/env_get.o src/libstddjb/env_get.lo: src/libstddjb/env_get.c src/include/skalibs/posixplz.h
src/libstddjb/env_get2.o src/libstddjb/env_get2.lo: src/libstddjb/env_get2.c src/include/skalibs/bytestr.h src/include/skalibs/env.h
src/libstddjb/env_len.o src/libstddjb/env_len.lo: src/libstddjb/env_len.c src/include/skalibs/env.h
diff --git a/src/include/skalibs/env.h b/src/include/skalibs/env.h
@@ -23,5 +23,6 @@ extern int envdir_internal (char const *, stralloc *, unsigned int, char) ;
#define envdir_chomp(path, sa) envdir_internal(path, (sa), SKALIBS_ENVDIR_NOCHOMP, '\n')
#define envdir_verbatim_chomp(path, sa) envdir_internal(path, (sa), SKALIBS_ENVDIR_VERBATIM, '\n')
#define envdir_verbatim(path, sa) envdir_internal(path, (sa), SKALIBS_ENVDIR_VERBATIM|SKALIBS_ENVDIR_NOCHOMP, '\n')
+extern int env_dump (char const *, mode_t, char const *const *) ;
#endif
diff --git a/src/libstddjb/env_dump.c b/src/libstddjb/env_dump.c
@@ -0,0 +1,51 @@
+/* ISC license. */
+
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <skalibs/bytestr.h>
+#include <skalibs/env.h>
+#include <skalibs/djbunix.h>
+
+ /* XXX: breaks layering, but really openat() should be supported everywhere */
+#include <skalibs/unix-transactional.h>
+
+#define SUFFIX ":envdump:XXXXXX"
+
+int env_dump (char const *dir, mode_t mode, char const *const *envp)
+{
+ int fd ;
+ size_t dirlen = strlen(dir) ;
+ char tmpdir[dirlen + sizeof(SUFFIX)] ;
+ memcpy(tmpdir, dir, dirlen) ;
+ memcpy(tmpdir + dirlen, SUFFIX, sizeof(SUFFIX)) ;
+ if (!mkdtemp(tmpdir)) return 0 ;
+ fd = open_read(tmpdir) ;
+ if (fd == -1) goto err ;
+ for (; *envp ; envp++)
+ {
+ size_t len = str_chr(*envp, '=') ;
+ size_t vallen = strlen(*envp + len + 1) ;
+ char fn[len + 1] ;
+ memcpy(fn, *envp, len + 1) ;
+ len = openwritenclose_at(fd, fn, *envp + len + 1, vallen) ;
+ if (len < vallen) goto cerr ;
+ }
+ fd_close(fd) ;
+ if (chmod(tmpdir, mode) == -1) goto err ;
+ if (rename(tmpdir, dir) == -1) goto err ;
+ return 1 ;
+
+ cerr:
+ fd_close(fd) ;
+ err:
+ {
+ int e = errno ;
+ rm_rf(tmpdir) ;
+ errno = e ;
+ }
+ return 0 ;
+}