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 | ** decode.c - libtar code to decode 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> |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 16 | #include <stdlib.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 17 | #include <sys/param.h> |
| 18 | #include <pwd.h> |
| 19 | #include <grp.h> |
| 20 | |
| 21 | #ifdef STDC_HEADERS |
| 22 | # include <string.h> |
| 23 | #endif |
| 24 | |
| 25 | |
| 26 | /* determine full path name */ |
| 27 | char * |
| 28 | th_get_pathname(TAR *t) |
| 29 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 30 | if (t->th_buf.gnu_longname) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 31 | return t->th_buf.gnu_longname; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 32 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 33 | /* allocate the th_pathname buffer if not already */ |
| 34 | if (t->th_pathname == NULL) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 35 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 36 | t->th_pathname = malloc(MAXPATHLEN * sizeof(char)); |
| 37 | if (t->th_pathname == NULL) |
| 38 | /* out of memory */ |
| 39 | return NULL; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 40 | } |
| 41 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 42 | if (t->th_buf.prefix[0] == '\0') |
| 43 | { |
| 44 | snprintf(t->th_pathname, MAXPATHLEN, "%.100s", t->th_buf.name); |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | snprintf(t->th_pathname, MAXPATHLEN, "%.155s/%.100s", |
| 49 | t->th_buf.prefix, t->th_buf.name); |
| 50 | } |
| 51 | |
| 52 | /* will be deallocated in tar_close() */ |
| 53 | return t->th_pathname; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | uid_t |
| 58 | th_get_uid(TAR *t) |
| 59 | { |
| 60 | int uid; |
| 61 | struct passwd *pw; |
| 62 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 63 | if (!(t->options & TAR_USE_NUMERIC_ID)) { |
| 64 | pw = getpwnam(t->th_buf.uname); |
| 65 | if (pw != NULL) |
| 66 | return pw->pw_uid; |
| 67 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 68 | |
| 69 | /* if the password entry doesn't exist */ |
| 70 | sscanf(t->th_buf.uid, "%o", &uid); |
| 71 | return uid; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | gid_t |
| 76 | th_get_gid(TAR *t) |
| 77 | { |
| 78 | int gid; |
| 79 | struct group *gr; |
| 80 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 81 | if (!(t->options & TAR_USE_NUMERIC_ID)) { |
| 82 | gr = getgrnam(t->th_buf.gname); |
| 83 | if (gr != NULL) |
| 84 | return gr->gr_gid; |
| 85 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 86 | |
| 87 | /* if the group entry doesn't exist */ |
| 88 | sscanf(t->th_buf.gid, "%o", &gid); |
| 89 | return gid; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | mode_t |
| 94 | th_get_mode(TAR *t) |
| 95 | { |
| 96 | mode_t mode; |
| 97 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 98 | mode = (mode_t)oct_to_int(t->th_buf.mode, sizeof(t->th_buf.mode)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 99 | if (! (mode & S_IFMT)) |
| 100 | { |
| 101 | switch (t->th_buf.typeflag) |
| 102 | { |
| 103 | case SYMTYPE: |
| 104 | mode |= S_IFLNK; |
| 105 | break; |
| 106 | case CHRTYPE: |
| 107 | mode |= S_IFCHR; |
| 108 | break; |
| 109 | case BLKTYPE: |
| 110 | mode |= S_IFBLK; |
| 111 | break; |
| 112 | case DIRTYPE: |
| 113 | mode |= S_IFDIR; |
| 114 | break; |
| 115 | case FIFOTYPE: |
| 116 | mode |= S_IFIFO; |
| 117 | break; |
| 118 | case AREGTYPE: |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 119 | if (t->th_buf.name[strnlen(t->th_buf.name, T_NAMELEN) - 1] == '/') |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 120 | { |
| 121 | mode |= S_IFDIR; |
| 122 | break; |
| 123 | } |
| 124 | /* FALLTHROUGH */ |
| 125 | case LNKTYPE: |
| 126 | case REGTYPE: |
| 127 | default: |
| 128 | mode |= S_IFREG; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return mode; |
| 133 | } |
| 134 | |
| 135 | |