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> |
| 20 | |
| 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)); |
| 47 | if (tar_extract_file(t, filename) != 0) |
| 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 |
| 82 | printf("filename: %s\n", filename); |
| 83 | /* |
| 84 | if (strcmp(filename, "/") == 0) { |
| 85 | printf("skipping /\n"); |
| 86 | continue; |
| 87 | } |
| 88 | */ |
| 89 | if (tar_extract_file(t, buf) != 0) |
| 90 | return -1; |
| 91 | } |
| 92 | return (i == 1 ? 0 : -1); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | int |
| 97 | tar_append_tree(TAR *t, char *realdir, char *savedir) |
| 98 | { |
| 99 | char realpath[MAXPATHLEN]; |
| 100 | char savepath[MAXPATHLEN]; |
| 101 | struct dirent *dent; |
| 102 | DIR *dp; |
| 103 | struct stat s; |
| 104 | |
| 105 | #ifdef DEBUG |
| 106 | printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n", |
| 107 | t, realdir, (savedir ? savedir : "[NULL]")); |
| 108 | #endif |
| 109 | |
| 110 | if (tar_append_file(t, realdir, savedir) != 0) |
| 111 | return -1; |
| 112 | |
| 113 | #ifdef DEBUG |
| 114 | puts(" tar_append_tree(): done with tar_append_file()..."); |
| 115 | #endif |
| 116 | |
| 117 | dp = opendir(realdir); |
| 118 | if (dp == NULL) |
| 119 | { |
| 120 | if (errno == ENOTDIR) |
| 121 | return 0; |
| 122 | return -1; |
| 123 | } |
| 124 | while ((dent = readdir(dp)) != NULL) |
| 125 | { |
| 126 | if (strcmp(dent->d_name, ".") == 0 || |
| 127 | strcmp(dent->d_name, "..") == 0) |
| 128 | continue; |
| 129 | |
| 130 | snprintf(realpath, MAXPATHLEN, "%s/%s", realdir, |
| 131 | dent->d_name); |
| 132 | if (savedir) |
| 133 | snprintf(savepath, MAXPATHLEN, "%s/%s", savedir, |
| 134 | dent->d_name); |
| 135 | |
| 136 | if (lstat(realpath, &s) != 0) |
| 137 | return -1; |
| 138 | |
| 139 | if (S_ISDIR(s.st_mode)) |
| 140 | { |
| 141 | if (tar_append_tree(t, realpath, |
| 142 | (savedir ? savepath : NULL)) != 0) |
| 143 | return -1; |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | if (tar_append_file(t, realpath, |
| 148 | (savedir ? savepath : NULL)) != 0) |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | closedir(dp); |
| 153 | |
| 154 | return 0; |
| 155 | } |