18971c
From a3dc6a721873f0365612a5acee952a5f7d33853a Mon Sep 17 00:00:00 2001
18971c
From: Harald Hoyer <harald@redhat.com>
18971c
Date: Fri, 24 Jan 2014 15:27:51 +0100
18971c
Subject: [PATCH] add skipcpio utility
18971c
18971c
skipcpio skips a cpio archive at the beginning of a file.
18971c
18971c
It is used for skipping an early cpio archive for lsinitrd.
18971c
---
18971c
 Makefile            |  13 ++++-
18971c
 skipcpio/skipcpio.c | 123 ++++++++++++++++++++++++++++++++++++++++++++
18971c
 2 files changed, 135 insertions(+), 1 deletion(-)
18971c
 create mode 100644 skipcpio/skipcpio.c
18971c
18971c
diff --git a/Makefile b/Makefile
18971c
index e7da9484..5441d8f4 100644
18971c
--- a/Makefile
18971c
+++ b/Makefile
18971c
@@ -40,7 +40,7 @@ manpages = $(man1pages) $(man5pages) $(man7pages) $(man8pages)
18971c
 
18971c
 .PHONY: install clean archive rpm testimage test all check AUTHORS doc dracut-version.sh
18971c
 
18971c
-all: dracut-version.sh dracut-install
18971c
+all: dracut-version.sh dracut-install skipcpio/skipcpio
18971c
 
18971c
 DRACUT_INSTALL_OBJECTS = \
18971c
         install/dracut-install.o \
18971c
@@ -61,8 +61,15 @@ install/dracut-install: $(DRACUT_INSTALL_OBJECTS)
18971c
 dracut-install: install/dracut-install
18971c
 	ln -fs $< $@
18971c
 
18971c
+SKIPCPIO_OBJECTS= \
18971c
+	skipcpio/skipcpio.o
18971c
+
18971c
+skipcpio/skipcpio.o: skipcpio/skipcpio.c
18971c
+skipcpio/skipcpio: skipcpio/skipcpio.o
18971c
+
18971c
 indent:
18971c
 	indent -i8 -nut -br -linux -l120 install/dracut-install.c
18971c
+	indent -i8 -nut -br -linux -l120 skipcpio/skipcpio.c
18971c
 
18971c
 doc: $(manpages) dracut.html
18971c
 
18971c
@@ -136,6 +143,9 @@ endif
18971c
 	if [ -f install/dracut-install ]; then \
18971c
 		install -m 0755 install/dracut-install $(DESTDIR)$(pkglibdir)/dracut-install; \
18971c
 	fi
18971c
+	if [ -f skipcpio/skipcpio ]; then \
18971c
+		install -m 0755 skipcpio/skipcpio $(DESTDIR)$(pkglibdir)/skipcpio; \
18971c
+	fi
18971c
 	mkdir -p $(DESTDIR)${prefix}/lib/kernel/install.d
18971c
 	install -m 0755 50-dracut.install $(DESTDIR)${prefix}/lib/kernel/install.d/50-dracut.install
18971c
 	install -m 0755 51-dracut-rescue.install $(DESTDIR)${prefix}/lib/kernel/install.d/51-dracut-rescue.install
18971c
@@ -155,6 +165,7 @@ clean:
18971c
 	$(RM) dracut-*.rpm dracut-*.tar.bz2
18971c
 	$(RM) dracut-version.sh
18971c
 	$(RM) dracut-install install/dracut-install $(DRACUT_INSTALL_OBJECTS)
18971c
+	$(RM) skipcpio/skipcpio $(SKIPCPIO_OBJECTS)
18971c
 	$(RM) $(manpages) dracut.html
18971c
 	$(MAKE) -C test clean
18971c
 
