extract.c

filter: Statementstmt 52.0% 76 / 146 Branchbran 41.2% 66 / 160 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 46.4% 142 / 306 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« marshal.c ↑ Summary arch_io.c »
1 /*
2 * extract.c - reusable per-entry write helpers (Phase A of XS refactor).
3 *
4 * Pure C except where the plugin's read_data callback drags pTHX_ in.
5 * No callers wired up yet; Phase B onward replaces Perl loops with
6 * single XSUB calls into these helpers.
7 */
8  
9 #define PERL_NO_GET_CONTEXT
10 #include "EXTERN.h"
11 #include "perl.h"
12  
13 #include "archive_plugin.h"
14 #include "extract.h"
15  
16 #include "arch_compat.h"
17  
18 #include <errno.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22  
23 #define ARCHIVE_PATH_MAX 4096
24 #define ARCHIVE_DATA_CHUNK (64 * 1024)
25  
26 /* ============================================================
27 * Path safety
28 * ============================================================ */
29  
30 /* A separator for the platform we are about to write on. Archive members
31 * always name their components with '/', but Windows accepts '\' just as
32 * well, so an entry called "..\..\evil" walks out of the destination on a
33 * scanner that only knows about '/'. Both are separators everywhere here:
34 * a POSIX filename really containing a backslash is then split into two
35 * components, which is a harmless over-refusal, and refusing is the side
36 * to be wrong on. */
37 static int
38 2546 archive_is_sep(char c)
39 {
40 2546 ▶ return c == '/' || c == '\\';
Branch: 2 branches
BranchTrueFalseTotal
return c == '/' || c == '\\'; 2405 141 2546
return c == '/' || c == '\\'; 4 2401 2405
41 }
42  
43 int
44 153 archive_path_is_safe(const char *name, size_t name_len)
45 {
46 153 size_t i = 0;
47 153 ▶ if (name_len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (name_len == 0) return 0; 0 153 153
48 153 ▶ if (archive_is_sep(name[0])) return 0; /* absolute */
Branch: 1 branch
BranchTrueFalseTotal
if (archive_is_sep(name[0])) return 0; /* absolute */ 1 152 153
49 /* "C:foo" and "C:\foo" are both anchored to a drive on Windows. */
50 152 ▶ if (name_len >= 2 && name[1] == ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (name_len >= 2 && name[1] == ':') return 0; 152 0 152
if (name_len >= 2 && name[1] == ':') return 0; 2 150 152
51 434 ▶ while (i < name_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < name_len) { 288 146 434
52 288 size_t start = i;
53 2537 ▶ while (i < name_len && !archive_is_sep(name[i])) i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < name_len && !archive_is_sep(name[i])) i++; 2393 144 2537
while (i < name_len && !archive_is_sep(name[i])) i++; 2249 144 2393
54 288 size_t comp = i - start;
55 288 ▶ if (comp == 2 && name[start] == '.' && name[start + 1] == '.') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (comp == 2 && name[start] == '.' && name[start + 1] == '.') return 0; 4 284 288
if (comp == 2 && name[start] == '.' && name[start + 1] == '.') return 0; 4 0 4
if (comp == 2 && name[start] == '.' && name[start + 1] == '.') return 0; 4 0 4
56 284 ▶ if (i < name_len) i++; /* skip the separator */
Branch: 1 branch
BranchTrueFalseTotal
if (i < name_len) i++; /* skip the separator */ 140 144 284
57 }
58 146 return 1;
59 }
60  
61 /* ============================================================
62 * Recursive mkpath
63 * ============================================================ */
64  
65 int
66 164 archive_mkpath(const char *path, uint32_t mode)
67 {
68 char buf[ARCHIVE_PATH_MAX];
69 164 size_t n = strlen(path);
70 size_t i;
71 164 ▶ if (n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 0; 0 164 164
72 164 ▶ if (n >= sizeof buf) { errno = ENAMETOOLONG; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (n >= sizeof buf) { errno = ENAMETOOLONG; return -1; } 0 164 164
73 164 memcpy(buf, path, n + 1);
74  
75 4352 ▶ for (i = 0; i <= n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= n; i++) { 4188 164 4352
76 4188 ▶ if (buf[i] == '/' || buf[i] == '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (buf[i] == '/' || buf[i] == '\0') { 3558 630 4188
if (buf[i] == '/' || buf[i] == '\0') { 164 3394 3558
77 794 ▶ if (i == 0) continue; /* leading "/" */
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) continue; /* leading "/" */ 164 630 794
78 630 char saved = buf[i];
79 630 buf[i] = '\0';
80 struct stat st;
81 630 ▶ if (stat(buf, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(buf, &st) < 0) { 38 592 630
82 38 ▶ if (errno != ENOENT) { buf[i] = saved; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (errno != ENOENT) { buf[i] = saved; return -1; } 0 38 38
83 38 ▶ if (arch_mkdir(buf, mode ? mode : 0755) < 0 && errno != EEXIST) {
Branch: 3 branches
BranchTrueFalseTotal
if (arch_mkdir(buf, mode ? mode : 0755) < 0 && errno != EEXIST) { 38 0 38
if (arch_mkdir(buf, mode ? mode : 0755) < 0 && errno != EEXIST) { 0 38 38
if (arch_mkdir(buf, mode ? mode : 0755) < 0 && errno != EEXIST) { 0 0 0
84 0 buf[i] = saved;
85 0 return -1;
86 }
87 592 ▶ } else if (!S_ISDIR(st.st_mode)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!S_ISDIR(st.st_mode)) { 0 592 592
88 0 buf[i] = saved;
89 0 errno = ENOTDIR;
90 0 return -1;
91 }
92 630 buf[i] = saved;
93 }
94 }
95 164 return 0;
96 }
97  
98 int
99 145 archive_make_parent_dir(const char *path)
100 {
101 145 const char *slash = strrchr(path, '/');
102 145 ▶ if (!slash || slash == path) return 0; /* no parent or "/x" */
Branch: 2 branches
BranchTrueFalseTotal
if (!slash || slash == path) return 0; /* no parent or "/x" */ 145 0 145
if (!slash || slash == path) return 0; /* no parent or "/x" */ 0 145 145
103 145 size_t parent_len = (size_t)(slash - path);
104 char buf[ARCHIVE_PATH_MAX];
105 145 ▶ if (parent_len >= sizeof buf) { errno = ENAMETOOLONG; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (parent_len >= sizeof buf) { errno = ENAMETOOLONG; return -1; } 0 145 145
106 145 memcpy(buf, path, parent_len);
107 145 buf[parent_len] = '\0';
108 145 return archive_mkpath(buf, 0755);
109 }
110  
111 /* ============================================================
112 * Directory / symlink entries
113 * ============================================================ */
114  
115 int
116 2 archive_extract_dir(const char *out_path, uint32_t mode)
117 {
118 2 ▶ if (archive_mkpath(out_path, mode ? (mode & 07777) : 0755) < 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (archive_mkpath(out_path, mode ? (mode & 07777) : 0755) < 0) return -1; 2 0 2
if (archive_mkpath(out_path, mode ? (mode & 07777) : 0755) < 0) return -1; 0 2 2
119 2 ▶ if (mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode) { 2 0 2
120 /* mkpath used `mode` for newly-created components; existing
121 * components keep their existing mode. Force the leaf to
122 * match what the archive wanted. */
123 2 ▶ if (chmod(out_path, mode & 07777) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (chmod(out_path, mode & 07777) < 0) return -1; 0 2 2
124 }
125 2 return 0;
126 }
127  
128 int
129 2 archive_extract_symlink(const char *out_path, const char *target)
130 {
131 struct stat st;
132 2 ▶ if (arch_lstat(out_path, &st) == 0 && arch_is_symlink(&st)) {
Branch: 2 branches
BranchTrueFalseTotal
if (arch_lstat(out_path, &st) == 0 && arch_is_symlink(&st)) { 0 2 2
if (arch_lstat(out_path, &st) == 0 && arch_is_symlink(&st)) { 0 0 0
133 0 unlink(out_path);
134 }
135 2 ▶ if (arch_symlink(target, out_path) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (arch_symlink(target, out_path) < 0) return -1; 0 2 2
136 2 return 0;
137 }
138  
139 /* ============================================================
140 * Metadata application
141 * ============================================================ */
142  
143 int
144 84 archive_apply_metadata_fd(int fd, const ArchiveEntry *e, int apply_xattrs)
145 {
146 84 ▶ if (e->mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mode) { 84 0 84
147 84 ▶ if (arch_fchmod(fd, e->mode & 07777) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (arch_fchmod(fd, e->mode & 07777) < 0) return -1; 0 84 84
148 }
149 84 ▶ if (apply_xattrs && e->xattrs && e->xattr_count > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (apply_xattrs && e->xattrs && e->xattr_count > 0) { 84 0 84
if (apply_xattrs && e->xattrs && e->xattr_count > 0) { 0 84 84
if (apply_xattrs && e->xattrs && e->xattr_count > 0) { 0 0 0
150 0 ▶ if (archive_apply_xattrs(fd, e->xattrs, e->xattr_count) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (archive_apply_xattrs(fd, e->xattrs, e->xattr_count) < 0) return -1; 0 0 0
151 }
152 84 return 0;
153 }
154  
155 int
156 0 archive_apply_metadata_path(const char *path, const ArchiveEntry *e)
157 {
158 0 ▶ if (e->mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mode) { 0 0 0
159 0 ▶ if (chmod(path, e->mode & 07777) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (chmod(path, e->mode & 07777) < 0) return -1; 0 0 0
160 }
161 0 ▶ if (e->mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mtime) { 0 0 0
162 struct utimbuf t;
163 0 t.actime = (time_t)e->mtime;
164 0 t.modtime = (time_t)e->mtime;
165 0 ▶ if (utime(path, &t) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (utime(path, &t) < 0) return -1; 0 0 0
166 }
167 0 return 0;
168 }
169  
170 /* ============================================================
171 * Payload pump
172 * ============================================================ */
173  
174 int
175 84 archive_write_entry_data(pTHX_ const ArchivePlugin *plugin,
176 void *cursor, int fd)
177 {
178 char buf[ARCHIVE_DATA_CHUNK];
179 163 for (;;) {
180 247 int n = plugin->read_data(aTHX_ plugin, cursor, buf, sizeof buf);
181 247 ▶ if (n < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return -1; 0 247 247
182 247 ▶ if (n == 0) break; /* end of entry */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; /* end of entry */ 84 163 247
183 163 size_t put = 0;
184 326 ▶ while (put < (size_t)n) {
Branch: 1 branch
BranchTrueFalseTotal
while (put < (size_t)n) { 163 163 326
185 163 ssize_t w = write(fd, buf + put, (size_t)n - put);
186 163 ▶ if (w < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) { 0 163 163
187 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
188 0 return -1;
189 }
190 163 put += (size_t)w;
191 }
192 }
193 84 return 0;
194 }
195  
196 /* ============================================================
197 * Composite extract
198 * ============================================================ */
199  
200 int
201 84 archive_extract_entry(pTHX_ const ArchivePlugin *plugin,
202 void *cursor,
203 const ArchiveEntry *e,
204 const char *out_path,
205 int apply_xattrs,
206 const char **errstage)
207 {
208 int fd;
209  
210 84 ▶ if (errstage) *errstage = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = NULL; 84 0 84
211  
212 84 fd = open(out_path, O_WRONLY | O_CREAT | O_TRUNC | ARCH_O_BINARY,
213 84 ▶ e->mode ? (mode_t)(e->mode & 07777) : (mode_t)0644);
Branch: 1 branch
BranchTrueFalseTotal
e->mode ? (mode_t)(e->mode & 07777) : (mode_t)0644); 84 0 84
214 84 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 84 84
215 0 ▶ if (errstage) *errstage = "open";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "open"; 0 0 0
216 0 return -1;
217 }
218  
219 84 ▶ if (archive_write_entry_data(aTHX_ plugin, cursor, fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_write_entry_data(aTHX_ plugin, cursor, fd) < 0) { 0 84 84
220 0 ▶ if (errstage) *errstage = "write";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "write"; 0 0 0
221 0 int saved = errno;
222 0 close(fd);
223 0 unlink(out_path);
224 0 errno = saved;
225 0 return -1;
226 }
227  
228 84 ▶ if (archive_apply_metadata_fd(fd, e, apply_xattrs) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_apply_metadata_fd(fd, e, apply_xattrs) < 0) { 0 84 84
229 0 ▶ if (errstage) *errstage = "chmod_or_xattr";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "chmod_or_xattr"; 0 0 0
230 0 int saved = errno;
231 0 close(fd);
232 0 errno = saved;
233 0 return -1;
234 }
235  
236 84 ▶ if (close(fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (close(fd) < 0) { 0 84 84
237 0 ▶ if (errstage) *errstage = "close";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "close"; 0 0 0
238 0 return -1;
239 }
240  
241 84 ▶ if (e->mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mtime) { 4 80 84
242 struct utimbuf t;
243 4 t.actime = (time_t)e->mtime;
244 4 t.modtime = (time_t)e->mtime;
245 4 ▶ if (utime(out_path, &t) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (utime(out_path, &t) < 0) { 0 4 4
246 0 ▶ if (errstage) *errstage = "utime";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "utime"; 0 0 0
247 0 return -1;
248 }
249 }
250  
251 84 return 0;
252 }
253  
254 int
255 0 archive_extract_bytes(const ArchiveEntry *e,
256 const char *out_path,
257 const char *content, size_t content_len,
258 int apply_xattrs,
259 const char **errstage)
260 {
261 int fd;
262  
263 0 ▶ if (errstage) *errstage = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = NULL; 0 0 0
264  
265 0 fd = open(out_path, O_WRONLY | O_CREAT | O_TRUNC | ARCH_O_BINARY,
266 0 ▶ e->mode ? (mode_t)(e->mode & 07777) : (mode_t)0644);
Branch: 1 branch
BranchTrueFalseTotal
e->mode ? (mode_t)(e->mode & 07777) : (mode_t)0644); 0 0 0
267 0 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 0 0
268 0 ▶ if (errstage) *errstage = "open";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "open"; 0 0 0
269 0 return -1;
270 }
271  
272 0 ▶ if (content_len > 0 && content) {
Branch: 2 branches
BranchTrueFalseTotal
if (content_len > 0 && content) { 0 0 0
if (content_len > 0 && content) { 0 0 0
273 0 size_t put = 0;
274 0 ▶ while (put < content_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (put < content_len) { 0 0 0
275 0 ssize_t w = write(fd, content + put, content_len - put);
276 0 ▶ if (w < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) { 0 0 0
277 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
278 0 ▶ if (errstage) *errstage = "write";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "write"; 0 0 0
279 0 int saved = errno;
280 0 close(fd);
281 0 unlink(out_path);
282 0 errno = saved;
283 0 return -1;
284 }
285 0 put += (size_t)w;
286 }
287 }
288  
289 0 ▶ if (archive_apply_metadata_fd(fd, e, apply_xattrs) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (archive_apply_metadata_fd(fd, e, apply_xattrs) < 0) { 0 0 0
290 0 ▶ if (errstage) *errstage = "chmod_or_xattr";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "chmod_or_xattr"; 0 0 0
291 0 int saved = errno;
292 0 close(fd);
293 0 errno = saved;
294 0 return -1;
295 }
296  
297 0 ▶ if (close(fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (close(fd) < 0) { 0 0 0
298 0 ▶ if (errstage) *errstage = "close";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "close"; 0 0 0
299 0 return -1;
300 }
301  
302 0 ▶ if (e->mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mtime) { 0 0 0
303 struct utimbuf t;
304 0 t.actime = (time_t)e->mtime;
305 0 t.modtime = (time_t)e->mtime;
306 0 ▶ if (utime(out_path, &t) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (utime(out_path, &t) < 0) { 0 0 0
307 0 ▶ if (errstage) *errstage = "utime";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "utime"; 0 0 0
308 0 return -1;
309 }
310 }
311  
312 0 return 0;
313 }
« marshal.c ↑ Summary arch_io.c »