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