Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 4 | magnus | 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 | #include <internal.h> |
||
| 14 | |||
| 15 | #include <stdio.h> |
||
| 16 | #include <sys/param.h> |
||
| 17 | #include <dirent.h> |
||
| 18 | #include <errno.h> |
||
| 19 | |||
| 20 | #ifdef STDC_HEADERS |
||
| 21 | # include <string.h> |
||
| 22 | #endif |
||
| 23 | |||
| 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 | |||
| 62 | #ifdef DEBUG |
||
| 63 | printf("==> tar_extract_all(TAR *t, \"%s\")\n", |
||
| 64 | (prefix ? prefix : "(null)")); |
||
| 65 | #endif |
||
| 66 | |||
| 67 | while ((i = th_read(t)) == 0) |
||
| 68 | { |
||
| 69 | #ifdef DEBUG |
||
| 70 | puts(" tar_extract_all(): calling th_get_pathname()"); |
||
| 71 | #endif |
||
| 72 | filename = th_get_pathname(t); |
||
| 73 | if (t->options & TAR_VERBOSE) |
||
| 74 | th_print_long_ls(t); |
||
| 75 | if (prefix != NULL) |
||
| 76 | snprintf(buf, sizeof(buf), "%s/%s", prefix, filename); |
||
| 77 | else |
||
| 78 | strlcpy(buf, filename, sizeof(buf)); |
||
| 79 | free(filename); |
||
| 80 | #ifdef DEBUG |
||
| 81 | printf(" tar_extract_all(): calling tar_extract_file(t, " |
||
| 82 | "\"%s\")\n", buf); |
||
| 83 | #endif |
||
| 84 | if (tar_extract_file(t, buf) != 0) |
||
| 85 | return -1; |
||
| 86 | } |
||
| 87 | |||
| 88 | return (i == 1 ? 0 : -1); |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | int |
||
| 93 | tar_append_tree(TAR *t, char *realdir, char *savedir) |
||
| 94 | { |
||
| 95 | char realpath[MAXPATHLEN]; |
||
| 96 | char savepath[MAXPATHLEN]; |
||
| 97 | struct dirent *dent; |
||
| 98 | DIR *dp; |
||
| 99 | struct stat s; |
||
| 100 | |||
| 101 | #ifdef DEBUG |
||
| 102 | printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n", |
||
| 103 | t, realdir, (savedir ? savedir : "[NULL]")); |
||
| 104 | #endif |
||
| 105 | |||
| 106 | if (tar_append_file(t, realdir, savedir) != 0) |
||
| 107 | return -1; |
||
| 108 | |||
| 109 | #ifdef DEBUG |
||
| 110 | puts(" tar_append_tree(): done with tar_append_file()..."); |
||
| 111 | #endif |
||
| 112 | |||
| 113 | dp = opendir(realdir); |
||
| 114 | if (dp == NULL) |
||
| 115 | { |
||
| 116 | if (errno == ENOTDIR) |
||
| 117 | return 0; |
||
| 118 | return -1; |
||
| 119 | } |
||
| 120 | while ((dent = readdir(dp)) != NULL) |
||
| 121 | { |
||
| 122 | if (strcmp(dent->d_name, ".") == 0 || |
||
| 123 | strcmp(dent->d_name, "..") == 0) |
||
| 124 | continue; |
||
| 125 | |||
| 126 | snprintf(realpath, MAXPATHLEN, "%s/%s", realdir, |
||
| 127 | dent->d_name); |
||
| 128 | if (savedir) |
||
| 129 | snprintf(savepath, MAXPATHLEN, "%s/%s", savedir, |
||
| 130 | dent->d_name); |
||
| 131 | |||
| 132 | if (lstat(realpath, &s) != 0) |
||
| 133 | return -1; |
||
| 134 | |||
| 135 | if (S_ISDIR(s.st_mode)) |
||
| 136 | { |
||
| 137 | if (tar_append_tree(t, realpath, |
||
| 138 | (savedir ? savepath : NULL)) != 0) |
||
| 139 | return -1; |
||
| 140 | continue; |
||
| 141 | } |
||
| 142 | |||
| 143 | if (tar_append_file(t, realpath, |
||
| 144 | (savedir ? savepath : NULL)) != 0) |
||
| 145 | return -1; |
||
| 146 | } |
||
| 147 | |||
| 148 | closedir(dp); |
||
| 149 | |||
| 150 | return 0; |
||
| 151 | } |
||
| 152 | |||
| 153 |