1 // SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/stat.h>
6 #include "erofs/print.h"
7 #include "erofs/cache.h"
8 #include "erofs/diskbuf.h"
9 #include "erofs/inode.h"
10 #include "erofs/list.h"
11 #include "erofs/tar.h"
12 #include "erofs/xattr.h"
13 #include "erofs/blobchunk.h"
14 #include "erofs/rebuild.h"
15 #if defined(HAVE_ZLIB)
16 #include <zlib.h>
17 #endif
18
19 /* This file is a tape/volume header. Ignore it on extraction. */
20 #define GNUTYPE_VOLHDR 'V'
21
22 struct tar_header {
23 char name[100]; /* 0-99 */
24 char mode[8]; /* 100-107 */
25 char uid[8]; /* 108-115 */
26 char gid[8]; /* 116-123 */
27 char size[12]; /* 124-135 */
28 char mtime[12]; /* 136-147 */
29 char chksum[8]; /* 148-155 */
30 char typeflag; /* 156-156 */
31 char linkname[100]; /* 157-256 */
32 char magic[6]; /* 257-262 */
33 char version[2]; /* 263-264 */
34 char uname[32]; /* 265-296 */
35 char gname[32]; /* 297-328 */
36 char devmajor[8]; /* 329-336 */
37 char devminor[8]; /* 337-344 */
38 char prefix[155]; /* 345-499 */
39 char padding[12]; /* 500-512 (pad to exactly the 512 byte) */
40 };
41
42 #ifdef HAVE_LIBLZMA
43 #include <lzma.h>
44 struct erofs_iostream_liblzma {
45 u8 inbuf[32768];
46 lzma_stream strm;
47 int fd;
48 };
49 #endif
50
erofs_iostream_close(struct erofs_iostream * ios)51 void erofs_iostream_close(struct erofs_iostream *ios)
52 {
53 free(ios->buffer);
54 if (ios->decoder == EROFS_IOS_DECODER_GZIP) {
55 #if defined(HAVE_ZLIB)
56 gzclose(ios->handler);
57 #endif
58 return;
59 } else if (ios->decoder == EROFS_IOS_DECODER_LIBLZMA) {
60 #if defined(HAVE_LIBLZMA)
61 lzma_end(&ios->lzma->strm);
62 close(ios->lzma->fd);
63 free(ios->lzma);
64 #endif
65 return;
66 }
67 close(ios->vf.fd);
68 }
69
erofs_iostream_open(struct erofs_iostream * ios,int fd,int decoder)70 int erofs_iostream_open(struct erofs_iostream *ios, int fd, int decoder)
71 {
72 s64 fsz;
73
74 ios->feof = false;
75 ios->tail = ios->head = 0;
76 ios->decoder = decoder;
77 ios->dumpfd = -1;
78 if (decoder == EROFS_IOS_DECODER_GZIP) {
79 #if defined(HAVE_ZLIB)
80 ios->handler = gzdopen(fd, "r");
81 if (!ios->handler)
82 return -ENOMEM;
83 ios->sz = fsz = 0;
84 ios->bufsize = 32768;
85 #else
86 return -EOPNOTSUPP;
87 #endif
88 } else if (decoder == EROFS_IOS_DECODER_LIBLZMA) {
89 #ifdef HAVE_LIBLZMA
90 lzma_ret ret;
91
92 ios->lzma = malloc(sizeof(*ios->lzma));
93 if (!ios->lzma)
94 return -ENOMEM;
95 ios->lzma->fd = fd;
96 ios->lzma->strm = (lzma_stream)LZMA_STREAM_INIT;
97 ret = lzma_auto_decoder(&ios->lzma->strm,
98 UINT64_MAX, LZMA_CONCATENATED);
99 if (ret != LZMA_OK)
100 return -EFAULT;
101 ios->sz = fsz = 0;
102 ios->bufsize = 32768;
103 #else
104 return -EOPNOTSUPP;
105 #endif
106 } else {
107 ios->vf.fd = fd;
108 fsz = lseek(fd, 0, SEEK_END);
109 if (fsz <= 0) {
110 ios->feof = !fsz;
111 ios->sz = 0;
112 } else {
113 ios->sz = fsz;
114 if (lseek(fd, 0, SEEK_SET))
115 return -EIO;
116 #ifdef HAVE_POSIX_FADVISE
117 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL))
118 erofs_warn("failed to fadvise: %s, ignored.",
119 erofs_strerror(-errno));
120 #endif
121 }
122 ios->bufsize = 32768;
123 }
124
125 do {
126 ios->buffer = malloc(ios->bufsize);
127 if (ios->buffer)
128 break;
129 ios->bufsize >>= 1;
130 } while (ios->bufsize >= 1024);
131
132 if (!ios->buffer)
133 return -ENOMEM;
134 return 0;
135 }
136
erofs_iostream_read(struct erofs_iostream * ios,void ** buf,u64 bytes)137 int erofs_iostream_read(struct erofs_iostream *ios, void **buf, u64 bytes)
138 {
139 unsigned int rabytes = ios->tail - ios->head;
140 int ret;
141
142 if (rabytes >= bytes) {
143 *buf = ios->buffer + ios->head;
144 ios->head += bytes;
145 return bytes;
146 }
147
148 if (ios->head) {
149 memmove(ios->buffer, ios->buffer + ios->head, rabytes);
150 ios->head = 0;
151 ios->tail = rabytes;
152 }
153
154 if (!ios->feof) {
155 if (ios->decoder == EROFS_IOS_DECODER_GZIP) {
156 #if defined(HAVE_ZLIB)
157 ret = gzread(ios->handler, ios->buffer + rabytes,
158 ios->bufsize - rabytes);
159 if (!ret) {
160 int errnum;
161 const char *errstr;
162
163 errstr = gzerror(ios->handler, &errnum);
164 if (errnum != Z_STREAM_END) {
165 erofs_err("failed to gzread: %s", errstr);
166 return -EIO;
167 }
168 ios->feof = true;
169 }
170 ios->tail += ret;
171 #else
172 return -EOPNOTSUPP;
173 #endif
174 } else if (ios->decoder == EROFS_IOS_DECODER_LIBLZMA) {
175 #ifdef HAVE_LIBLZMA
176 struct erofs_iostream_liblzma *lzma = ios->lzma;
177 lzma_action action = LZMA_RUN;
178 lzma_ret ret2;
179
180 if (!lzma->strm.avail_in) {
181 lzma->strm.next_in = lzma->inbuf;
182 ret = read(lzma->fd, lzma->inbuf,
183 sizeof(lzma->inbuf));
184 if (ret < 0)
185 return -errno;
186 lzma->strm.avail_in = ret;
187 if (ret < sizeof(lzma->inbuf))
188 action = LZMA_FINISH;
189 }
190 lzma->strm.next_out = (u8 *)ios->buffer + rabytes;
191 lzma->strm.avail_out = ios->bufsize - rabytes;
192
193 ret2 = lzma_code(&lzma->strm, action);
194 if (ret2 != LZMA_OK) {
195 if (ret2 == LZMA_STREAM_END)
196 ios->feof = true;
197 else
198 return -EIO;
199 }
200 ret = ios->bufsize - rabytes - lzma->strm.avail_out;
201 ios->tail += ret;
202 #else
203 return -EOPNOTSUPP;
204 #endif
205 } else {
206 ret = erofs_io_read(&ios->vf, ios->buffer + rabytes,
207 ios->bufsize - rabytes);
208 if (ret < 0)
209 return ret;
210 ios->tail += ret;
211 if (ret < ios->bufsize - rabytes)
212 ios->feof = true;
213 }
214 if (__erofs_unlikely(ios->dumpfd >= 0))
215 if (write(ios->dumpfd, ios->buffer + rabytes, ret) < ret)
216 erofs_err("failed to dump %d bytes of the raw stream: %s",
217 ret, erofs_strerror(-errno));
218 }
219 *buf = ios->buffer;
220 ret = min_t(int, ios->tail, min_t(u64, bytes, INT_MAX));
221 ios->head = ret;
222 return ret;
223 }
224
erofs_iostream_bread(struct erofs_iostream * ios,void * buf,u64 bytes)225 int erofs_iostream_bread(struct erofs_iostream *ios, void *buf, u64 bytes)
226 {
227 u64 rem = bytes;
228 void *src;
229 int ret;
230
231 do {
232 ret = erofs_iostream_read(ios, &src, rem);
233 if (ret < 0)
234 return ret;
235 memcpy(buf, src, ret);
236 rem -= ret;
237 } while (rem && ret);
238
239 return bytes - rem;
240 }
241
erofs_iostream_lskip(struct erofs_iostream * ios,u64 sz)242 int erofs_iostream_lskip(struct erofs_iostream *ios, u64 sz)
243 {
244 unsigned int rabytes = ios->tail - ios->head;
245 int ret;
246 void *dummy;
247
248 if (rabytes >= sz) {
249 ios->head += sz;
250 return 0;
251 }
252
253 sz -= rabytes;
254 ios->head = ios->tail = 0;
255 if (ios->feof)
256 return sz;
257
258 if (ios->sz && __erofs_likely(ios->dumpfd < 0)) {
259 s64 cur = erofs_io_lseek(&ios->vf, sz, SEEK_CUR);
260
261 if (cur > ios->sz)
262 return cur - ios->sz;
263 return 0;
264 }
265
266 do {
267 ret = erofs_iostream_read(ios, &dummy, sz);
268 if (ret < 0)
269 return ret;
270 sz -= ret;
271 } while (!(ios->feof || !ret || !sz));
272
273 return sz;
274 }
275
tarerofs_otoi(const char * ptr,int len)276 static long long tarerofs_otoi(const char *ptr, int len)
277 {
278 char inp[32];
279 char *endp = inp;
280 long long val;
281
282 memcpy(inp, ptr, len);
283 inp[len] = '\0';
284
285 errno = 0;
286 val = strtol(inp, &endp, 8);
287 if ((*endp == '\0' && endp == inp) |
288 (*endp != '\0' && *endp != ' '))
289 errno = EINVAL;
290 return val;
291 }
292
tarerofs_parsenum(const char * ptr,int len)293 static long long tarerofs_parsenum(const char *ptr, int len)
294 {
295 /*
296 * For fields containing numbers or timestamps that are out of range
297 * for the basic format, the GNU format uses a base-256 representation
298 * instead of an ASCII octal number.
299 */
300 if (*(char *)ptr == '\200') {
301 long long res = 0;
302
303 while (--len)
304 res = (res << 8) + (u8)*(++ptr);
305 return res;
306 }
307 return tarerofs_otoi(ptr, len);
308 }
309
310 struct tarerofs_xattr_item {
311 struct list_head list;
312 char *kv;
313 unsigned int len, namelen;
314 };
315
tarerofs_insert_xattr(struct list_head * xattrs,char * kv,int namelen,int len,bool skip)316 int tarerofs_insert_xattr(struct list_head *xattrs,
317 char *kv, int namelen, int len, bool skip)
318 {
319 struct tarerofs_xattr_item *item;
320 char *nv;
321
322 DBG_BUGON(namelen >= len);
323 list_for_each_entry(item, xattrs, list) {
324 if (!strncmp(item->kv, kv, namelen + 1)) {
325 if (skip)
326 return 0;
327 goto found;
328 }
329 }
330
331 item = malloc(sizeof(*item));
332 if (!item)
333 return -ENOMEM;
334 item->kv = NULL;
335 item->namelen = namelen;
336 namelen = 0;
337 list_add_tail(&item->list, xattrs);
338 found:
339 nv = realloc(item->kv, len);
340 if (!nv)
341 return -ENOMEM;
342 item->kv = nv;
343 item->len = len;
344 memcpy(nv + namelen, kv + namelen, len - namelen);
345 return 0;
346 }
347
tarerofs_merge_xattrs(struct list_head * dst,struct list_head * src)348 int tarerofs_merge_xattrs(struct list_head *dst, struct list_head *src)
349 {
350 struct tarerofs_xattr_item *item;
351
352 list_for_each_entry(item, src, list) {
353 int ret;
354
355 ret = tarerofs_insert_xattr(dst, item->kv, item->namelen,
356 item->len, true);
357 if (ret)
358 return ret;
359 }
360 return 0;
361 }
362
tarerofs_remove_xattrs(struct list_head * xattrs)363 void tarerofs_remove_xattrs(struct list_head *xattrs)
364 {
365 struct tarerofs_xattr_item *item, *n;
366
367 list_for_each_entry_safe(item, n, xattrs, list) {
368 DBG_BUGON(!item->kv);
369 free(item->kv);
370 list_del(&item->list);
371 free(item);
372 }
373 }
374
tarerofs_apply_xattrs(struct erofs_inode * inode,struct list_head * xattrs)375 int tarerofs_apply_xattrs(struct erofs_inode *inode, struct list_head *xattrs)
376 {
377 struct tarerofs_xattr_item *item;
378 int ret;
379
380 list_for_each_entry(item, xattrs, list) {
381 const char *v = item->kv + item->namelen + 1;
382 unsigned int vsz = item->len - item->namelen - 1;
383
384 if (item->len <= item->namelen - 1) {
385 DBG_BUGON(item->len < item->namelen - 1);
386 continue;
387 }
388 item->kv[item->namelen] = '\0';
389 erofs_dbg("Recording xattr(%s)=\"%s\" (of %u bytes) to file %s",
390 item->kv, v, vsz, inode->i_srcpath);
391 ret = erofs_setxattr(inode, item->kv, v, vsz);
392 if (ret == -ENODATA)
393 erofs_err("Failed to set xattr(%s)=%s to file %s",
394 item->kv, v, inode->i_srcpath);
395 else if (ret)
396 return ret;
397 }
398 return 0;
399 }
400
401 static const char lookup_table[65] =
402 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
403
base64_decode(const char * src,int len,u8 * dst)404 static int base64_decode(const char *src, int len, u8 *dst)
405 {
406 int i, bits = 0, ac = 0;
407 const char *p;
408 u8 *cp = dst;
409
410 if(!(len % 4)) {
411 /* Check for and ignore any end padding */
412 if (src[len - 2] == '=' && src[len - 1] == '=')
413 len -= 2;
414 else if (src[len - 1] == '=')
415 --len;
416 }
417
418 for (i = 0; i < len; i++) {
419 p = strchr(lookup_table, src[i]);
420 if (p == NULL || src[i] == 0)
421 return -2;
422 ac += (p - lookup_table) << bits;
423 bits += 6;
424 if (bits >= 8) {
425 *cp++ = ac & 0xff;
426 ac >>= 8;
427 bits -= 8;
428 }
429 }
430 if (ac)
431 return -1;
432 return cp - dst;
433 }
434
tarerofs_parse_pax_header(struct erofs_iostream * ios,struct erofs_pax_header * eh,u32 size)435 int tarerofs_parse_pax_header(struct erofs_iostream *ios,
436 struct erofs_pax_header *eh, u32 size)
437 {
438 char *buf, *p;
439 int ret;
440
441 buf = malloc(size);
442 if (!buf)
443 return -ENOMEM;
444 p = buf;
445
446 ret = erofs_iostream_bread(ios, buf, size);
447 if (ret != size)
448 goto out;
449
450 while (p < buf + size) {
451 char *kv, *value;
452 int len, n;
453 /* extended records are of the format: "LEN NAME=VALUE\n" */
454 ret = sscanf(p, "%d %n", &len, &n);
455 if (ret < 1 || len <= n || len > buf + size - p) {
456 ret = -EIO;
457 goto out;
458 }
459 kv = p + n;
460 p += len;
461 len -= n;
462
463 if (p[-1] != '\n') {
464 ret = -EIO;
465 goto out;
466 }
467 p[-1] = '\0';
468
469 value = memchr(kv, '=', p - kv);
470 if (!value) {
471 ret = -EIO;
472 goto out;
473 } else {
474 long long lln;
475
476 value++;
477
478 if (!strncmp(kv, "path=", sizeof("path=") - 1)) {
479 int j = p - 1 - value;
480 free(eh->path);
481 eh->path = strdup(value);
482 while (eh->path[j - 1] == '/')
483 eh->path[--j] = '\0';
484 } else if (!strncmp(kv, "linkpath=",
485 sizeof("linkpath=") - 1)) {
486 free(eh->link);
487 eh->link = strdup(value);
488 } else if (!strncmp(kv, "mtime=",
489 sizeof("mtime=") - 1)) {
490 ret = sscanf(value, "%lld %n", &lln, &n);
491 if(ret < 1) {
492 ret = -EIO;
493 goto out;
494 }
495 eh->st.st_mtime = lln;
496 if (value[n] == '.') {
497 ret = sscanf(value + n + 1, "%d", &n);
498 if (ret < 1) {
499 ret = -EIO;
500 goto out;
501 }
502 ST_MTIM_NSEC_SET(&eh->st, n);
503 } else {
504 ST_MTIM_NSEC_SET(&eh->st, 0);
505 }
506 eh->use_mtime = true;
507 } else if (!strncmp(kv, "size=",
508 sizeof("size=") - 1)) {
509 ret = sscanf(value, "%lld %n", &lln, &n);
510 if(ret < 1 || value[n] != '\0') {
511 ret = -EIO;
512 goto out;
513 }
514 eh->st.st_size = lln;
515 eh->use_size = true;
516 } else if (!strncmp(kv, "uid=", sizeof("uid=") - 1)) {
517 ret = sscanf(value, "%lld %n", &lln, &n);
518 if(ret < 1 || value[n] != '\0') {
519 ret = -EIO;
520 goto out;
521 }
522 eh->st.st_uid = lln;
523 eh->use_uid = true;
524 } else if (!strncmp(kv, "gid=", sizeof("gid=") - 1)) {
525 ret = sscanf(value, "%lld %n", &lln, &n);
526 if(ret < 1 || value[n] != '\0') {
527 ret = -EIO;
528 goto out;
529 }
530 eh->st.st_gid = lln;
531 eh->use_gid = true;
532 } else if (!strncmp(kv, "SCHILY.xattr.",
533 sizeof("SCHILY.xattr.") - 1)) {
534 char *key = kv + sizeof("SCHILY.xattr.") - 1;
535
536 --len; /* p[-1] == '\0' */
537 ret = tarerofs_insert_xattr(&eh->xattrs, key,
538 value - key - 1,
539 len - (key - kv), false);
540 if (ret)
541 goto out;
542 } else if (!strncmp(kv, "LIBARCHIVE.xattr.",
543 sizeof("LIBARCHIVE.xattr.") - 1)) {
544 char *key;
545 key = kv + sizeof("LIBARCHIVE.xattr.") - 1;
546
547 --len; /* p[-1] == '\0' */
548 ret = base64_decode(value, len - (value - kv),
549 (u8 *)value);
550 if (ret < 0) {
551 ret = -EFSCORRUPTED;
552 goto out;
553 }
554
555 ret = tarerofs_insert_xattr(&eh->xattrs, key,
556 value - key - 1,
557 value - key + ret, false);
558 if (ret)
559 goto out;
560 } else {
561 erofs_info("unrecognized pax keyword \"%s\", ignoring", kv);
562 }
563 }
564 }
565 ret = 0;
566 out:
567 free(buf);
568 return ret;
569 }
570
tarerofs_remove_inode(struct erofs_inode * inode)571 void tarerofs_remove_inode(struct erofs_inode *inode)
572 {
573 struct erofs_dentry *d;
574
575 --inode->i_nlink;
576 if (!S_ISDIR(inode->i_mode))
577 return;
578
579 /* remove all subdirss */
580 list_for_each_entry(d, &inode->i_subdirs, d_child) {
581 if (!is_dot_dotdot(d->name))
582 tarerofs_remove_inode(d->inode);
583 erofs_iput(d->inode);
584 d->inode = NULL;
585 }
586 --inode->i_parent->i_nlink;
587 }
588
tarerofs_write_uncompressed_file(struct erofs_inode * inode,struct erofs_tarfile * tar)589 static int tarerofs_write_uncompressed_file(struct erofs_inode *inode,
590 struct erofs_tarfile *tar)
591 {
592 struct erofs_sb_info *sbi = inode->sbi;
593 erofs_blk_t nblocks;
594 erofs_off_t pos;
595 void *buf;
596 int ret;
597
598 inode->datalayout = EROFS_INODE_FLAT_PLAIN;
599 nblocks = DIV_ROUND_UP(inode->i_size, 1U << sbi->blkszbits);
600
601 ret = erofs_allocate_inode_bh_data(inode, nblocks);
602 if (ret)
603 return ret;
604
605 for (pos = 0; pos < inode->i_size; pos += ret) {
606 ret = erofs_iostream_read(&tar->ios, &buf, inode->i_size - pos);
607 if (ret < 0)
608 break;
609 if (erofs_dev_write(sbi, buf,
610 erofs_pos(sbi, inode->u.i_blkaddr) + pos,
611 ret)) {
612 ret = -EIO;
613 break;
614 }
615 }
616 inode->idata_size = 0;
617 inode->datasource = EROFS_INODE_DATA_SOURCE_NONE;
618 return 0;
619 }
620
tarerofs_write_file_data(struct erofs_inode * inode,struct erofs_tarfile * tar)621 static int tarerofs_write_file_data(struct erofs_inode *inode,
622 struct erofs_tarfile *tar)
623 {
624 void *buf;
625 int fd, nread;
626 u64 off, j;
627
628 if (!inode->i_diskbuf) {
629 inode->i_diskbuf = calloc(1, sizeof(*inode->i_diskbuf));
630 if (!inode->i_diskbuf)
631 return -ENOSPC;
632 } else {
633 erofs_diskbuf_close(inode->i_diskbuf);
634 }
635
636 fd = erofs_diskbuf_reserve(inode->i_diskbuf, 0, &off);
637 if (fd < 0)
638 return -EBADF;
639
640 for (j = inode->i_size; j; ) {
641 nread = erofs_iostream_read(&tar->ios, &buf, j);
642 if (nread < 0)
643 break;
644 if (write(fd, buf, nread) != nread) {
645 nread = -EIO;
646 break;
647 }
648 j -= nread;
649 }
650 erofs_diskbuf_commit(inode->i_diskbuf, inode->i_size);
651 inode->datasource = EROFS_INODE_DATA_SOURCE_DISKBUF;
652 return 0;
653 }
654
tarerofs_parse_tar(struct erofs_inode * root,struct erofs_tarfile * tar)655 int tarerofs_parse_tar(struct erofs_inode *root, struct erofs_tarfile *tar)
656 {
657 char path[PATH_MAX];
658 struct erofs_pax_header eh = tar->global;
659 struct erofs_sb_info *sbi = root->sbi;
660 bool whout, opq, e = false;
661 struct stat st;
662 erofs_off_t tar_offset, dataoff;
663
664 struct tar_header *th;
665 struct erofs_dentry *d;
666 struct erofs_inode *inode;
667 unsigned int j, csum, cksum;
668 int ckksum, ret, rem;
669
670 root->dev = tar->dev;
671 if (eh.path)
672 eh.path = strdup(eh.path);
673 if (eh.link)
674 eh.link = strdup(eh.link);
675 init_list_head(&eh.xattrs);
676
677 restart:
678 rem = tar->offset & 511;
679 if (rem) {
680 if (erofs_iostream_lskip(&tar->ios, 512 - rem)) {
681 ret = -EIO;
682 goto out;
683 }
684 tar->offset += 512 - rem;
685 }
686
687 tar_offset = tar->offset;
688 ret = erofs_iostream_read(&tar->ios, (void **)&th, sizeof(*th));
689 if (ret != sizeof(*th)) {
690 if (tar->headeronly_mode || tar->ddtaridx_mode) {
691 ret = 1;
692 goto out;
693 }
694 erofs_err("failed to read header block @ %llu", tar_offset);
695 ret = -EIO;
696 goto out;
697 }
698 tar->offset += sizeof(*th);
699
700 /* chksum field itself treated as ' ' */
701 csum = tarerofs_otoi(th->chksum, sizeof(th->chksum));
702 if (errno) {
703 if (*th->name == '\0') {
704 out_eot:
705 if (e) { /* end of tar 2 empty blocks */
706 ret = 1;
707 goto out;
708 }
709 e = true; /* empty jump to next block */
710 goto restart;
711 }
712 erofs_err("invalid chksum @ %llu", tar_offset);
713 ret = -EBADMSG;
714 goto out;
715 }
716 cksum = 0;
717 for (j = 0; j < 8; ++j)
718 cksum += (unsigned int)' ';
719 ckksum = cksum;
720 for (j = 0; j < 148; ++j) {
721 cksum += (unsigned int)((u8*)th)[j];
722 ckksum += (int)((char*)th)[j];
723 }
724 for (j = 156; j < 500; ++j) {
725 cksum += (unsigned int)((u8*)th)[j];
726 ckksum += (int)((char*)th)[j];
727 }
728 if (!tar->ddtaridx_mode && csum != cksum && csum != ckksum) {
729 /* should not bail out here, just in case */
730 if (*th->name == '\0') {
731 DBG_BUGON(1);
732 goto out_eot;
733 }
734 erofs_err("chksum mismatch @ %llu", tar_offset);
735 ret = -EBADMSG;
736 goto out;
737 }
738
739 if (th->typeflag == GNUTYPE_VOLHDR) {
740 if (th->size[0])
741 erofs_warn("GNUTYPE_VOLHDR with non-zeroed size @ %llu",
742 tar_offset);
743 /* anyway, strncpy could cause some GCC warning here */
744 memcpy(sbi->volume_name, th->name, sizeof(sbi->volume_name));
745 goto restart;
746 }
747
748 if (memcmp(th->magic, "ustar", 5)) {
749 erofs_err("invalid tar magic @ %llu", tar_offset);
750 ret = -EIO;
751 goto out;
752 }
753
754 st.st_mode = tarerofs_otoi(th->mode, sizeof(th->mode));
755 if (errno)
756 goto invalid_tar;
757
758 if (eh.use_uid) {
759 st.st_uid = eh.st.st_uid;
760 } else {
761 st.st_uid = tarerofs_parsenum(th->uid, sizeof(th->uid));
762 if (errno)
763 goto invalid_tar;
764 }
765
766 if (eh.use_gid) {
767 st.st_gid = eh.st.st_gid;
768 } else {
769 st.st_gid = tarerofs_parsenum(th->gid, sizeof(th->gid));
770 if (errno)
771 goto invalid_tar;
772 }
773
774 if (eh.use_size) {
775 st.st_size = eh.st.st_size;
776 } else {
777 st.st_size = tarerofs_parsenum(th->size, sizeof(th->size));
778 if (errno)
779 goto invalid_tar;
780 }
781
782 if (eh.use_mtime) {
783 st.st_mtime = eh.st.st_mtime;
784 ST_MTIM_NSEC_SET(&st, ST_MTIM_NSEC(&eh.st));
785 } else {
786 st.st_mtime = tarerofs_parsenum(th->mtime, sizeof(th->mtime));
787 if (errno)
788 goto invalid_tar;
789 ST_MTIM_NSEC_SET(&st, 0);
790 }
791
792 if (th->typeflag <= '7' && !eh.path) {
793 eh.path = path;
794 j = 0;
795 if (*th->prefix) {
796 memcpy(path, th->prefix, sizeof(th->prefix));
797 path[sizeof(th->prefix)] = '\0';
798 j = strlen(path);
799 if (path[j - 1] != '/') {
800 path[j] = '/';
801 path[++j] = '\0';
802 }
803 }
804 memcpy(path + j, th->name, sizeof(th->name));
805 path[j + sizeof(th->name)] = '\0';
806 j = strlen(path);
807 while (path[j - 1] == '/')
808 path[--j] = '\0';
809 }
810
811 dataoff = tar->offset;
812 tar->offset += st.st_size;
813 switch(th->typeflag) {
814 case '0':
815 case '7':
816 case '1':
817 st.st_mode |= S_IFREG;
818 if (tar->headeronly_mode || tar->ddtaridx_mode)
819 tar->offset -= st.st_size;
820 break;
821 case '2':
822 st.st_mode |= S_IFLNK;
823 break;
824 case '3':
825 st.st_mode |= S_IFCHR;
826 break;
827 case '4':
828 st.st_mode |= S_IFBLK;
829 break;
830 case '5':
831 st.st_mode |= S_IFDIR;
832 break;
833 case '6':
834 st.st_mode |= S_IFIFO;
835 break;
836 case 'g':
837 ret = tarerofs_parse_pax_header(&tar->ios, &tar->global,
838 st.st_size);
839 if (ret)
840 goto out;
841 if (tar->global.path) {
842 free(eh.path);
843 eh.path = strdup(tar->global.path);
844 }
845 if (tar->global.link) {
846 free(eh.link);
847 eh.link = strdup(tar->global.link);
848 }
849 goto restart;
850 case 'x':
851 ret = tarerofs_parse_pax_header(&tar->ios, &eh, st.st_size);
852 if (ret)
853 goto out;
854 goto restart;
855 case 'L':
856 free(eh.path);
857 eh.path = malloc(st.st_size + 1);
858 if (st.st_size != erofs_iostream_bread(&tar->ios, eh.path,
859 st.st_size))
860 goto invalid_tar;
861 eh.path[st.st_size] = '\0';
862 goto restart;
863 case 'K':
864 free(eh.link);
865 eh.link = malloc(st.st_size + 1);
866 if (st.st_size > PATH_MAX || st.st_size !=
867 erofs_iostream_bread(&tar->ios, eh.link, st.st_size))
868 goto invalid_tar;
869 eh.link[st.st_size] = '\0';
870 goto restart;
871 default:
872 erofs_info("unrecognized typeflag %xh @ %llu - ignoring",
873 th->typeflag, tar_offset);
874 (void)erofs_iostream_lskip(&tar->ios, st.st_size);
875 ret = 0;
876 goto out;
877 }
878
879 st.st_rdev = 0;
880 if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
881 int major, minor;
882
883 major = tarerofs_parsenum(th->devmajor, sizeof(th->devmajor));
884 if (errno) {
885 erofs_err("invalid device major @ %llu", tar_offset);
886 goto out;
887 }
888
889 minor = tarerofs_parsenum(th->devminor, sizeof(th->devminor));
890 if (errno) {
891 erofs_err("invalid device minor @ %llu", tar_offset);
892 goto out;
893 }
894
895 st.st_rdev = (major << 8) | (minor & 0xff) | ((minor & ~0xff) << 12);
896 } else if (th->typeflag == '1' || th->typeflag == '2') {
897 if (!eh.link)
898 eh.link = strndup(th->linkname, sizeof(th->linkname));
899 }
900
901 /* EROFS metadata index referring to the original tar data */
902 if (tar->index_mode && sbi->extra_devices &&
903 erofs_blkoff(sbi, dataoff)) {
904 erofs_err("invalid tar data alignment @ %llu", tar_offset);
905 ret = -EIO;
906 goto out;
907 }
908
909 erofs_dbg("parsing %s (mode %05o)", eh.path, st.st_mode);
910
911 d = erofs_rebuild_get_dentry(root, eh.path, tar->aufs, &whout, &opq, true);
912 if (IS_ERR(d)) {
913 ret = PTR_ERR(d);
914 goto out;
915 }
916
917 if (!d) {
918 /* some tarballs include '.' which indicates the root directory */
919 if (!S_ISDIR(st.st_mode)) {
920 ret = -ENOTDIR;
921 goto out;
922 }
923 inode = root;
924 } else if (opq) {
925 DBG_BUGON(d->type == EROFS_FT_UNKNOWN);
926 DBG_BUGON(!d->inode);
927 /*
928 * needed if the tar tree is used soon, thus we have no chance
929 * to generate it from xattrs. No impact to mergefs.
930 */
931 d->inode->opaque = true;
932 ret = erofs_set_opaque_xattr(d->inode);
933 goto out;
934 } else if (th->typeflag == '1') { /* hard link cases */
935 struct erofs_dentry *d2;
936 bool dumb;
937
938 if (S_ISDIR(st.st_mode)) {
939 ret = -EISDIR;
940 goto out;
941 }
942
943 if (d->type != EROFS_FT_UNKNOWN) {
944 tarerofs_remove_inode(d->inode);
945 erofs_iput(d->inode);
946 }
947 d->inode = NULL;
948
949 d2 = erofs_rebuild_get_dentry(root, eh.link, tar->aufs,
950 &dumb, &dumb, false);
951 if (IS_ERR(d2)) {
952 ret = PTR_ERR(d2);
953 goto out;
954 }
955 if (d2->type == EROFS_FT_UNKNOWN) {
956 ret = -ENOENT;
957 goto out;
958 }
959 if (S_ISDIR(d2->inode->i_mode)) {
960 ret = -EISDIR;
961 goto out;
962 }
963 inode = erofs_igrab(d2->inode);
964 d->inode = inode;
965 d->type = d2->type;
966 ++inode->i_nlink;
967 ret = 0;
968 goto out;
969 } else if (d->type != EROFS_FT_UNKNOWN) {
970 if (d->type != EROFS_FT_DIR || !S_ISDIR(st.st_mode)) {
971 struct erofs_inode *parent = d->inode->i_parent;
972
973 tarerofs_remove_inode(d->inode);
974 erofs_iput(d->inode);
975 d->inode = parent;
976 goto new_inode;
977 }
978 inode = d->inode;
979 } else {
980 new_inode:
981 inode = erofs_new_inode(sbi);
982 if (IS_ERR(inode)) {
983 ret = PTR_ERR(inode);
984 goto out;
985 }
986 inode->dev = tar->dev;
987 inode->i_parent = d->inode;
988 d->inode = inode;
989 d->type = erofs_mode_to_ftype(st.st_mode);
990 }
991
992 if (whout) {
993 inode->i_mode = (inode->i_mode & ~S_IFMT) | S_IFCHR;
994 inode->u.i_rdev = EROFS_WHITEOUT_DEV;
995 d->type = EROFS_FT_CHRDEV;
996
997 /*
998 * Mark the parent directory as copied-up to avoid exposing
999 * whiteouts if mounted. See kernel commit b79e05aaa166
1000 * ("ovl: no direct iteration for dir with origin xattr")
1001 */
1002 inode->i_parent->whiteouts = true;
1003 } else {
1004 inode->i_mode = st.st_mode;
1005 if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
1006 inode->u.i_rdev = erofs_new_encode_dev(st.st_rdev);
1007 }
1008
1009 inode->i_srcpath = strdup(eh.path);
1010 if (!inode->i_srcpath) {
1011 ret = -ENOMEM;
1012 goto out;
1013 }
1014
1015 ret = __erofs_fill_inode(inode, &st, eh.path);
1016 if (ret)
1017 goto out;
1018 inode->i_size = st.st_size;
1019
1020 if (!S_ISDIR(inode->i_mode)) {
1021 if (S_ISLNK(inode->i_mode)) {
1022 inode->i_size = strlen(eh.link);
1023 inode->i_link = malloc(inode->i_size + 1);
1024 memcpy(inode->i_link, eh.link, inode->i_size + 1);
1025 } else if (inode->i_size) {
1026 if (tar->headeronly_mode) {
1027 ret = erofs_write_zero_inode(inode);
1028 } else if (tar->ddtaridx_mode) {
1029 dataoff = le64_to_cpu(*(__le64 *)(th->devmajor));
1030 if (tar->rvsp_mode) {
1031 inode->datasource = EROFS_INODE_DATA_SOURCE_RESVSP;
1032 inode->i_ino[1] = dataoff;
1033 ret = 0;
1034 } else {
1035 ret = tarerofs_write_chunkes(inode, dataoff);
1036 }
1037 } else if (tar->rvsp_mode) {
1038 inode->datasource = EROFS_INODE_DATA_SOURCE_RESVSP;
1039 inode->i_ino[1] = dataoff;
1040 if (erofs_iostream_lskip(&tar->ios, inode->i_size))
1041 ret = -EIO;
1042 else
1043 ret = 0;
1044 } else if (tar->index_mode) {
1045 ret = tarerofs_write_chunkes(inode, dataoff);
1046 if (!ret && erofs_iostream_lskip(&tar->ios,
1047 inode->i_size))
1048 ret = -EIO;
1049 } else if (tar->try_no_reorder &&
1050 !cfg.c_compr_opts[0].alg &&
1051 !cfg.c_inline_data) {
1052 ret = tarerofs_write_uncompressed_file(inode, tar);
1053 } else {
1054 ret = tarerofs_write_file_data(inode, tar);
1055 }
1056 if (ret)
1057 goto out;
1058 }
1059 inode->i_nlink++;
1060 } else if (!inode->i_nlink) {
1061 ret = erofs_init_empty_dir(inode);
1062 if (ret)
1063 goto out;
1064 }
1065
1066 ret = tarerofs_merge_xattrs(&eh.xattrs, &tar->global.xattrs);
1067 if (ret)
1068 goto out;
1069
1070 ret = tarerofs_apply_xattrs(inode, &eh.xattrs);
1071
1072 out:
1073 if (eh.path != path)
1074 free(eh.path);
1075 free(eh.link);
1076 tarerofs_remove_xattrs(&eh.xattrs);
1077 return ret;
1078
1079 invalid_tar:
1080 erofs_err("invalid tar @ %llu", tar_offset);
1081 ret = -EIO;
1082 goto out;
1083 }
1084