commit 30d0c173d87b92a9ad2f3c1b643463a894abb1d9
parent 2f017fbdd689e025437783cc8d675a7d304b37c3
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date: Sat, 8 Dec 2018 15:10:09 +0000
Really add mkfootemp and autosurf, I guess ?
Diffstat:
14 files changed, 173 insertions(+), 2 deletions(-)
diff --git a/package/deps.mak b/package/deps.mak
@@ -144,11 +144,12 @@ src/libposixplz/execvep_internal.o src/libposixplz/execvep_internal.lo: src/libp
src/libposixplz/execvep_loose.o src/libposixplz/execvep_loose.lo: src/libposixplz/execvep_loose.c src/libposixplz/posixplz-internal.h src/include/skalibs/posixplz.h
src/libposixplz/getpeereid.o src/libposixplz/getpeereid.lo: src/libposixplz/getpeereid.c src/include/skalibs/nonposix.h src/include/skalibs/posixplz.h src/include/skalibs/sysdeps.h
src/libposixplz/memmem.o src/libposixplz/memmem.lo: src/libposixplz/memmem.c src/include/skalibs/posixplz.h src/include/skalibs/sysdeps.h
-src/libposixplz/mkLtemp.o src/libposixplz/mkLtemp.lo: src/libposixplz/mkLtemp.c src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h
+src/libposixplz/mkLtemp.o src/libposixplz/mkLtemp.lo: src/libposixplz/mkLtemp.c src/include/skalibs/posixplz.h
src/libposixplz/mkbtemp.o src/libposixplz/mkbtemp.lo: src/libposixplz/mkbtemp.c src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h
src/libposixplz/mkctemp.o src/libposixplz/mkctemp.lo: src/libposixplz/mkctemp.c src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h
src/libposixplz/mkfiletemp.o src/libposixplz/mkfiletemp.lo: src/libposixplz/mkfiletemp.c src/include/skalibs/posixplz.h src/include/skalibs/surf.h
-src/libposixplz/mkltemp.o src/libposixplz/mkltemp.lo: src/libposixplz/mkltemp.c src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h
+src/libposixplz/mklinktemp.o src/libposixplz/mklinktemp.lo: src/libposixplz/mklinktemp.c src/include/skalibs/djbunix.h src/libposixplz/posixplz-internal.h src/include/skalibs/posixplz.h
+src/libposixplz/mkltemp.o src/libposixplz/mkltemp.lo: src/libposixplz/mkltemp.c src/include/skalibs/posixplz.h
src/libposixplz/mkptemp.o src/libposixplz/mkptemp.lo: src/libposixplz/mkptemp.c src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h
src/libposixplz/setgroups.o src/libposixplz/setgroups.lo: src/libposixplz/setgroups.c src/include/skalibs/nonposix.h src/include/skalibs/setgroups.h src/include/skalibs/sysdeps.h
src/libposixplz/strnlen.o src/libposixplz/strnlen.lo: src/libposixplz/strnlen.c src/include/skalibs/bytestr.h src/include/skalibs/posixplz.h src/include/skalibs/sysdeps.h
diff --git a/src/include/skalibs/functypes.h b/src/include/skalibs/functypes.h
@@ -49,4 +49,7 @@ typedef alliovfunc_t *alliovfunc_t_ref ;
typedef int createfunc_t (char const *, mode_t, void *) ;
typedef createfunc_t *createfunc_t_ref ;
+typedef int linkfunc_t (char const *, char const *) ;
+typedef linkfunc_t *linkfunc_t_ref ;
+
#endif
diff --git a/src/include/skalibs/posixplz.h b/src/include/skalibs/posixplz.h
@@ -48,6 +48,7 @@ extern pid_t doublefork (void) ;
extern int touch (char const *) ;
extern int mkfiletemp (char *, createfunc_t_ref, mode_t, void *) ;
+extern int mklinktemp (char const *, char *, linkfunc_t_ref) ;
extern int mkptemp (char *) ;
extern int mkltemp (char const *, char *) ;
extern int mkLtemp (char const *, char *) ;
diff --git a/src/libposixplz/mkLtemp.c b/src/libposixplz/mkLtemp.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <unistd.h>
+
+#include <skalibs/posixplz.h>
+
+int mkLtemp (char const *src, char *dst)
+{
+ return mklinktemp(src, dst, &link) ;
+}
diff --git a/src/libposixplz/mkbtemp.c b/src/libposixplz/mkbtemp.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <sys/stat.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/posixplz.h>
+
+static int f (char const *fn, mode_t mode, void *data)
+{
+ dev_t *devp = data ;
+ int r = mknod(fn, (mode & 00777) | S_IFBLK, *devp) ;
+ if (r == -1) return -1 ;
+ r = open_readb(fn) ;
+ if (r == -1) unlink_void(fn) ;
+ return r ;
+}
+
+int mkbtemp (char *s, mode_t mode, dev_t dev)
+{
+ return mkfiletemp(s, &f, mode, &dev) ;
+}
diff --git a/src/libposixplz/mkctemp.c b/src/libposixplz/mkctemp.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <sys/stat.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/posixplz.h>
+
+static int f (char const *fn, mode_t mode, void *data)
+{
+ dev_t *devp = data ;
+ int r = mknod(fn, (mode & 00777) | S_IFCHR, *devp) ;
+ if (r == -1) return -1 ;
+ r = open_readb(fn) ;
+ if (r == -1) unlink_void(fn) ;
+ return r ;
+}
+
+int mkctemp (char *s, mode_t mode, dev_t dev)
+{
+ return mkfiletemp(s, &f, mode, &dev) ;
+}
diff --git a/src/libposixplz/mkfiletemp.c b/src/libposixplz/mkfiletemp.c
@@ -0,0 +1,22 @@
+/* ISC license. */
+
+#include <string.h>
+#include <errno.h>
+
+#include <skalibs/surf.h>
+#include <skalibs/posixplz.h>
+
+int mkfiletemp (char *s, createfunc_t_ref f, mode_t mode, void *data)
+{
+ size_t len = strlen(s) ;
+ size_t xlen = 0 ;
+ int r ;
+ for (; xlen < len ; xlen++) if (s[len - 1 - xlen] != 'X') break ;
+ if (xlen < 6) return (errno = EINVAL, -1) ;
+ do
+ {
+ autosurf_name(s + len - xlen, xlen) ;
+ r = (*f)(s, mode, data) ;
+ } while (r == -1 && errno == EEXIST) ;
+ return r ;
+}
diff --git a/src/libposixplz/mklinktemp.c b/src/libposixplz/mklinktemp.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <unistd.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/posixplz.h>
+
+#include "posixplz-internal.h"
+
+static int f (char const *dst, mode_t mode, void *data)
+{
+ linkarg_t *la = data ;
+ (void)mode ;
+ return (*la->lf)(la->src, dst) ;
+}
+
+int mklinktemp (char const *src, char *dst, linkfunc_t_ref lf)
+{
+ linkarg_t la = { .lf = lf, .src = src } ;
+ return mkfiletemp(dst, &f, 0600, &la) ;
+}
diff --git a/src/libposixplz/mkltemp.c b/src/libposixplz/mkltemp.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <unistd.h>
+
+#include <skalibs/posixplz.h>
+
+int mkltemp (char const *src, char *dst)
+{
+ return mklinktemp(src, dst, &symlink) ;
+}
diff --git a/src/libposixplz/mkptemp.c b/src/libposixplz/mkptemp.c
@@ -0,0 +1,20 @@
+/* ISC license. */
+
+#include <sys/stat.h>
+
+#include <skalibs/djbunix.h>
+#include <skalibs/posixplz.h>
+
+static int f (char const *fn, mode_t mode, void *data)
+{
+ int r = mkfifo(fn, mode) ;
+ if (r == -1) return -1 ;
+ r = open_readb(fn) ;
+ if (r == -1) unlink_void(fn) ;
+ return r ;
+}
+
+int mkptemp (char *s)
+{
+ return mkfiletemp(s, &f, 0600, 0) ;
+}
diff --git a/src/libposixplz/posixplz-internal.h b/src/libposixplz/posixplz-internal.h
@@ -3,6 +3,13 @@
#ifndef POSIXPLZ_INTERNAL_H
#define POSIXPLZ_INTERNAL_H
+typedef struct linkarg_s linkarg_t, *linkarg_t_ref ;
+struct linkarg_s
+{
+ linkfunc_t_ref lf ;
+ char const *src ;
+} ;
+
extern void execvep_internal (char const *, char const *const *, char const *const *, char const *) ;
#endif
diff --git a/src/librandom/autosurf.c b/src/librandom/autosurf.c
@@ -0,0 +1,18 @@
+/* ISC license. */
+
+#include <skalibs/random.h>
+#include <skalibs/surf.h>
+
+void autosurf (char *s, size_t n)
+{
+ static SURFSchedule ctx = SURFSCHEDULE_ZERO ;
+ static int need4seed = 1 ;
+ if (need4seed)
+ {
+ char tmp[160] ;
+ random_makeseed(tmp) ;
+ surf_init(&ctx, tmp) ;
+ need4seed = 0 ;
+ }
+ return surf(&ctx, s, n) ;
+}
diff --git a/src/librandom/autosurf_name.c b/src/librandom/autosurf_name.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <skalibs/surf.h>
+#include "random-internal.h"
+
+void autosurf_name (char *s, size_t n)
+{
+ autosurf(s, n) ;
+ while (n--) s[n] = random_oklist[s[n] & 63] ;
+}
diff --git a/src/librandom/random_oklist.c b/src/librandom/random_oklist.c
@@ -0,0 +1,6 @@
+/* ISC license. */
+
+#include "random-internal.h"
+
+static char const random_oklist_[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZghijklmnopqrstuvwxyz-_0123456789abcdef" ;
+char const *random_oklist = random_oklist_ ;