summaryrefslogtreecommitdiff
path: root/tar/skip.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-13 17:16:06 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-06-15 17:43:42 +0200
commite1a47443ad2ee424b2be9c2d4f2761dfe85ded1a (patch)
tree1c8aec5af456820f2fa236355ffa2d5ffc31f04e /tar/skip.c
parent46b30ac7559ca26f5446b05d852eaaefe432a290 (diff)
Add utility to turn a POSIX/PAX tar archive into a squashfs image
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tar/skip.c')
-rw-r--r--tar/skip.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/tar/skip.c b/tar/skip.c
new file mode 100644
index 0000000..69208b2
--- /dev/null
+++ b/tar/skip.c
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+#include "util.h"
+#include "tar.h"
+
+#include <stdio.h>
+
+static int skip_bytes(int fd, uint64_t size)
+{
+ unsigned char buffer[1024];
+ ssize_t ret;
+ size_t diff;
+
+ while (size != 0) {
+ diff = sizeof(buffer);
+ if (diff > size)
+ diff = size;
+
+ ret = read_retry(fd, buffer, diff);
+
+ if (ret < 0) {
+ perror("reading tar record data");
+ return -1;
+ }
+
+ if ((size_t)ret < diff) {
+ fputs("unexpected end of file\n", stderr);
+ return -1;
+ }
+
+ size -= diff;
+ }
+
+ return 0;
+}
+
+int skip_padding(int fd, uint64_t size)
+{
+ size_t tail = size % 512;
+
+ return tail ? skip_bytes(fd, 512 - tail) : 0;
+}
+
+int skip_entry(int fd, uint64_t size)
+{
+ size_t tail = size % 512;
+
+ return skip_bytes(fd, tail ? (size + 512 - tail) : size);
+}