18971c
diff --git a/skipcpio/skipcpio.c b/skipcpio/skipcpio.c
18971c
new file mode 100644
18971c
index 00000000..fbf391bf
18971c
--- /dev/null
18971c
+++ b/skipcpio/skipcpio.c
18971c
@@ -0,0 +1,123 @@
18971c
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
18971c
+
18971c
+/* dracut-install.c  -- install files and executables
18971c
+
18971c
+   Copyright (C) 2012 Harald Hoyer
18971c
+   Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
18971c
+
18971c
+   This program is free software: you can redistribute it and/or modify
18971c
+   under the terms of the GNU Lesser General Public License as published by
18971c
+   the Free Software Foundation; either version 2.1 of the License, or
18971c
+   (at your option) any later version.
18971c
+
18971c
+   This program is distributed in the hope that it will be useful, but
18971c
+   WITHOUT ANY WARRANTY; without even the implied warranty of
18971c
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18971c
+   Lesser General Public License for more details.
18971c
+
18971c
+   You should have received a copy of the GNU Lesser General Public License
18971c
+   along with this program; If not, see <http://www.gnu.org/licenses/>.
18971c
+*/
18971c
+
18971c
+#define PROGRAM_VERSION_STRING "1"
18971c
+
18971c
+#ifndef _GNU_SOURCE
18971c
+#define _GNU_SOURCE
18971c
+#endif
18971c
+
18971c
+#include <stdbool.h>
18971c
+#include <stdio.h>
18971c
+#include <stdlib.h>
18971c
+#include <unistd.h>
18971c
+#include <string.h>
18971c
+
18971c
+#define CPIO_END "TRAILER!!!"
18971c
+#define CPIO_ENDLEN (sizeof(CPIO_END)-1)
18971c
+
18971c
+static char buf[CPIO_ENDLEN * 2 + 1];
18971c
+
18971c
+int main(int argc, char **argv)
18971c
+{
18971c
+        FILE *f;
18971c
+        size_t s;
18971c
+        long pos = 0;
18971c
+
18971c
+        if (argc != 2) {
18971c
+                fprintf(stderr, "Usage: %s <file>\n", argv[0]);
18971c
+                exit(1);
18971c
+        }
18971c
+
18971c
+        f = fopen(argv[1], "r");
18971c
+
18971c
+        if (f == NULL) {
18971c
+                fprintf(stderr, "Cannot open file '%s'\n", argv[1]);
18971c
+                exit(1);
18971c
+        }
18971c
+
18971c
+        s = fread(buf, 6, 1, f);
18971c
+        if (s <= 0) {
18971c
+                fprintf(stderr, "Read error from file '%s'\n", argv[1]);
18971c
+                fclose(f);
18971c
+                exit(1);
18971c
+        }
18971c
+        fseek(f, 0, SEEK_SET);
18971c
+
18971c
+        /* check, if this is a cpio archive */
18971c
+        if ((buf[0] == 0x71 && buf[1] == 0xc7)
18971c
+            || (buf[0] == '0' && buf[1] == '7' && buf[0] == '0' && buf[1] == '7' && buf[0] == '0' && buf[1] == '1')) {
18971c
+
18971c
+                /* Search for CPIO_END */
18971c
+                do {
18971c
+                        char *h;
18971c
+                        fseek(f, pos, SEEK_SET);
18971c
+                        buf[sizeof(buf) - 1] = 0;
18971c
+                        s = fread(buf, CPIO_ENDLEN, 2, f);
18971c
+                        if (s <= 0)
18971c
+                                break;
18971c
+
18971c
+                        h = strstr(buf, CPIO_END);
18971c
+                        if (h) {
18971c
+                                pos = (h - buf) + pos + CPIO_ENDLEN;
18971c
+                                fseek(f, pos, SEEK_SET);
18971c
+                                break;
18971c
+                        }
18971c
+                        pos += CPIO_ENDLEN;
18971c
+                } while (!feof(f));
18971c
+
18971c
+                if (feof(f)) {
18971c
+                        /* CPIO_END not found, just cat the whole file */
18971c
+                        fseek(f, 0, SEEK_SET);
18971c
+                } else {
18971c
+                        /* skip zeros */
18971c
+                        while (!feof(f)) {
18971c
+                                size_t i;
18971c
+
18971c
+                                buf[sizeof(buf) - 1] = 0;
18971c
+                                s = fread(buf, 1, sizeof(buf) - 1, f);
18971c
+                                if (s <= 0)
18971c
+                                        break;
18971c
+
18971c
+                                for (i = 0; (i < s) && (buf[i] == 0); i++) ;
18971c
+
18971c
+                                if (buf[i] != 0) {
18971c
+                                        pos += i;
18971c
+                                        fseek(f, pos, SEEK_SET);
18971c
+                                        break;
18971c
+                                }
18971c
+
18971c
+                                pos += s;
18971c
+                        }
18971c
+                }
18971c
+        }
18971c
+        /* cat out the rest */
18971c
+        while (!feof(f)) {
18971c
+                s = fread(buf, 1, sizeof(buf), f);
18971c
+                if (s <= 0)
18971c
+                        break;
18971c
+
18971c
+                s = fwrite(buf, 1, s, stdout);
18971c
+                if (s <= 0)
18971c
+                        break;
18971c
+        }
18971c
+        fclose(f);
18971c
+}