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 | ** encode.c - libtar code to encode tar header blocks |
| 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> |
| 16 | #include <pwd.h> |
| 17 | #include <grp.h> |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #ifdef STDC_HEADERS |
| 21 | # include <string.h> |
| 22 | # include <stdlib.h> |
| 23 | #endif |
| 24 | |
| 25 | |
| 26 | /* magic, version, and checksum */ |
| 27 | void |
| 28 | th_finish(TAR *t) |
| 29 | { |
| 30 | if (t->options & TAR_GNU) |
| 31 | { |
| 32 | /* we're aiming for this result, but must do it in |
| 33 | * two calls to avoid FORTIFY segfaults on some Linux |
| 34 | * systems: |
| 35 | * strncpy(t->th_buf.magic, "ustar ", 8); |
| 36 | */ |
| 37 | strncpy(t->th_buf.magic, "ustar ", 6); |
| 38 | strncpy(t->th_buf.version, " ", 2); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | strncpy(t->th_buf.version, TVERSION, TVERSLEN); |
| 43 | strncpy(t->th_buf.magic, TMAGIC, TMAGLEN); |
| 44 | } |
| 45 | |
| 46 | int_to_oct(th_crc_calc(t), t->th_buf.chksum, 8); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /* map a file mode to a typeflag */ |
| 51 | void |
| 52 | th_set_type(TAR *t, mode_t mode) |
| 53 | { |
| 54 | if (S_ISLNK(mode)) |
| 55 | t->th_buf.typeflag = SYMTYPE; |
| 56 | if (S_ISREG(mode)) |
| 57 | t->th_buf.typeflag = REGTYPE; |
| 58 | if (S_ISDIR(mode)) |
| 59 | t->th_buf.typeflag = DIRTYPE; |
| 60 | if (S_ISCHR(mode)) |
| 61 | t->th_buf.typeflag = CHRTYPE; |
| 62 | if (S_ISBLK(mode)) |
| 63 | t->th_buf.typeflag = BLKTYPE; |
| 64 | if (S_ISFIFO(mode) || S_ISSOCK(mode)) |
| 65 | t->th_buf.typeflag = FIFOTYPE; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /* encode file path */ |
| 70 | void |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 71 | th_set_path(TAR *t, const char *pathname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 72 | { |
| 73 | char suffix[2] = ""; |
| 74 | char *tmp; |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 75 | size_t pathname_len = strlen(pathname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 76 | |
| 77 | #ifdef DEBUG |
| 78 | printf("in th_set_path(th, pathname=\"%s\")\n", pathname); |
| 79 | #endif |
| 80 | |
| 81 | if (t->th_buf.gnu_longname != NULL) |
| 82 | free(t->th_buf.gnu_longname); |
| 83 | t->th_buf.gnu_longname = NULL; |
| 84 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 85 | /* old archive compatibility (not needed for gnu): add trailing / to directories */ |
| 86 | if (pathname[pathname_len - 1] != '/' && TH_ISDIR(t)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 87 | strcpy(suffix, "/"); |
| 88 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 89 | if (pathname_len >= T_NAMELEN && (t->options & TAR_GNU)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 90 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 91 | /* GNU-style long name (no file name length limit) */ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 92 | t->th_buf.gnu_longname = strdup(pathname); |
| 93 | strncpy(t->th_buf.name, t->th_buf.gnu_longname, T_NAMELEN); |
| 94 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 95 | else if (pathname_len >= T_NAMELEN) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 96 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 97 | /* POSIX-style prefix field: |
| 98 | * The maximum length of a file name is limited to 256 characters, |
| 99 | * provided that the file name can be split at a directory separator |
| 100 | * in two parts. The first part being at most 155 bytes long and |
| 101 | * the second part being at most 100 bytes long. So, in most cases |
| 102 | * the maximum file name length will be shorter than 256 characters. |
| 103 | */ |
| 104 | char tail_path[T_NAMELEN + 1]; |
| 105 | tmp = strchr(&(pathname[pathname_len - T_NAMELEN]), '/'); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 106 | if (tmp == NULL) |
| 107 | { |
| 108 | printf("!!! '/' not found in \"%s\"\n", pathname); |
| 109 | return; |
| 110 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 111 | snprintf(tail_path, T_NAMELEN + 1, "%s%s", &tmp[1], suffix); |
| 112 | strncpy(t->th_buf.name, tail_path, T_NAMELEN); |
| 113 | |
| 114 | /* |
| 115 | * first part, max = 155 == sizeof(t->th_buf.prefix) , include NULL if it fits |
| 116 | * trailing '/' is added during decode: decode.c/th_get_pathname() |
| 117 | */ |
| 118 | if (tmp - pathname >= 155) { |
| 119 | strncpy(t->th_buf.prefix, pathname, 155); |
| 120 | } else { |
| 121 | snprintf(t->th_buf.prefix, (tmp - pathname + 1), "%s", pathname); |
| 122 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 123 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 124 | else { |
| 125 | /* any short name for all formats, or classic tar format (99 chars max) */ |
| 126 | snprintf(t->th_buf.name, T_NAMELEN, "%s%s", pathname, suffix); |
| 127 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 128 | |
| 129 | #ifdef DEBUG |
| 130 | puts("returning from th_set_path()..."); |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* encode link path */ |
| 136 | void |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 137 | th_set_link(TAR *t, const char *linkname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 138 | { |
| 139 | #ifdef DEBUG |
| 140 | printf("==> th_set_link(th, linkname=\"%s\")\n", linkname); |
| 141 | #endif |
| 142 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 143 | if (strlen(linkname) >= T_NAMELEN && (t->options & TAR_GNU)) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 144 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 145 | /* --format=gnu: GNU-style long name (no file name length limit) */ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 146 | t->th_buf.gnu_longlink = strdup(linkname); |
| 147 | strcpy(t->th_buf.linkname, "././@LongLink"); |
| 148 | } |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 149 | else if (strlen(linkname) >= T_NAMELEN) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 150 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 151 | /* --format=ustar: 100 chars max limit for symbolic links */ |
| 152 | strncpy(t->th_buf.linkname, linkname, T_NAMELEN); |
| 153 | if (t->th_buf.gnu_longlink != NULL) |
| 154 | free(t->th_buf.gnu_longlink); |
| 155 | t->th_buf.gnu_longlink = NULL; |
| 156 | } else { |
| 157 | /* all short links or v7 tar format: The maximum length of a symbolic link name is limited to 99 characters */ |
| 158 | snprintf(t->th_buf.linkname, T_NAMELEN, "%s", linkname); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 159 | if (t->th_buf.gnu_longlink != NULL) |
| 160 | free(t->th_buf.gnu_longlink); |
| 161 | t->th_buf.gnu_longlink = NULL; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /* encode device info */ |
| 167 | void |
| 168 | th_set_device(TAR *t, dev_t device) |
| 169 | { |
| 170 | #ifdef DEBUG |
| 171 | printf("th_set_device(): major = %d, minor = %d\n", |
| 172 | major(device), minor(device)); |
| 173 | #endif |
| 174 | int_to_oct(major(device), t->th_buf.devmajor, 8); |
| 175 | int_to_oct(minor(device), t->th_buf.devminor, 8); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /* encode user info */ |
| 180 | void |
| 181 | th_set_user(TAR *t, uid_t uid) |
| 182 | { |
| 183 | struct passwd *pw; |
| 184 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 185 | if (!(t->options & TAR_USE_NUMERIC_ID)) { |
| 186 | pw = getpwuid(uid); |
| 187 | if (pw != NULL) |
| 188 | strlcpy(t->th_buf.uname, pw->pw_name, sizeof(t->th_buf.uname)); |
| 189 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 190 | |
| 191 | int_to_oct(uid, t->th_buf.uid, 8); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /* encode group info */ |
| 196 | void |
| 197 | th_set_group(TAR *t, gid_t gid) |
| 198 | { |
| 199 | struct group *gr; |
| 200 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 201 | if (!(t->options & TAR_USE_NUMERIC_ID)) { |
| 202 | gr = getgrgid(gid); |
| 203 | if (gr != NULL) |
| 204 | strlcpy(t->th_buf.gname, gr->gr_name, sizeof(t->th_buf.gname)); |
| 205 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 206 | |
| 207 | int_to_oct(gid, t->th_buf.gid, 8); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /* encode file mode */ |
| 212 | void |
| 213 | th_set_mode(TAR *t, mode_t fmode) |
| 214 | { |
| 215 | if (S_ISSOCK(fmode)) |
| 216 | { |
| 217 | fmode &= ~S_IFSOCK; |
| 218 | fmode |= S_IFIFO; |
| 219 | } |
| 220 | int_to_oct(fmode, (t)->th_buf.mode, 8); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | void |
| 225 | th_set_from_stat(TAR *t, struct stat *s) |
| 226 | { |
| 227 | th_set_type(t, s->st_mode); |
| 228 | if (S_ISCHR(s->st_mode) || S_ISBLK(s->st_mode)) |
| 229 | th_set_device(t, s->st_rdev); |
| 230 | th_set_user(t, s->st_uid); |
| 231 | th_set_group(t, s->st_gid); |
| 232 | th_set_mode(t, s->st_mode); |
| 233 | th_set_mtime(t, s->st_mtime); |
| 234 | if (S_ISREG(s->st_mode)) |
| 235 | th_set_size(t, s->st_size); |
| 236 | else |
| 237 | th_set_size(t, 0); |
| 238 | } |