]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
- don't push data line wise to the compressor, accumulate it
authorJoerg Sonnenberger <joerg.sonnenberger@gmail.com>
Sat, 26 Jul 2008 01:00:11 +0000 (21:00 -0400)
committerJoerg Sonnenberger <joerg.sonnenberger@gmail.com>
Sat, 26 Jul 2008 01:00:11 +0000 (21:00 -0400)
  in local buffer
- reduce required error checking as fewer writes to the
  compressor need attention
- explicitly initialise the work string, don't depend on memset
  for that
- optimise inner loop of sed backend to take ~25% less time
- simplify and optimise uuencoding to always work on full lines,
  padding them explicitly with NUL (harmless changes in output).
  Overall uuencoding requires more than 50% less time.

SVN-Revision: 164

libarchive/archive_write_set_format_shar.c

index e954a78989833304dad120a9754a137fe2f36b6e..aa76471b2350a48ab614a0bb3e5b6a875e085153 100644 (file)
@@ -1,5 +1,6 @@
 /*-
  * Copyright (c) 2003-2007 Tim Kientzle
+ * Copyright (c) 2008 Joerg Sonnenberger
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -29,7 +30,6 @@ __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_format_shar.c,v 1.19 20
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
-#include <stdarg.h>
 #include <stdio.h>
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
@@ -49,11 +49,11 @@ struct shar {
        struct archive_entry    *entry;
        int                      has_data;
        char                    *last_dir;
-       char                     outbuff[1024];
-       size_t                   outbytes;
+
+       /* Line buffer for uuencoded dump format */
+       char                     outbuff[45];
        size_t                   outpos;
-       int                      uuavail;
-       char                     uubuffer[3];
+
        int                      wrote_header;
        struct archive_string    work;
 };
