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 | |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 38 | #ifdef HAVE_EXT4_CRYPT |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 39 | #include "ext4crypt_tar.h" |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 40 | #endif |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 41 | |
| 42 | #ifdef USE_FSCRYPT |
| 43 | #include "fscrypt_policy.h" |
| 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 | |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 557 | #ifdef HAVE_EXT4_CRYPT |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 558 | if(t->th_buf.eep != NULL) |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 559 | { |
| 560 | #ifdef DEBUG |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 561 | printf("tar_extract_file(): restoring EXT4 crypt policy %s to dir %s\n", t->th_buf.eep->master_key_descriptor, realname); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 562 | #endif |
| 563 | char binary_policy[EXT4_KEY_DESCRIPTOR_SIZE]; |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 564 | if (!lookup_ref_tar(t->th_buf.eep->master_key_descriptor, &binary_policy[0])) { |
| 565 | printf("error looking up proper e4crypt policy for '%s' - %s\n", realname, t->th_buf.eep->master_key_descriptor); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 566 | return -1; |
| 567 | } |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 568 | char policy_hex[EXT4_KEY_DESCRIPTOR_SIZE_HEX]; |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 569 | policy_to_hex(binary_policy, policy_hex); |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 570 | printf("restoring policy %s > '%s' to '%s'\n", t->th_buf.eep->master_key_descriptor, policy_hex, realname); |
| 571 | memcpy(&t->th_buf.eep->master_key_descriptor, binary_policy, EXT4_KEY_DESCRIPTOR_SIZE); |
| 572 | if (!e4crypt_policy_set_struct(realname, t->th_buf.eep)) |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 573 | { |
Ethan Yonker | fefe591 | 2017-09-30 22:22:13 -0500 | [diff] [blame] | 574 | printf("tar_extract_file(): failed to restore EXT4 crypt policy to dir '%s' '%s'!!!\n", realname, policy_hex); |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 575 | //return -1; // This may not be an error in some cases, so log and ignore |
| 576 | } |
| 577 | } |
| 578 | #endif |
| 579 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 580 | #ifdef USE_FSCRYPT |
| 581 | if(t->th_buf.fep != NULL) |
| 582 | { |
| 583 | #ifdef DEBUG |
| 584 | printf("tar_extract_file(): restoring fscrypt policy %s to dir %s\n", t->th_buf.fep->master_key_descriptor, realname); |
| 585 | #endif |
| 586 | uint8_t binary_policy[FS_KEY_DESCRIPTOR_SIZE]; |
| 587 | if (!lookup_ref_tar(t->th_buf.fep->master_key_descriptor, &binary_policy[0])) { |
| 588 | printf("error looking up proper fscrypt policy for '%s' - %s\n", realname, t->th_buf.fep->master_key_descriptor); |
| 589 | return -1; |
| 590 | } |
| 591 | char policy_hex[FS_KEY_DESCRIPTOR_SIZE_HEX]; |
| 592 | policy_to_hex(binary_policy, policy_hex); |
| 593 | printf("restoring policy %s > '%s' to '%s'\n", t->th_buf.fep->master_key_descriptor, policy_hex, realname); |
| 594 | memcpy(&t->th_buf.fep->master_key_descriptor, binary_policy, FS_KEY_DESCRIPTOR_SIZE); |
| 595 | if (!fscrypt_policy_set_struct(realname, t->th_buf.fep)) |
| 596 | { |
| 597 | printf("tar_extract_file(): failed to restore fscrypt policy to dir '%s' '%s'!!!\n", realname, policy_hex); |
| 598 | //return -1; // This may not be an error in some cases, so log and ignore |
| 599 | } |
| 600 | } |
| 601 | else |
| 602 | printf("NULL FSCRYPT\n"); |
| 603 | #endif |
| 604 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | |
| 609 | /* FIFO */ |
| 610 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 611 | tar_extract_fifo(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 612 | { |
| 613 | mode_t mode; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 614 | const char *filename; |
| 615 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 616 | |
| 617 | if (!TH_ISFIFO(t)) |
| 618 | { |
| 619 | errno = EINVAL; |
| 620 | return -1; |
| 621 | } |
| 622 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 623 | pn = th_get_pathname(t); |
| 624 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 625 | mode = th_get_mode(t); |
| 626 | |
| 627 | if (mkdirhier(dirname(filename)) == -1) |
| 628 | return -1; |
| 629 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 630 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 631 | printf(" ==> extracting: %s (fifo)\n", filename); |
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 | if (mkfifo(filename, mode) == -1) |
| 634 | { |
| 635 | #ifdef DEBUG |
| 636 | perror("mkfifo()"); |
| 637 | #endif |
| 638 | return -1; |
| 639 | } |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 644 | /* extract file contents from a tarchive */ |
| 645 | int |
| 646 | tar_extract_file_contents(TAR *t, void *buf, size_t *lenp) |
| 647 | { |
| 648 | char block[T_BLOCKSIZE]; |
| 649 | int64_t size, i; |
| 650 | ssize_t k; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 651 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 652 | #ifdef DEBUG |
| 653 | printf(" ==> tar_extract_file_contents\n"); |
| 654 | #endif |
| 655 | |
| 656 | if (!TH_ISREG(t)) |
| 657 | { |
| 658 | errno = EINVAL; |
| 659 | return -1; |
| 660 | } |
| 661 | |
| 662 | size = th_get_size(t); |
| 663 | if ((uint64_t)size > *lenp) |
| 664 | { |
| 665 | errno = ENOSPC; |
| 666 | return -1; |
| 667 | } |
| 668 | |
| 669 | /* extract the file */ |
| 670 | for (i = size; i >= T_BLOCKSIZE; i -= T_BLOCKSIZE) |
| 671 | { |
| 672 | k = tar_block_read(t, buf); |
| 673 | if (k != T_BLOCKSIZE) |
| 674 | { |
| 675 | if (k != -1) |
| 676 | errno = EINVAL; |
| 677 | return -1; |
| 678 | } |
| 679 | buf = (char *)buf + T_BLOCKSIZE; |
| 680 | } |
| 681 | if (i > 0) { |
| 682 | k = tar_block_read(t, block); |
| 683 | if (k != T_BLOCKSIZE) |
| 684 | { |
| 685 | if (k != -1) |
| 686 | errno = EINVAL; |
| 687 | return -1; |
| 688 | } |
| 689 | memcpy(buf, block, i); |
| 690 | } |
| 691 | *lenp = (size_t)size; |
| 692 | |
| 693 | #ifdef DEBUG |
| 694 | printf("### done extracting contents\n"); |
| 695 | #endif |
| 696 | return 0; |
| 697 | } |