s6

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

ftrigw_clean.c (1018B)


      1 /* ISC license. */
      2 
      3 #include <string.h>
      4 #include <unistd.h>
      5 #include <errno.h>
      6 #include <skalibs/direntry.h>
      7 #include <skalibs/djbunix.h>
      8 #include "ftrig1.h"
      9 #include <s6/ftrigw.h>
     10 
     11 int ftrigw_clean (char const *path)
     12 {
     13   size_t pathlen = strlen(path) ;
     14   int e = 0 ;
     15   DIR *dir = opendir(path) ;
     16   if (!dir) return 0 ;
     17   {
     18     char tmp[pathlen + FTRIG1_PREFIXLEN + 45] ;
     19     memcpy(tmp, path, pathlen) ;
     20     tmp[pathlen] = '/' ; tmp[pathlen + FTRIG1_PREFIXLEN + 44] = 0 ;
     21     for (;;)
     22     {
     23       direntry *d ;
     24       int fd ;
     25       errno = 0 ;
     26       d = readdir(dir) ;
     27       if (!d) break ;
     28       if (strncmp(d->d_name, FTRIG1_PREFIX, FTRIG1_PREFIXLEN)) continue ;
     29       if (strlen(d->d_name) != FTRIG1_PREFIXLEN + 43) continue ;
     30       memcpy(tmp + pathlen + 1, d->d_name, FTRIG1_PREFIXLEN + 43) ;
     31       fd = open_write(tmp) ;
     32       if (fd >= 0) fd_close(fd) ;
     33       else if ((errno == ENXIO) && (unlink(tmp) < 0)) e = errno ;
     34     }
     35   }
     36   dir_close(dir) ;
     37   if (errno) e = errno ;
     38   return e ? (errno = e, 0) : 1 ;
     39 }