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 | ** util.c - miscellaneous utility code for libtar |
| 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 <sys/param.h> |
| 17 | #include <errno.h> |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 18 | #include <linux/capability.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 19 | |
| 20 | #ifdef STDC_HEADERS |
| 21 | # include <string.h> |
| 22 | #endif |
| 23 | |
| 24 | |
| 25 | /* hashing function for pathnames */ |
| 26 | int |
| 27 | path_hashfunc(char *key, int numbuckets) |
| 28 | { |
| 29 | char buf[MAXPATHLEN]; |
| 30 | char *p; |
| 31 | |
| 32 | strcpy(buf, key); |
| 33 | p = basename(buf); |
| 34 | |
| 35 | return (((unsigned int)p[0]) % numbuckets); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | /* matching function for dev_t's */ |
| 40 | int |
| 41 | dev_match(dev_t *dev1, dev_t *dev2) |
| 42 | { |
| 43 | return !memcmp(dev1, dev2, sizeof(dev_t)); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /* matching function for ino_t's */ |
| 48 | int |
| 49 | ino_match(ino_t *ino1, ino_t *ino2) |
| 50 | { |
| 51 | return !memcmp(ino1, ino2, sizeof(ino_t)); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /* hashing function for dev_t's */ |
| 56 | int |
| 57 | dev_hash(dev_t *dev) |
| 58 | { |
| 59 | return *dev % 16; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /* hashing function for ino_t's */ |
| 64 | int |
| 65 | ino_hash(ino_t *inode) |
| 66 | { |
| 67 | return *inode % 256; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | ** mkdirhier() - create all directories in a given path |
| 73 | ** returns: |
| 74 | ** 0 success |
| 75 | ** 1 all directories already exist |
| 76 | ** -1 (and sets errno) error |
| 77 | */ |
| 78 | int |
| 79 | mkdirhier(char *path) |
| 80 | { |
| 81 | char src[MAXPATHLEN], dst[MAXPATHLEN] = ""; |
| 82 | char *dirp, *nextp = src; |
| 83 | int retval = 1; |
| 84 | |
| 85 | if (strlcpy(src, path, sizeof(src)) > sizeof(src)) |
| 86 | { |
| 87 | errno = ENAMETOOLONG; |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | if (path[0] == '/') |
| 92 | strcpy(dst, "/"); |
| 93 | |
| 94 | while ((dirp = strsep(&nextp, "/")) != NULL) |
| 95 | { |
| 96 | if (*dirp == '\0') |
| 97 | continue; |
| 98 | |
| 99 | if (dst[0] != '\0') |
| 100 | strcat(dst, "/"); |
| 101 | strcat(dst, dirp); |
| 102 | |
| 103 | if (mkdir(dst, 0777) == -1) |
| 104 | { |
| 105 | if (errno != EEXIST) |
| 106 | return -1; |
| 107 | } |
| 108 | else |
| 109 | retval = 0; |
| 110 | } |
| 111 | |
| 112 | return retval; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* calculate header checksum */ |
| 117 | int |
| 118 | th_crc_calc(TAR *t) |
| 119 | { |
| 120 | int i, sum = 0; |
| 121 | |
| 122 | for (i = 0; i < T_BLOCKSIZE; i++) |
| 123 | sum += ((unsigned char *)(&(t->th_buf)))[i]; |
| 124 | for (i = 0; i < 8; i++) |
| 125 | sum += (' ' - (unsigned char)t->th_buf.chksum[i]); |
| 126 | |
| 127 | return sum; |
| 128 | } |
| 129 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 130 | /* calculate a signed header checksum */ |
| 131 | int |
| 132 | th_signed_crc_calc(TAR *t) |
| 133 | { |
| 134 | int i, sum = 0; |
| 135 | |
| 136 | for (i = 0; i < T_BLOCKSIZE; i++) |
| 137 | sum += ((signed char *)(&(t->th_buf)))[i]; |
| 138 | for (i = 0; i < 8; i++) |
| 139 | sum += (' ' - (signed char)t->th_buf.chksum[i]); |
| 140 | |
| 141 | return sum; |
| 142 | } |
| 143 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 144 | /* string-octal to integer conversion */ |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 145 | int64_t |
| 146 | oct_to_int(char *oct, size_t octlen) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 147 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 148 | long long int val; |
| 149 | char tmp[octlen + 1]; |
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 | memcpy(tmp, oct, octlen); |
| 152 | tmp[octlen] = '\0'; |
| 153 | return sscanf(oct, "%llo", &val) == 1 ? (int64_t)val : 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 157 | /* string-octal or binary to integer conversion */ |
| 158 | int64_t oct_to_int_ex(char *oct, size_t octlen) |
| 159 | { |
| 160 | if (*(unsigned char *)oct & 0x80) { |
| 161 | int64_t val = 0; |
| 162 | char tmp[octlen]; |
| 163 | unsigned char *p; |
| 164 | unsigned int i; |
| 165 | |
| 166 | memcpy(tmp, oct, octlen); |
| 167 | *tmp &= 0x7f; |
| 168 | p = (unsigned char *)tmp + octlen - sizeof(val); |
| 169 | for (i = 0; i < sizeof(val); ++i) { |
| 170 | val <<= 8; |
| 171 | val |= *(p++); |
| 172 | } |
| 173 | return val; |
| 174 | } |
| 175 | return oct_to_int(oct, octlen); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /* integer to NULL-terminated string-octal conversion */ |
| 180 | void int_to_oct(int64_t num, char *oct, size_t octlen) |
| 181 | { |
| 182 | char tmp[sizeof(num)*3 + 1]; |
| 183 | int olen; |
| 184 | |
| 185 | olen = sprintf(tmp, "%0*llo", (int)octlen, (long long)num); |
| 186 | memcpy(oct, tmp + olen - octlen + 1, octlen); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /* integer to string-octal conversion, or binary as necessary */ |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 191 | void |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 192 | int_to_oct_ex(int64_t num, char *oct, size_t octlen) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 193 | { |
James Christopher Adduono | 6f57f7c | 2016-03-01 16:01:53 -0500 | [diff] [blame] | 194 | if (num < 0 || num >= ((int64_t)1 << ((octlen - 1) * 3))) { |
| 195 | unsigned char *p; |
| 196 | unsigned int i; |
| 197 | |
| 198 | memset(oct, 0, octlen); |
| 199 | p = (unsigned char *)oct + octlen; |
| 200 | for (i = 0; i < sizeof(num); ++i) { |
| 201 | *(--p) = num & 0xff; |
| 202 | num >>= 8; |
| 203 | } |
| 204 | if (num < 0) { |
| 205 | for (; i < octlen; ++i) { |
| 206 | *(--p) = 0xff; |
| 207 | } |
| 208 | } |
| 209 | *(unsigned char *)oct |= 0x80; |
| 210 | return; |
| 211 | } |
| 212 | int_to_oct(num, oct, octlen); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 213 | } |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 214 | |
| 215 | void print_caps(struct vfs_cap_data *cap_data) { |
Mohd Faraz | 566ef6b | 2023-01-16 16:16:21 +0100 | [diff] [blame] | 216 | LOG(" magic_etc=%u \n", cap_data->magic_etc); |
| 217 | LOG(" data[0].permitted=%u \n", cap_data->data[0].permitted); |
| 218 | LOG(" data[0].inheritable=%u \n", cap_data->data[0].inheritable); |
| 219 | LOG(" data[1].permitted=%u \n", cap_data->data[1].permitted); |
| 220 | LOG(" data[1].inheritable=%u \n", cap_data->data[1].inheritable); |
Ethan Yonker | 7118774 | 2017-01-13 13:30:10 -0600 | [diff] [blame] | 221 | } |