| 1 |
|
| /* |
| 2 |
|
| * arch_io.c - registry + byte source/sink implementations. |
| 3 |
|
| * |
| 4 |
|
| * Mirrors the same shape File::Raw uses for file_plugin.h: a small |
| 5 |
|
| * static array of registered plugins, lookup by name, probe-based |
| 6 |
|
| * auto-detection. Sister dists call archive_register_plugin from BOOT. |
| 7 |
|
| * |
| 8 |
|
| * Byte sources/sinks: fd, membuf, gzip streaming inflater/deflater. |
| 9 |
|
| * The gzip wrappers talk to libz directly; when File::Raw::Gzip 0.02 |
| 10 |
|
| * exposes its streaming state machine in gz.h these wrappers can be |
| 11 |
|
| * simplified to a pull-through. |
| 12 |
|
| */ |
| 13 |
|
| |
| 14 |
|
| #define PERL_NO_GET_CONTEXT |
| 15 |
|
| #include "EXTERN.h" |
| 16 |
|
| #include "perl.h" |
| 17 |
|
| #include "XSUB.h" |
| 18 |
|
| |
| 19 |
|
| #include "archive_plugin.h" |
| 20 |
|
| #include "arch_io.h" |
| 21 |
|
| |
| 22 |
|
| #include <errno.h> |
| 23 |
|
| #include <fcntl.h> |
| 24 |
|
| #include <stdlib.h> |
| 25 |
|
| #include <string.h> |
| 26 |
|
| #include <unistd.h> |
| 27 |
|
| #include <zlib.h> |
| 28 |
|
| |
| 29 |
|
| #ifdef __linux__ |
| 30 |
|
| # include <sys/xattr.h> |
| 31 |
|
| # define ARCHIVE_HAVE_XATTR 1 |
| 32 |
|
| #endif |
| 33 |
|
| #ifdef __APPLE__ |
| 34 |
|
| # include <sys/xattr.h> |
| 35 |
|
| # define ARCHIVE_HAVE_XATTR 1 |
| 36 |
|
| # define ARCHIVE_DARWIN_XATTR 1 |
| 37 |
|
| #endif |
| 38 |
|
| #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 39 |
|
| # include <sys/types.h> |
| 40 |
|
| # include <sys/extattr.h> |
| 41 |
|
| # define ARCHIVE_HAVE_EXTATTR 1 |
| 42 |
|
| #endif |
| 43 |
|
| |
| 44 |
|
| /* ============================================================ |
| 45 |
|
| * Registry |
| 46 |
|
| * ============================================================ */ |
| 47 |
|
| |
| 48 |
|
| #define MAX_ARCHIVE_PLUGINS 16 |
| 49 |
|
| static const ArchivePlugin *g_plugins[MAX_ARCHIVE_PLUGINS]; |
| 50 |
|
| static int g_plugin_count = 0; |
| 51 |
|
| |
| 52 |
|
| int |
| 53 |
29 |
| archive_register_plugin(pTHX_ const ArchivePlugin *plugin) |
| 54 |
|
| { |
| 55 |
29 |
▶ | if (!plugin || !plugin->name || !*plugin->name) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!plugin || !plugin->name || !*plugin->name) return -1; |
29 |
0 |
29 |
| if (!plugin || !plugin->name || !*plugin->name) return -1; |
29 |
0 |
29 |
| if (!plugin || !plugin->name || !*plugin->name) return -1; |
0 |
29 |
29 |
|
| 56 |
29 |
▶ | if (g_plugin_count >= MAX_ARCHIVE_PLUGINS) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (g_plugin_count >= MAX_ARCHIVE_PLUGINS) return -1; |
0 |
29 |
29 |
|
| 57 |
|
| int i; |
| 58 |
29 |
▶ | for (i = 0; i < g_plugin_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < g_plugin_count; i++) { |
0 |
29 |
29 |
|
| 59 |
0 |
▶ | if (strcmp(g_plugins[i]->name, plugin->name) == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(g_plugins[i]->name, plugin->name) == 0) return 0; |
0 |
0 |
0 |
|
| 60 |
|
| } |
| 61 |
29 |
| g_plugins[g_plugin_count++] = plugin; |
| 62 |
29 |
| return 1; |
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
|
| int |
| 66 |
0 |
| archive_unregister_plugin(pTHX_ const char *name) |
| 67 |
|
| { |
| 68 |
|
| int i, j; |
| 69 |
0 |
▶ | if (!name) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!name) return 0; |
0 |
0 |
0 |
|
| 70 |
0 |
▶ | for (i = 0; i < g_plugin_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < g_plugin_count; i++) { |
0 |
0 |
0 |
|
| 71 |
0 |
▶ | if (strcmp(g_plugins[i]->name, name) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(g_plugins[i]->name, name) == 0) { |
0 |
0 |
0 |
|
| 72 |
0 |
▶ | for (j = i + 1; j < g_plugin_count; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = i + 1; j < g_plugin_count; j++) { |
0 |
0 |
0 |
|
| 73 |
0 |
| g_plugins[j - 1] = g_plugins[j]; |
| 74 |
|
| } |
| 75 |
0 |
| g_plugin_count--; |
| 76 |
0 |
| return 1; |
| 77 |
|
| } |
| 78 |
|
| } |
| 79 |
0 |
| return 0; |
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
|
| const ArchivePlugin * |
| 83 |
111 |
| archive_lookup_plugin(pTHX_ const char *name) |
| 84 |
|
| { |
| 85 |
|
| int i; |
| 86 |
111 |
▶ | if (!name) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!name) return NULL; |
0 |
111 |
111 |
|
| 87 |
112 |
▶ | for (i = 0; i < g_plugin_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < g_plugin_count; i++) { |
111 |
1 |
112 |
|
| 88 |
111 |
▶ | if (strcmp(g_plugins[i]->name, name) == 0) return g_plugins[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(g_plugins[i]->name, name) == 0) return g_plugins[i]; |
110 |
1 |
111 |
|
| 89 |
|
| } |
| 90 |
1 |
| return NULL; |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| const ArchivePlugin * |
| 94 |
0 |
| archive_probe_for(pTHX_ const char *bytes, size_t len) |
| 95 |
|
| { |
| 96 |
0 |
| int i, best_score = 0; |
| 97 |
0 |
| const ArchivePlugin *best = NULL; |
| 98 |
0 |
▶ | for (i = 0; i < g_plugin_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < g_plugin_count; i++) { |
0 |
0 |
0 |
|
| 99 |
0 |
▶ | if (!g_plugins[i]->probe) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g_plugins[i]->probe) continue; |
0 |
0 |
0 |
|
| 100 |
0 |
| int score = g_plugins[i]->probe(bytes, len); |
| 101 |
0 |
▶ | if (score > best_score) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (score > best_score) { |
0 |
0 |
0 |
|
| 102 |
0 |
| best_score = score; |
| 103 |
0 |
| best = g_plugins[i]; |
| 104 |
|
| } |
| 105 |
|
| } |
| 106 |
0 |
| return best; |
| 107 |
|
| } |
| 108 |
|
| |
| 109 |
|
| /* ============================================================ |
| 110 |
|
| * fd source / sink |
| 111 |
|
| * ============================================================ */ |
| 112 |
|
| |
| 113 |
|
| int |
| 114 |
2768 |
| archive_pull_fd(void *state, char *buf, size_t len) |
| 115 |
|
| { |
| 116 |
2768 |
| archive_fd_state_t *s = (archive_fd_state_t *)state; |
| 117 |
2768 |
| ssize_t total = 0; |
| 118 |
5535 |
▶ | while ((size_t)total < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((size_t)total < len) { |
2769 |
2766 |
5535 |
|
| 119 |
2769 |
| ssize_t n = read(s->fd, buf + total, len - total); |
| 120 |
2769 |
▶ | if (n < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) { |
0 |
2769 |
2769 |
|
| 121 |
0 |
▶ | if (errno == EINTR) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == EINTR) continue; |
0 |
0 |
0 |
|
| 122 |
0 |
| return -1; |
| 123 |
|
| } |
| 124 |
2769 |
▶ | if (n == 0) break; /* EOF */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) break; /* EOF */ |
2 |
2767 |
2769 |
|
| 125 |
2767 |
| total += n; |
| 126 |
|
| } |
| 127 |
2768 |
| return (int)total; |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
|
| int |
| 131 |
2151 |
| archive_push_fd(void *state, const char *buf, size_t len) |
| 132 |
|
| { |
| 133 |
2151 |
| archive_fd_state_t *s = (archive_fd_state_t *)state; |
| 134 |
2151 |
| size_t total = 0; |
| 135 |
4302 |
▶ | while (total < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (total < len) { |
2151 |
2151 |
4302 |
|
| 136 |
2151 |
| ssize_t n = write(s->fd, buf + total, len - total); |
| 137 |
2151 |
▶ | if (n < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) { |
0 |
2151 |
2151 |
|
| 138 |
0 |
▶ | if (errno == EINTR) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == EINTR) continue; |
0 |
0 |
0 |
|
| 139 |
0 |
| return -1; |
| 140 |
|
| } |
| 141 |
2151 |
| total += n; |
| 142 |
|
| } |
| 143 |
2151 |
| return (int)total; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
|
| /* ============================================================ |
| 147 |
|
| * membuf source / sink |
| 148 |
|
| * ============================================================ */ |
| 149 |
|
| |
| 150 |
|
| int |
| 151 |
0 |
| archive_pull_membuf(void *state, char *buf, size_t len) |
| 152 |
|
| { |
| 153 |
0 |
| archive_membuf_src_t *s = (archive_membuf_src_t *)state; |
| 154 |
0 |
▶ | size_t avail = (s->pos < s->len) ? (s->len - s->pos) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t avail = (s->pos < s->len) ? (s->len - s->pos) : 0; |
0 |
0 |
0 |
|
| 155 |
0 |
| size_t take = (avail < len) ? avail : len; |
| 156 |
0 |
▶ | if (take) memcpy(buf, s->buf + s->pos, take); |
Branch: 1 branch
| Branch | True | False | Total |
| if (take) memcpy(buf, s->buf + s->pos, take); |
0 |
0 |
0 |
|
| 157 |
0 |
| s->pos += take; |
| 158 |
0 |
| return (int)take; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
|
| void |
| 162 |
0 |
| archive_membuf_sink_init(archive_membuf_sink_t *s) |
| 163 |
|
| { |
| 164 |
0 |
| s->buf = NULL; |
| 165 |
0 |
| s->cap = 0; |
| 166 |
0 |
| s->len = 0; |
| 167 |
0 |
| } |
| 168 |
|
| |
| 169 |
|
| void |
| 170 |
0 |
| archive_membuf_sink_free(archive_membuf_sink_t *s) |
| 171 |
|
| { |
| 172 |
0 |
▶ | if (s->buf) free(s->buf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->buf) free(s->buf); |
0 |
0 |
0 |
|
| 173 |
0 |
| s->buf = NULL; |
| 174 |
0 |
| s->cap = 0; |
| 175 |
0 |
| s->len = 0; |
| 176 |
0 |
| } |
| 177 |
|
| |
| 178 |
|
| int |
| 179 |
0 |
| archive_push_membuf(void *state, const char *buf, size_t len) |
| 180 |
|
| { |
| 181 |
0 |
| archive_membuf_sink_t *s = (archive_membuf_sink_t *)state; |
| 182 |
0 |
▶ | if (s->len + len > s->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->len + len > s->cap) { |
0 |
0 |
0 |
|
| 183 |
0 |
▶ | size_t want = s->cap ? s->cap * 2 : 4096; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t want = s->cap ? s->cap * 2 : 4096; |
0 |
0 |
0 |
|
| 184 |
0 |
▶ | while (want < s->len + len) want *= 2; |
Branch: 1 branch
| Branch | True | False | Total |
| while (want < s->len + len) want *= 2; |
0 |
0 |
0 |
|
| 185 |
0 |
| char *p = (char *)realloc(s->buf, want); |
| 186 |
0 |
▶ | if (!p) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return -1; |
0 |
0 |
0 |
|
| 187 |
0 |
| s->buf = p; |
| 188 |
0 |
| s->cap = want; |
| 189 |
|
| } |
| 190 |
0 |
| memcpy(s->buf + s->len, buf, len); |
| 191 |
0 |
| s->len += len; |
| 192 |
0 |
| return (int)len; |
| 193 |
|
| } |
| 194 |
|
| |
| 195 |
|
| /* ============================================================ |
| 196 |
|
| * gzip streaming source |
| 197 |
|
| * ============================================================ */ |
| 198 |
|
| |
| 199 |
|
| struct archive_gz_src { |
| 200 |
|
| int fd; |
| 201 |
|
| z_stream zs; |
| 202 |
|
| int zs_inited; |
| 203 |
|
| int stream_end; |
| 204 |
|
| char *raw; |
| 205 |
|
| size_t raw_cap; |
| 206 |
|
| int eof; |
| 207 |
|
| }; |
| 208 |
|
| |
| 209 |
|
| archive_gz_src_t * |
| 210 |
8 |
| archive_gz_src_new(int fd, size_t chunk_size) |
| 211 |
|
| { |
| 212 |
8 |
| archive_gz_src_t *s = (archive_gz_src_t *)calloc(1, sizeof *s); |
| 213 |
8 |
▶ | if (!s) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return NULL; |
0 |
8 |
8 |
|
| 214 |
8 |
| s->fd = fd; |
| 215 |
8 |
▶ | s->raw_cap = chunk_size ? chunk_size : ARCHIVE_DEFAULT_CHUNK; |
Branch: 1 branch
| Branch | True | False | Total |
| s->raw_cap = chunk_size ? chunk_size : ARCHIVE_DEFAULT_CHUNK; |
8 |
0 |
8 |
|
| 216 |
8 |
| s->raw = (char *)malloc(s->raw_cap); |
| 217 |
8 |
▶ | if (!s->raw) { free(s); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->raw) { free(s); return NULL; } |
0 |
8 |
8 |
|
| 218 |
8 |
▶ | if (inflateInit2(&s->zs, MAX_WBITS | 32) != Z_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (inflateInit2(&s->zs, MAX_WBITS | 32) != Z_OK) { |
0 |
8 |
8 |
|
| 219 |
0 |
| free(s->raw); |
| 220 |
0 |
| free(s); |
| 221 |
0 |
| return NULL; |
| 222 |
|
| } |
| 223 |
8 |
| s->zs_inited = 1; |
| 224 |
8 |
| return s; |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
|
| void |
| 228 |
8 |
| archive_gz_src_free(archive_gz_src_t *s) |
| 229 |
|
| { |
| 230 |
8 |
▶ | if (!s) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return; |
0 |
8 |
8 |
|
| 231 |
8 |
▶ | if (s->zs_inited) inflateEnd(&s->zs); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->zs_inited) inflateEnd(&s->zs); |
8 |
0 |
8 |
|
| 232 |
8 |
| free(s->raw); |
| 233 |
8 |
| free(s); |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
|
| int |
| 237 |
127 |
| archive_pull_gz(void *state, char *buf, size_t len) |
| 238 |
|
| { |
| 239 |
127 |
| archive_gz_src_t *s = (archive_gz_src_t *)state; |
| 240 |
127 |
▶ | if (s->stream_end) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->stream_end) return 0; |
0 |
127 |
127 |
|
| 241 |
|
| |
| 242 |
127 |
| s->zs.next_out = (Bytef *)buf; |
| 243 |
127 |
| s->zs.avail_out = (uInt)len; |
| 244 |
|
| |
| 245 |
328 |
▶ | while (s->zs.avail_out > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (s->zs.avail_out > 0) { |
207 |
121 |
328 |
|
| 246 |
207 |
▶ | if (s->zs.avail_in == 0 && !s->eof) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->zs.avail_in == 0 && !s->eof) { |
88 |
119 |
207 |
| if (s->zs.avail_in == 0 && !s->eof) { |
88 |
0 |
88 |
|
| 247 |
88 |
| ssize_t n = read(s->fd, s->raw, s->raw_cap); |
| 248 |
88 |
▶ | if (n < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) { |
0 |
88 |
88 |
|
| 249 |
0 |
▶ | if (errno == EINTR) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno == EINTR) continue; |
0 |
0 |
0 |
|
| 250 |
0 |
| return -1; |
| 251 |
|
| } |
| 252 |
88 |
▶ | if (n == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) { |
0 |
88 |
88 |
|
| 253 |
0 |
| s->eof = 1; |
| 254 |
|
| } else { |
| 255 |
88 |
| s->zs.next_in = (Bytef *)s->raw; |
| 256 |
88 |
| s->zs.avail_in = (uInt)n; |
| 257 |
|
| } |
| 258 |
|
| } |
| 259 |
|
| |
| 260 |
207 |
▶ | int rc = inflate(&s->zs, s->eof ? Z_FINISH : Z_NO_FLUSH); |
Branch: 1 branch
| Branch | True | False | Total |
| int rc = inflate(&s->zs, s->eof ? Z_FINISH : Z_NO_FLUSH); |
0 |
207 |
207 |
|
| 261 |
207 |
▶ | if (rc == Z_STREAM_END) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == Z_STREAM_END) { |
5 |
202 |
207 |
|
| 262 |
5 |
| s->stream_end = 1; |
| 263 |
5 |
| break; |
| 264 |
|
| } |
| 265 |
202 |
▶ | if (rc == Z_BUF_ERROR && s->eof && s->zs.avail_in == 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (rc == Z_BUF_ERROR && s->eof && s->zs.avail_in == 0) { |
0 |
202 |
202 |
| if (rc == Z_BUF_ERROR && s->eof && s->zs.avail_in == 0) { |
0 |
0 |
0 |
| if (rc == Z_BUF_ERROR && s->eof && s->zs.avail_in == 0) { |
0 |
0 |
0 |
|
| 266 |
0 |
| return -1; |
| 267 |
|
| } |
| 268 |
202 |
▶ | if (rc != Z_OK && rc != Z_BUF_ERROR) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (rc != Z_OK && rc != Z_BUF_ERROR) return -1; |
1 |
201 |
202 |
| if (rc != Z_OK && rc != Z_BUF_ERROR) return -1; |
1 |
0 |
1 |
|
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
126 |
| return (int)(len - s->zs.avail_out); |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
|
| /* ============================================================ |
| 275 |
|
| * gzip streaming sink |
| 276 |
|
| * ============================================================ */ |
| 277 |
|
| |
| 278 |
|
| struct archive_gz_sink { |
| 279 |
|
| int fd; |
| 280 |
|
| z_stream zs; |
| 281 |
|
| int zs_inited; |
| 282 |
|
| char *out; |
| 283 |
|
| size_t out_cap; |
| 284 |
|
| }; |
| 285 |
|
| |
| 286 |
|
| archive_gz_sink_t * |
| 287 |
4 |
| archive_gz_sink_new(int fd, size_t chunk_size, int level) |
| 288 |
|
| { |
| 289 |
4 |
| archive_gz_sink_t *s = (archive_gz_sink_t *)calloc(1, sizeof *s); |
| 290 |
4 |
▶ | if (!s) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return NULL; |
0 |
4 |
4 |
|
| 291 |
4 |
| s->fd = fd; |
| 292 |
4 |
▶ | s->out_cap = chunk_size ? chunk_size : ARCHIVE_DEFAULT_CHUNK; |
Branch: 1 branch
| Branch | True | False | Total |
| s->out_cap = chunk_size ? chunk_size : ARCHIVE_DEFAULT_CHUNK; |
4 |
0 |
4 |
|
| 293 |
4 |
| s->out = (char *)malloc(s->out_cap); |
| 294 |
4 |
▶ | if (!s->out) { free(s); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->out) { free(s); return NULL; } |
0 |
4 |
4 |
|
| 295 |
4 |
▶ | if (level < 0 || level > 9) level = 6; |
Branch: 2 branches
| Branch | True | False | Total |
| if (level < 0 || level > 9) level = 6; |
4 |
0 |
4 |
| if (level < 0 || level > 9) level = 6; |
0 |
4 |
4 |
|
| 296 |
4 |
▶ | if (deflateInit2(&s->zs, level, Z_DEFLATED, MAX_WBITS | 16, 8, |
Branch: 1 branch
| Branch | True | False | Total |
| if (deflateInit2(&s->zs, level, Z_DEFLATED, MAX_WBITS | 16, 8, |
0 |
4 |
4 |
|
| 297 |
|
| Z_DEFAULT_STRATEGY) != Z_OK) { |
| 298 |
0 |
| free(s->out); |
| 299 |
0 |
| free(s); |
| 300 |
0 |
| return NULL; |
| 301 |
|
| } |
| 302 |
4 |
| s->zs_inited = 1; |
| 303 |
4 |
| return s; |
| 304 |
|
| } |
| 305 |
|
| |
| 306 |
|
| static int |
| 307 |
26 |
| gz_sink_drain(archive_gz_sink_t *s, int finish) |
| 308 |
|
| { |
| 309 |
26 |
▶ | int flush = finish ? Z_FINISH : Z_NO_FLUSH; |
Branch: 1 branch
| Branch | True | False | Total |
| int flush = finish ? Z_FINISH : Z_NO_FLUSH; |
4 |
22 |
26 |
|
| 310 |
79 |
| for (;;) { |
| 311 |
105 |
| s->zs.next_out = (Bytef *)s->out; |
| 312 |
105 |
| s->zs.avail_out = (uInt)s->out_cap; |
| 313 |
105 |
| int rc = deflate(&s->zs, flush); |
| 314 |
105 |
| size_t produced = s->out_cap - s->zs.avail_out; |
| 315 |
105 |
▶ | if (produced) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (produced) { |
88 |
17 |
105 |
|
| 316 |
88 |
| archive_fd_state_t fdst = { s->fd }; |
| 317 |
88 |
▶ | if (archive_push_fd(&fdst, s->out, produced) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (archive_push_fd(&fdst, s->out, produced) < 0) return -1; |
0 |
88 |
88 |
|
| 318 |
|
| } |
| 319 |
105 |
▶ | if (rc == Z_STREAM_END) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == Z_STREAM_END) return 0; |
4 |
101 |
105 |
|
| 320 |
101 |
▶ | if (rc != Z_OK) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != Z_OK) return -1; |
0 |
101 |
101 |
|
| 321 |
101 |
▶ | if (!finish && s->zs.avail_in == 0) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!finish && s->zs.avail_in == 0) return 0; |
101 |
0 |
101 |
| if (!finish && s->zs.avail_in == 0) return 0; |
22 |
79 |
101 |
|
| 322 |
|
| } |
| 323 |
|
| } |
| 324 |
|
| |
| 325 |
|
| int |
| 326 |
22 |
| archive_push_gz(void *state, const char *buf, size_t len) |
| 327 |
|
| { |
| 328 |
22 |
| archive_gz_sink_t *s = (archive_gz_sink_t *)state; |
| 329 |
22 |
| s->zs.next_in = (Bytef *)buf; |
| 330 |
22 |
| s->zs.avail_in = (uInt)len; |
| 331 |
22 |
▶ | if (gz_sink_drain(s, 0) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gz_sink_drain(s, 0) < 0) return -1; |
0 |
22 |
22 |
|
| 332 |
22 |
| return (int)len; |
| 333 |
|
| } |
| 334 |
|
| |
| 335 |
|
| int |
| 336 |
4 |
| archive_gz_sink_finish(archive_gz_sink_t *s) |
| 337 |
|
| { |
| 338 |
4 |
| s->zs.next_in = NULL; |
| 339 |
4 |
| s->zs.avail_in = 0; |
| 340 |
4 |
| return gz_sink_drain(s, 1); |
| 341 |
|
| } |
| 342 |
|
| |
| 343 |
|
| void |
| 344 |
4 |
| archive_gz_sink_free(archive_gz_sink_t *s) |
| 345 |
|
| { |
| 346 |
4 |
▶ | if (!s) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return; |
0 |
4 |
4 |
|
| 347 |
4 |
▶ | if (s->zs_inited) deflateEnd(&s->zs); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->zs_inited) deflateEnd(&s->zs); |
4 |
0 |
4 |
|
| 348 |
4 |
| free(s->out); |
| 349 |
4 |
| free(s); |
| 350 |
|
| } |
| 351 |
|
| |
| 352 |
|
| /* ============================================================ |
| 353 |
|
| * Cross-platform xattr application |
| 354 |
|
| * ============================================================ */ |
| 355 |
|
| |
| 356 |
|
| int |
| 357 |
0 |
| archive_apply_xattrs(int fd, const ArchiveXattr *xattrs, size_t n) |
| 358 |
|
| { |
| 359 |
0 |
▶ | if (!xattrs || !n) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!xattrs || !n) return 0; |
0 |
0 |
0 |
| if (!xattrs || !n) return 0; |
0 |
0 |
0 |
|
| 360 |
|
| #if defined(ARCHIVE_HAVE_XATTR) |
| 361 |
|
| size_t i; |
| 362 |
0 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
0 |
0 |
0 |
|
| 363 |
|
| char keybuf[256]; |
| 364 |
0 |
▶ | if (xattrs[i].key_len >= sizeof keybuf) { errno = ENAMETOOLONG; return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (xattrs[i].key_len >= sizeof keybuf) { errno = ENAMETOOLONG; return -1; } |
0 |
0 |
0 |
|
| 365 |
0 |
| memcpy(keybuf, xattrs[i].key, xattrs[i].key_len); |
| 366 |
0 |
| keybuf[xattrs[i].key_len] = '\0'; |
| 367 |
|
| # if defined(ARCHIVE_DARWIN_XATTR) |
| 368 |
|
| if (fsetxattr(fd, keybuf, xattrs[i].value, xattrs[i].value_len, 0, 0) < 0) return -1; |
| 369 |
|
| # else |
| 370 |
0 |
▶ | if (fsetxattr(fd, keybuf, xattrs[i].value, xattrs[i].value_len, 0) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fsetxattr(fd, keybuf, xattrs[i].value, xattrs[i].value_len, 0) < 0) return -1; |
0 |
0 |
0 |
|
| 371 |
|
| # endif |
| 372 |
|
| } |
| 373 |
0 |
| return 0; |
| 374 |
|
| #elif defined(ARCHIVE_HAVE_EXTATTR) |
| 375 |
|
| size_t i; |
| 376 |
|
| for (i = 0; i < n; i++) { |
| 377 |
|
| char keybuf[256]; |
| 378 |
|
| if (xattrs[i].key_len >= sizeof keybuf) { errno = ENAMETOOLONG; return -1; } |
| 379 |
|
| memcpy(keybuf, xattrs[i].key, xattrs[i].key_len); |
| 380 |
|
| keybuf[xattrs[i].key_len] = '\0'; |
| 381 |
|
| const char *name = keybuf; |
| 382 |
|
| int ns = EXTATTR_NAMESPACE_USER; |
| 383 |
|
| if (strncmp(keybuf, "user.", 5) == 0) name = keybuf + 5; |
| 384 |
|
| else if (strncmp(keybuf, "system.", 7) == 0) { |
| 385 |
|
| name = keybuf + 7; |
| 386 |
|
| ns = EXTATTR_NAMESPACE_SYSTEM; |
| 387 |
|
| } |
| 388 |
|
| if (extattr_set_fd(fd, ns, name, xattrs[i].value, xattrs[i].value_len) < 0) return -1; |
| 389 |
|
| } |
| 390 |
|
| return 0; |
| 391 |
|
| #else |
| 392 |
|
| (void)fd; |
| 393 |
|
| return 0; |
| 394 |
|
| #endif |
| 395 |
|
| } |