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