bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 1998-2003 University of Illinois Board of Trustees |
| 3 | ** Copyright 1998-2003 Mark D. Roth |
| 4 | ** All rights reserved. |
| 5 | ** |
| 6 | ** wrapper.c - libtar high-level wrapper code |
| 7 | ** |
| 8 | ** Mark D. Roth <roth@uiuc.edu> |
| 9 | ** Campus Information Technologies and Educational Services |
| 10 | ** University of Illinois at Urbana-Champaign |
| 11 | */ |
| 12 | |
| 13 | #define DEBUG |
| 14 | #include <internal.h> |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <sys/param.h> |
| 18 | #include <dirent.h> |
| 19 | #include <errno.h> |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 21 | #ifdef STDC_HEADERS |
| 22 | # include <string.h> |
| 23 | #endif |
| 24 | |
| 25 | int |
| 26 | tar_extract_glob(TAR *t, char *globname, char *prefix) |
| 27 | { |
| 28 | char *filename; |
| 29 | char buf[MAXPATHLEN]; |
| 30 | int i; |
| 31 | |
| 32 | while ((i = th_read(t)) == 0) |
| 33 | { |
| 34 | filename = th_get_pathname(t); |
| 35 | if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD)) |
| 36 | { |
| 37 | if (TH_ISREG(t) && tar_skip_regfile(t)) |
| 38 | return -1; |
| 39 | continue; |
| 40 | } |
| 41 | if (t->options & TAR_VERBOSE) |
| 42 | th_print_long_ls(t); |
| 43 | if (prefix != NULL) |
| 44 | snprintf(buf, sizeof(buf), "%s/%s", prefix, filename); |
| 45 | else |
| 46 | strlcpy(buf, filename, sizeof(buf)); |
Dees_Troy | ee6632c | 2013-02-27 18:07:32 +0000 | [diff] [blame] | 47 | if (tar_extract_file(t, filename, prefix) != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | return (i == 1 ? 0 : -1); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | int |
| 56 | tar_extract_all(TAR *t, char *prefix) |
| 57 | { |
| 58 | char *filename; |
| 59 | char buf[MAXPATHLEN]; |
| 60 | int i; |
| 61 | printf("prefix: %s\n", prefix); |
| 62 | #ifdef DEBUG |
| 63 | printf("==> tar_extract_all(TAR *t, \"%s\")\n", |
| 64 | (prefix ? prefix : "(null)")); |
| 65 | #endif |
| 66 | while ((i = th_read(t)) == 0) |
| 67 | { |
| 68 | #ifdef DEBUG |
| 69 | puts(" tar_extract_all(): calling th_get_pathname()"); |
| 70 | #endif |
| 71 | filename = th_get_pathname(t); |
| 72 | if (t->options & TAR_VERBOSE) |
| 73 | th_print_long_ls(t); |
| 74 | if (prefix != NULL) |
| 75 | snprintf(buf, sizeof(buf), "%s/%s", prefix, filename); |
| 76 | else |
| 77 | strlcpy(buf, filename, sizeof(buf)); |
| 78 | #ifdef DEBUG |
| 79 | printf(" tar_extract_all(): calling tar_extract_file(t, " |
| 80 | "\"%s\")\n", buf); |
| 81 | #endif |
Dees_Troy | 7179659 | 2013-03-14 20:16:29 +0000 | [diff] [blame] | 82 | printf("item name: '%s'\n", filename); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 83 | /* |
| 84 | if (strcmp(filename, "/") == 0) { |
| 85 | printf("skipping /\n"); |
| 86 | continue; |
| 87 | } |
| 88 | */ |
Dees_Troy | ee6632c | 2013-02-27 18:07:32 +0000 | [diff] [blame] | 89 | if (tar_extract_file(t, buf, prefix) != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | return (i == 1 ? 0 : -1); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | int |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 97 | tar_append_tree(TAR *t, char *realdir, char *savedir, char *exclude) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 98 | { |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 99 | #ifdef DEBUG |
| 100 | printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n", |
| 101 | (long unsigned int)t, realdir, (savedir ? savedir : "[NULL]")); |
| 102 | #endif |
| 103 | |
| 104 | char temp[1024]; |
| 105 | int skip = 0, i, n_spaces = 0; |
| 106 | char ** excluded = NULL; |
| 107 | char * p = NULL; |
| 108 | if (exclude) { |
| 109 | strcpy(temp, exclude); |
| 110 | p = strtok(exclude, " "); |
| 111 | if (p == NULL) { |
| 112 | excluded = realloc(excluded, sizeof(char*) * (++n_spaces)); |
| 113 | excluded[0] = temp; |
| 114 | } else { |
| 115 | while (p) { |
| 116 | excluded = realloc(excluded, sizeof(char*) * (++n_spaces)); |
| 117 | excluded[n_spaces-1] = p; |
| 118 | p = strtok(NULL, " "); |
| 119 | } |
| 120 | } |
| 121 | excluded = realloc(excluded, sizeof(char*) * (n_spaces+1)); |
| 122 | excluded[n_spaces] = 0; |
| 123 | for (i = 0; i < (n_spaces+1); i++) { |
| 124 | if (realdir == excluded[i]) { |
| 125 | printf(" excluding '%s'\n", excluded[i]); |
| 126 | skip = 1; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | if (skip == 0) { |
| 132 | if (tar_append_file(t, realdir, savedir) != 0) |
| 133 | return -1; |
| 134 | } |
| 135 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 136 | char realpath[MAXPATHLEN]; |
| 137 | char savepath[MAXPATHLEN]; |
| 138 | struct dirent *dent; |
| 139 | DIR *dp; |
| 140 | struct stat s; |
| 141 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 142 | dp = opendir(realdir); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 143 | if (dp == NULL) { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 144 | if (errno == ENOTDIR) |
| 145 | return 0; |
| 146 | return -1; |
| 147 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 148 | while ((dent = readdir(dp)) != NULL) { |
| 149 | if(strcmp(dent->d_name, ".") == 0 |
| 150 | || strcmp(dent->d_name, "..") == 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 151 | continue; |
| 152 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 153 | if (exclude) { |
| 154 | int omit = 0; |
| 155 | for (i = 0; i < (n_spaces+1); i++) { |
| 156 | if (dent->d_name == excluded[i]) { |
| 157 | printf(" excluding '%s'\n", excluded[i]); |
| 158 | omit = 1; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | if (omit) |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | snprintf(realpath, MAXPATHLEN, "%s/%s", realdir, dent->d_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 167 | if (savedir) |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 168 | snprintf(savepath, MAXPATHLEN, "%s/%s", savedir, dent->d_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 169 | |
| 170 | if (lstat(realpath, &s) != 0) |
| 171 | return -1; |
| 172 | |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 173 | if (S_ISDIR(s.st_mode)) { |
| 174 | if (tar_append_tree(t, realpath, (savedir ? savepath : NULL), (exclude ? exclude : NULL)) != 0) |
| 175 | return -1; |
| 176 | continue; |
| 177 | } else { |
| 178 | if (tar_append_file(t, realpath, (savedir ? savepath : NULL)) != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 179 | return -1; |
| 180 | continue; |
| 181 | } |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 182 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 183 | closedir(dp); |
Dees_Troy | 83bd483 | 2013-05-04 12:39:56 +0000 | [diff] [blame] | 184 | free(excluded); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 188 | |
| 189 | |
| 190 | int |
| 191 | tar_find(TAR *t, char *searchstr) |
| 192 | { |
| 193 | if (!searchstr) |
| 194 | return 0; |
| 195 | |
| 196 | char *filename; |
| 197 | int i, entryfound = 0; |
| 198 | #ifdef DEBUG |
| 199 | printf("==> tar_find(0x%lx, %s)\n", (long unsigned int)t, searchstr); |
| 200 | #endif |
| 201 | while ((i = th_read(t)) == 0) { |
| 202 | filename = th_get_pathname(t); |
| 203 | if (fnmatch(searchstr, filename, FNM_FILE_NAME | FNM_PERIOD) == 0) { |
| 204 | entryfound++; |
| 205 | #ifdef DEBUG |
| 206 | printf("Found matching entry: %s\n", filename); |
| 207 | #endif |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | #ifdef DEBUG |
| 212 | if (!entryfound) |
| 213 | printf("No matching entry found.\n"); |
| 214 | #endif |
| 215 | |
| 216 | return entryfound; |
| 217 | } |