s6

Mirror/fork of https://skarnet.org/software/s6/
git clone https://ccx.te2000.cz/git/s6
Log | Files | Refs | README | LICENSE

ftrig1_make.c (1229B)


      1 /* ISC license. */
      2 
      3 #include <string.h>
      4 #include <stdio.h>
      5 #include <fcntl.h>
      6 
      7 #include <skalibs/posixplz.h>
      8 #include <skalibs/tai.h>
      9 #include <skalibs/stralloc.h>
     10 #include <skalibs/djbunix.h>
     11 
     12 #include "ftrig1.h"
     13 
     14 int ftrig1_make (ftrig1_t *f, char const *path)
     15 {
     16   ftrig1_t ff = FTRIG1_ZERO ;
     17   size_t pathlen = strlen(path) ;
     18   char tmp[pathlen + FTRIG1_PREFIXLEN + 36] ;
     19   
     20   memcpy(tmp, path, pathlen) ;
     21   tmp[pathlen] = '/' ; tmp[pathlen+1] = '.' ;
     22   memcpy(tmp + pathlen + 2, FTRIG1_PREFIX, FTRIG1_PREFIXLEN) ;
     23   tmp[pathlen + 2 + FTRIG1_PREFIXLEN] = ':' ;
     24   if (!timestamp(tmp + pathlen + 3 + FTRIG1_PREFIXLEN)) return 0 ;
     25   memcpy(tmp + pathlen + FTRIG1_PREFIXLEN + 28, ":XXXXXX", 8) ;
     26   ff.fd = mkptemp2(tmp, O_NONBLOCK|O_CLOEXEC) ;
     27   if (ff.fd == -1) return 0 ;
     28   ff.fdw = open_write(tmp) ;
     29   if (ff.fdw == -1) goto err1 ;
     30   if (!stralloc_ready(&ff.name, pathlen + FTRIG1_PREFIXLEN + 36)) goto err2 ;
     31   stralloc_copyb(&ff.name, tmp, pathlen + 1) ;
     32   stralloc_catb(&ff.name, tmp + pathlen + 2, FTRIG1_PREFIXLEN + 34) ;
     33   if (rename(tmp, ff.name.s) == -1) goto err3 ;
     34   *f = ff ;
     35   return 1 ;
     36 
     37  err3:
     38   stralloc_free(&ff.name) ;
     39  err2:
     40   fd_close(ff.fdw) ;
     41  err1:
     42   fd_close(ff.fd) ;
     43   unlink_void(tmp) ;
     44   return 0 ;
     45 }