/* Return nonzero if file NAME is excluded. */ bool excluded_name (char const *name, struct tar_stat_info *st) { struct exclist *ep; const char *rname = NULL; char *bname = NULL; bool result; int nr = 0; name += FILE_SYSTEM_PREFIX_LEN (name); /* Try global exclusion list first */ if (excluded_file_name (excluded, name)) return true; if (!st) return false; for (result = false; st && !result; st = st->parent, nr = EXCL_NON_RECURSIVE) { for (ep = st->exclude_list; ep; ep = ep->next) { if (ep->flags & nr) continue; if ((result = excluded_file_name (ep->excluded, name))) break; if (!rname) { rname = name; /* Skip leading ./ */ while (*rname == '.' && ISSLASH (rname[1])) rname += 2; } if ((result = excluded_file_name (ep->excluded, rname))) break; if (!bname) bname = base_name (name); if ((result = excluded_file_name (ep->excluded, bname))) break; } } free (bname); return result; }
int main (int argc, char **argv) { int exclude_options = 0; struct exclude *exclude = new_exclude (); set_program_name (argv[0]); if (argc == 1) error (1, 0, "usage: %s file -- words...", argv[0]); while (--argc) { char *opt = *++argv; if (opt[0] == '-') { int neg = 0; int flag; char *s = opt + 1; if (opt[1] == '-' && opt[2] == 0) { argc--; break; } if (strlen (s) > 3 && memcmp (s, "no-", 3) == 0) { neg = 1; s += 3; } flag = XARGMATCH (opt, s, exclude_keywords, exclude_flags); if (neg) exclude_options &= ~flag; else exclude_options |= flag; } else if (add_exclude_file (add_exclude, exclude, opt, exclude_options, '\n') != 0) error (1, errno, "error loading %s", opt); } for (; argc; --argc) { char *word = *++argv; printf ("%s: %d\n", word, excluded_file_name (exclude, word)); } return 0; }
static bool dir_read (struct file_data const *dir, struct dirdata *dirdata) { register struct dirent *next; register size_t i; /* Address of block containing the files that are described. */ char const **names; /* Number of files in directory. */ size_t nnames; /* Allocated and used storage for file name data. */ char *data; size_t data_alloc, data_used; dirdata->names = 0; dirdata->data = 0; nnames = 0; data = 0; if (dir->desc != -1) { /* Open the directory and check for errors. */ register DIR *reading = opendir (dir->name); if (!reading) return false; /* Initialize the table of filenames. */ data_alloc = 512; data_used = 0; dirdata->data = data = xmalloc (data_alloc); /* Read the directory entries, and insert the subfiles into the `data' table. */ while ((errno = 0, (next = readdir (reading)) != 0)) { char *d_name = next->d_name; size_t d_size = _D_EXACT_NAMLEN (next) + 1; /* Ignore "." and "..". */ if (d_name[0] == '.' && (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0))) continue; if (excluded_file_name (excluded, d_name)) continue; while (data_alloc < data_used + d_size) { if (PTRDIFF_MAX / 2 <= data_alloc) xalloc_die (); dirdata->data = data = xrealloc (data, data_alloc *= 2); } memcpy (data + data_used, d_name, d_size); data_used += d_size; nnames++; } if (errno) { int e = errno; closedir (reading); errno = e; return false; } #if CLOSEDIR_VOID closedir (reading); #else if (closedir (reading) != 0) return false; #endif } /* Create the `names' table from the `data' table. */ if (PTRDIFF_MAX / sizeof *names - 1 <= nnames) xalloc_die (); dirdata->names = names = xmalloc ((nnames + 1) * sizeof *names); dirdata->nnames = nnames; for (i = 0; i < nnames; i++) { names[i] = data; data += strlen (data) + 1; } names[nnames] = 0; return true; }
/* Return nonzero if file NAME is excluded. */ bool excluded_name (char const *name) { return excluded_file_name (excluded, name + FILE_SYSTEM_PREFIX_LEN (name)); }