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 | ** append.c - libtar code to append files to 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 | |
| 15 | #include <stdio.h> |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 20 | #include <time.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 21 | #include <sys/param.h> |
| 22 | #include <sys/types.h> |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 23 | #include <stdbool.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 24 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 25 | #include <sys/capability.h> |
| 26 | #include <sys/xattr.h> |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 27 | #include <linux/fs.h> |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 28 | #include <linux/xattr.h> |
| 29 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 30 | #ifdef STDC_HEADERS |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | #ifdef HAVE_UNISTD_H |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 36 | #include <unistd.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 37 | #endif |
| 38 | |
Matt Mower | 8741364 | 2017-01-17 21:14:46 -0600 | [diff] [blame] | 39 | #include <selinux/selinux.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 40 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 41 | #ifdef USE_FSCRYPT |
| 42 | #include "fscrypt_policy.h" |
| 43 | #endif |
| 44 | |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 45 | #include "android_utils.h" |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 46 | |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 47 | #ifdef TW_LIBTAR_DEBUG |
| 48 | #define DEBUG 1 |
| 49 | #endif |
| 50 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 51 | struct tar_dev |
| 52 | { |
| 53 | dev_t td_dev; |
| 54 | libtar_hash_t *td_h; |
| 55 | }; |
| 56 | typedef struct tar_dev tar_dev_t; |
| 57 | |
| 58 | struct tar_ino |
| 59 | { |
| 60 | ino_t ti_ino; |
| 61 | char ti_name[MAXPATHLEN]; |
| 62 | }; |
| 63 | typedef struct tar_ino tar_ino_t; |
| 64 | |
| 65 | |
| 66 | /* free memory associated with a tar_dev_t */ |
| 67 | void |
| 68 | tar_dev_free(tar_dev_t *tdp) |
| 69 | { |
| 70 | libtar_hash_free(tdp->td_h, free); |
| 71 | free(tdp); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /* appends a file to the tar archive */ |
| 76 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 77 | tar_append_file(TAR *t, const char *realname, const char *savename) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 78 | { |
| 79 | struct stat s; |
| 80 | int i; |
| 81 | libtar_hashptr_t hp; |
| 82 | tar_dev_t *td = NULL; |
| 83 | tar_ino_t *ti = NULL; |
| 84 | char path[MAXPATHLEN]; |
| 85 | |
| 86 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 87 | LOG("==> tar_append_file(TAR=0x%p (\"%s\"), realname=\"%s\", " |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 88 | "savename=\"%s\")\n", (void*) t, t->pathname, realname, |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 89 | (savename ? savename : "[NULL]")); |
| 90 | #endif |
| 91 | |
| 92 | if (lstat(realname, &s) != 0) |
| 93 | { |
| 94 | #ifdef DEBUG |
| 95 | perror("lstat()"); |
| 96 | #endif |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | /* set header block */ |
| 101 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 102 | LOG("tar_append_file(): setting header block..."); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 103 | #endif |
| 104 | memset(&(t->th_buf), 0, sizeof(struct tar_header)); |
| 105 | th_set_from_stat(t, &s); |
| 106 | |
| 107 | /* set the header path */ |
| 108 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 109 | LOG("tar_append_file(): setting header path..."); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 110 | #endif |
| 111 | th_set_path(t, (savename ? savename : realname)); |
| 112 | |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 113 | /* get selinux context */ |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 114 | if (t->options & TAR_STORE_SELINUX) |
| 115 | { |
| 116 | if (t->th_buf.selinux_context != NULL) |
| 117 | { |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 118 | free(t->th_buf.selinux_context); |
| 119 | t->th_buf.selinux_context = NULL; |
| 120 | } |
| 121 | |
| 122 | security_context_t selinux_context = NULL; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 123 | if (lgetfilecon(realname, &selinux_context) >= 0) |
| 124 | { |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 125 | t->th_buf.selinux_context = strdup(selinux_context); |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 126 | LOG(" ==> set selinux context: %s\n", selinux_context); |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 127 | freecon(selinux_context); |
| 128 | } |
| 129 | else |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 130 | { |
| 131 | #ifdef DEBUG |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 132 | perror("Failed to get selinux context"); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 133 | #endif |
| 134 | } |
Vojtech Bocek | 25fd68d | 2013-08-27 03:10:10 +0200 | [diff] [blame] | 135 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 136 | |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 137 | #ifdef USE_FSCRYPT |
| 138 | if (TH_ISDIR(t) && t->options & TAR_STORE_FSCRYPT_POL) |
| 139 | { |
| 140 | if (t->th_buf.fep != NULL) |
| 141 | { |
| 142 | free(t->th_buf.fep); |
| 143 | t->th_buf.fep = NULL; |
| 144 | } |
Mohd Faraz | 97b8499 | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 145 | t->th_buf.fep = (fscrypt_policy *)malloc(sizeof(fscrypt_policy)); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 146 | if (!t->th_buf.fep) { |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 147 | LOG("malloc fs_encryption_policy\n"); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | if (fscrypt_policy_get_struct(realname, t->th_buf.fep)) { |
Mohd Faraz | 97b8499 | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 152 | uint8_t size, hex_size, *descriptor; |
| 153 | size = get_policy_size(t->th_buf.fep, false); |
| 154 | hex_size = get_policy_size(t->th_buf.fep, true); |
| 155 | descriptor = get_policy_descriptor(t->th_buf.fep); |
| 156 | char user_ce[4], user_de[4], system_de[4]; |
| 157 | sprintf(user_ce,"%u%s", t->th_buf.fep->version, USER_CE_FSCRYPT_POLICY); |
| 158 | sprintf(user_de,"%u%s", t->th_buf.fep->version, USER_DE_FSCRYPT_POLICY); |
| 159 | sprintf(system_de,"%u%s", t->th_buf.fep->version, SYSTEM_DE_FSCRYPT_POLICY); |
| 160 | #ifdef DEBUG |
| 161 | LOG("version: %u\n", t->th_buf.fep->version); |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 162 | #endif |
Mohd Faraz | 97b8499 | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 163 | uint8_t tar_policy[size]; |
| 164 | char policy_hex[hex_size]; |
bigbiff | 2e344ab | 2021-05-07 10:41:55 -0400 | [diff] [blame] | 165 | memset(tar_policy, 0, sizeof(tar_policy)); |
Mohd Faraz | 97b8499 | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 166 | bytes_to_hex(descriptor, size, policy_hex); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 167 | if (lookup_ref_key(t->th_buf.fep, &tar_policy[0])) { |
Mohd Faraz | 97b8499 | 2023-01-07 21:26:56 +0100 | [diff] [blame] | 168 | if (strncmp((char *) tar_policy, user_ce, sizeof(user_ce) - 1) == 0 |
| 169 | || strncmp((char *) tar_policy, user_de, sizeof(user_de) - 1) == 0 |
| 170 | || strncmp((char *) tar_policy, system_de, sizeof(system_de)) == 0) { |
| 171 | memcpy(descriptor, tar_policy, size); |
| 172 | LOG("found fscrypt policy '%s' - '%s' - '%s'\n", realname, descriptor, policy_hex); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 173 | } else { |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 174 | LOG("failed to match fscrypt tar policy for '%s' - '%s'\n", realname, policy_hex); |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 175 | free(t->th_buf.fep); |
| 176 | t->th_buf.fep = NULL; |
| 177 | } |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 178 | } else { |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 179 | LOG("failed to lookup fscrypt tar policy for '%s' - '%s'\n", realname, policy_hex); |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 180 | free(t->th_buf.fep); |
| 181 | t->th_buf.fep = NULL; |
bigbiff | 7ba7500 | 2020-04-11 20:47:09 -0400 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | else { |
| 185 | // no policy found, but this is not an error as not all dirs will have a policy |
| 186 | free(t->th_buf.fep); |
| 187 | t->th_buf.fep = NULL; |
| 188 | } |
| 189 | } |
| 190 | #endif |
Ethan Yonker | 79f88bd | 2016-12-09 14:52:12 -0600 | [diff] [blame] | 191 | |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 192 | /* get posix file capabilities */ |
| 193 | if (TH_ISREG(t) && t->options & TAR_STORE_POSIX_CAP) |
| 194 | { |
| 195 | if (t->th_buf.has_cap_data) |
| 196 | { |
| 197 | memset(&t->th_buf.cap_data, 0, sizeof(struct vfs_cap_data)); |
| 198 | t->th_buf.has_cap_data = 0; |
| 199 | } |
| 200 | |
| 201 | if (getxattr(realname, XATTR_NAME_CAPS, &t->th_buf.cap_data, sizeof(struct vfs_cap_data)) >= 0) |
| 202 | { |
| 203 | t->th_buf.has_cap_data = 1; |
| 204 | #if 1 //def DEBUG |
| 205 | print_caps(&t->th_buf.cap_data); |
| 206 | #endif |
| 207 | } |
| 208 | } |
| 209 | |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 210 | /* get android user.default xattr */ |
| 211 | if (TH_ISDIR(t) && t->options & TAR_STORE_ANDROID_USER_XATTR) |
| 212 | { |
| 213 | if (getxattr(realname, "user.default", NULL, 0) >= 0) |
| 214 | { |
| 215 | t->th_buf.has_user_default = 1; |
| 216 | #if 1 //def DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 217 | LOG("storing xattr user.default\n"); |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 218 | #endif |
| 219 | } |
| 220 | if (getxattr(realname, "user.inode_cache", NULL, 0) >= 0) |
| 221 | { |
| 222 | t->th_buf.has_user_cache = 1; |
| 223 | #if 1 //def DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 224 | LOG("storing xattr user.inode_cache\n"); |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 225 | #endif |
| 226 | } |
| 227 | if (getxattr(realname, "user.inode_code_cache", NULL, 0) >= 0) |
| 228 | { |
| 229 | t->th_buf.has_user_code_cache = 1; |
| 230 | #if 1 //def DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 231 | LOG("storing xattr user.inode_code_cache\n"); |
Ethan Yonker | 8d039f7 | 2017-02-03 14:26:15 -0600 | [diff] [blame] | 232 | #endif |
| 233 | } |
| 234 | } |
| 235 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 236 | /* check if it's a hardlink */ |
| 237 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 238 | LOG("tar_append_file(): checking inode cache for hardlink..."); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 239 | #endif |
| 240 | libtar_hashptr_reset(&hp); |
| 241 | if (libtar_hash_getkey(t->h, &hp, &(s.st_dev), |
| 242 | (libtar_matchfunc_t)dev_match) != 0) |
| 243 | td = (tar_dev_t *)libtar_hashptr_data(&hp); |
| 244 | else |
| 245 | { |
| 246 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 247 | LOG("+++ adding hash for device (0x%x, 0x%x)...\n", |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 248 | major(s.st_dev), minor(s.st_dev)); |
| 249 | #endif |
| 250 | td = (tar_dev_t *)calloc(1, sizeof(tar_dev_t)); |
| 251 | td->td_dev = s.st_dev; |
| 252 | td->td_h = libtar_hash_new(256, (libtar_hashfunc_t)ino_hash); |
| 253 | if (td->td_h == NULL) |
| 254 | return -1; |
| 255 | if (libtar_hash_add(t->h, td) == -1) |
| 256 | return -1; |
| 257 | } |
| 258 | libtar_hashptr_reset(&hp); |
| 259 | if (libtar_hash_getkey(td->td_h, &hp, &(s.st_ino), |
| 260 | (libtar_matchfunc_t)ino_match) != 0) |
| 261 | { |
| 262 | ti = (tar_ino_t *)libtar_hashptr_data(&hp); |
| 263 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 264 | LOG(" tar_append_file(): encoding hard link \"%s\" " |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 265 | "to \"%s\"...\n", realname, ti->ti_name); |
| 266 | #endif |
| 267 | t->th_buf.typeflag = LNKTYPE; |
| 268 | th_set_link(t, ti->ti_name); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 273 | LOG("+++ adding entry: device (0x%d,0x%x), inode %lu" |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 274 | "(\"%s\")...\n", major(s.st_dev), minor(s.st_dev), |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 275 | (unsigned long) s.st_ino, realname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 276 | #endif |
| 277 | ti = (tar_ino_t *)calloc(1, sizeof(tar_ino_t)); |
| 278 | if (ti == NULL) |
| 279 | return -1; |
| 280 | ti->ti_ino = s.st_ino; |
| 281 | snprintf(ti->ti_name, sizeof(ti->ti_name), "%s", |
| 282 | savename ? savename : realname); |
| 283 | libtar_hash_add(td->td_h, ti); |
| 284 | } |
| 285 | |
| 286 | /* check if it's a symlink */ |
| 287 | if (TH_ISSYM(t)) |
| 288 | { |
| 289 | i = readlink(realname, path, sizeof(path)); |
| 290 | if (i == -1) |
| 291 | return -1; |
| 292 | if (i >= MAXPATHLEN) |
| 293 | i = MAXPATHLEN - 1; |
| 294 | path[i] = '\0'; |
| 295 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 296 | LOG("tar_append_file(): encoding symlink \"%s\" -> " |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 297 | "\"%s\"...\n", realname, path); |
| 298 | #endif |
| 299 | th_set_link(t, path); |
| 300 | } |
| 301 | |
| 302 | /* print file info */ |
| 303 | if (t->options & TAR_VERBOSE) |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 304 | LOG("%s\n", th_get_pathname(t)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 305 | |
| 306 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 307 | LOG("tar_append_file(): writing header"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 308 | #endif |
| 309 | /* write header */ |
| 310 | if (th_write(t) != 0) |
| 311 | { |
| 312 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 313 | LOG("t->fd = %ld\n", t->fd); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 314 | #endif |
| 315 | return -1; |
| 316 | } |
| 317 | #ifdef DEBUG |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 318 | LOG("tar_append_file(): back from th_write()"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 319 | #endif |
| 320 | |
| 321 | /* if it's a regular file, write the contents as well */ |
| 322 | if (TH_ISREG(t) && tar_append_regfile(t, realname) != 0) |
| 323 | return -1; |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | /* write EOF indicator */ |
| 330 | int |
| 331 | tar_append_eof(TAR *t) |
| 332 | { |
| 333 | int i, j; |
| 334 | char block[T_BLOCKSIZE]; |
| 335 | |
| 336 | memset(&block, 0, T_BLOCKSIZE); |
| 337 | for (j = 0; j < 2; j++) |
| 338 | { |
| 339 | i = tar_block_write(t, &block); |
| 340 | if (i != T_BLOCKSIZE) |
| 341 | { |
| 342 | if (i != -1) |
| 343 | errno = EINVAL; |
| 344 | return -1; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | /* add file contents to a tarchive */ |
| 353 | int |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 354 | tar_append_regfile(TAR *t, const char *realname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 355 | { |
| 356 | char block[T_BLOCKSIZE]; |
| 357 | int filefd; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 358 | int64_t i, size; |
| 359 | ssize_t j; |
| 360 | int rv = -1; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 361 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 362 | #if defined(O_BINARY) |
| 363 | filefd = open(realname, O_RDONLY|O_BINARY); |
| 364 | #else |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 365 | filefd = open(realname, O_RDONLY); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 366 | #endif |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 367 | if (filefd == -1) |
| 368 | { |
| 369 | #ifdef DEBUG |
| 370 | perror("open()"); |
| 371 | #endif |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | size = th_get_size(t); |
| 376 | for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE) |
| 377 | { |
| 378 | j = read(filefd, &block, T_BLOCKSIZE); |
| 379 | if (j != T_BLOCKSIZE) |
| 380 | { |
| 381 | if (j != -1) |
| 382 | errno = EINVAL; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 383 | goto fail; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 384 | } |
| 385 | if (tar_block_write(t, &block) == -1) |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 386 | goto fail; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (i > 0) |
| 390 | { |
| 391 | j = read(filefd, &block, i); |
| 392 | if (j == -1) |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 393 | goto fail; |
| 394 | memset(&(block[i]), 0, T_BLOCKSIZE - i); |
| 395 | if (tar_block_write(t, &block) == -1) |
| 396 | goto fail; |
| 397 | } |
| 398 | |
| 399 | /* success! */ |
| 400 | rv = 0; |
| 401 | fail: |
| 402 | close(filefd); |
| 403 | |
| 404 | return rv; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /* add file contents to a tarchive */ |
| 409 | int |
| 410 | tar_append_file_contents(TAR *t, const char *savename, mode_t mode, |
| 411 | uid_t uid, gid_t gid, void *buf, size_t len) |
| 412 | { |
| 413 | struct stat st; |
| 414 | |
| 415 | memset(&st, 0, sizeof(st)); |
| 416 | st.st_mode = S_IFREG | mode; |
| 417 | st.st_uid = uid; |
| 418 | st.st_gid = gid; |
| 419 | st.st_mtime = time(NULL); |
| 420 | st.st_size = len; |
| 421 | |
| 422 | th_set_from_stat(t, &st); |
| 423 | th_set_path(t, savename); |
| 424 | |
| 425 | /* write header */ |
| 426 | if (th_write(t) != 0) |
| 427 | { |
| 428 | #ifdef DEBUG |
bigbiff | a957f07 | 2021-03-07 18:20:29 -0500 | [diff] [blame] | 429 | fprintf(stderr, "tar_append_file_contents(): could not write header, t->fd = %ld\n", t->fd); |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 430 | #endif |
| 431 | return -1; |
| 432 | } |
| 433 | |
| 434 | return tar_append_buffer(t, buf, len); |
| 435 | } |
| 436 | |
| 437 | int |
| 438 | tar_append_buffer(TAR *t, void *buf, size_t len) |
| 439 | { |
| 440 | char block[T_BLOCKSIZE]; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 441 | int i; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 442 | size_t size = len; |
| 443 | |
| 444 | for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE) |
| 445 | { |
| 446 | if (tar_block_write(t, buf) == -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 447 | return -1; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 448 | buf = (char *)buf + T_BLOCKSIZE; |
| 449 | } |
| 450 | |
| 451 | if (i > 0) |
| 452 | { |
| 453 | memcpy(block, buf, i); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 454 | memset(&(block[i]), 0, T_BLOCKSIZE - i); |
| 455 | if (tar_block_write(t, &block) == -1) |
| 456 | return -1; |
| 457 | } |
| 458 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |