]>
git.ipfire.org Git - thirdparty/dracut.git/blob - skipcpio/skipcpio.c
1 /* dracut-install.c -- install files and executables
3 Copyright (C) 2012 Harald Hoyer
4 Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 This program is free software: you can redistribute it and/or modify
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program; If not, see <http://www.gnu.org/licenses/>.
20 #define PROGRAM_VERSION_STRING "1"
30 #define CPIO_MAGIC "070701"
31 #define CPIO_END "TRAILER!!!"
32 #define CPIO_ENDLEN (sizeof(CPIO_END) - 1)
34 #define CPIO_ALIGNMENT 4
51 } __attribute__((packed
));
55 char filename
[CPIO_ENDLEN
];
56 } __attribute__((packed
));
59 struct buf_struct cpio
;
60 char copy_buffer
[2048];
63 static union buf_union buf
;
65 #define ALIGN_UP(n, a) (((n) + (a) - 1) & (~((a) - 1)))
67 int main(int argc
, char **argv
)
73 fprintf(stderr
, "Usage: %s <file>\n", argv
[0]);
77 f
= fopen(argv
[1], "r");
80 fprintf(stderr
, "Cannot open file '%s'\n", argv
[1]);
84 s
= fread(&buf
.cpio
, sizeof(buf
.cpio
), 1, f
);
86 fprintf(stderr
, "Read error from file '%s'\n", argv
[1]);
90 fseek(f
, 0, SEEK_SET
);
92 /* check, if this is a cpio archive */
93 if (memcmp(buf
.cpio
.h
.c_magic
, CPIO_MAGIC
, 6) == 0) {
97 unsigned long filesize
;
98 unsigned long filename_length
;
101 // zero string, spilling into next unused field, to use strtol
102 buf
.cpio
.h
.c_chksum
[0] = 0;
103 filename_length
= strtoul(buf
.cpio
.h
.c_namesize
, NULL
, 16);
104 pos
= ALIGN_UP(pos
+ sizeof(struct cpio_header
) + filename_length
, CPIO_ALIGNMENT
);
106 // zero string, spilling into next unused field, to use strtol
107 buf
.cpio
.h
.c_dev_maj
[0] = 0;
108 filesize
= strtoul(buf
.cpio
.h
.c_filesize
, NULL
, 16);
109 pos
= ALIGN_UP(pos
+ filesize
, CPIO_ALIGNMENT
);
111 if (filename_length
== (CPIO_ENDLEN
+ 1)
112 && strncmp(buf
.cpio
.filename
, CPIO_END
, CPIO_ENDLEN
) == 0) {
113 fseek(f
, pos
, SEEK_SET
);
117 if (fseek(f
, pos
, SEEK_SET
) != 0) {
122 if (fread(&buf
.cpio
, sizeof(buf
.cpio
), 1, f
) != 1) {
127 if (memcmp(buf
.cpio
.h
.c_magic
, CPIO_MAGIC
, 6) != 0) {
128 fprintf(stderr
, "Corrupt CPIO archive!\n");
134 /* CPIO_END not found, just cat the whole file */
135 fseek(f
, 0, SEEK_SET
);
141 s
= fread(buf
.copy_buffer
, 1, sizeof(buf
.copy_buffer
) - 1, f
);
145 for (i
= 0; (i
< s
) && (buf
.copy_buffer
[i
] == 0); i
++) ;
147 if (buf
.copy_buffer
[i
] != 0) {
150 fseek(f
, pos
, SEEK_SET
);
158 /* cat out the rest */
160 s
= fread(buf
.copy_buffer
, 1, sizeof(buf
.copy_buffer
), f
);
164 s
= fwrite(buf
.copy_buffer
, 1, s
, stdout
);