summaryrefslogtreecommitdiff
path: root/src/libstddjb/rm_rf_in_tmp.c
blob: cc13e95adf5329fd33df20ac5516e6021ed72ab6 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* ISC license. */

#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <skalibs/stralloc.h>
#include <skalibs/direntry.h>
#include <skalibs/djbunix.h>

static int rmstarindir (DIR *dir, stralloc *tmp, size_t ipos) /* WARNING: closes dir */
{
  size_t tmpbase = tmp->len ;
  for (;;)
  {
    direntry *d ;
    errno = 0 ;
    d = readdir(dir) ;
    if (!d) break ;
    if (d->d_name[0] == '.')
      if (((d->d_name[1] == '.') && (d->d_name[2] == 0)) || (d->d_name[1] == 0))
        continue ;
    if (!stralloc_cats(tmp, d->d_name) || !stralloc_0(tmp)) goto closeanderr ;
  }
  if (errno) goto closeanderr ;
  dir_close(dir) ;

  {
    size_t tmpstop = tmp->len ;
    size_t fnbase = strlen(tmp->s + ipos) ;
    size_t i = tmpbase ;
    if (!stralloc_readyplus(tmp, fnbase+1)) goto err ;
    stralloc_catb(tmp, tmp->s + ipos, fnbase) ;
    stralloc_catb(tmp, "/", 1) ;
    fnbase = tmp->len ;
    for (; i < tmpstop ; i += tmp->len - fnbase)
    {
      size_t n = strlen(tmp->s + i) ;
      tmp->len = fnbase ;
      if (!stralloc_readyplus(tmp, n+1)) goto err ;
      stralloc_catb(tmp, tmp->s + i, n+1) ;
      if (rm_rf_in_tmp(tmp, tmpstop) == -1) goto err ;
    }
  }
  tmp->len = tmpbase ;
  return 0 ;

closeanderr:
  {
    int e = errno ;
    dir_close(dir) ;
    errno = e ;
  }
err:
  tmp->len = tmpbase ;
  return -1 ;
}

int rm_rf_in_tmp (stralloc *tmp, size_t ipos)
{
  if (unlink(tmp->s + ipos) == 0) return 0 ;
  if (errno == ENOENT) return 0 ;
  if ((errno != EISDIR) && (errno != EPERM)) return -1 ;
  {
    int h = (errno == EPERM) ;
    DIR *dir = opendir(tmp->s + ipos) ;
    if (!dir)
    {
      if (h && (errno == ENOTDIR)) errno = EPERM ;
      return -1 ;
    }
    if (rmstarindir(dir, tmp, ipos) == -1) return -1 ;
  }
  return rmdir(tmp->s + ipos) ;
}

int rmstar_tmp (char const *dirname, stralloc *tmp)
{
  size_t tmpbase = tmp->len ;
  if (!stralloc_cats(tmp, dirname)) return -1 ;
  if (!stralloc_0(tmp)) goto err ;
  {
    DIR *dir = opendir(dirname) ;
    if (!dir) goto err ;
    if (rmstarindir(dir, tmp, tmpbase) == -1) goto err ;
  }
  tmp->len = tmpbase ;
  return 0 ;

err:
  tmp->len = tmpbase ;
  return -1 ;
}