@@ -67,24 +67,6 @@ static ssize_t       archive_write_shar_data_sed(struct archive_write *,
 static ssize_t archive_write_shar_data_uuencode(struct archive_write *,
                    const void * buff, size_t);
 static int     archive_write_shar_finish_entry(struct archive_write *);
-static int     shar_printf(struct archive_write *, const char *fmt, ...);
-static void    uuencode_group(struct shar *);
-
-static int
-shar_printf(struct archive_write *a, const char *fmt, ...)
-{
-       struct shar *shar;
-       va_list ap;
-       int ret;
-
-       shar = (struct shar *)a->format_data;
-       va_start(ap, fmt);
-       archive_string_empty(&(shar->work));
-       archive_string_vsprintf(&(shar->work), fmt, ap);
-       ret = ((a->compressor.write)(a, shar->work.s, strlen(shar->work.s)));
-       va_end(ap);
-       return (ret);
-}
 
 /*
  * Set output format to 'shar' format.
@@ -105,6 +87,7 @@ archive_write_set_format_shar(struct archive *_a)
                return (ARCHIVE_FATAL);
        }
        memset(shar, 0, sizeof(*shar));
+       archive_string_init(&shar->work);
        a->format_data = shar;
 
        a->pad_uncompressed = 0;
@@ -146,16 +129,11 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
        const char *name;
        char *p, *pp;
        struct shar *shar;
-       int ret;
 
        shar = (struct shar *)a->format_data;
        if (!shar->wrote_header) {
-               ret = shar_printf(a, "#!/bin/sh\n");
-               if (ret != ARCHIVE_OK)
-                       return (ret);
-               ret = shar_printf(a, "# This is a shell archive\n");
-               if (ret != ARCHIVE_OK)
-                       return (ret);
+               archive_strcat(&shar->work, "#!/bin/sh\n");
+               archive_strcat(&shar->work, "# This is a shell archive\n");
                shar->wrote_header = 1;
        }
 
@@ -193,9 +171,7 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
        }
 
        /* Stock preparation for all file types. */
-       ret = shar_printf(a, "echo x %s\n", name);
-       if (ret != ARCHIVE_OK)
-               return (ret);
+       archive_string_sprintf(&shar->work, "echo x %s\n", name);
 
        if (archive_entry_filetype(entry) != AE_IFDIR) {
                /* Try to create the dir. */
@@ -210,12 +186,8 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
                                /* Don't try to "mkdir ." */
                                free(p);
                        } else if (shar->last_dir == NULL) {
-                               ret = shar_printf(a,
+                               archive_string_sprintf(&shar->work,
                                    "mkdir -p %s > /dev/null 2>&1\n", p);
-                               if (ret != ARCHIVE_OK) {
-                                       free(p);
-                                       return (ret);
-                               }
                                shar->last_dir = p;
                        } else if (strcmp(p, shar->last_dir) == 0) {
                                /* We've already created this exact dir. */
@@ -225,12 +197,8 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
                                /* We've already created a subdir. */
                                free(p);
                        } else {
-                               ret = shar_printf(a,
+                               archive_string_sprintf(&shar->work,
                                    "mkdir -p %s > /dev/null 2>&1\n", p);
-                               if (ret != ARCHIVE_OK) {
-                                       free(p);
-                                       return (ret);
-                               }
                                free(shar->last_dir);
                                shar->last_dir = p;
                        }
@@ -242,51 +210,41 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
        /* Handle file-type specific issues. */
        shar->has_data = 0;
        if ((linkname = archive_entry_hardlink(entry)) != NULL) {
-               ret = shar_printf(a, "ln -f %s %s\n", linkname, name);
-               if (ret != ARCHIVE_OK)
-                       return (ret);
+               archive_string_sprintf(&shar->work, "ln -f %s %s\n",
+                   linkname, name);
        } else if ((linkname = archive_entry_symlink(entry)) != NULL) {
-               ret = shar_printf(a, "ln -fs %s %s\n", linkname, name);
-               if (ret != ARCHIVE_OK)
-                       return (ret);
+               archive_string_sprintf(&shar->work, "ln -fs %s %s\n",
+                   linkname, name);
        } else {
                switch(archive_entry_filetype(entry)) {
                case AE_IFREG:
                        if (archive_entry_size(entry) == 0) {
                                /* More portable than "touch." */
-                               ret = shar_printf(a, "test -e \"%s\" || :> \"%s\"\n", name, name);
-                               if (ret != ARCHIVE_OK)
-                                       return (ret);
+                               archive_string_sprintf(&shar->work,
+                                   "test -e \"%s\" || :> \"%s\"\n",
+                                   name, name);
                        } else {
                                if (shar->dump) {
-                                       ret = shar_printf(a,
+                                       archive_string_sprintf(&shar->work,
                                            "uudecode -p > %s << 'SHAR_END'\n",
                                            name);
-                                       if (ret != ARCHIVE_OK)
-                                               return (ret);
-                                       ret = shar_printf(a, "begin %o %s\n",
+                                       archive_string_sprintf(&shar->work,
+                                           "begin %o %s\n",
                                            archive_entry_mode(entry) & 0777,
                                            name);
-                                       if (ret != ARCHIVE_OK)
-                                               return (ret);
                                } else {
-                                       ret = shar_printf(a,
+                                       archive_string_sprintf(&shar->work,
                                            "sed 's/^X//' > %s << 'SHAR_END'\n",
                                            name);
-                                       if (ret != ARCHIVE_OK)
-                                               return (ret);
                                }
                                shar->has_data = 1;
                                shar->end_of_line = 1;
                                shar->outpos = 0;
-                               shar->outbytes = 0;
                        }
                        break;
                case AE_IFDIR:
-                       ret = shar_printf(a, "mkdir -p %s > /dev/null 2>&1\n",
-                           name);
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
+                       archive_string_sprintf(&shar->work,
+                           "mkdir -p %s > /dev/null 2>&1\n", name);
                        /* Record that we just created this directory. */
                        if (shar->last_dir != NULL)
                                free(shar->last_dir);
@@ -302,23 +260,20 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
                         */
                        break;
                case AE_IFIFO:
-                       ret = shar_printf(a, "mkfifo %s\n", name);
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
+                       archive_string_sprintf(&shar->work,
+                           "mkfifo %s\n", name);
                        break;
                case AE_IFCHR:
-                       ret = shar_printf(a, "mknod %s c %d %d\n", name,
+                       archive_string_sprintf(&shar->work,
+                           "mknod %s c %d %d\n", name,
                            archive_entry_rdevmajor(entry),
                            archive_entry_rdevminor(entry));
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
                        break;
                case AE_IFBLK:
-                       ret = shar_printf(a, "mknod %s b %d %d\n", name,
+                       archive_string_sprintf(&shar->work,
+                           "mknod %s b %d %d\n", name,
                            archive_entry_rdevmajor(entry),
                            archive_entry_rdevminor(entry));
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
                        break;
                default:
                        return (ARCHIVE_WARN);
@@ -328,69 +283,119 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
        return (ARCHIVE_OK);
 }
 
-/* XXX TODO: This could be more efficient XXX */
 static ssize_t
 archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
 {
+       static const ensured = 65533;
        struct shar *shar;
        const char *src;
+       char *buf, *buf_end;
        int ret;
-       size_t written = n;
+       size_t line, written = n;
 
        shar = (struct shar *)a->format_data;
-       if (!shar->has_data)
+       if (!shar->has_data || n == 0)
                return (0);
 
        src = (const char *)buff;
-       ret = ARCHIVE_OK;
-       shar->outpos = 0;
-       while (n-- > 0) {
-               if (shar->end_of_line) {
-                       shar->outbuff[shar->outpos++] = 'X';
-                       shar->end_of_line = 0;
-               }
-               if (*src == '\n')
-                       shar->end_of_line = 1;
-               shar->outbuff[shar->outpos++] = *src++;
 
-               if (shar->outpos > sizeof(shar->outbuff) - 2) {
-                       ret = (a->compressor.write)(a, shar->outbuff,
-                           shar->outpos);
+       /*
+        * ensure is the number of bytes in buffer before expanding the
+        * current character.  Each operation writes the current character
+        * and optionally the start-of-new-line marker.  This can happen
+        * twice before entering the loop, so make sure three additional
+        * bytes can be written.
+        */
+       if (archive_string_ensure(&shar->work, ensured + 3) == NULL)
+               __archive_errx(1, "Out of memory");
+
+       if (shar->work.length > ensured) {
+               ret = (*a->compressor.write)(a, shar->work.s,
+                   shar->work.length);
+               if (ret != ARCHIVE_OK)
+                       return (ARCHIVE_FATAL);
+               archive_string_empty(&shar->work);
+       }
+       buf = shar->work.s + shar->work.length;
+       buf_end = shar->work.s + ensured;
+
+       if (shar->end_of_line) {
+               *buf++ = 'X';
+               shar->end_of_line = 0;
+       }
+
+       while (n-- != 0) {
+               if ((*buf++ = *src++) == '\n') {
+                       if (n == 0)
+                               shar->end_of_line = 1;
+                       else
+                               *buf++ = 'X';
+               }
+               
+               if (buf >= buf_end) {
+                       shar->work.length = buf - shar->work.s;
+                       ret = (*a->compressor.write)(a, shar->work.s,
+                           shar->work.length);
                        if (ret != ARCHIVE_OK)
-                               return (ret);
-                       shar->outpos = 0;
+                               return (ARCHIVE_FATAL);
+                       archive_string_empty(&shar->work);
+                       buf = shar->work.s;
                }
        }
 
-       if (shar->outpos > 0)
-               ret = (a->compressor.write)(a, shar->outbuff, shar->outpos);
-       if (ret != ARCHIVE_OK)
-               return (ret);
+       shar->work.length = buf - shar->work.s;
+
        return (written);
 }
 
 #define        UUENC(c)        (((c)!=0) ? ((c) & 077) + ' ': '`')
 
-/* XXX This could be a lot more efficient. XXX */
 static void
-uuencode_group(struct shar *shar)
+uuencode_group(const char _in[3], char out[4])
+{
+       const unsigned char *in = (const unsigned char *)_in;
+       int t;
+
+       t = (in[0] << 16) | (in[1] << 8) | in[2];
+       out[0] = UUENC( 0x3f & (t >> 18) );
+       out[1] = UUENC( 0x3f & (t >> 12) );
+       out[2] = UUENC( 0x3f & (t >> 6) );
+       out[3] = UUENC( 0x3f & t );
+}
+
+static int
+uuencode_line(struct shar *shar, const char *inbuf, size_t len)
 {
-       int     t;
-
-       t = 0;
-       if (shar->uuavail > 0)
-               t = 0xff0000 & (shar->uubuffer[0] << 16);
-       if (shar->uuavail > 1)
-               t |= 0x00ff00 & (shar->uubuffer[1] << 8);
-       if (shar->uuavail > 2)
-               t |= 0x0000ff & (shar->uubuffer[2]);
-       shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>18) );
-       shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>12) );
-       shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>6) );
-       shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t) );
-       shar->uuavail = 0;
-       shar->outbytes += shar->uuavail;
-       shar->outbuff[shar->outpos] = 0;
+       char tmp_buf[3], *buf;
+       size_t alloc_len;
+
+       /* len <= 45 -> expanded to 60 + len byte + new line */
+       alloc_len = shar->work.length + 62;
+       if (archive_string_ensure(&shar->work, alloc_len) == NULL)
+               __archive_errx(1, "Out of memory");
+
+       buf = shar->work.s + shar->work.length;
+       *buf++ = UUENC(len);
+       while (len >= 3) {
+               uuencode_group(inbuf, buf);
+               len -= 3;
+               inbuf += 3;
+               buf += 4;
+       }
+       if (len != 0) {
+               tmp_buf[0] = inbuf[0];
+               if (len == 1)
+                       tmp_buf[1] = '\0';
+               else
+                       tmp_buf[1] = inbuf[1];
+               tmp_buf[2] = '\0';
+               uuencode_group(inbuf, buf);
+               buf += 4;
+       }
+       *buf++ = '\n';
+       if (buf - shar->work.s > shar->work.length + 62)
+               __archive_errx(1, "Buffer overflow");
+       shar->work.length = buf - shar->work.s;
 }
 
 static ssize_t
@@ -406,21 +411,39 @@ archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
        if (!shar->has_data)
                return (ARCHIVE_OK);
        src = (const char *)buff;
-       n = length;
-       while (n-- > 0) {
-               if (shar->uuavail == 3)
-                       uuencode_group(shar);
-               if (shar->outpos >= 60) {
-                       ret = shar_printf(a, "%c%s\n", UUENC(shar->outbytes),
-                           shar->outbuff);
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
-                       shar->outpos = 0;
-                       shar->outbytes = 0;
+
+       if (shar->outpos != 0) {
+               n = 45 - shar->outpos;
+               if (n > length)
+                       n = length;
+               memcpy(shar->outbuff + shar->outpos, src, n);
+               if (shar->outpos + n < 45) {
+                       shar->outpos += n;
+                       return length;
                }
+               uuencode_line(shar, shar->outbuff, 45);
+               src += n;
+               n = length - n;
+       } else {
+               n = length;
+       }
+
+       while (n >= 45) {
+               uuencode_line(shar, src, 45);
+               src += 45;
+               n -= 45;
 
-               shar->uubuffer[shar->uuavail++] = *src++;
-               shar->outbytes++;
+               if (shar->work.length < 65536)
+                       continue;
+               ret = (*a->compressor.write)(a, shar->work.s,
+                   shar->work.length);
+               if (ret != ARCHIVE_OK)
+                       return (ARCHIVE_FATAL);
+               archive_string_empty(&shar->work);
+       }
+       if (n != 0) {
+               memcpy(shar->outbuff, src, n);
+               shar->outpos = n;
        }
        return (length);
 }
@@ -439,54 +462,34 @@ archive_write_shar_finish_entry(struct archive_write *a)
        if (shar->dump) {
                /* Finish uuencoded data. */
                if (shar->has_data) {
-                       if (shar->uuavail > 0)
-                               uuencode_group(shar);
-                       if (shar->outpos > 0) {
-                               ret = shar_printf(a, "%c%s\n",
-                                   UUENC(shar->outbytes), shar->outbuff);
-                               if (ret != ARCHIVE_OK)
-                                       return (ret);
-                               shar->outpos = 0;
-                               shar->uuavail = 0;
-                               shar->outbytes = 0;
-                       }
-                       ret = shar_printf(a, "%c\n", UUENC(0));
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
-                       ret = shar_printf(a, "end\n", UUENC(0));
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
-                       ret = shar_printf(a, "SHAR_END\n");
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
+                       if (shar->outpos > 0)
+                               uuencode_line(shar, shar->outbuff,
+                                   shar->outpos);
+                       archive_strcat(&shar->work, "`\nend\n");
+                       archive_strcat(&shar->work, "SHAR_END\n");
                }
                /* Restore file mode, owner, flags. */
                /*
                 * TODO: Don't immediately restore mode for
                 * directories; defer that to end of script.
                 */
-               ret = shar_printf(a, "chmod %o %s\n",
+               archive_string_sprintf(&shar->work, "chmod %o %s\n",
                    archive_entry_mode(shar->entry) & 07777,
                    archive_entry_pathname(shar->entry));
-               if (ret != ARCHIVE_OK)
-                       return (ret);
 
                u = archive_entry_uname(shar->entry);
                g = archive_entry_gname(shar->entry);
                if (u != NULL || g != NULL) {
-                       ret = shar_printf(a, "chown %s%s%s %s\n",
+                       archive_string_sprintf(&shar->work,
+                           "chown %s%s%s %s\n",
                            (u != NULL) ? u : "",
                            (g != NULL) ? ":" : "", (g != NULL) ? g : "",
                            archive_entry_pathname(shar->entry));
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
                }
 
                if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
-                       ret = shar_printf(a, "chflags %s %s\n", p,
-                           archive_entry_pathname(shar->entry));
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
+                       archive_string_sprintf(&shar->work, "chflags %s %s\n",
+                           p, archive_entry_pathname(shar->entry));
                }
 
                /* TODO: restore ACLs */
@@ -494,20 +497,24 @@ archive_write_shar_finish_entry(struct archive_write *a)
        } else {
                if (shar->has_data) {
                        /* Finish sed-encoded data:  ensure last line ends. */
-                       if (!shar->end_of_line) {
-                               ret = shar_printf(a, "\n");
-                               if (ret != ARCHIVE_OK)
-                                       return (ret);
-                       }
-                       ret = shar_printf(a, "SHAR_END\n");
-                       if (ret != ARCHIVE_OK)
-                               return (ret);
+                       if (!shar->end_of_line)
+                               archive_strappend_char(&shar->work, '\n');
+                       archive_strcat(&shar->work, "SHAR_END\n");
                }
        }
 
        archive_entry_free(shar->entry);
        shar->entry = NULL;
-       return (0);
+
+       if (shar->work.length < 65536)
+               return (ARCHIVE_OK);
+
+       ret = (*a->compressor.write)(a, shar->work.s, shar->work.length);
+       if (ret != ARCHIVE_OK)
+               return (ARCHIVE_FATAL);
+       archive_string_empty(&shar->work);
+
+       return (ARCHIVE_OK);
 }
 
 static int
@@ -529,17 +536,22 @@ archive_write_shar_finish(struct archive_write *a)
         * shar format, then sets another format (which would invoke
         * shar_finish to free the format-specific data).
         */
-       if (shar->wrote_header) {
-               ret = shar_printf(a, "exit\n");
-               if (ret != ARCHIVE_OK)
-                       return (ret);
-               /* Shar output is never padded. */
-               archive_write_set_bytes_in_last_block(&a->archive, 1);
-               /*
-                * TODO: shar should also suppress padding of
-                * uncompressed data within gzip/bzip2 streams.
-                */
-       }
+       if (shar->wrote_header == 0)
+               return (ARCHIVE_OK);
+
+       archive_strcat(&shar->work, "exit\n");
+
+       ret = (*a->compressor.write)(a, shar->work.s, shar->work.length);
+       if (ret != ARCHIVE_OK)
+               return (ARCHIVE_FATAL);
+
+       /* Shar output is never padded. */
+       archive_write_set_bytes_in_last_block(&a->archive, 1);
+       /*
+        * TODO: shar should also suppress padding of
+        * uncompressed data within gzip/bzip2 streams.
+        */
+
        return (ARCHIVE_OK);
 }
 
@@ -551,10 +563,9 @@ archive_write_shar_destroy(struct archive_write *a)
        shar = (struct shar *)a->format_data;
        if (shar == NULL)
                return (ARCHIVE_OK);
-       if (shar->entry != NULL)
-               archive_entry_free(shar->entry);
-       if (shar->last_dir != NULL)
-               free(shar->last_dir);
+
+       archive_entry_free(shar->entry);
+       free(shar->last_dir);
        archive_string_free(&(shar->work));
        free(shar);
        a->format_data = NULL;