[Toybox] [PATCH] cpio: allow -it and fix two bugs in -o

Isaac Dunham ibid.ag at gmail.com
Tue Mar 25 23:30:59 PDT 2014


There are a few rough spots in the current cpio -o code.

* Contrary to the comments, FLAG_o is 2. The simplest approach is to
 make the code match the comments.

* nlen is equal to len+1, and must always be set after getline() and
 further processing. (Using len++ instead may be better, long-term.)
 Fixes a segfault.

* Check nlen instead of llen when padding is 4 - (namelen % 4)

* Add another zero to the padding for the TRAILER!!! entry.
Note: xwrite(afd,  toybuf, sprintf(toybuf, string,...);
could be replaced with dprinf(afd, string, ...); xwrite(afd, &zero, 4);

This patch fixes these.

-------------- next part --------------
diff --git a/toys/pending/cpio.c b/toys/pending/cpio.c
index d1fd8a1..ce5d455 100644
--- a/toys/pending/cpio.c
+++ b/toys/pending/cpio.c
@@ -13,7 +13,7 @@
  * In order: magic ino mode uid gid nlink mtime filesize devmajor devminor
  * rdevmajor rdevminor namesize check
 
-USE_CPIO(NEWTOY(cpio, "duH:i|t|F:o|v(verbose)[!io][!ot]", TOYFLAG_BIN))
+USE_CPIO(NEWTOY(cpio, "duH:i|t|F:v(verbose)o|[!io][!ot]", TOYFLAG_BIN))
 
 config CPIO
   bool "cpio"
@@ -156,13 +156,14 @@ void cpio_main(void)
 
     for (;;) {
       struct stat st;
-      unsigned nlen = strlen(name)+1, error = 0, zero = 0;
+      unsigned nlen, error = 0, zero = 0;
       int len, fd;
       ssize_t llen;
 
       len = getline(&name, &size, stdin);
       if (len<1) break;
       if (name[len-1] == '\n') name[--len] = 0;
+      nlen = len+1;
       if (lstat(name, &st) || (fd = open(name, O_RDONLY))<0) {
         perror_msg("%s", name);
         continue;
@@ -197,14 +198,14 @@ void cpio_main(void)
           xwrite(afd, toybuf, nlen);
         }
         llen = st.st_size & 3;
-        if (nlen) write(afd, &zero, 4-llen);
+        if (llen) write(afd, &zero, 4-llen);
       }
       close(fd);
     }
     free(name);
 
     xwrite(afd, toybuf,
-      sprintf(toybuf, "070701%040X%056X%08XTRAILER!!!%c%c%c",
-              1, 0x0b, 0, 0, 0, 0));
+      sprintf(toybuf, "070701%040X%056X%08XTRAILER!!!%c%c%c%c",
+              1, 0x0b, 0, 0, 0, 0, 0));
   }
 }


More information about the Toybox mailing list