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 |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 68 | LOG("tar_set_file_perms(): setting perms: %s (mode %04o, uid %d, gid %d)\n", |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 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 |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 170 | LOG("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 |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 179 | LOG("tar_extract_file(): restoring posix capabilities to file %s\n", realname); |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 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 |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 196 | LOG("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 |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 220 | LOG(" ==> 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 | |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 236 | LOG(" ==> 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 |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 283 | LOG("### done extracting %s\n", filename); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 356 | LOG(" ==> 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 | |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 389 | LOG(" ==> extracting: %s (symlink to %s)\n", |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 428 | LOG(" ==> extracting: %s (character device %ld,%ld)\n", |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 466 | LOG(" ==> extracting: %s (block device %ld,%ld)\n", |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 499 | LOG(" ==> extracting: %s (mode %04o, directory)\n", filename, |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 534 | LOG("tar_extract_file(): restoring android user.default xattr to %s\n", realname); |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 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 |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 543 | LOG("tar_extract_file(): restoring android user.inode_cache xattr to %s\n", realname); |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 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 |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 550 | LOG("tar_extract_file(): restoring android user.inode_code_cache xattr to %s\n", realname); |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 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 | { |
Mohd Faraz | 1e6061c | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 560 | uint8_t hex_size, size, *descriptor; |
| 561 | hex_size = get_policy_size(t->th_buf.fep, true); |
| 562 | size = get_policy_size(t->th_buf.fep, false); |
| 563 | descriptor = get_policy_descriptor(t->th_buf.fep); |
| 564 | char policy_hex[hex_size]; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 565 | #ifdef DEBUG |
Mohd Faraz | 1e6061c | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 566 | bytes_to_hex(descriptor, size, policy_hex); |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 567 | LOG("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] | 568 | #endif |
bigbiff | cefb0de | 2021-08-15 18:52:04 -0400 | [diff] [blame] | 569 | bool policy_lookup_error = false; |
Mohd Faraz | 1e6061c | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 570 | uint8_t binary_policy[size]; |
| 571 | memset(&binary_policy, 0, size); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 572 | |
Mohd Faraz | 1e6061c | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 573 | if (!lookup_ref_tar(t->th_buf.fep, &binary_policy[0])) { |
| 574 | LOG("error looking up fscrypt policy for '%s' - %s\n", realname, descriptor); |
bigbiff | cefb0de | 2021-08-15 18:52:04 -0400 | [diff] [blame] | 575 | policy_lookup_error = true; |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 576 | } |
Mohd Faraz | 1e6061c | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 577 | memcpy(descriptor, binary_policy, size); |
| 578 | bytes_to_hex(descriptor, size, policy_hex); |
| 579 | if (!policy_lookup_error) |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 580 | { |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 581 | LOG("attempting to restore policy: %s\n", policy_hex); |
bigbiff | cefb0de | 2021-08-15 18:52:04 -0400 | [diff] [blame] | 582 | if (!fscrypt_policy_set_struct(realname, t->th_buf.fep)) |
| 583 | { |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 584 | LOG("tar_extract_file(): failed to restore fscrypt policy to dir '%s' '%s'!!!\n", realname, policy_hex); |
bigbiff | cefb0de | 2021-08-15 18:52:04 -0400 | [diff] [blame] | 585 | //return -1; // This may not be an error in some cases, so log and ignore |
| 586 | } |
| 587 | } else |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 588 | LOG("No policy was found. Continuing restore."); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 589 | } |
| 590 | else |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 591 | LOG("NULL FSCRYPT\n"); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 592 | #endif |
| 593 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | |
| 598 | /* FIFO */ |
| 599 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 600 | tar_extract_fifo(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 601 | { |
| 602 | mode_t mode; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 603 | const char *filename; |
| 604 | char *pn; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 605 | |
| 606 | if (!TH_ISFIFO(t)) |
| 607 | { |
| 608 | errno = EINVAL; |
| 609 | return -1; |
| 610 | } |
| 611 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 612 | pn = th_get_pathname(t); |
| 613 | filename = (realname ? realname : pn); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 614 | mode = th_get_mode(t); |
| 615 | |
| 616 | if (mkdirhier(dirname(filename)) == -1) |
| 617 | return -1; |
| 618 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 619 | |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 620 | LOG(" ==> extracting: %s (fifo)\n", filename); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 621 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 622 | if (mkfifo(filename, mode) == -1) |
| 623 | { |
| 624 | #ifdef DEBUG |
| 625 | perror("mkfifo()"); |
| 626 | #endif |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 633 | /* extract file contents from a tarchive */ |
| 634 | int |
| 635 | tar_extract_file_contents(TAR *t, void *buf, size_t *lenp) |
| 636 | { |
| 637 | char block[T_BLOCKSIZE]; |
| 638 | int64_t size, i; |
| 639 | ssize_t k; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 640 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 641 | #ifdef DEBUG |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 642 | LOG(" ==> tar_extract_file_contents\n"); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 643 | #endif |
| 644 | |
| 645 | if (!TH_ISREG(t)) |
| 646 | { |
| 647 | errno = EINVAL; |
| 648 | return -1; |
| 649 | } |
| 650 | |
| 651 | size = th_get_size(t); |
| 652 | if ((uint64_t)size > *lenp) |
| 653 | { |
| 654 | errno = ENOSPC; |
| 655 | return -1; |
| 656 | } |
| 657 | |
| 658 | /* extract the file */ |
| 659 | for (i = size; i >= T_BLOCKSIZE; i -= T_BLOCKSIZE) |
| 660 | { |
| 661 | k = tar_block_read(t, buf); |
| 662 | if (k != T_BLOCKSIZE) |
| 663 | { |
| 664 | if (k != -1) |
| 665 | errno = EINVAL; |
| 666 | return -1; |
| 667 | } |
| 668 | buf = (char *)buf + T_BLOCKSIZE; |
| 669 | } |
| 670 | if (i > 0) { |
| 671 | k = tar_block_read(t, block); |
| 672 | if (k != T_BLOCKSIZE) |
| 673 | { |
| 674 | if (k != -1) |
| 675 | errno = EINVAL; |
| 676 | return -1; |
| 677 | } |
| 678 | memcpy(buf, block, i); |
| 679 | } |
| 680 | *lenp = (size_t)size; |
| 681 | |
| 682 | #ifdef DEBUG |
Mohd Faraz | 1524764 | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 683 | LOG("### done extracting contents\n"); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 684 | #endif |
| 685 | return 0; |
| 686 | } |