extract.c

filter: Statementstmt 51.0% 73 / 143 Branchbran 38.1% 58 / 152 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 44.4% 131 / 295 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 <errno.h>
17 #include <fcntl.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <utime.h>
25  
26 #define ARCHIVE_PATH_MAX 4096
27 #define ARCHIVE_DATA_CHUNK (64 * 1024)
28  
29 /* ============================================================
30 * Path safety
31 * ============================================================ */
32  
33 int
34 148 archive_path_is_safe(const char *name, size_t name_len)
35 {
36 148 size_t i = 0;
37 148 ▶ if (name_len == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (name_len == 0) return 0; 0 148 148
38 148 ▶ if (name[0] == '/') return 0; /* absolute */
Branch: 1 branch
BranchTrueFalseTotal
if (name[0] == '/') return 0; /* absolute */ 0 148 148
39 579 ▶ while (i < name_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < name_len) { 285 146 431
40 285 size_t start = i;
41 2527 ▶ while (i < name_len && name[i] != '/') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < name_len && name[i] != '/') i++; 2383 144 2527
while (i < name_len && name[i] != '/') i++; 2242 141 2383
42 285 size_t comp = i - start;
43 285 ▶ if (comp == 2 && name[start] == '.' && name[start + 1] == '.') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (comp == 2 && name[start] == '.' && name[start + 1] == '.') return 0; 2 283 285
if (comp == 2 && name[start] == '.' && name[start + 1] == '.') return 0; 2 0 2
if (comp == 2 && name[start] == '.' && name[start + 1] == '.') return 0; 2 0 2
44 283 ▶ if (i < name_len) i++; /* skip the '/' */
Branch: 1 branch
BranchTrueFalseTotal
if (i < name_len) i++; /* skip the '/' */ 144 139 283
45 }
46 146 return 1;
47 }
48  
49 /* ============================================================
50 * Recursive mkpath
51 * ============================================================ */
52  
53 int
54 159 archive_mkpath(const char *path, uint32_t mode)
55 {
56 char buf[ARCHIVE_PATH_MAX];
57 159 size_t n = strlen(path);
58 size_t i;
59 159 ▶ if (n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 0; 0 159 159
60 159 ▶ if (n >= sizeof buf) { errno = ENAMETOOLONG; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (n >= sizeof buf) { errno = ENAMETOOLONG; return -1; } 0 159 159
61 159 memcpy(buf, path, n + 1);
62  
63 4217 ▶ for (i = 0; i <= n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= n; i++) { 4058 159 4217
64 4058 ▶ if (buf[i] == '/' || buf[i] == '\0') {
Branch: 2 branches
BranchTrueFalseTotal
if (buf[i] == '/' || buf[i] == '\0') { 3443 615 4058
if (buf[i] == '/' || buf[i] == '\0') { 159 3284 3443
65 774 ▶ if (i == 0) continue; /* leading "/" */
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) continue; /* leading "/" */ 159 615 774
66 615 char saved = buf[i];
67 615 buf[i] = '\0';
68 struct stat st;
69 615 ▶ if (stat(buf, &st) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(buf, &st) < 0) { 37 578 615
70 37 ▶ if (errno != ENOENT) { buf[i] = saved; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (errno != ENOENT) { buf[i] = saved; return -1; } 0 37 37
71 37 ▶ if (mkdir(buf, mode ? mode : 0755) < 0 && errno != EEXIST) {
Branch: 3 branches
BranchTrueFalseTotal
if (mkdir(buf, mode ? mode : 0755) < 0 && errno != EEXIST) { 37 0 37
if (mkdir(buf, mode ? mode : 0755) < 0 && errno != EEXIST) { 0 37 37
if (mkdir(buf, mode ? mode : 0755) < 0 && errno != EEXIST) { 0 0 0
72 0 buf[i] = saved;
73 0 return -1;
74 }
75 578 ▶ } else if (!S_ISDIR(st.st_mode)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!S_ISDIR(st.st_mode)) { 0 578 578
76 0 buf[i] = saved;
77 0 errno = ENOTDIR;
78 0 return -1;
79 }
80 615 buf[i] = saved;
81 }
82 }
83 159 return 0;
84 }
85  
86 int
87 145 archive_make_parent_dir(const char *path)
88 {
89 145 const char *slash = strrchr(path, '/');
90 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
91 145 size_t parent_len = (size_t)(slash - path);
92 char buf[ARCHIVE_PATH_MAX];
93 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
94 145 memcpy(buf, path, parent_len);
95 145 buf[parent_len] = '\0';
96 145 return archive_mkpath(buf, 0755);
97 }
98  
99 /* ============================================================
100 * Directory / symlink entries
101 * ============================================================ */
102  
103 int
104 2 archive_extract_dir(const char *out_path, uint32_t mode)
105 {
106 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
107 2 ▶ if (mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode) { 2 0 2
108 /* mkpath used `mode` for newly-created components; existing
109 * components keep their existing mode. Force the leaf to
110 * match what the archive wanted. */
111 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
112 }
113 2 return 0;
114 }
115  
116 int
117 2 archive_extract_symlink(const char *out_path, const char *target)
118 {
119 struct stat st;
120 2 ▶ if (lstat(out_path, &st) == 0 && S_ISLNK(st.st_mode)) {
Branch: 2 branches
BranchTrueFalseTotal
if (lstat(out_path, &st) == 0 && S_ISLNK(st.st_mode)) { 0 2 2
if (lstat(out_path, &st) == 0 && S_ISLNK(st.st_mode)) { 0 0 0
121 0 unlink(out_path);
122 }
123 2 ▶ if (symlink(target, out_path) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (symlink(target, out_path) < 0) return -1; 0 2 2
124 2 return 0;
125 }
126  
127 /* ============================================================
128 * Metadata application
129 * ============================================================ */
130  
131 int
132 84 archive_apply_metadata_fd(int fd, const ArchiveEntry *e, int apply_xattrs)
133 {
134 84 ▶ if (e->mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mode) { 84 0 84
135 84 ▶ if (fchmod(fd, e->mode & 07777) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (fchmod(fd, e->mode & 07777) < 0) return -1; 0 84 84
136 }
137 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
138 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
139 }
140 84 return 0;
141 }
142  
143 int
144 0 archive_apply_metadata_path(const char *path, const ArchiveEntry *e)
145 {
146 0 ▶ if (e->mode) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mode) { 0 0 0
147 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
148 }
149 0 ▶ if (e->mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mtime) { 0 0 0
150 struct utimbuf t;
151 0 t.actime = (time_t)e->mtime;
152 0 t.modtime = (time_t)e->mtime;
153 0 ▶ if (utime(path, &t) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (utime(path, &t) < 0) return -1; 0 0 0
154 }
155 0 return 0;
156 }
157  
158 /* ============================================================
159 * Payload pump
160 * ============================================================ */
161  
162 int
163 84 archive_write_entry_data(pTHX_ const ArchivePlugin *plugin,
164 void *cursor, int fd)
165 {
166 char buf[ARCHIVE_DATA_CHUNK];
167 163 for (;;) {
168 247 int n = plugin->read_data(aTHX_ plugin, cursor, buf, sizeof buf);
169 247 ▶ if (n < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return -1; 0 247 247
170 247 ▶ if (n == 0) break; /* end of entry */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; /* end of entry */ 84 163 247
171 163 size_t put = 0;
172 326 ▶ while (put < (size_t)n) {
Branch: 1 branch
BranchTrueFalseTotal
while (put < (size_t)n) { 163 163 326
173 163 ssize_t w = write(fd, buf + put, (size_t)n - put);
174 163 ▶ if (w < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) { 0 163 163
175 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
176 0 return -1;
177 }
178 163 put += (size_t)w;
179 }
180 }
181 84 return 0;
182 }
183  
184 /* ============================================================
185 * Composite extract
186 * ============================================================ */
187  
188 int
189 84 archive_extract_entry(pTHX_ const ArchivePlugin *plugin,
190 void *cursor,
191 const ArchiveEntry *e,
192 const char *out_path,
193 int apply_xattrs,
194 const char **errstage)
195 {
196 int fd;
197  
198 84 ▶ if (errstage) *errstage = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = NULL; 84 0 84
199  
200 84 fd = open(out_path, O_WRONLY | O_CREAT | O_TRUNC,
201 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
202 84 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 84 84
203 0 ▶ if (errstage) *errstage = "open";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "open"; 0 0 0
204 0 return -1;
205 }
206  
207 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
208 0 ▶ if (errstage) *errstage = "write";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "write"; 0 0 0
209 0 int saved = errno;
210 0 close(fd);
211 0 unlink(out_path);
212 0 errno = saved;
213 0 return -1;
214 }
215  
216 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
217 0 ▶ if (errstage) *errstage = "chmod_or_xattr";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "chmod_or_xattr"; 0 0 0
218 0 int saved = errno;
219 0 close(fd);
220 0 errno = saved;
221 0 return -1;
222 }
223  
224 84 ▶ if (close(fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (close(fd) < 0) { 0 84 84
225 0 ▶ if (errstage) *errstage = "close";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "close"; 0 0 0
226 0 return -1;
227 }
228  
229 84 ▶ if (e->mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mtime) { 4 80 84
230 struct utimbuf t;
231 4 t.actime = (time_t)e->mtime;
232 4 t.modtime = (time_t)e->mtime;
233 4 ▶ if (utime(out_path, &t) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (utime(out_path, &t) < 0) { 0 4 4
234 0 ▶ if (errstage) *errstage = "utime";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "utime"; 0 0 0
235 0 return -1;
236 }
237 }
238  
239 84 return 0;
240 }
241  
242 int
243 0 archive_extract_bytes(const ArchiveEntry *e,
244 const char *out_path,
245 const char *content, size_t content_len,
246 int apply_xattrs,
247 const char **errstage)
248 {
249 int fd;
250  
251 0 ▶ if (errstage) *errstage = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = NULL; 0 0 0
252  
253 0 fd = open(out_path, O_WRONLY | O_CREAT | O_TRUNC,
254 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
255 0 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 0 0
256 0 ▶ if (errstage) *errstage = "open";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "open"; 0 0 0
257 0 return -1;
258 }
259  
260 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
261 0 size_t put = 0;
262 0 ▶ while (put < content_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (put < content_len) { 0 0 0
263 0 ssize_t w = write(fd, content + put, content_len - put);
264 0 ▶ if (w < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) { 0 0 0
265 0 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 0 0 0
266 0 ▶ if (errstage) *errstage = "write";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "write"; 0 0 0
267 0 int saved = errno;
268 0 close(fd);
269 0 unlink(out_path);
270 0 errno = saved;
271 0 return -1;
272 }
273 0 put += (size_t)w;
274 }
275 }
276  
277 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
278 0 ▶ if (errstage) *errstage = "chmod_or_xattr";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "chmod_or_xattr"; 0 0 0
279 0 int saved = errno;
280 0 close(fd);
281 0 errno = saved;
282 0 return -1;
283 }
284  
285 0 ▶ if (close(fd) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (close(fd) < 0) { 0 0 0
286 0 ▶ if (errstage) *errstage = "close";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "close"; 0 0 0
287 0 return -1;
288 }
289  
290 0 ▶ if (e->mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->mtime) { 0 0 0
291 struct utimbuf t;
292 0 t.actime = (time_t)e->mtime;
293 0 t.modtime = (time_t)e->mtime;
294 0 ▶ if (utime(out_path, &t) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (utime(out_path, &t) < 0) { 0 0 0
295 0 ▶ if (errstage) *errstage = "utime";
Branch: 1 branch
BranchTrueFalseTotal
if (errstage) *errstage = "utime"; 0 0 0
296 0 return -1;
297 }
298 }
299  
300 0 return 0;
301 }
« marshal.c ↑ Summary arch_io.c »