wok-next diff busybox/stuff/busybox-1.10.0-cpio.u @ rev 544

busybox/cpio: avoid cannot make dir warnings
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Apr 12 12:36:17 2008 +0200 (2008-04-12)
parents
children 2602d2710c44
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/busybox/stuff/busybox-1.10.0-cpio.u	Sat Apr 12 12:36:17 2008 +0200
     1.3 @@ -0,0 +1,220 @@
     1.4 +diff -purN busybox-1.10.0/archival/Config.in busybox/archival/Config.in
     1.5 +--- busybox-1.10.0/archival/Config.in	2008-03-29 21:47:37.000000000 +0100
     1.6 ++++ busybox-1.10.0/archival/Config.in	2008-03-29 21:47:37.000000000 +0100
     1.7 +@@ -78,6 +78,14 @@ config CPIO
     1.8 + 	  Unless you have a specific application which requires cpio, you should
     1.9 + 	  probably say N here.
    1.10 + 
    1.11 ++config FEATURE_CPIO_O
    1.12 ++	bool "Support for archive creation"
    1.13 ++	default n
    1.14 ++	depends on CPIO
    1.15 ++	help
    1.16 ++	  This implementation of cpio can create cpio archives in the "newc"
    1.17 ++	  format only.
    1.18 ++
    1.19 + config DPKG
    1.20 + 	bool "dpkg"
    1.21 + 	default n
    1.22 +diff -purN busybox-1.10.0/archival/cpio.c busybox/archival/cpio.c
    1.23 +--- busybox-1.10.0/archival/cpio.c	2008-03-29 21:47:55.000000000 +0100
    1.24 ++++ busybox-1.10.0/archival/cpio.c	2008-03-29 21:47:55.000000000 +0100
    1.25 +@@ -21,12 +21,146 @@
    1.26 + #define CPIO_OPT_FILE                   0x10
    1.27 + #define CPIO_OPT_CREATE_LEADING_DIR     0x20
    1.28 + #define CPIO_OPT_PRESERVE_MTIME         0x40
    1.29 ++#define CPIO_OPT_CREATE                 0x80
    1.30 ++#define CPIO_OPT_FORMAT                0x100
    1.31 ++
    1.32 ++#if ENABLE_FEATURE_CPIO_O
    1.33 ++static void cpio_pad(off_t *size, int n)
    1.34 ++{
    1.35 ++    int i;
    1.36 ++    for (*size += i = (-*size) & n; --i >= 0; bb_putchar(0));
    1.37 ++}
    1.38 ++
    1.39 ++static void cpio_o(void)
    1.40 ++{
    1.41 ++    struct name_s {
    1.42 ++        struct name_s *next;
    1.43 ++        char name[0];
    1.44 ++    };
    1.45 ++    struct inodes_s {
    1.46 ++        struct name_s *names;
    1.47 ++        struct inodes_s *next;
    1.48 ++        struct stat st;
    1.49 ++    } *links = NULL;
    1.50 ++    off_t bytes = 0; // output bytes count
    1.51 ++#if CONFIG_FEATURE_COPYBUF_KB < 1
    1.52 ++    char buf[1024];
    1.53 ++#else
    1.54 ++    char buf[CONFIG_FEATURE_COPYBUF_KB * 1024];
    1.55 ++#endif
    1.56 ++	
    1.57 ++    while (1) {
    1.58 ++        const char *name = "TRAILER!!!";
    1.59 ++        char *line = xmalloc_getline(stdin);
    1.60 ++        // allocate inode struct each loop to avoid struct stat copy
    1.61 ++        struct inodes_s *inode = xzalloc(sizeof(*inode)); // die if fail
    1.62 ++
    1.63 ++        inode->st.st_nlink++; // =1
    1.64 ++        if (line) {
    1.65 ++            /* Strip leading `./' from the filename.  */
    1.66 ++            for (name = line; name[0] == '.' && name[1] == '/';) {
    1.67 ++                while (*++name == '/');
    1.68 ++            }
    1.69 ++            if (!*name) goto free_and_continue; // line empty
    1.70 ++            if (lstat(name, &inode->st)) {
    1.71 ++          abort_cpio_o:
    1.72 ++                bb_perror_msg_and_die(name);
    1.73 ++            }
    1.74 ++        }
    1.75 ++
    1.76 ++        // hard links will are stored and will be processed later
    1.77 ++        if (!S_ISDIR(inode->st.st_mode) && inode->st.st_nlink > 1) {
    1.78 ++            struct name_s *n;
    1.79 ++            struct inodes_s *l;
    1.80 ++
    1.81 ++            for (l = links; l && l->st.st_ino != inode->st.st_ino; l = l->next);
    1.82 ++            if (l == NULL) { // not found: new hard links set
    1.83 ++                l = inode; // l->names = NULL; l->st = inode->st
    1.84 ++                l->next = links;
    1.85 ++                links = l;
    1.86 ++            }
    1.87 ++            n = xmalloc(sizeof(*n) + strlen(name) + 1); // die if fail
    1.88 ++            strcpy(n->name, name);
    1.89 ++            n->next = l->names;
    1.90 ++            l->names = n; // will not free inode if l == inode
    1.91 ++            goto free_and_continue;
    1.92 ++        }
    1.93 ++
    1.94 ++        // no more files ? process hard links
    1.95 ++        if (!line && links) {
    1.96 ++            struct name_s *n;
    1.97 ++
    1.98 ++            free(inode); // trailer pseudo inode
    1.99 ++            inode = links;
   1.100 ++            n = links->names;
   1.101 ++            name = line = xstrdup(n->name);    // line will free *name memory
   1.102 ++            links->names = n->next;
   1.103 ++            if (links->names == NULL)          // inode will free *links memory
   1.104 ++                links = links->next;
   1.105 ++            else links->st.st_size = 0;        // not last link: no data
   1.106 ++            free(n);
   1.107 ++        }
   1.108 ++
   1.109 ++        bytes += printf("070701%08lx%08lx%08lx%08lx%08lx%08lx%08lx"
   1.110 ++                        "%08lx%08lx%08lx%08lx%08lx%08lx%s%c",
   1.111 ++                        (unsigned long) inode->st.st_ino,
   1.112 ++                        (unsigned long) inode->st.st_mode, 
   1.113 ++                        (unsigned long) inode->st.st_uid,
   1.114 ++                        (unsigned long) inode->st.st_gid,
   1.115 ++                        (unsigned long) inode->st.st_nlink,
   1.116 ++                        (unsigned long) inode->st.st_mtime,
   1.117 ++                        (unsigned long) inode->st.st_size,
   1.118 ++                        (unsigned long) major(inode->st.st_dev),
   1.119 ++                        (unsigned long) minor(inode->st.st_dev),
   1.120 ++                        (unsigned long) major(inode->st.st_rdev), 
   1.121 ++                        (unsigned long) minor(inode->st.st_rdev),
   1.122 ++                        strlen(name) + 1UL, 0UL, name, 0);
   1.123 ++
   1.124 ++        cpio_pad(&bytes, (line) ? 4-1 : 512-1);
   1.125 ++
   1.126 ++        if (inode->st.st_size) {
   1.127 ++
   1.128 ++            if (S_ISLNK(inode->st.st_mode)) {
   1.129 ++                char *lpath = xmalloc_readlink_or_warn(name);
   1.130 ++
   1.131 ++                if (!lpath) goto abort_cpio_o;
   1.132 ++                bytes += printf("%s", lpath);
   1.133 ++                free(lpath);
   1.134 ++            }
   1.135 ++
   1.136 ++            if (S_ISREG(inode->st.st_mode)) {
   1.137 ++                int fd = open_or_warn(name, O_RDONLY);
   1.138 ++
   1.139 ++                while (1) {
   1.140 ++                    int len = full_read(fd, buf, sizeof(buf));
   1.141 ++                    if (len < 0) goto abort_cpio_o;
   1.142 ++                    if (len == 0) break;
   1.143 ++                    bytes += len;
   1.144 ++                    fwrite(buf, 1, len, stdout);
   1.145 ++                }
   1.146 ++                close(fd);
   1.147 ++            }
   1.148 ++
   1.149 ++            cpio_pad(&bytes, 4-1);
   1.150 ++        }
   1.151 ++
   1.152 ++        if (!line) return; // was trailer
   1.153 ++
   1.154 ++    free_and_continue:
   1.155 ++        if (!inode->names) free(inode);
   1.156 ++            free(line);
   1.157 ++    }
   1.158 ++}
   1.159 ++#endif
   1.160 + 
   1.161 + int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
   1.162 + int cpio_main(int argc, char **argv)
   1.163 + {
   1.164 + 	archive_handle_t *archive_handle;
   1.165 + 	char *cpio_filename = NULL;
   1.166 ++#if ENABLE_FEATURE_CPIO_O
   1.167 ++	const char *cpio_fmt = "";
   1.168 ++#endif
   1.169 + 	unsigned opt;
   1.170 + 
   1.171 + 	/* Initialise */
   1.172 +@@ -35,10 +169,26 @@ int cpio_main(int argc, char **argv)
   1.173 + 	archive_handle->seek = seek_by_read;
   1.174 + 	archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
   1.175 + 
   1.176 ++#if ENABLE_FEATURE_CPIO_O
   1.177 ++	opt = getopt32(argv, "ituvF:dmoH:", &cpio_filename,&cpio_fmt);
   1.178 ++
   1.179 ++	if (opt & CPIO_OPT_CREATE) {
   1.180 ++		if (*cpio_fmt != 'n')
   1.181 ++			goto cpio_show_usage;
   1.182 ++		if (cpio_filename) {
   1.183 ++			fclose(stdout);
   1.184 ++			stdout = fopen(cpio_filename,"w");
   1.185 ++		}
   1.186 ++		cpio_o();
   1.187 ++		return EXIT_SUCCESS;
   1.188 ++	}
   1.189 ++#else
   1.190 + 	opt = getopt32(argv, "ituvF:dm", &cpio_filename);
   1.191 ++#endif
   1.192 + 
   1.193 + 	/* One of either extract or test options must be given */
   1.194 + 	if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
   1.195 ++ cpio_show_usage:
   1.196 + 		bb_show_usage();
   1.197 + 	}
   1.198 + 
   1.199 +diff -purN busybox-1.10.0/include/usage.h busybox/include/usage.h
   1.200 +--- busybox-1.10.0/include/usage.h	2008-03-29 21:48:22.000000000 +0100
   1.201 ++++ busybox-1.10.0/include/usage.h	2008-03-29 21:48:22.000000000 +0100
   1.202 +@@ -496,13 +496,19 @@
   1.203 +      "\n	-l,-s	Create (sym)links" \
   1.204 + 
   1.205 + #define cpio_trivial_usage \
   1.206 +-       "-[dimtuv][F cpiofile]"
   1.207 ++       "-[dim" USE_FEATURE_CPIO_O("o") "tuv][F cpiofile]" \
   1.208 ++       USE_FEATURE_CPIO_O( "[H newc]" ) 
   1.209 + #define cpio_full_usage \
   1.210 +-       "Extract or list files from a cpio archive\n" \
   1.211 ++       "Extract or list files from a cpio archive" \
   1.212 ++       USE_FEATURE_CPIO_O( ", or create a cpio archive" ) "\n" \
   1.213 +        "Main operation mode:" \
   1.214 +      "\n	d	Make leading directories" \
   1.215 +      "\n	i	Extract" \
   1.216 +      "\n	m	Preserve mtime" \
   1.217 ++       USE_FEATURE_CPIO_O( \
   1.218 ++     "\n	o	Create" \
   1.219 ++     "\n	H newc	Define format" \
   1.220 ++       ) \
   1.221 +      "\n	t	List" \
   1.222 +      "\n	v	Verbose" \
   1.223 +      "\n	u	Unconditional overwrite" \