changeset 1098:46d83f3c7546

Here's a revised cpio. I've reduced the use of malloc(), dropped an extra function call, and -at least in theory- allowed proper handling of non-regular files. (If we have a file we can't read, we still should record it when it's of a type where file content is ignored).
author Isaac Dunham <ibid.ag@gmail.com>
date Sun, 27 Oct 2013 19:11:07 -0500
parents 54b2776de2f4
children 71326585c02d
files toys/pending/cpio.c
diffstat 1 files changed, 6 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/cpio.c	Sun Oct 27 00:02:56 2013 -0500
+++ b/toys/pending/cpio.c	Sun Oct 27 19:11:07 2013 -0500
@@ -27,9 +27,10 @@
 /* Iterate through a list of files, read from stdin.
  * No users need rw.
  */
-void loopfiles_stdin(void (*function)(int fd, char *name))
+void loopfiles_stdin(void (*function)(int fd, char *name, struct stat st))
 {
   int fd;
+  struct stat st;
   char *name = toybuf;
 
   while (name != NULL){
@@ -39,9 +40,10 @@
     if (name != NULL) {
       if (toybuf[strlen(name) - 1] == '\n' ) { 
         toybuf[strlen(name) - 1 ] = '\0';
+        if (lstat(name, &st) == -1) continue;
 	fd = open(name, O_RDONLY);
-	if (fd > 0) {
-          function(fd, name);
+	if (fd > 0 || !S_ISREG(st.st_mode)) {
+          function(fd, name, st);
 	  close(fd);
 	}
 	errno = 0;
@@ -105,13 +107,6 @@
   if (buf.st_size % 4) write(1, &n, 4 - (buf.st_size % 4));
 }
 
-void write_cpio_call(int fd, char *name)
-{
-  struct stat buf;
-  if (lstat(name, &buf) == -1) return;
-  write_cpio_member(fd, name, buf);
-}
-
 //convert hex to uint; mostly to allow using bits of non-terminated strings
 unsigned int htou(char * hex)
 {
@@ -224,7 +219,7 @@
 {
   switch (toys.optflags & (FLAG_i | FLAG_o | FLAG_t)) {
     case FLAG_o:
-      loopfiles_stdin(write_cpio_call);
+      loopfiles_stdin(write_cpio_member);
       write(1, "07070100000000000000000000000000000000000000010000000000000000"
       "000000000000000000000000000000000000000B00000000TRAILER!!!\0\0\0", 124);
       break;