[Toybox] [PATCH 2/2] pending/file: add cpio, tar, zip formats.
Isaac Dunham
ibid.ag at gmail.com
Wed Feb 17 15:08:55 PST 2016
---
toys/pending/file.c | 29 ++++++++++++++++++++++++++---
1 file changed, 26 insertions(+), 3 deletions(-)
diff --git a/toys/pending/file.c b/toys/pending/file.c
index 1d09471..4d5d14c 100644
--- a/toys/pending/file.c
+++ b/toys/pending/file.c
@@ -166,9 +166,32 @@ static void do_regular_file(int fd, char *name)
xprintf("Java class file, version %d.%d\n",
(int)peek_be(s+6, 2), (int)peek_be(s, 2));
- // TODO: cpio archive.
- // TODO: tar archive.
- // TODO: zip/jar/apk archive.
+ // https://people.freebsd.org/~kientzle/libarchive/man/cpio.5.txt
+ // the lengths for cpio are size of header + 9 bytes, since any valid
+ // cpio archive ends with a record for "TARGET!!!"
+ else if (len>85 && strstart(&s, "07070")) {
+ char *cpioformat = "unknown type";
+ if (toybuf[5] == '7') cpioformat = "pre-SVR4 or odc";
+ else if (toybuf[5] == '1') cpioformat = "SVR4 with no CRC";
+ else if (toybuf[5] == '2') cpioformat = "SVR4 with CRC";
+ xprintf("ASCII cpio archive (%s)\n", cpioformat);
+ }
+ else if (len>33 && (peek(&s,2)==0143561 || peek(&s,2)==070707)) {
+ if (peek(&s, 2) == 0143561) printf("byte-swapped ");
+ xprintf("cpio archive\n");
+ }
+ // tar archive (ustar/pax or gnu)
+ else if (len>500 && !strncmp(s+257, "ustar", 5)) {
+ xprintf("POSIX tar archive%s\n", strncmp(s+262," ",2)?"":" (GNU)");
+ }
+ // zip/jar/apk archive, ODF/OOXML document, or such
+ else if (len>5 && strstart(&s, "PK\03\04")) {
+ int ver = (int)(char)(toybuf[4]);
+ xprintf("Zip archive data");
+ if (ver)
+ xprintf(", requires at least v%d.%d to extract", ver/10, ver%10);
+ xputc('\n');
+ }
else {
char *what = 0;
int i, bytes;
--
2.7.1
1455750535.0
More information about the Toybox
mailing list