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 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 13 | #include <internal.h> |
| 14 | |
| 15 | #include <stdio.h> |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 16 | #include <stdlib.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 17 | #include <sys/param.h> |
| 18 | #include <dirent.h> |
| 19 | #include <errno.h> |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 20 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 21 | #ifdef STDC_HEADERS |
| 22 | # include <string.h> |
| 23 | #endif |
| 24 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 25 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 26 | int |
| 27 | tar_extract_glob(TAR *t, char *globname, char *prefix) |
| 28 | { |
| 29 | char *filename; |
| 30 | char buf[MAXPATHLEN]; |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 31 | int i, fd = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 32 | |
| 33 | while ((i = th_read(t)) == 0) |
| 34 | { |
| 35 | filename = th_get_pathname(t); |
| 36 | if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD)) |
| 37 | { |
| 38 | if (TH_ISREG(t) && tar_skip_regfile(t)) |
| 39 | return -1; |
| 40 | continue; |
| 41 | } |
| 42 | if (t->options & TAR_VERBOSE) |
| 43 | th_print_long_ls(t); |
| 44 | if (prefix != NULL) |
| 45 | snprintf(buf, sizeof(buf), "%s/%s", prefix, filename); |
| 46 | else |
| 47 | strlcpy(buf, filename, sizeof(buf)); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 48 | if (tar_extract_file(t, buf, prefix, &fd) != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | return (i == 1 ? 0 : -1); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | int |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 57 | tar_extract_all(TAR *t, char *prefix, const int *progress_fd) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 58 | { |
| 59 | char *filename; |
| 60 | char buf[MAXPATHLEN]; |
| 61 | int i; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 62 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 63 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 64 | LOG("==> tar_extract_all(TAR *t, \"%s\")\n", |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 65 | (prefix ? prefix : "(null)")); |
| 66 | #endif |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 67 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 68 | while ((i = th_read(t)) == 0) |
| 69 | { |
| 70 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 71 | LOG(" tar_extract_all(): calling th_get_pathname()"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 72 | #endif |
| 73 | filename = th_get_pathname(t); |
| 74 | if (t->options & TAR_VERBOSE) |
| 75 | th_print_long_ls(t); |
| 76 | if (prefix != NULL) |
| 77 | snprintf(buf, sizeof(buf), "%s/%s", prefix, filename); |
| 78 | else |
| 79 | strlcpy(buf, filename, sizeof(buf)); |
| 80 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 81 | LOG(" tar_extract_all(): calling tar_extract_file(t, " |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 82 | "\"%s\")\n", buf); |
| 83 | #endif |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 84 | if (tar_extract_file(t, buf, prefix, progress_fd) != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 85 | return -1; |
| 86 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 87 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 88 | return (i == 1 ? 0 : -1); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 93 | tar_append_tree(TAR *t, char *realdir, char *savedir) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 94 | { |
| 95 | char realpath[MAXPATHLEN]; |
| 96 | char savepath[MAXPATHLEN]; |
| 97 | struct dirent *dent; |
| 98 | DIR *dp; |
| 99 | struct stat s; |
| 100 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 101 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 102 | LOG("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n", |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 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 |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 110 | LOG(" tar_append_tree(): done with tar_append_file()..."); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 111 | #endif |
| 112 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 113 | dp = opendir(realdir); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 114 | if (dp == NULL) |
| 115 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 116 | if (errno == ENOTDIR) |
| 117 | return 0; |
| 118 | return -1; |
| 119 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 120 | while ((dent = readdir(dp)) != NULL) |
| 121 | { |
| 122 | if (strcmp(dent->d_name, ".") == 0 || |
| 123 | strcmp(dent->d_name, "..") == 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 124 | continue; |
| 125 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 126 | snprintf(realpath, MAXPATHLEN, "%s/%s", realdir, |
| 127 | dent->d_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 128 | if (savedir) |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 129 | snprintf(savepath, MAXPATHLEN, "%s/%s", savedir, |
| 130 | dent->d_name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 131 | |
| 132 | if (lstat(realpath, &s) != 0) |
| 133 | return -1; |
| 134 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 135 | if (S_ISDIR(s.st_mode)) |
| 136 | { |
| 137 | if (tar_append_tree(t, realpath, |
| 138 | (savedir ? savepath : NULL)) != 0) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 139 | return -1; |
| 140 | continue; |
| 141 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 142 | |
| 143 | if (tar_append_file(t, realpath, |
| 144 | (savedir ? savepath : NULL)) != 0) |
| 145 | return -1; |
bigbiff bigbiff | 86e77bc | 2013-08-26 21:36:23 -0400 | [diff] [blame] | 146 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 147 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 148 | closedir(dp); |
| 149 | |
| 150 | return 0; |
| 151 | } |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 152 | |
| 153 | |
| 154 | int |
| 155 | tar_find(TAR *t, char *searchstr) |
| 156 | { |
| 157 | if (!searchstr) |
| 158 | return 0; |
| 159 | |
| 160 | char *filename; |
| 161 | int i, entryfound = 0; |
| 162 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 163 | LOG("==> tar_find(0x%lx, %s)\n", (long unsigned int)t, searchstr); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 164 | #endif |
| 165 | while ((i = th_read(t)) == 0) { |
| 166 | filename = th_get_pathname(t); |
| 167 | if (fnmatch(searchstr, filename, FNM_FILE_NAME | FNM_PERIOD) == 0) { |
| 168 | entryfound++; |
| 169 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 170 | LOG("Found matching entry: %s\n", filename); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 171 | #endif |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | #ifdef DEBUG |
| 176 | if (!entryfound) |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 177 | LOG("No matching entry found.\n"); |
n0d3 | 3b51163 | 2013-03-06 21:14:15 +0200 | [diff] [blame] | 178 | #endif |
| 179 | |
| 180 | return entryfound; |
| 181 | } |