2012-08-27 11:51:08 -08:00
|
|
|
/* ncdu - NCurses Disk Usage
|
|
|
|
|
|
2012-01-18 01:40:50 -09:00
|
|
|
Copyright (c) 2007-2012 Yoran Heling
|
2007-07-21 05:55:51 -08:00
|
|
|
|
|
|
|
|
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:
|
2012-08-27 11:51:08 -08:00
|
|
|
|
2007-07-21 05:55:51 -08:00
|
|
|
The above copyright notice and this permission notice shall be included
|
|
|
|
|
in all copies or substantial portions of the Software.
|
2012-08-27 11:51:08 -08:00
|
|
|
|
2007-07-21 05:55:51 -08:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2009-04-11 00:48:24 -08:00
|
|
|
#include "exclude.h"
|
2007-07-21 05:55:51 -08:00
|
|
|
|
2009-04-11 00:53:39 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <fnmatch.h>
|
|
|
|
|
|
2007-07-21 05:55:51 -08:00
|
|
|
|
|
|
|
|
struct exclude {
|
|
|
|
|
char *pattern;
|
|
|
|
|
struct exclude *next;
|
2009-04-11 00:48:24 -08:00
|
|
|
} *excludes = NULL;
|
2007-07-21 05:55:51 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-04-11 00:48:24 -08:00
|
|
|
void exclude_add(char *pat) {
|
|
|
|
|
struct exclude **n;
|
2007-07-21 05:55:51 -08:00
|
|
|
|
2009-04-11 00:48:24 -08:00
|
|
|
n = &excludes;
|
|
|
|
|
while(*n != NULL)
|
|
|
|
|
n = &((*n)->next);
|
2007-07-21 05:55:51 -08:00
|
|
|
|
2009-04-11 00:48:24 -08:00
|
|
|
*n = (struct exclude *) calloc(1, sizeof(struct exclude));
|
|
|
|
|
(*n)->pattern = (char *) malloc(strlen(pat)+1);
|
|
|
|
|
strcpy((*n)->pattern, pat);
|
2007-07-21 05:55:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-04-11 00:48:24 -08:00
|
|
|
int exclude_addfile(char *file) {
|
2007-07-21 05:55:51 -08:00
|
|
|
FILE *f;
|
|
|
|
|
char buf[256];
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
if((f = fopen(file, "r")) == NULL)
|
2009-04-11 00:48:24 -08:00
|
|
|
return 1;
|
2007-07-21 05:55:51 -08:00
|
|
|
|
|
|
|
|
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;
|
2009-04-11 00:48:24 -08:00
|
|
|
exclude_add(buf);
|
2007-07-21 05:55:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(f);
|
2009-04-11 00:48:24 -08:00
|
|
|
return 0;
|
2007-07-21 05:55:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-04-11 00:48:24 -08:00
|
|
|
int exclude_match(char *path) {
|
|
|
|
|
struct exclude *n;
|
2007-07-21 05:55:51 -08:00
|
|
|
char *c;
|
|
|
|
|
|
2009-04-11 00:48:24 -08:00
|
|
|
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() {
|
|
|
|
|
struct exclude *n, *l;
|
|
|
|
|
|
|
|
|
|
for(n=excludes; n!=NULL; n=l) {
|
|
|
|
|
l = n->next;
|
2010-04-27 02:49:12 -08:00
|
|
|
free(n->pattern);
|
2009-04-11 00:48:24 -08:00
|
|
|
free(n);
|
|
|
|
|
}
|
|
|
|
|
excludes = NULL;
|
2007-07-21 05:55:51 -08:00
|
|
|
}
|
|
|
|
|
|
2013-04-10 06:41:26 -08:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Exclusion of directories that contain only cached information.
|
|
|
|
|
* See http://www.brynosaurus.com/cachedir/
|
|
|
|
|
*/
|
2013-04-12 04:53:33 -08:00
|
|
|
#define CACHEDIR_TAG_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
|
2013-04-10 06:41:26 -08:00
|
|
|
|
|
|
|
|
int has_cachedir_tag(const char *name) {
|
2013-04-12 04:53:33 -08:00
|
|
|
char buf[1024];
|
|
|
|
|
FILE *f;
|
|
|
|
|
int match = 0;
|
|
|
|
|
const int signature_l = sizeof CACHEDIR_TAG_SIGNATURE - 1;
|
|
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%s/CACHEDIR.TAG", name);
|
|
|
|
|
f = fopen(buf, "rb");
|
|
|
|
|
if(f != NULL) {
|
|
|
|
|
match = ((fread(buf, 1, signature_l, f) == signature_l) &&
|
|
|
|
|
!memcmp(buf, CACHEDIR_TAG_SIGNATURE, signature_l));
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
return match;
|
2013-04-10 06:41:26 -08:00
|
|
|
}
|