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 | ** extract.c - libtar code to extract a file from a tar archive |
| 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 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 15 | #include <inttypes.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 16 | #include <stdio.h> |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 17 | #include <string.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 18 | #include <sys/param.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <errno.h> |
| 22 | #include <utime.h> |
| 23 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 24 | #include <sys/capability.h> |
| 25 | #include <sys/xattr.h> |
| 26 | #include <linux/xattr.h> |
| 27 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 28 | #ifdef STDC_HEADERS |
| 29 | # include <stdlib.h> |
| 30 | #endif |
| 31 | |
| 32 | #ifdef HAVE_UNISTD_H |
| 33 | # include <unistd.h> |
| 34 | #endif |
| 35 | |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 36 | #include <selinux/selinux.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 37 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 38 | #ifdef USE_FSCRYPT |
| 39 | #include "fscrypt_policy.h" |
| 40 | #endif |
| 41 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 42 | #ifdef TW_LIBTAR_DEBUG |
| 43 | #define DEBUG 1 |
| 44 | #endif |
| 45 | |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 46 | #include "android_utils.h" |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 47 | |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 48 | const unsigned long long progress_size = (unsigned long long)(T_BLOCKSIZE); |
| 49 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 50 | static int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 51 | tar_set_file_perms(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 52 | { |
| 53 | mode_t mode; |
| 54 | uid_t uid; |
| 55 | gid_t gid; |
| 56 | struct utimbuf ut; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 57 | const char *filename; |
| 58 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 59 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 60 | pn = th_get_pathname(t); |
| 61 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 62 | mode = th_get_mode(t); |
| 63 | uid = th_get_uid(t); |
| 64 | gid = th_get_gid(t); |
| 65 | ut.modtime = ut.actime = th_get_mtime(t); |
| 66 | |
Dees_Troy | 7179659 | 2013-03-14 20:16:29 +0000 | [diff] [blame] | 67 | #ifdef DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 68 | printf("tar_set_file_perms(): setting perms: %s (mode %04o, uid %d, gid %d)\n", |
| 69 | filename, mode, uid, gid); |
Dees_Troy | 7179659 | 2013-03-14 20:16:29 +0000 | [diff] [blame] | 70 | #endif |
| 71 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 72 | /* change owner/group */ |
| 73 | if (geteuid() == 0) |
| 74 | #ifdef HAVE_LCHOWN |
| 75 | if (lchown(filename, uid, gid) == -1) |
| 76 | { |
| 77 | # ifdef DEBUG |
| 78 | fprintf(stderr, "lchown(\"%s\", %d, %d): %s\n", |
| 79 | filename, uid, gid, strerror(errno)); |
| 80 | # endif |
| 81 | #else /* ! HAVE_LCHOWN */ |
| 82 | if (!TH_ISSYM(t) && chown(filename, uid, gid) == -1) |
| 83 | { |
| 84 | # ifdef DEBUG |
| 85 | fprintf(stderr, "chown(\"%s\", %d, %d): %s\n", |
| 86 | filename, uid, gid, strerror(errno)); |
| 87 | # endif |
| 88 | #endif /* HAVE_LCHOWN */ |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | /* change access/modification time */ |
| 93 | if (!TH_ISSYM(t) && utime(filename, &ut) == -1) |
| 94 | { |
| 95 | #ifdef DEBUG |
| 96 | perror("utime()"); |
| 97 | #endif |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | /* change permissions */ |
| 102 | if (!TH_ISSYM(t) && chmod(filename, mode) == -1) |
| 103 | { |
| 104 | #ifdef DEBUG |
| 105 | perror("chmod()"); |
| 106 | #endif |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /* switchboard */ |
| 115 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 116 | tar_extract_file(TAR *t, const char *realname, const char *prefix, const int *progress_fd) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 117 | { |
| 118 | int i; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 119 | #ifdef LIBTAR_FILE_HASH |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 120 | char *lnp; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 121 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 122 | int pathname_len; |
| 123 | int realname_len; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 124 | #endif |
bigbiff bigbiff | 71e5aa4 | 2013-02-26 20:10:16 -0500 | [diff] [blame] | 125 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 126 | if (t->options & TAR_NOOVERWRITE) |
| 127 | { |
| 128 | struct stat s; |
| 129 | |
| 130 | if (lstat(realname, &s) == 0 || errno != ENOENT) |
| 131 | { |
| 132 | errno = EEXIST; |
| 133 | return -1; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (TH_ISDIR(t)) |
| 138 | { |
| 139 | i = tar_extract_dir(t, realname); |
| 140 | if (i == 1) |
| 141 | i = 0; |
| 142 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 143 | else if (TH_ISLNK(t)) |
Dees_Troy | ee6632c | 2013-02-27 18:07:32 +0000 | [diff] [blame] | 144 | i = tar_extract_hardlink(t, realname, prefix); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 145 | else if (TH_ISSYM(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 146 | i = tar_extract_symlink(t, realname); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 147 | else if (TH_ISCHR(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 148 | i = tar_extract_chardev(t, realname); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 149 | else if (TH_ISBLK(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 150 | i = tar_extract_blockdev(t, realname); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 151 | else if (TH_ISFIFO(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 152 | i = tar_extract_fifo(t, realname); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 153 | else /* if (TH_ISREG(t)) */ |
Ethan Yonker | 1b7a31b | 2014-07-03 15:09:22 -0500 | [diff] [blame] | 154 | i = tar_extract_regfile(t, realname, progress_fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 155 | |
| 156 | if (i != 0) { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 157 | fprintf(stderr, "tar_extract_file(): failed to extract %s !!!\n", realname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 158 | return i; |
| 159 | } |
| 160 | |
| 161 | i = tar_set_file_perms(t, realname); |
| 162 | if (i != 0) { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 163 | fprintf(stderr, "tar_extract_file(): failed to set permissions on %s !!!\n", realname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 164 | return i; |
| 165 | } |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 166 | |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 167 | if((t->options & TAR_STORE_SELINUX) && t->th_buf.selinux_context != NULL) |
| 168 | { |
| 169 | #ifdef DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 170 | printf("tar_extract_file(): restoring SELinux context %s to file %s\n", t->th_buf.selinux_context, realname); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 171 | #endif |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 172 | if (lsetfilecon(realname, t->th_buf.selinux_context) < 0) |
| 173 | fprintf(stderr, "tar_extract_file(): failed to restore SELinux context %s to file %s !!!\n", t->th_buf.selinux_context, realname); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 174 | } |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 175 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 176 | if((t->options & TAR_STORE_POSIX_CAP) && t->th_buf.has_cap_data) |
| 177 | { |
| 178 | #if 1 //def DEBUG |
| 179 | printf("tar_extract_file(): restoring posix capabilities to file %s\n", realname); |
| 180 | print_caps(&t->th_buf.cap_data); |
| 181 | #endif |
| 182 | if (setxattr(realname, XATTR_NAME_CAPS, &t->th_buf.cap_data, sizeof(struct vfs_cap_data), 0) < 0) |
| 183 | fprintf(stderr, "tar_extract_file(): failed to restore posix capabilities to file %s !!!\n", realname); |
| 184 | } |
| 185 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 186 | #ifdef LIBTAR_FILE_HASH |
| 187 | pn = th_get_pathname(t); |
| 188 | pathname_len = strlen(pn) + 1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 189 | realname_len = strlen(realname) + 1; |
| 190 | lnp = (char *)calloc(1, pathname_len + realname_len); |
| 191 | if (lnp == NULL) |
| 192 | return -1; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 193 | strcpy(&lnp[0], pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 194 | strcpy(&lnp[pathname_len], realname); |
| 195 | #ifdef DEBUG |
| 196 | printf("tar_extract_file(): calling libtar_hash_add(): key=\"%s\", " |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 197 | "value=\"%s\"\n", pn, realname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 198 | #endif |
| 199 | if (libtar_hash_add(t->h, lnp) != 0) |
| 200 | return -1; |
| 201 | free(lnp); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 202 | #endif |
| 203 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /* extract regular file */ |
| 209 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 210 | tar_extract_regfile(TAR *t, const char *realname, const int *progress_fd) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 211 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 212 | int64_t size, i; |
| 213 | ssize_t k; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 214 | int fdout; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 215 | char buf[T_BLOCKSIZE]; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 216 | const char *filename; |
| 217 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 218 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 219 | #ifdef DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 220 | printf(" ==> tar_extract_regfile(realname=\"%s\")\n", realname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 221 | #endif |
| 222 | |
| 223 | if (!TH_ISREG(t)) |
| 224 | { |
| 225 | errno = EINVAL; |
| 226 | return -1; |
| 227 | } |
| 228 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 229 | pn = th_get_pathname(t); |
| 230 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 231 | size = th_get_size(t); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 232 | |
| 233 | if (mkdirhier(dirname(filename)) == -1) |
| 234 | return -1; |
| 235 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 236 | printf(" ==> extracting: %s (file size %" PRId64 " bytes)\n", |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 237 | filename, size); |
| 238 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 239 | fdout = open(filename, O_WRONLY | O_CREAT | O_TRUNC |
| 240 | #ifdef O_BINARY |
| 241 | | O_BINARY |
| 242 | #endif |
| 243 | , 0666); |
| 244 | if (fdout == -1) |
| 245 | { |
| 246 | #ifdef DEBUG |
| 247 | perror("open()"); |
| 248 | #endif |
| 249 | return -1; |
| 250 | } |
| 251 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 252 | /* extract the file */ |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 253 | for (i = size; i > 0; i -= T_BLOCKSIZE) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 254 | { |
| 255 | k = tar_block_read(t, buf); |
| 256 | if (k != T_BLOCKSIZE) |
| 257 | { |
| 258 | if (k != -1) |
| 259 | errno = EINVAL; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 260 | close(fdout); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | /* write block to output file */ |
| 265 | if (write(fdout, buf, |
| 266 | ((i > T_BLOCKSIZE) ? T_BLOCKSIZE : i)) == -1) |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 267 | { |
| 268 | close(fdout); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 269 | return -1; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 270 | } |
Ethan Yonker | 472f506 | 2016-02-25 13:47:30 -0600 | [diff] [blame] | 271 | else |
| 272 | { |
| 273 | if (*progress_fd != 0) |
| 274 | write(*progress_fd, &progress_size, sizeof(progress_size)); |
| 275 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* close output file */ |
| 279 | if (close(fdout) == -1) |
| 280 | return -1; |
| 281 | |
| 282 | #ifdef DEBUG |
| 283 | printf("### done extracting %s\n", filename); |
| 284 | #endif |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /* skip regfile */ |
| 291 | int |
| 292 | tar_skip_regfile(TAR *t) |
| 293 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 294 | int64_t size, i; |
| 295 | ssize_t k; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 296 | char buf[T_BLOCKSIZE]; |
| 297 | |
| 298 | if (!TH_ISREG(t)) |
| 299 | { |
| 300 | errno = EINVAL; |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | size = th_get_size(t); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 305 | for (i = size; i > 0; i -= T_BLOCKSIZE) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 306 | { |
| 307 | k = tar_block_read(t, buf); |
| 308 | if (k != T_BLOCKSIZE) |
| 309 | { |
| 310 | if (k != -1) |
| 311 | errno = EINVAL; |
| 312 | return -1; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /* hardlink */ |
| 321 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 322 | tar_extract_hardlink(TAR * t, const char *realname, const char *prefix) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 323 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 324 | const char *filename; |
| 325 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 326 | char *linktgt = NULL; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 327 | char *newtgt = NULL; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 328 | char *lnp; |
| 329 | libtar_hashptr_t hp; |
| 330 | |
| 331 | if (!TH_ISLNK(t)) |
| 332 | { |
| 333 | errno = EINVAL; |
| 334 | return -1; |
| 335 | } |
| 336 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 337 | pn = th_get_pathname(t); |
| 338 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 339 | if (mkdirhier(dirname(filename)) == -1) |
| 340 | return -1; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 341 | if (unlink(filename) == -1 && errno != ENOENT) |
| 342 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 343 | libtar_hashptr_reset(&hp); |
| 344 | if (libtar_hash_getkey(t->h, &hp, th_get_linkname(t), |
| 345 | (libtar_matchfunc_t)libtar_str_match) != 0) |
| 346 | { |
| 347 | lnp = (char *)libtar_hashptr_data(&hp); |
| 348 | linktgt = &lnp[strlen(lnp) + 1]; |
| 349 | } |
| 350 | else |
| 351 | linktgt = th_get_linkname(t); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 352 | |
| 353 | newtgt = strdup(linktgt); |
Dees_Troy | ee6632c | 2013-02-27 18:07:32 +0000 | [diff] [blame] | 354 | sprintf(linktgt, "%s/%s", prefix, newtgt); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 355 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 356 | printf(" ==> extracting: %s (link to %s)\n", filename, linktgt); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 357 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 358 | if (link(linktgt, filename) == -1) |
| 359 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 360 | fprintf(stderr, "tar_extract_hardlink(): failed restore of hardlink '%s' but returning as if nothing bad happened\n", filename); |
Dees_Troy | f96fb97 | 2013-03-01 22:34:25 +0000 | [diff] [blame] | 361 | return 0; // Used to be -1 |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /* symlink */ |
| 369 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 370 | tar_extract_symlink(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 371 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 372 | const char *filename; |
| 373 | char *pn; |
bigbiff bigbiff | 71e5aa4 | 2013-02-26 20:10:16 -0500 | [diff] [blame] | 374 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 375 | if (!TH_ISSYM(t)) |
| 376 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 377 | errno = EINVAL; |
| 378 | return -1; |
| 379 | } |
| 380 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 381 | pn = th_get_pathname(t); |
| 382 | filename = (realname ? realname : pn); |
| 383 | if (mkdirhier(dirname(filename)) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 384 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 385 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 386 | if (unlink(filename) == -1 && errno != ENOENT) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 387 | return -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 388 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 389 | printf(" ==> extracting: %s (symlink to %s)\n", |
| 390 | filename, th_get_linkname(t)); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 391 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 392 | if (symlink(th_get_linkname(t), filename) == -1) |
| 393 | { |
| 394 | #ifdef DEBUG |
| 395 | perror("symlink()"); |
| 396 | #endif |
| 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /* character device */ |
| 405 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 406 | tar_extract_chardev(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 407 | { |
| 408 | mode_t mode; |
| 409 | unsigned long devmaj, devmin; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 410 | const char *filename; |
| 411 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 412 | |
| 413 | if (!TH_ISCHR(t)) |
| 414 | { |
| 415 | errno = EINVAL; |
| 416 | return -1; |
| 417 | } |
| 418 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 419 | pn = th_get_pathname(t); |
| 420 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 421 | mode = th_get_mode(t); |
| 422 | devmaj = th_get_devmajor(t); |
| 423 | devmin = th_get_devminor(t); |
| 424 | |
| 425 | if (mkdirhier(dirname(filename)) == -1) |
| 426 | return -1; |
| 427 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 428 | printf(" ==> extracting: %s (character device %ld,%ld)\n", |
| 429 | filename, devmaj, devmin); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 430 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 431 | if (mknod(filename, mode | S_IFCHR, |
| 432 | compat_makedev(devmaj, devmin)) == -1) |
| 433 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 434 | fprintf(stderr, "tar_extract_chardev(): failed restore of character device '%s' but returning as if nothing bad happened\n", filename); |
| 435 | return 0; // Used to be -1 |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | /* block device */ |
| 443 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 444 | tar_extract_blockdev(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 445 | { |
| 446 | mode_t mode; |
| 447 | unsigned long devmaj, devmin; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 448 | const char *filename; |
| 449 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 450 | |
| 451 | if (!TH_ISBLK(t)) |
| 452 | { |
| 453 | errno = EINVAL; |
| 454 | return -1; |
| 455 | } |
| 456 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 457 | pn = th_get_pathname(t); |
| 458 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 459 | mode = th_get_mode(t); |
| 460 | devmaj = th_get_devmajor(t); |
| 461 | devmin = th_get_devminor(t); |
| 462 | |
| 463 | if (mkdirhier(dirname(filename)) == -1) |
| 464 | return -1; |
| 465 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 466 | printf(" ==> extracting: %s (block device %ld,%ld)\n", |
| 467 | filename, devmaj, devmin); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 468 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 469 | if (mknod(filename, mode | S_IFBLK, |
| 470 | compat_makedev(devmaj, devmin)) == -1) |
| 471 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 472 | fprintf(stderr, "tar_extract_blockdev(): failed restore of block device '%s' but returning as if nothing bad happened\n", filename); |
| 473 | return 0; // Used to be -1 |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 479 | /* directory */ |
| 480 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 481 | tar_extract_dir(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 482 | { |
| 483 | mode_t mode; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 484 | const char *filename; |
| 485 | char *pn; |
| 486 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 487 | if (!TH_ISDIR(t)) |
| 488 | { |
| 489 | errno = EINVAL; |
| 490 | return -1; |
| 491 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 492 | pn = th_get_pathname(t); |
| 493 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 494 | mode = th_get_mode(t); |
| 495 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 496 | if (mkdirhier(dirname(filename)) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 497 | return -1; |
| 498 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 499 | printf(" ==> extracting: %s (mode %04o, directory)\n", filename, |
| 500 | mode); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 501 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 502 | if (mkdir(filename, mode) == -1) |
| 503 | { |
| 504 | if (errno == EEXIST) |
| 505 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 506 | if (chmod(filename, mode) == -1) |
| 507 | { |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 508 | #ifdef DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 509 | perror("chmod()"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 510 | #endif |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 511 | return -1; |
| 512 | } |
| 513 | else |
| 514 | { |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 515 | #if 1 //def DEBUG |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 516 | puts(" *** using existing directory"); |
| 517 | #endif |
| 518 | return 1; |
| 519 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 520 | } |
| 521 | else |
| 522 | { |
| 523 | #ifdef DEBUG |
| 524 | perror("mkdir()"); |
| 525 | #endif |
| 526 | return -1; |
| 527 | } |
| 528 | } |
| 529 | |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 530 | if (t->options & TAR_STORE_ANDROID_USER_XATTR) |
| 531 | { |
| 532 | if (t->th_buf.has_user_default) { |
| 533 | #if 1 //def DEBUG |
| 534 | printf("tar_extract_file(): restoring android user.default xattr to %s\n", realname); |
| 535 | #endif |
| 536 | if (setxattr(realname, "user.default", NULL, 0, 0) < 0) { |
| 537 | fprintf(stderr, "tar_extract_file(): failed to restore android user.default to file %s !!!\n", realname); |
| 538 | return -1; |
| 539 | } |
| 540 | } |
| 541 | if (t->th_buf.has_user_cache) { |
| 542 | #if 1 //def DEBUG |
| 543 | printf("tar_extract_file(): restoring android user.inode_cache xattr to %s\n", realname); |
| 544 | #endif |
| 545 | if (write_path_inode(realname, "cache", "user.inode_cache")) |
| 546 | return -1; |
| 547 | } |
| 548 | if (t->th_buf.has_user_code_cache) { |
| 549 | #if 1 //def DEBUG |
| 550 | printf("tar_extract_file(): restoring android user.inode_code_cache xattr to %s\n", realname); |
| 551 | #endif |
| 552 | if (write_path_inode(realname, "code_cache", "user.inode_code_cache")) |
| 553 | return -1; |
| 554 | } |
| 555 | } |
| 556 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 557 | #ifdef USE_FSCRYPT |
| 558 | if(t->th_buf.fep != NULL) |
| 559 | { |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame^] | 560 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 561 | char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX]; |
| 562 | #else |
| 563 | char policy_hex[FSCRYPT_KEY_IDENTIFIER_HEX_SIZE]; |
| 564 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 565 | #ifdef DEBUG |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame^] | 566 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 567 | bytes_to_hex(t->th_buf.fep->master_key_descriptor, FS_KEY_DESCRIPTOR_SIZE, policy_hex); |
| 568 | #else |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 569 | bytes_to_hex(t->th_buf.fep->master_key_identifier, FSCRYPT_KEY_IDENTIFIER_SIZE, policy_hex); |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame^] | 570 | #endif |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 571 | printf("tar_extract_dir(): restoring fscrypt policy %s to dir %s\n", (char *)policy_hex, realname); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 572 | #endif |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame^] | 573 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 574 | uint8_t binary_policy[FS_KEY_DESCRIPTOR_SIZE]; |
| 575 | memset(&binary_policy, 0, FS_KEY_DESCRIPTOR_SIZE); |
| 576 | #else |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 577 | uint8_t binary_policy[FSCRYPT_KEY_IDENTIFIER_SIZE]; |
| 578 | memset(&binary_policy, 0, FSCRYPT_KEY_IDENTIFIER_SIZE); |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame^] | 579 | #endif |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 580 | |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame^] | 581 | #ifdef USE_FSCRYPT_POLICY_V1 |
| 582 | if (!lookup_ref_tar(t->th_buf.fep->master_key_descriptor, &binary_policy[0])) { |
| 583 | printf("error looking up fscrypt policy for '%s' - %s\n", realname, t->th_buf.fep->master_key_descriptor); |
| 584 | return -1; |
| 585 | } |
| 586 | memcpy(&t->th_buf.fep->master_key_descriptor, binary_policy, FS_KEY_DESCRIPTOR_SIZE); |
| 587 | bytes_to_hex(t->th_buf.fep->master_key_descriptor, FS_KEY_DESCRIPTOR_SIZE, policy_hex); |
| 588 | #else |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 589 | if (!lookup_ref_tar(t->th_buf.fep->master_key_identifier, &binary_policy[0])) { |
| 590 | printf("error looking up fscrypt policy for '%s' - %s\n", realname, t->th_buf.fep->master_key_identifier); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 591 | return -1; |
| 592 | } |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 593 | memcpy(&t->th_buf.fep->master_key_identifier, binary_policy, FSCRYPT_KEY_IDENTIFIER_SIZE); |
| 594 | bytes_to_hex(t->th_buf.fep->master_key_identifier, FSCRYPT_KEY_IDENTIFIER_SIZE, policy_hex); |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame^] | 595 | #endif |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 596 | printf("attempting to restore policy: %s\n", policy_hex); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 597 | if (!fscrypt_policy_set_struct(realname, t->th_buf.fep)) |
| 598 | { |
| 599 | printf("tar_extract_file(): failed to restore fscrypt policy to dir '%s' '%s'!!!\n", realname, policy_hex); |
| 600 | //return -1; // This may not be an error in some cases, so log and ignore |
| 601 | } |
| 602 | } |
| 603 | else |
| 604 | printf("NULL FSCRYPT\n"); |
| 605 | #endif |
| 606 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | |
| 611 | /* FIFO */ |
| 612 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 613 | tar_extract_fifo(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 614 | { |
| 615 | mode_t mode; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 616 | const char *filename; |
| 617 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 618 | |
| 619 | if (!TH_ISFIFO(t)) |
| 620 | { |
| 621 | errno = EINVAL; |
| 622 | return -1; |
| 623 | } |
| 624 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 625 | pn = th_get_pathname(t); |
| 626 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 627 | mode = th_get_mode(t); |
| 628 | |
| 629 | if (mkdirhier(dirname(filename)) == -1) |
| 630 | return -1; |
| 631 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 632 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 633 | printf(" ==> extracting: %s (fifo)\n", filename); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 634 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 635 | if (mkfifo(filename, mode) == -1) |
| 636 | { |
| 637 | #ifdef DEBUG |
| 638 | perror("mkfifo()"); |
| 639 | #endif |
| 640 | return -1; |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 646 | /* extract file contents from a tarchive */ |
| 647 | int |
| 648 | tar_extract_file_contents(TAR *t, void *buf, size_t *lenp) |
| 649 | { |
| 650 | char block[T_BLOCKSIZE]; |
| 651 | int64_t size, i; |
| 652 | ssize_t k; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 653 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 654 | #ifdef DEBUG |
| 655 | printf(" ==> tar_extract_file_contents\n"); |
| 656 | #endif |
| 657 | |
| 658 | if (!TH_ISREG(t)) |
| 659 | { |
| 660 | errno = EINVAL; |
| 661 | return -1; |
| 662 | } |
| 663 | |
| 664 | size = th_get_size(t); |
| 665 | if ((uint64_t)size > *lenp) |
| 666 | { |
| 667 | errno = ENOSPC; |
| 668 | return -1; |
| 669 | } |
| 670 | |
| 671 | /* extract the file */ |
| 672 | for (i = size; i >= T_BLOCKSIZE; i -= T_BLOCKSIZE) |
| 673 | { |
| 674 | k = tar_block_read(t, buf); |
| 675 | if (k != T_BLOCKSIZE) |
| 676 | { |
| 677 | if (k != -1) |
| 678 | errno = EINVAL; |
| 679 | return -1; |
| 680 | } |
| 681 | buf = (char *)buf + T_BLOCKSIZE; |
| 682 | } |
| 683 | if (i > 0) { |
| 684 | k = tar_block_read(t, block); |
| 685 | if (k != T_BLOCKSIZE) |
| 686 | { |
| 687 | if (k != -1) |
| 688 | errno = EINVAL; |
| 689 | return -1; |
| 690 | } |
| 691 | memcpy(buf, block, i); |
| 692 | } |
| 693 | *lenp = (size_t)size; |
| 694 | |
| 695 | #ifdef DEBUG |
| 696 | printf("### done extracting contents\n"); |
| 697 | #endif |
| 698 | return 0; |
| 699 | } |