summaryrefslogtreecommitdiff
path: root/src/libs6/ftrigw_clean.c
blob: e540aba7c772b99581c408a5f190b9520b1f10a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* ISC license. */

#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <skalibs/direntry.h>
#include <skalibs/djbunix.h>
#include "ftrig1.h"
#include <s6/ftrigw.h>

int ftrigw_clean (char const *path)
{
  size_t pathlen = strlen(path) ;
  int e = 0 ;
  DIR *dir = opendir(path) ;
  if (!dir) return 0 ;
  {
    char tmp[pathlen + FTRIG1_PREFIXLEN + 35] ;
    memcpy(tmp, path, pathlen) ;
    tmp[pathlen] = '/' ; tmp[pathlen + FTRIG1_PREFIXLEN + 34] = 0 ;
    for (;;)
    {
      direntry *d ;
      int fd ;
      errno = 0 ;
      d = readdir(dir) ;
      if (!d) break ;
      if (strncmp(d->d_name, FTRIG1_PREFIX, FTRIG1_PREFIXLEN)) continue ;
      if (strlen(d->d_name) != FTRIG1_PREFIXLEN + 33) continue ;
      memcpy(tmp + pathlen + 1, d->d_name, FTRIG1_PREFIXLEN + 33) ;
      fd = open_write(tmp) ;
      if (fd >= 0) fd_close(fd) ;
      else if ((errno == ENXIO) && (unlink(tmp) < 0)) e = errno ;
    }
  }
  dir_close(dir) ;
  if (errno) e = errno ;
  return e ? (errno = e, 0) : 1 ;
}