summaryrefslogtreecommitdiff
path: root/src/exclude.c
blob: c1c018d2cd4c6338db12ebe361b3d416bb0aa6ce (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* ncdu - NCurses Disk Usage

  Copyright (c) 2007-2022 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#include "global.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fnmatch.h>


static struct exclude {
  char *pattern;
  struct exclude *next;
} *excludes = NULL;



void exclude_add(char *pat) {
  struct exclude **n;

  n = &excludes;
  while(*n != NULL)
    n = &((*n)->next);

  *n = (struct exclude *) xcalloc(1, sizeof(struct exclude));
  (*n)->pattern = (char *) xmalloc(strlen(pat)+1);
  strcpy((*n)->pattern, pat);
}


int exclude_addfile(char *file) {
  FILE *f;
  char buf[256];
  int len;

  if((f = fopen(file, "r")) == NULL)
    return 1;

  while(fgets(buf, 256, f) != NULL) {
    len = strlen(buf)-1;
    while(len >=0 && (buf[len] == '\r' || buf[len] == '\n'))
      buf[len--] = '\0';
    if(len < 0)
      continue;
    exclude_add(buf);
  }

  len = ferror(f);
  fclose(f);
  return len;
}


int exclude_match(char *path) {
  struct exclude *n;
  char *c;

  for(n=excludes; n!=NULL; n=n->next) {
    if(!fnmatch(n->pattern, path, 0))
      return 1;
    for(c = path; *c; c++)
      if(*c == '/' && c[1] != '/' && !fnmatch(n->pattern, c+1, 0))
        return 1;
  }
  return 0;
}


void exclude_clear(void) {
  struct exclude *n, *l;

  for(n=excludes; n!=NULL; n=l) {
    l = n->next;
    free(n->pattern);
    free(n);
  }
  excludes = NULL;
}


/*
 * Exclusion of directories that contain only cached information.
 * See http://www.brynosaurus.com/cachedir/
 */
#define CACHEDIR_TAG_FILENAME "CACHEDIR.TAG"
#define CACHEDIR_TAG_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"

int has_cachedir_tag(const char *name) {
  static int path_l = 1024;
  static char *path = NULL;
  int l;
  char buf[sizeof CACHEDIR_TAG_SIGNATURE - 1];
  FILE *f;
  int match = 0;

  /* Compute the required length for `path`. */
  l = strlen(name) + sizeof CACHEDIR_TAG_FILENAME + 2;
  if(l > path_l || path == NULL) {
    path_l = path_l * 2;
    if(path_l < l)
      path_l = l;
    /* We don't need to copy the content of `path`, so it's more efficient to
     * use `free` + `malloc`. */
    free(path);
    path = xmalloc(path_l);
  }
  snprintf(path, path_l, "%s/%s", name, CACHEDIR_TAG_FILENAME);
  f = fopen(path, "rb");

  if(f != NULL) {
    match = ((fread(buf, 1, sizeof buf, f) == sizeof buf) &&
                !memcmp(buf, CACHEDIR_TAG_SIGNATURE, sizeof buf));
    fclose(f);
  }
  return match;
}