160b4ad09SPeter Avalos /*-
260b4ad09SPeter Avalos * Copyright (c) 2003-2007 Tim Kientzle
38029ab02SPeter Avalos * Copyright (c) 2008 Joerg Sonnenberger
460b4ad09SPeter Avalos * All rights reserved.
560b4ad09SPeter Avalos *
660b4ad09SPeter Avalos * Redistribution and use in source and binary forms, with or without
760b4ad09SPeter Avalos * modification, are permitted provided that the following conditions
860b4ad09SPeter Avalos * are met:
960b4ad09SPeter Avalos * 1. Redistributions of source code must retain the above copyright
1060b4ad09SPeter Avalos * notice, this list of conditions and the following disclaimer.
1160b4ad09SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
1260b4ad09SPeter Avalos * notice, this list of conditions and the following disclaimer in the
1360b4ad09SPeter Avalos * documentation and/or other materials provided with the distribution.
1460b4ad09SPeter Avalos *
1560b4ad09SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1660b4ad09SPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1760b4ad09SPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1860b4ad09SPeter Avalos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1960b4ad09SPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2060b4ad09SPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2160b4ad09SPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2260b4ad09SPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2360b4ad09SPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2460b4ad09SPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2560b4ad09SPeter Avalos */
2660b4ad09SPeter Avalos
2760b4ad09SPeter Avalos #include "archive_platform.h"
289c82a63eSPeter Avalos __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_shar.c 189438 2009-03-06 05:58:56Z kientzle $");
2960b4ad09SPeter Avalos
3060b4ad09SPeter Avalos #ifdef HAVE_ERRNO_H
3160b4ad09SPeter Avalos #include <errno.h>
3260b4ad09SPeter Avalos #endif
3360b4ad09SPeter Avalos #include <stdio.h>
3460b4ad09SPeter Avalos #ifdef HAVE_STDLIB_H
3560b4ad09SPeter Avalos #include <stdlib.h>
3660b4ad09SPeter Avalos #endif
3760b4ad09SPeter Avalos #ifdef HAVE_STRING_H
3860b4ad09SPeter Avalos #include <string.h>
3960b4ad09SPeter Avalos #endif
4060b4ad09SPeter Avalos
4160b4ad09SPeter Avalos #include "archive.h"
4260b4ad09SPeter Avalos #include "archive_entry.h"
4360b4ad09SPeter Avalos #include "archive_private.h"
4460b4ad09SPeter Avalos #include "archive_write_private.h"
45*085658deSDaniel Fojt #include "archive_write_set_format_private.h"
4660b4ad09SPeter Avalos
4760b4ad09SPeter Avalos struct shar {
4860b4ad09SPeter Avalos int dump;
4960b4ad09SPeter Avalos int end_of_line;
5060b4ad09SPeter Avalos struct archive_entry *entry;
5160b4ad09SPeter Avalos int has_data;
5260b4ad09SPeter Avalos char *last_dir;
538029ab02SPeter Avalos
548029ab02SPeter Avalos /* Line buffer for uuencoded dump format */
558029ab02SPeter Avalos char outbuff[45];
5660b4ad09SPeter Avalos size_t outpos;
578029ab02SPeter Avalos
5860b4ad09SPeter Avalos int wrote_header;
5960b4ad09SPeter Avalos struct archive_string work;
608029ab02SPeter Avalos struct archive_string quoted_name;
6160b4ad09SPeter Avalos };
6260b4ad09SPeter Avalos
63c09f92d2SPeter Avalos static int archive_write_shar_close(struct archive_write *);
64c09f92d2SPeter Avalos static int archive_write_shar_free(struct archive_write *);
6560b4ad09SPeter Avalos static int archive_write_shar_header(struct archive_write *,
6660b4ad09SPeter Avalos struct archive_entry *);
6760b4ad09SPeter Avalos static ssize_t archive_write_shar_data_sed(struct archive_write *,
6860b4ad09SPeter Avalos const void * buff, size_t);
6960b4ad09SPeter Avalos static ssize_t archive_write_shar_data_uuencode(struct archive_write *,
7060b4ad09SPeter Avalos const void * buff, size_t);
7160b4ad09SPeter Avalos static int archive_write_shar_finish_entry(struct archive_write *);
7260b4ad09SPeter Avalos
738029ab02SPeter Avalos /*
748029ab02SPeter Avalos * Copy the given string to the buffer, quoting all shell meta characters
758029ab02SPeter Avalos * found.
768029ab02SPeter Avalos */
778029ab02SPeter Avalos static void
shar_quote(struct archive_string * buf,const char * str,int in_shell)788029ab02SPeter Avalos shar_quote(struct archive_string *buf, const char *str, int in_shell)
7960b4ad09SPeter Avalos {
808029ab02SPeter Avalos static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
818029ab02SPeter Avalos size_t len;
8260b4ad09SPeter Avalos
838029ab02SPeter Avalos while (*str != '\0') {
848029ab02SPeter Avalos if ((len = strcspn(str, meta)) != 0) {
858029ab02SPeter Avalos archive_strncat(buf, str, len);
868029ab02SPeter Avalos str += len;
878029ab02SPeter Avalos } else if (*str == '\n') {
888029ab02SPeter Avalos if (in_shell)
898029ab02SPeter Avalos archive_strcat(buf, "\"\n\"");
908029ab02SPeter Avalos else
918029ab02SPeter Avalos archive_strcat(buf, "\\n");
928029ab02SPeter Avalos ++str;
938029ab02SPeter Avalos } else {
948029ab02SPeter Avalos archive_strappend_char(buf, '\\');
958029ab02SPeter Avalos archive_strappend_char(buf, *str);
968029ab02SPeter Avalos ++str;
978029ab02SPeter Avalos }
988029ab02SPeter Avalos }
9960b4ad09SPeter Avalos }
10060b4ad09SPeter Avalos
10160b4ad09SPeter Avalos /*
10260b4ad09SPeter Avalos * Set output format to 'shar' format.
10360b4ad09SPeter Avalos */
10460b4ad09SPeter Avalos int
archive_write_set_format_shar(struct archive * _a)10560b4ad09SPeter Avalos archive_write_set_format_shar(struct archive *_a)
10660b4ad09SPeter Avalos {
10760b4ad09SPeter Avalos struct archive_write *a = (struct archive_write *)_a;
10860b4ad09SPeter Avalos struct shar *shar;
10960b4ad09SPeter Avalos
110c09f92d2SPeter Avalos archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
111c09f92d2SPeter Avalos ARCHIVE_STATE_NEW, "archive_write_set_format_shar");
112c09f92d2SPeter Avalos
11360b4ad09SPeter Avalos /* If someone else was already registered, unregister them. */
114c09f92d2SPeter Avalos if (a->format_free != NULL)
115c09f92d2SPeter Avalos (a->format_free)(a);
11660b4ad09SPeter Avalos
117e95abc47Szrj shar = (struct shar *)calloc(1, sizeof(*shar));
11860b4ad09SPeter Avalos if (shar == NULL) {
11960b4ad09SPeter Avalos archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data");
12060b4ad09SPeter Avalos return (ARCHIVE_FATAL);
12160b4ad09SPeter Avalos }
1228029ab02SPeter Avalos archive_string_init(&shar->work);
1238029ab02SPeter Avalos archive_string_init(&shar->quoted_name);
12460b4ad09SPeter Avalos a->format_data = shar;
1258029ab02SPeter Avalos a->format_name = "shar";
12660b4ad09SPeter Avalos a->format_write_header = archive_write_shar_header;
127c09f92d2SPeter Avalos a->format_close = archive_write_shar_close;
128c09f92d2SPeter Avalos a->format_free = archive_write_shar_free;
12960b4ad09SPeter Avalos a->format_write_data = archive_write_shar_data_sed;
13060b4ad09SPeter Avalos a->format_finish_entry = archive_write_shar_finish_entry;
13160b4ad09SPeter Avalos a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE;
13260b4ad09SPeter Avalos a->archive.archive_format_name = "shar";
13360b4ad09SPeter Avalos return (ARCHIVE_OK);
13460b4ad09SPeter Avalos }
13560b4ad09SPeter Avalos
13660b4ad09SPeter Avalos /*
13760b4ad09SPeter Avalos * An alternate 'shar' that uses uudecode instead of 'sed' to encode
13860b4ad09SPeter Avalos * file contents and can therefore be used to archive binary files.
13960b4ad09SPeter Avalos * In addition, this variant also attempts to restore ownership, file modes,
14060b4ad09SPeter Avalos * and other extended file information.
14160b4ad09SPeter Avalos */
14260b4ad09SPeter Avalos int
archive_write_set_format_shar_dump(struct archive * _a)14360b4ad09SPeter Avalos archive_write_set_format_shar_dump(struct archive *_a)
14460b4ad09SPeter Avalos {
14560b4ad09SPeter Avalos struct archive_write *a = (struct archive_write *)_a;
14660b4ad09SPeter Avalos struct shar *shar;
14760b4ad09SPeter Avalos
14860b4ad09SPeter Avalos archive_write_set_format_shar(&a->archive);
14960b4ad09SPeter Avalos shar = (struct shar *)a->format_data;
15060b4ad09SPeter Avalos shar->dump = 1;
15160b4ad09SPeter Avalos a->format_write_data = archive_write_shar_data_uuencode;
15260b4ad09SPeter Avalos a->archive.archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
15360b4ad09SPeter Avalos a->archive.archive_format_name = "shar dump";
15460b4ad09SPeter Avalos return (ARCHIVE_OK);
15560b4ad09SPeter Avalos }
15660b4ad09SPeter Avalos
15760b4ad09SPeter Avalos static int
archive_write_shar_header(struct archive_write * a,struct archive_entry * entry)15860b4ad09SPeter Avalos archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
15960b4ad09SPeter Avalos {
16060b4ad09SPeter Avalos const char *linkname;
16160b4ad09SPeter Avalos const char *name;
16260b4ad09SPeter Avalos char *p, *pp;
16360b4ad09SPeter Avalos struct shar *shar;
16460b4ad09SPeter Avalos
16560b4ad09SPeter Avalos shar = (struct shar *)a->format_data;
16660b4ad09SPeter Avalos if (!shar->wrote_header) {
1678029ab02SPeter Avalos archive_strcat(&shar->work, "#!/bin/sh\n");
1688029ab02SPeter Avalos archive_strcat(&shar->work, "# This is a shell archive\n");
16960b4ad09SPeter Avalos shar->wrote_header = 1;
17060b4ad09SPeter Avalos }
17160b4ad09SPeter Avalos
17260b4ad09SPeter Avalos /* Save the entry for the closing. */
17360b4ad09SPeter Avalos archive_entry_free(shar->entry);
17460b4ad09SPeter Avalos shar->entry = archive_entry_clone(entry);
17560b4ad09SPeter Avalos name = archive_entry_pathname(entry);
17660b4ad09SPeter Avalos
17760b4ad09SPeter Avalos /* Handle some preparatory issues. */
17860b4ad09SPeter Avalos switch(archive_entry_filetype(entry)) {
17960b4ad09SPeter Avalos case AE_IFREG:
18060b4ad09SPeter Avalos /* Only regular files have non-zero size. */
18160b4ad09SPeter Avalos break;
18260b4ad09SPeter Avalos case AE_IFDIR:
18360b4ad09SPeter Avalos archive_entry_set_size(entry, 0);
18460b4ad09SPeter Avalos /* Don't bother trying to recreate '.' */
18560b4ad09SPeter Avalos if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0)
18660b4ad09SPeter Avalos return (ARCHIVE_OK);
18760b4ad09SPeter Avalos break;
18860b4ad09SPeter Avalos case AE_IFIFO:
18960b4ad09SPeter Avalos case AE_IFCHR:
19060b4ad09SPeter Avalos case AE_IFBLK:
19160b4ad09SPeter Avalos /* All other file types have zero size in the archive. */
19260b4ad09SPeter Avalos archive_entry_set_size(entry, 0);
19360b4ad09SPeter Avalos break;
19460b4ad09SPeter Avalos default:
19560b4ad09SPeter Avalos archive_entry_set_size(entry, 0);
19660b4ad09SPeter Avalos if (archive_entry_hardlink(entry) == NULL &&
19760b4ad09SPeter Avalos archive_entry_symlink(entry) == NULL) {
198*085658deSDaniel Fojt __archive_write_entry_filetype_unsupported(
199*085658deSDaniel Fojt &a->archive, entry, "shar");
20060b4ad09SPeter Avalos return (ARCHIVE_WARN);
20160b4ad09SPeter Avalos }
20260b4ad09SPeter Avalos }
20360b4ad09SPeter Avalos
2048029ab02SPeter Avalos archive_string_empty(&shar->quoted_name);
2058029ab02SPeter Avalos shar_quote(&shar->quoted_name, name, 1);
2068029ab02SPeter Avalos
20760b4ad09SPeter Avalos /* Stock preparation for all file types. */
2088029ab02SPeter Avalos archive_string_sprintf(&shar->work, "echo x %s\n", shar->quoted_name.s);
20960b4ad09SPeter Avalos
21060b4ad09SPeter Avalos if (archive_entry_filetype(entry) != AE_IFDIR) {
21160b4ad09SPeter Avalos /* Try to create the dir. */
21260b4ad09SPeter Avalos p = strdup(name);
21360b4ad09SPeter Avalos pp = strrchr(p, '/');
21460b4ad09SPeter Avalos /* If there is a / character, try to create the dir. */
21560b4ad09SPeter Avalos if (pp != NULL) {
21660b4ad09SPeter Avalos *pp = '\0';
21760b4ad09SPeter Avalos
21860b4ad09SPeter Avalos /* Try to avoid a lot of redundant mkdir commands. */
21960b4ad09SPeter Avalos if (strcmp(p, ".") == 0) {
22060b4ad09SPeter Avalos /* Don't try to "mkdir ." */
22160b4ad09SPeter Avalos free(p);
22260b4ad09SPeter Avalos } else if (shar->last_dir == NULL) {
2238029ab02SPeter Avalos archive_strcat(&shar->work, "mkdir -p ");
2248029ab02SPeter Avalos shar_quote(&shar->work, p, 1);
2258029ab02SPeter Avalos archive_strcat(&shar->work,
2268029ab02SPeter Avalos " > /dev/null 2>&1\n");
22760b4ad09SPeter Avalos shar->last_dir = p;
22860b4ad09SPeter Avalos } else if (strcmp(p, shar->last_dir) == 0) {
22960b4ad09SPeter Avalos /* We've already created this exact dir. */
23060b4ad09SPeter Avalos free(p);
23160b4ad09SPeter Avalos } else if (strlen(p) < strlen(shar->last_dir) &&
23260b4ad09SPeter Avalos strncmp(p, shar->last_dir, strlen(p)) == 0) {
23360b4ad09SPeter Avalos /* We've already created a subdir. */
23460b4ad09SPeter Avalos free(p);
23560b4ad09SPeter Avalos } else {
2368029ab02SPeter Avalos archive_strcat(&shar->work, "mkdir -p ");
2378029ab02SPeter Avalos shar_quote(&shar->work, p, 1);
2388029ab02SPeter Avalos archive_strcat(&shar->work,
2398029ab02SPeter Avalos " > /dev/null 2>&1\n");
24060b4ad09SPeter Avalos shar->last_dir = p;
24160b4ad09SPeter Avalos }
24260b4ad09SPeter Avalos } else {
24360b4ad09SPeter Avalos free(p);
24460b4ad09SPeter Avalos }
24560b4ad09SPeter Avalos }
24660b4ad09SPeter Avalos
24760b4ad09SPeter Avalos /* Handle file-type specific issues. */
24860b4ad09SPeter Avalos shar->has_data = 0;
24960b4ad09SPeter Avalos if ((linkname = archive_entry_hardlink(entry)) != NULL) {
2508029ab02SPeter Avalos archive_strcat(&shar->work, "ln -f ");
2518029ab02SPeter Avalos shar_quote(&shar->work, linkname, 1);
2528029ab02SPeter Avalos archive_string_sprintf(&shar->work, " %s\n",
2538029ab02SPeter Avalos shar->quoted_name.s);
25460b4ad09SPeter Avalos } else if ((linkname = archive_entry_symlink(entry)) != NULL) {
2558029ab02SPeter Avalos archive_strcat(&shar->work, "ln -fs ");
2568029ab02SPeter Avalos shar_quote(&shar->work, linkname, 1);
2578029ab02SPeter Avalos archive_string_sprintf(&shar->work, " %s\n",
2588029ab02SPeter Avalos shar->quoted_name.s);
25960b4ad09SPeter Avalos } else {
26060b4ad09SPeter Avalos switch(archive_entry_filetype(entry)) {
26160b4ad09SPeter Avalos case AE_IFREG:
26260b4ad09SPeter Avalos if (archive_entry_size(entry) == 0) {
26360b4ad09SPeter Avalos /* More portable than "touch." */
2648029ab02SPeter Avalos archive_string_sprintf(&shar->work,
2658029ab02SPeter Avalos "test -e \"%s\" || :> \"%s\"\n",
2668029ab02SPeter Avalos shar->quoted_name.s, shar->quoted_name.s);
26760b4ad09SPeter Avalos } else {
26860b4ad09SPeter Avalos if (shar->dump) {
269c09f92d2SPeter Avalos unsigned int mode = archive_entry_mode(entry) & 0777;
2708029ab02SPeter Avalos archive_string_sprintf(&shar->work,
2718029ab02SPeter Avalos "uudecode -p > %s << 'SHAR_END'\n",
2728029ab02SPeter Avalos shar->quoted_name.s);
2738029ab02SPeter Avalos archive_string_sprintf(&shar->work,
274c09f92d2SPeter Avalos "begin %o ", mode);
2758029ab02SPeter Avalos shar_quote(&shar->work, name, 0);
2768029ab02SPeter Avalos archive_strcat(&shar->work, "\n");
27760b4ad09SPeter Avalos } else {
2788029ab02SPeter Avalos archive_string_sprintf(&shar->work,
27960b4ad09SPeter Avalos "sed 's/^X//' > %s << 'SHAR_END'\n",
2808029ab02SPeter Avalos shar->quoted_name.s);
28160b4ad09SPeter Avalos }
28260b4ad09SPeter Avalos shar->has_data = 1;
28360b4ad09SPeter Avalos shar->end_of_line = 1;
28460b4ad09SPeter Avalos shar->outpos = 0;
28560b4ad09SPeter Avalos }
28660b4ad09SPeter Avalos break;
28760b4ad09SPeter Avalos case AE_IFDIR:
2888029ab02SPeter Avalos archive_string_sprintf(&shar->work,
2898029ab02SPeter Avalos "mkdir -p %s > /dev/null 2>&1\n",
2908029ab02SPeter Avalos shar->quoted_name.s);
29160b4ad09SPeter Avalos /* Record that we just created this directory. */
29260b4ad09SPeter Avalos free(shar->last_dir);
29360b4ad09SPeter Avalos
29460b4ad09SPeter Avalos shar->last_dir = strdup(name);
29560b4ad09SPeter Avalos /* Trim a trailing '/'. */
29660b4ad09SPeter Avalos pp = strrchr(shar->last_dir, '/');
29760b4ad09SPeter Avalos if (pp != NULL && pp[1] == '\0')
29860b4ad09SPeter Avalos *pp = '\0';
29960b4ad09SPeter Avalos /*
30060b4ad09SPeter Avalos * TODO: Put dir name/mode on a list to be fixed
30160b4ad09SPeter Avalos * up at end of archive.
30260b4ad09SPeter Avalos */
30360b4ad09SPeter Avalos break;
30460b4ad09SPeter Avalos case AE_IFIFO:
3058029ab02SPeter Avalos archive_string_sprintf(&shar->work,
3068029ab02SPeter Avalos "mkfifo %s\n", shar->quoted_name.s);
30760b4ad09SPeter Avalos break;
30860b4ad09SPeter Avalos case AE_IFCHR:
3098029ab02SPeter Avalos archive_string_sprintf(&shar->work,
310c09f92d2SPeter Avalos "mknod %s c %ju %ju\n", shar->quoted_name.s,
311c09f92d2SPeter Avalos (uintmax_t)archive_entry_rdevmajor(entry),
312c09f92d2SPeter Avalos (uintmax_t)archive_entry_rdevminor(entry));
31360b4ad09SPeter Avalos break;
31460b4ad09SPeter Avalos case AE_IFBLK:
3158029ab02SPeter Avalos archive_string_sprintf(&shar->work,
316c09f92d2SPeter Avalos "mknod %s b %ju %ju\n", shar->quoted_name.s,
317c09f92d2SPeter Avalos (uintmax_t)archive_entry_rdevmajor(entry),
318c09f92d2SPeter Avalos (uintmax_t)archive_entry_rdevminor(entry));
31960b4ad09SPeter Avalos break;
32060b4ad09SPeter Avalos default:
32160b4ad09SPeter Avalos return (ARCHIVE_WARN);
32260b4ad09SPeter Avalos }
32360b4ad09SPeter Avalos }
32460b4ad09SPeter Avalos
32560b4ad09SPeter Avalos return (ARCHIVE_OK);
32660b4ad09SPeter Avalos }
32760b4ad09SPeter Avalos
32860b4ad09SPeter Avalos static ssize_t
archive_write_shar_data_sed(struct archive_write * a,const void * buff,size_t n)32960b4ad09SPeter Avalos archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
33060b4ad09SPeter Avalos {
3318029ab02SPeter Avalos static const size_t ensured = 65533;
33260b4ad09SPeter Avalos struct shar *shar;
33360b4ad09SPeter Avalos const char *src;
3348029ab02SPeter Avalos char *buf, *buf_end;
33560b4ad09SPeter Avalos int ret;
33660b4ad09SPeter Avalos size_t written = n;
33760b4ad09SPeter Avalos
33860b4ad09SPeter Avalos shar = (struct shar *)a->format_data;
3398029ab02SPeter Avalos if (!shar->has_data || n == 0)
34060b4ad09SPeter Avalos return (0);
34160b4ad09SPeter Avalos
34260b4ad09SPeter Avalos src = (const char *)buff;
3438029ab02SPeter Avalos
3448029ab02SPeter Avalos /*
3458029ab02SPeter Avalos * ensure is the number of bytes in buffer before expanding the
3468029ab02SPeter Avalos * current character. Each operation writes the current character
3478029ab02SPeter Avalos * and optionally the start-of-new-line marker. This can happen
3488029ab02SPeter Avalos * twice before entering the loop, so make sure three additional
3498029ab02SPeter Avalos * bytes can be written.
3508029ab02SPeter Avalos */
351c09f92d2SPeter Avalos if (archive_string_ensure(&shar->work, ensured + 3) == NULL) {
352c09f92d2SPeter Avalos archive_set_error(&a->archive, ENOMEM, "Out of memory");
353c09f92d2SPeter Avalos return (ARCHIVE_FATAL);
354c09f92d2SPeter Avalos }
3558029ab02SPeter Avalos
3568029ab02SPeter Avalos if (shar->work.length > ensured) {
357c09f92d2SPeter Avalos ret = __archive_write_output(a, shar->work.s,
3588029ab02SPeter Avalos shar->work.length);
3598029ab02SPeter Avalos if (ret != ARCHIVE_OK)
3608029ab02SPeter Avalos return (ARCHIVE_FATAL);
3618029ab02SPeter Avalos archive_string_empty(&shar->work);
3628029ab02SPeter Avalos }
3638029ab02SPeter Avalos buf = shar->work.s + shar->work.length;
3648029ab02SPeter Avalos buf_end = shar->work.s + ensured;
3658029ab02SPeter Avalos
36660b4ad09SPeter Avalos if (shar->end_of_line) {
3678029ab02SPeter Avalos *buf++ = 'X';
36860b4ad09SPeter Avalos shar->end_of_line = 0;
36960b4ad09SPeter Avalos }
3708029ab02SPeter Avalos
3718029ab02SPeter Avalos while (n-- != 0) {
3728029ab02SPeter Avalos if ((*buf++ = *src++) == '\n') {
3738029ab02SPeter Avalos if (n == 0)
37460b4ad09SPeter Avalos shar->end_of_line = 1;
3758029ab02SPeter Avalos else
3768029ab02SPeter Avalos *buf++ = 'X';
3778029ab02SPeter Avalos }
37860b4ad09SPeter Avalos
3798029ab02SPeter Avalos if (buf >= buf_end) {
3808029ab02SPeter Avalos shar->work.length = buf - shar->work.s;
381c09f92d2SPeter Avalos ret = __archive_write_output(a, shar->work.s,
3828029ab02SPeter Avalos shar->work.length);
38360b4ad09SPeter Avalos if (ret != ARCHIVE_OK)
3848029ab02SPeter Avalos return (ARCHIVE_FATAL);
3858029ab02SPeter Avalos archive_string_empty(&shar->work);
3868029ab02SPeter Avalos buf = shar->work.s;
38760b4ad09SPeter Avalos }
38860b4ad09SPeter Avalos }
38960b4ad09SPeter Avalos
3908029ab02SPeter Avalos shar->work.length = buf - shar->work.s;
3918029ab02SPeter Avalos
39260b4ad09SPeter Avalos return (written);
39360b4ad09SPeter Avalos }
39460b4ad09SPeter Avalos
39560b4ad09SPeter Avalos #define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
39660b4ad09SPeter Avalos
39760b4ad09SPeter Avalos static void
uuencode_group(const char _in[3],char out[4])3988029ab02SPeter Avalos uuencode_group(const char _in[3], char out[4])
39960b4ad09SPeter Avalos {
4008029ab02SPeter Avalos const unsigned char *in = (const unsigned char *)_in;
40160b4ad09SPeter Avalos int t;
40260b4ad09SPeter Avalos
4038029ab02SPeter Avalos t = (in[0] << 16) | (in[1] << 8) | in[2];
4048029ab02SPeter Avalos out[0] = UUENC( 0x3f & (t >> 18) );
4058029ab02SPeter Avalos out[1] = UUENC( 0x3f & (t >> 12) );
4068029ab02SPeter Avalos out[2] = UUENC( 0x3f & (t >> 6) );
4078029ab02SPeter Avalos out[3] = UUENC( 0x3f & t );
4088029ab02SPeter Avalos }
4098029ab02SPeter Avalos
410c09f92d2SPeter Avalos static int
_uuencode_line(struct archive_write * a,struct shar * shar,const char * inbuf,size_t len)411c09f92d2SPeter Avalos _uuencode_line(struct archive_write *a, struct shar *shar, const char *inbuf, size_t len)
4128029ab02SPeter Avalos {
413c09f92d2SPeter Avalos char *buf;
4148029ab02SPeter Avalos size_t alloc_len;
4158029ab02SPeter Avalos
4168029ab02SPeter Avalos /* len <= 45 -> expanded to 60 + len byte + new line */
4178029ab02SPeter Avalos alloc_len = shar->work.length + 62;
418c09f92d2SPeter Avalos if (archive_string_ensure(&shar->work, alloc_len) == NULL) {
419c09f92d2SPeter Avalos archive_set_error(&a->archive, ENOMEM, "Out of memory");
420c09f92d2SPeter Avalos return (ARCHIVE_FATAL);
421c09f92d2SPeter Avalos }
4228029ab02SPeter Avalos
4238029ab02SPeter Avalos buf = shar->work.s + shar->work.length;
4248029ab02SPeter Avalos *buf++ = UUENC(len);
4258029ab02SPeter Avalos while (len >= 3) {
4268029ab02SPeter Avalos uuencode_group(inbuf, buf);
4278029ab02SPeter Avalos len -= 3;
4288029ab02SPeter Avalos inbuf += 3;
4298029ab02SPeter Avalos buf += 4;
4308029ab02SPeter Avalos }
4318029ab02SPeter Avalos if (len != 0) {
432c09f92d2SPeter Avalos char tmp_buf[3];
4338029ab02SPeter Avalos tmp_buf[0] = inbuf[0];
4348029ab02SPeter Avalos if (len == 1)
4358029ab02SPeter Avalos tmp_buf[1] = '\0';
4368029ab02SPeter Avalos else
4378029ab02SPeter Avalos tmp_buf[1] = inbuf[1];
4388029ab02SPeter Avalos tmp_buf[2] = '\0';
439c09f92d2SPeter Avalos uuencode_group(tmp_buf, buf);
4408029ab02SPeter Avalos buf += 4;
4418029ab02SPeter Avalos }
4428029ab02SPeter Avalos *buf++ = '\n';
443c09f92d2SPeter Avalos if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62)) {
444c09f92d2SPeter Avalos archive_set_error(&a->archive,
445c09f92d2SPeter Avalos ARCHIVE_ERRNO_MISC, "Buffer overflow");
446c09f92d2SPeter Avalos return (ARCHIVE_FATAL);
44760b4ad09SPeter Avalos }
448c09f92d2SPeter Avalos shar->work.length = buf - shar->work.s;
449c09f92d2SPeter Avalos return (ARCHIVE_OK);
450c09f92d2SPeter Avalos }
451c09f92d2SPeter Avalos
452c09f92d2SPeter Avalos #define uuencode_line(__a, __shar, __inbuf, __len) \
453c09f92d2SPeter Avalos do { \
454c09f92d2SPeter Avalos int r = _uuencode_line(__a, __shar, __inbuf, __len); \
455c09f92d2SPeter Avalos if (r != ARCHIVE_OK) \
456c09f92d2SPeter Avalos return (ARCHIVE_FATAL); \
457c09f92d2SPeter Avalos } while (0)
45860b4ad09SPeter Avalos
45960b4ad09SPeter Avalos static ssize_t
archive_write_shar_data_uuencode(struct archive_write * a,const void * buff,size_t length)46060b4ad09SPeter Avalos archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
46160b4ad09SPeter Avalos size_t length)
46260b4ad09SPeter Avalos {
46360b4ad09SPeter Avalos struct shar *shar;
46460b4ad09SPeter Avalos const char *src;
46560b4ad09SPeter Avalos size_t n;
46660b4ad09SPeter Avalos int ret;
46760b4ad09SPeter Avalos
46860b4ad09SPeter Avalos shar = (struct shar *)a->format_data;
46960b4ad09SPeter Avalos if (!shar->has_data)
47060b4ad09SPeter Avalos return (ARCHIVE_OK);
47160b4ad09SPeter Avalos src = (const char *)buff;
4728029ab02SPeter Avalos
4738029ab02SPeter Avalos if (shar->outpos != 0) {
4748029ab02SPeter Avalos n = 45 - shar->outpos;
4758029ab02SPeter Avalos if (n > length)
47660b4ad09SPeter Avalos n = length;
4778029ab02SPeter Avalos memcpy(shar->outbuff + shar->outpos, src, n);
4788029ab02SPeter Avalos if (shar->outpos + n < 45) {
4798029ab02SPeter Avalos shar->outpos += n;
4808029ab02SPeter Avalos return length;
4818029ab02SPeter Avalos }
482c09f92d2SPeter Avalos uuencode_line(a, shar, shar->outbuff, 45);
4838029ab02SPeter Avalos src += n;
4848029ab02SPeter Avalos n = length - n;
4858029ab02SPeter Avalos } else {
4868029ab02SPeter Avalos n = length;
48760b4ad09SPeter Avalos }
48860b4ad09SPeter Avalos
4898029ab02SPeter Avalos while (n >= 45) {
490c09f92d2SPeter Avalos uuencode_line(a, shar, src, 45);
4918029ab02SPeter Avalos src += 45;
4928029ab02SPeter Avalos n -= 45;
4938029ab02SPeter Avalos
4948029ab02SPeter Avalos if (shar->work.length < 65536)
4958029ab02SPeter Avalos continue;
496c09f92d2SPeter Avalos ret = __archive_write_output(a, shar->work.s,
4978029ab02SPeter Avalos shar->work.length);
4988029ab02SPeter Avalos if (ret != ARCHIVE_OK)
4998029ab02SPeter Avalos return (ARCHIVE_FATAL);
5008029ab02SPeter Avalos archive_string_empty(&shar->work);
5018029ab02SPeter Avalos }
5028029ab02SPeter Avalos if (n != 0) {
5038029ab02SPeter Avalos memcpy(shar->outbuff, src, n);
5048029ab02SPeter Avalos shar->outpos = n;
50560b4ad09SPeter Avalos }
50660b4ad09SPeter Avalos return (length);
50760b4ad09SPeter Avalos }
50860b4ad09SPeter Avalos
50960b4ad09SPeter Avalos static int
archive_write_shar_finish_entry(struct archive_write * a)51060b4ad09SPeter Avalos archive_write_shar_finish_entry(struct archive_write *a)
51160b4ad09SPeter Avalos {
51260b4ad09SPeter Avalos const char *g, *p, *u;
51360b4ad09SPeter Avalos struct shar *shar;
51460b4ad09SPeter Avalos int ret;
51560b4ad09SPeter Avalos
51660b4ad09SPeter Avalos shar = (struct shar *)a->format_data;
51760b4ad09SPeter Avalos if (shar->entry == NULL)
51860b4ad09SPeter Avalos return (0);
51960b4ad09SPeter Avalos
52060b4ad09SPeter Avalos if (shar->dump) {
52160b4ad09SPeter Avalos /* Finish uuencoded data. */
52260b4ad09SPeter Avalos if (shar->has_data) {
5238029ab02SPeter Avalos if (shar->outpos > 0)
524c09f92d2SPeter Avalos uuencode_line(a, shar, shar->outbuff,
5258029ab02SPeter Avalos shar->outpos);
5268029ab02SPeter Avalos archive_strcat(&shar->work, "`\nend\n");
5278029ab02SPeter Avalos archive_strcat(&shar->work, "SHAR_END\n");
52860b4ad09SPeter Avalos }
52960b4ad09SPeter Avalos /* Restore file mode, owner, flags. */
53060b4ad09SPeter Avalos /*
53160b4ad09SPeter Avalos * TODO: Don't immediately restore mode for
53260b4ad09SPeter Avalos * directories; defer that to end of script.
53360b4ad09SPeter Avalos */
5348029ab02SPeter Avalos archive_string_sprintf(&shar->work, "chmod %o ",
535c09f92d2SPeter Avalos (unsigned int)(archive_entry_mode(shar->entry) & 07777));
5368029ab02SPeter Avalos shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1);
5378029ab02SPeter Avalos archive_strcat(&shar->work, "\n");
53860b4ad09SPeter Avalos
53960b4ad09SPeter Avalos u = archive_entry_uname(shar->entry);
54060b4ad09SPeter Avalos g = archive_entry_gname(shar->entry);
54160b4ad09SPeter Avalos if (u != NULL || g != NULL) {
5428029ab02SPeter Avalos archive_strcat(&shar->work, "chown ");
5438029ab02SPeter Avalos if (u != NULL)
5448029ab02SPeter Avalos shar_quote(&shar->work, u, 1);
5458029ab02SPeter Avalos if (g != NULL) {
5468029ab02SPeter Avalos archive_strcat(&shar->work, ":");
5478029ab02SPeter Avalos shar_quote(&shar->work, g, 1);
5488029ab02SPeter Avalos }
5496b384f39SPeter Avalos archive_strcat(&shar->work, " ");
5508029ab02SPeter Avalos shar_quote(&shar->work,
5518029ab02SPeter Avalos archive_entry_pathname(shar->entry), 1);
5528029ab02SPeter Avalos archive_strcat(&shar->work, "\n");
55360b4ad09SPeter Avalos }
55460b4ad09SPeter Avalos
55560b4ad09SPeter Avalos if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
556ce5fd9c5SPeter Avalos archive_string_sprintf(&shar->work, "chflags %s ", p);
5578029ab02SPeter Avalos shar_quote(&shar->work,
5588029ab02SPeter Avalos archive_entry_pathname(shar->entry), 1);
5598029ab02SPeter Avalos archive_strcat(&shar->work, "\n");
56060b4ad09SPeter Avalos }
56160b4ad09SPeter Avalos
56260b4ad09SPeter Avalos /* TODO: restore ACLs */
56360b4ad09SPeter Avalos
56460b4ad09SPeter Avalos } else {
56560b4ad09SPeter Avalos if (shar->has_data) {
56660b4ad09SPeter Avalos /* Finish sed-encoded data: ensure last line ends. */
5678029ab02SPeter Avalos if (!shar->end_of_line)
5688029ab02SPeter Avalos archive_strappend_char(&shar->work, '\n');
5698029ab02SPeter Avalos archive_strcat(&shar->work, "SHAR_END\n");
57060b4ad09SPeter Avalos }
57160b4ad09SPeter Avalos }
57260b4ad09SPeter Avalos
57360b4ad09SPeter Avalos archive_entry_free(shar->entry);
57460b4ad09SPeter Avalos shar->entry = NULL;
5758029ab02SPeter Avalos
5768029ab02SPeter Avalos if (shar->work.length < 65536)
5778029ab02SPeter Avalos return (ARCHIVE_OK);
5788029ab02SPeter Avalos
579c09f92d2SPeter Avalos ret = __archive_write_output(a, shar->work.s, shar->work.length);
5808029ab02SPeter Avalos if (ret != ARCHIVE_OK)
5818029ab02SPeter Avalos return (ARCHIVE_FATAL);
5828029ab02SPeter Avalos archive_string_empty(&shar->work);
5838029ab02SPeter Avalos
5848029ab02SPeter Avalos return (ARCHIVE_OK);
58560b4ad09SPeter Avalos }
58660b4ad09SPeter Avalos
58760b4ad09SPeter Avalos static int
archive_write_shar_close(struct archive_write * a)588c09f92d2SPeter Avalos archive_write_shar_close(struct archive_write *a)
58960b4ad09SPeter Avalos {
59060b4ad09SPeter Avalos struct shar *shar;
59160b4ad09SPeter Avalos int ret;
59260b4ad09SPeter Avalos
59360b4ad09SPeter Avalos /*
59460b4ad09SPeter Avalos * TODO: Accumulate list of directory names/modes and
59560b4ad09SPeter Avalos * fix them all up at end-of-archive.
59660b4ad09SPeter Avalos */
59760b4ad09SPeter Avalos
59860b4ad09SPeter Avalos shar = (struct shar *)a->format_data;
59960b4ad09SPeter Avalos
60060b4ad09SPeter Avalos /*
60160b4ad09SPeter Avalos * Only write the end-of-archive markers if the archive was
60260b4ad09SPeter Avalos * actually started. This avoids problems if someone sets
60360b4ad09SPeter Avalos * shar format, then sets another format (which would invoke
60460b4ad09SPeter Avalos * shar_finish to free the format-specific data).
60560b4ad09SPeter Avalos */
6068029ab02SPeter Avalos if (shar->wrote_header == 0)
6078029ab02SPeter Avalos return (ARCHIVE_OK);
6088029ab02SPeter Avalos
6098029ab02SPeter Avalos archive_strcat(&shar->work, "exit\n");
6108029ab02SPeter Avalos
611c09f92d2SPeter Avalos ret = __archive_write_output(a, shar->work.s, shar->work.length);
61260b4ad09SPeter Avalos if (ret != ARCHIVE_OK)
6138029ab02SPeter Avalos return (ARCHIVE_FATAL);
6148029ab02SPeter Avalos
61560b4ad09SPeter Avalos /* Shar output is never padded. */
61660b4ad09SPeter Avalos archive_write_set_bytes_in_last_block(&a->archive, 1);
61760b4ad09SPeter Avalos /*
61860b4ad09SPeter Avalos * TODO: shar should also suppress padding of
61960b4ad09SPeter Avalos * uncompressed data within gzip/bzip2 streams.
62060b4ad09SPeter Avalos */
6218029ab02SPeter Avalos
62260b4ad09SPeter Avalos return (ARCHIVE_OK);
62360b4ad09SPeter Avalos }
62460b4ad09SPeter Avalos
62560b4ad09SPeter Avalos static int
archive_write_shar_free(struct archive_write * a)626c09f92d2SPeter Avalos archive_write_shar_free(struct archive_write *a)
62760b4ad09SPeter Avalos {
62860b4ad09SPeter Avalos struct shar *shar;
62960b4ad09SPeter Avalos
63060b4ad09SPeter Avalos shar = (struct shar *)a->format_data;
6318029ab02SPeter Avalos if (shar == NULL)
6328029ab02SPeter Avalos return (ARCHIVE_OK);
6338029ab02SPeter Avalos
63460b4ad09SPeter Avalos archive_entry_free(shar->entry);
63560b4ad09SPeter Avalos free(shar->last_dir);
63660b4ad09SPeter Avalos archive_string_free(&(shar->work));
6378029ab02SPeter Avalos archive_string_free(&(shar->quoted_name));
63860b4ad09SPeter Avalos free(shar);
63960b4ad09SPeter Avalos a->format_data = NULL;
64060b4ad09SPeter Avalos return (ARCHIVE_OK);
64160b4ad09SPeter Avalos }
642