2004-02-09 23:22:54 +00:00
|
|
|
/*-
|
2007-01-09 08:05:56 +00:00
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2008-08-31 07:10:40 +00:00
|
|
|
* Copyright (c) 2008 Joerg Sonnenberger
|
2004-02-09 23:22:54 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2007-01-09 08:05:56 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-02-09 23:22:54 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#include "archive_platform.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <errno.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <stdio.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <stdlib.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <string.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
#include "archive.h"
|
|
|
|
#include "archive_entry.h"
|
|
|
|
#include "archive_private.h"
|
2007-03-03 07:37:37 +00:00
|
|
|
#include "archive_write_private.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
struct shar {
|
|
|
|
int dump;
|
|
|
|
int end_of_line;
|
|
|
|
struct archive_entry *entry;
|
|
|
|
int has_data;
|
2004-03-06 05:44:13 +00:00
|
|
|
char *last_dir;
|
2008-08-31 07:10:40 +00:00
|
|
|
|
|
|
|
/* Line buffer for uuencoded dump format */
|
|
|
|
char outbuff[45];
|
2004-02-09 23:22:54 +00:00
|
|
|
size_t outpos;
|
2008-08-31 07:10:40 +00:00
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
int wrote_header;
|
2004-11-05 05:26:30 +00:00
|
|
|
struct archive_string work;
|
2008-08-31 07:10:40 +00:00
|
|
|
struct archive_string quoted_name;
|
2004-02-09 23:22:54 +00:00
|
|
|
};
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
static int archive_write_shar_finish(struct archive_write *);
|
|
|
|
static int archive_write_shar_destroy(struct archive_write *);
|
|
|
|
static int archive_write_shar_header(struct archive_write *,
|
2004-04-13 23:45:37 +00:00
|
|
|
struct archive_entry *);
|
2007-03-03 07:37:37 +00:00
|
|
|
static ssize_t archive_write_shar_data_sed(struct archive_write *,
|
2004-04-13 23:45:37 +00:00
|
|
|
const void * buff, size_t);
|
2007-03-03 07:37:37 +00:00
|
|
|
static ssize_t archive_write_shar_data_uuencode(struct archive_write *,
|
2004-04-13 23:45:37 +00:00
|
|
|
const void * buff, size_t);
|
2007-03-03 07:37:37 +00:00
|
|
|
static int archive_write_shar_finish_entry(struct archive_write *);
|
2004-04-13 23:45:37 +00:00
|
|
|
|
2008-08-31 07:10:40 +00:00
|
|
|
/*
|
|
|
|
* Copy the given string to the buffer, quoting all shell meta characters
|
|
|
|
* found.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
shar_quote(struct archive_string *buf, const char *str, int in_shell)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2008-08-31 07:10:40 +00:00
|
|
|
static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
while (*str != '\0') {
|
|
|
|
if ((len = strcspn(str, meta)) != 0) {
|
|
|
|
archive_strncat(buf, str, len);
|
|
|
|
str += len;
|
|
|
|
} else if (*str == '\n') {
|
|
|
|
if (in_shell)
|
|
|
|
archive_strcat(buf, "\"\n\"");
|
|
|
|
else
|
|
|
|
archive_strcat(buf, "\\n");
|
|
|
|
++str;
|
|
|
|
} else {
|
|
|
|
archive_strappend_char(buf, '\\');
|
|
|
|
archive_strappend_char(buf, *str);
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set output format to 'shar' format.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_set_format_shar(struct archive *_a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
2004-02-09 23:22:54 +00:00
|
|
|
struct shar *shar;
|
|
|
|
|
|
|
|
/* If someone else was already registered, unregister them. */
|
2007-03-03 07:37:37 +00:00
|
|
|
if (a->format_destroy != NULL)
|
|
|
|
(a->format_destroy)(a);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
shar = (struct shar *)malloc(sizeof(*shar));
|
2004-02-09 23:22:54 +00:00
|
|
|
if (shar == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data");
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
memset(shar, 0, sizeof(*shar));
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_init(&shar->work);
|
|
|
|
archive_string_init(&shar->quoted_name);
|
2004-02-09 23:22:54 +00:00
|
|
|
a->format_data = shar;
|
|
|
|
|
|
|
|
a->pad_uncompressed = 0;
|
2009-03-06 05:58:56 +00:00
|
|
|
a->format_name = "shar";
|
2004-02-09 23:22:54 +00:00
|
|
|
a->format_write_header = archive_write_shar_header;
|
|
|
|
a->format_finish = archive_write_shar_finish;
|
2007-03-03 07:37:37 +00:00
|
|
|
a->format_destroy = archive_write_shar_destroy;
|
2004-02-09 23:22:54 +00:00
|
|
|
a->format_write_data = archive_write_shar_data_sed;
|
|
|
|
a->format_finish_entry = archive_write_shar_finish_entry;
|
2008-03-15 11:04:45 +00:00
|
|
|
a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE;
|
|
|
|
a->archive.archive_format_name = "shar";
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An alternate 'shar' that uses uudecode instead of 'sed' to encode
|
|
|
|
* file contents and can therefore be used to archive binary files.
|
|
|
|
* In addition, this variant also attempts to restore ownership, file modes,
|
|
|
|
* and other extended file information.
|
|
|
|
*/
|
|
|
|
int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_set_format_shar_dump(struct archive *_a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2007-03-03 07:37:37 +00:00
|
|
|
struct archive_write *a = (struct archive_write *)_a;
|
2004-04-23 17:13:40 +00:00
|
|
|
struct shar *shar;
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_set_format_shar(&a->archive);
|
2006-11-10 06:39:46 +00:00
|
|
|
shar = (struct shar *)a->format_data;
|
2004-04-23 17:13:40 +00:00
|
|
|
shar->dump = 1;
|
2004-02-09 23:22:54 +00:00
|
|
|
a->format_write_data = archive_write_shar_data_uuencode;
|
2008-03-15 11:04:45 +00:00
|
|
|
a->archive.archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
|
|
|
|
a->archive.archive_format_name = "shar dump";
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
const char *linkname;
|
|
|
|
const char *name;
|
2004-03-06 05:44:13 +00:00
|
|
|
char *p, *pp;
|
2004-02-09 23:22:54 +00:00
|
|
|
struct shar *shar;
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
shar = (struct shar *)a->format_data;
|
2004-02-09 23:22:54 +00:00
|
|
|
if (!shar->wrote_header) {
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_strcat(&shar->work, "#!/bin/sh\n");
|
|
|
|
archive_strcat(&shar->work, "# This is a shell archive\n");
|
2004-02-09 23:22:54 +00:00
|
|
|
shar->wrote_header = 1;
|
|
|
|
}
|
|
|
|
|
2004-03-06 05:44:13 +00:00
|
|
|
/* Save the entry for the closing. */
|
2004-02-09 23:22:54 +00:00
|
|
|
if (shar->entry)
|
|
|
|
archive_entry_free(shar->entry);
|
|
|
|
shar->entry = archive_entry_clone(entry);
|
|
|
|
name = archive_entry_pathname(entry);
|
|
|
|
|
2004-03-06 05:44:13 +00:00
|
|
|
/* Handle some preparatory issues. */
|
2007-05-29 01:00:21 +00:00
|
|
|
switch(archive_entry_filetype(entry)) {
|
|
|
|
case AE_IFREG:
|
2004-03-06 05:44:13 +00:00
|
|
|
/* Only regular files have non-zero size. */
|
|
|
|
break;
|
2007-05-29 01:00:21 +00:00
|
|
|
case AE_IFDIR:
|
2004-04-20 20:21:36 +00:00
|
|
|
archive_entry_set_size(entry, 0);
|
|
|
|
/* Don't bother trying to recreate '.' */
|
|
|
|
if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0)
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
break;
|
2007-05-29 01:00:21 +00:00
|
|
|
case AE_IFIFO:
|
|
|
|
case AE_IFCHR:
|
|
|
|
case AE_IFBLK:
|
2004-03-06 05:44:13 +00:00
|
|
|
/* All other file types have zero size in the archive. */
|
|
|
|
archive_entry_set_size(entry, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
archive_entry_set_size(entry, 0);
|
|
|
|
if (archive_entry_hardlink(entry) == NULL &&
|
|
|
|
archive_entry_symlink(entry) == NULL) {
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
|
2004-03-06 05:44:13 +00:00
|
|
|
"shar format cannot archive this");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_empty(&shar->quoted_name);
|
|
|
|
shar_quote(&shar->quoted_name, name, 1);
|
|
|
|
|
2004-03-06 05:44:13 +00:00
|
|
|
/* Stock preparation for all file types. */
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work, "echo x %s\n", shar->quoted_name.s);
|
2004-03-06 05:44:13 +00:00
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
if (archive_entry_filetype(entry) != AE_IFDIR) {
|
2004-03-06 05:44:13 +00:00
|
|
|
/* Try to create the dir. */
|
|
|
|
p = strdup(name);
|
|
|
|
pp = strrchr(p, '/');
|
2004-04-20 20:21:36 +00:00
|
|
|
/* If there is a / character, try to create the dir. */
|
|
|
|
if (pp != NULL) {
|
2004-03-06 05:44:13 +00:00
|
|
|
*pp = '\0';
|
|
|
|
|
2004-04-20 20:21:36 +00:00
|
|
|
/* Try to avoid a lot of redundant mkdir commands. */
|
|
|
|
if (strcmp(p, ".") == 0) {
|
|
|
|
/* Don't try to "mkdir ." */
|
2007-05-19 05:09:09 +00:00
|
|
|
free(p);
|
2004-04-20 20:21:36 +00:00
|
|
|
} else if (shar->last_dir == NULL) {
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_strcat(&shar->work, "mkdir -p ");
|
|
|
|
shar_quote(&shar->work, p, 1);
|
|
|
|
archive_strcat(&shar->work,
|
|
|
|
" > /dev/null 2>&1\n");
|
2004-04-20 20:21:36 +00:00
|
|
|
shar->last_dir = p;
|
|
|
|
} else if (strcmp(p, shar->last_dir) == 0) {
|
|
|
|
/* We've already created this exact dir. */
|
|
|
|
free(p);
|
|
|
|
} else if (strlen(p) < strlen(shar->last_dir) &&
|
|
|
|
strncmp(p, shar->last_dir, strlen(p)) == 0) {
|
|
|
|
/* We've already created a subdir. */
|
|
|
|
free(p);
|
|
|
|
} else {
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_strcat(&shar->work, "mkdir -p ");
|
|
|
|
shar_quote(&shar->work, p, 1);
|
|
|
|
archive_strcat(&shar->work,
|
|
|
|
" > /dev/null 2>&1\n");
|
2004-04-20 20:21:36 +00:00
|
|
|
shar->last_dir = p;
|
|
|
|
}
|
2007-05-19 05:09:09 +00:00
|
|
|
} else {
|
|
|
|
free(p);
|
2004-03-06 05:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle file-type specific issues. */
|
2004-02-09 23:22:54 +00:00
|
|
|
shar->has_data = 0;
|
2004-11-05 05:26:30 +00:00
|
|
|
if ((linkname = archive_entry_hardlink(entry)) != NULL) {
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_strcat(&shar->work, "ln -f ");
|
|
|
|
shar_quote(&shar->work, linkname, 1);
|
|
|
|
archive_string_sprintf(&shar->work, " %s\n",
|
|
|
|
shar->quoted_name.s);
|
2004-11-05 05:26:30 +00:00
|
|
|
} else if ((linkname = archive_entry_symlink(entry)) != NULL) {
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_strcat(&shar->work, "ln -fs ");
|
|
|
|
shar_quote(&shar->work, linkname, 1);
|
|
|
|
archive_string_sprintf(&shar->work, " %s\n",
|
|
|
|
shar->quoted_name.s);
|
2004-11-05 05:26:30 +00:00
|
|
|
} else {
|
2007-05-29 01:00:21 +00:00
|
|
|
switch(archive_entry_filetype(entry)) {
|
|
|
|
case AE_IFREG:
|
2004-11-05 05:26:30 +00:00
|
|
|
if (archive_entry_size(entry) == 0) {
|
|
|
|
/* More portable than "touch." */
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work,
|
|
|
|
"test -e \"%s\" || :> \"%s\"\n",
|
|
|
|
shar->quoted_name.s, shar->quoted_name.s);
|
2004-11-05 05:26:30 +00:00
|
|
|
} else {
|
2004-02-09 23:22:54 +00:00
|
|
|
if (shar->dump) {
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work,
|
|
|
|
"uudecode -p > %s << 'SHAR_END'\n",
|
|
|
|
shar->quoted_name.s);
|
|
|
|
archive_string_sprintf(&shar->work,
|
|
|
|
"begin %o ",
|
|
|
|
archive_entry_mode(entry) & 0777);
|
|
|
|
shar_quote(&shar->work, name, 0);
|
|
|
|
archive_strcat(&shar->work, "\n");
|
2004-11-05 05:26:30 +00:00
|
|
|
} else {
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work,
|
2004-02-09 23:22:54 +00:00
|
|
|
"sed 's/^X//' > %s << 'SHAR_END'\n",
|
2008-08-31 07:10:40 +00:00
|
|
|
shar->quoted_name.s);
|
2004-11-05 05:26:30 +00:00
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
shar->has_data = 1;
|
|
|
|
shar->end_of_line = 1;
|
|
|
|
shar->outpos = 0;
|
|
|
|
}
|
|
|
|
break;
|
2007-05-29 01:00:21 +00:00
|
|
|
case AE_IFDIR:
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work,
|
|
|
|
"mkdir -p %s > /dev/null 2>&1\n",
|
|
|
|
shar->quoted_name.s);
|
2004-03-06 05:44:13 +00:00
|
|
|
/* Record that we just created this directory. */
|
|
|
|
if (shar->last_dir != NULL)
|
|
|
|
free(shar->last_dir);
|
|
|
|
|
|
|
|
shar->last_dir = strdup(name);
|
|
|
|
/* Trim a trailing '/'. */
|
|
|
|
pp = strrchr(shar->last_dir, '/');
|
|
|
|
if (pp != NULL && pp[1] == '\0')
|
|
|
|
*pp = '\0';
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* TODO: Put dir name/mode on a list to be fixed
|
|
|
|
* up at end of archive.
|
|
|
|
*/
|
|
|
|
break;
|
2007-05-29 01:00:21 +00:00
|
|
|
case AE_IFIFO:
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work,
|
|
|
|
"mkfifo %s\n", shar->quoted_name.s);
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
2007-05-29 01:00:21 +00:00
|
|
|
case AE_IFCHR:
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work,
|
|
|
|
"mknod %s c %d %d\n", shar->quoted_name.s,
|
2004-06-27 18:38:13 +00:00
|
|
|
archive_entry_rdevmajor(entry),
|
|
|
|
archive_entry_rdevminor(entry));
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
2007-05-29 01:00:21 +00:00
|
|
|
case AE_IFBLK:
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work,
|
|
|
|
"mknod %s b %d %d\n", shar->quoted_name.s,
|
2004-06-27 18:38:13 +00:00
|
|
|
archive_entry_rdevmajor(entry),
|
|
|
|
archive_entry_rdevminor(entry));
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
2006-11-15 05:14:20 +00:00
|
|
|
static ssize_t
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2008-08-31 07:10:40 +00:00
|
|
|
static const size_t ensured = 65533;
|
2004-02-09 23:22:54 +00:00
|
|
|
struct shar *shar;
|
|
|
|
const char *src;
|
2008-08-31 07:10:40 +00:00
|
|
|
char *buf, *buf_end;
|
2004-11-05 05:26:30 +00:00
|
|
|
int ret;
|
2006-11-15 05:20:14 +00:00
|
|
|
size_t written = n;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
shar = (struct shar *)a->format_data;
|
2008-08-31 07:10:40 +00:00
|
|
|
if (!shar->has_data || n == 0)
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
src = (const char *)buff;
|
2008-08-31 07:10:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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';
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2008-08-31 07:10:40 +00:00
|
|
|
if (buf >= buf_end) {
|
|
|
|
shar->work.length = buf - shar->work.s;
|
|
|
|
ret = (*a->compressor.write)(a, shar->work.s,
|
|
|
|
shar->work.length);
|
2004-11-05 05:26:30 +00:00
|
|
|
if (ret != ARCHIVE_OK)
|
2008-08-31 07:10:40 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
archive_string_empty(&shar->work);
|
|
|
|
buf = shar->work.s;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-31 07:10:40 +00:00
|
|
|
shar->work.length = buf - shar->work.s;
|
|
|
|
|
2006-11-15 05:14:20 +00:00
|
|
|
return (written);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
|
|
|
|
|
|
|
|
static void
|
2008-08-31 07:10:40 +00:00
|
|
|
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 void
|
|
|
|
uuencode_line(struct shar *shar, const char *inbuf, size_t len)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2008-08-31 07:10:40 +00:00
|
|
|
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) > (ptrdiff_t)(shar->work.length + 62))
|
|
|
|
__archive_errx(1, "Buffer overflow");
|
|
|
|
shar->work.length = buf - shar->work.s;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2006-11-15 05:14:20 +00:00
|
|
|
static ssize_t
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
|
2004-02-09 23:22:54 +00:00
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
struct shar *shar;
|
|
|
|
const char *src;
|
|
|
|
size_t n;
|
2004-11-05 05:26:30 +00:00
|
|
|
int ret;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
shar = (struct shar *)a->format_data;
|
2004-02-09 23:22:54 +00:00
|
|
|
if (!shar->has_data)
|
|
|
|
return (ARCHIVE_OK);
|
2006-11-10 06:39:46 +00:00
|
|
|
src = (const char *)buff;
|
2008-08-31 07:10:40 +00:00
|
|
|
|
|
|
|
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;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2008-08-31 07:10:40 +00:00
|
|
|
uuencode_line(shar, shar->outbuff, 45);
|
|
|
|
src += n;
|
|
|
|
n = length - n;
|
|
|
|
} else {
|
|
|
|
n = length;
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2008-08-31 07:10:40 +00:00
|
|
|
while (n >= 45) {
|
|
|
|
uuencode_line(shar, src, 45);
|
|
|
|
src += 45;
|
|
|
|
n -= 45;
|
|
|
|
|
|
|
|
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;
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
2006-11-15 05:14:20 +00:00
|
|
|
return (length);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_shar_finish_entry(struct archive_write *a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
const char *g, *p, *u;
|
|
|
|
struct shar *shar;
|
2004-11-05 05:26:30 +00:00
|
|
|
int ret;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
shar = (struct shar *)a->format_data;
|
2004-02-09 23:22:54 +00:00
|
|
|
if (shar->entry == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (shar->dump) {
|
|
|
|
/* Finish uuencoded data. */
|
|
|
|
if (shar->has_data) {
|
2008-08-31 07:10:40 +00:00
|
|
|
if (shar->outpos > 0)
|
|
|
|
uuencode_line(shar, shar->outbuff,
|
|
|
|
shar->outpos);
|
|
|
|
archive_strcat(&shar->work, "`\nend\n");
|
|
|
|
archive_strcat(&shar->work, "SHAR_END\n");
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
/* Restore file mode, owner, flags. */
|
|
|
|
/*
|
|
|
|
* TODO: Don't immediately restore mode for
|
|
|
|
* directories; defer that to end of script.
|
|
|
|
*/
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work, "chmod %o ",
|
|
|
|
archive_entry_mode(shar->entry) & 07777);
|
|
|
|
shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1);
|
|
|
|
archive_strcat(&shar->work, "\n");
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
u = archive_entry_uname(shar->entry);
|
|
|
|
g = archive_entry_gname(shar->entry);
|
|
|
|
if (u != NULL || g != NULL) {
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_strcat(&shar->work, "chown ");
|
|
|
|
if (u != NULL)
|
|
|
|
shar_quote(&shar->work, u, 1);
|
|
|
|
if (g != NULL) {
|
|
|
|
archive_strcat(&shar->work, ":");
|
|
|
|
shar_quote(&shar->work, g, 1);
|
|
|
|
}
|
|
|
|
shar_quote(&shar->work,
|
|
|
|
archive_entry_pathname(shar->entry), 1);
|
|
|
|
archive_strcat(&shar->work, "\n");
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2004-04-26 23:37:54 +00:00
|
|
|
if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_sprintf(&shar->work, "chflags %s ",
|
|
|
|
p, archive_entry_pathname(shar->entry));
|
|
|
|
shar_quote(&shar->work,
|
|
|
|
archive_entry_pathname(shar->entry), 1);
|
|
|
|
archive_strcat(&shar->work, "\n");
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: restore ACLs */
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (shar->has_data) {
|
|
|
|
/* Finish sed-encoded data: ensure last line ends. */
|
2008-08-31 07:10:40 +00:00
|
|
|
if (!shar->end_of_line)
|
|
|
|
archive_strappend_char(&shar->work, '\n');
|
|
|
|
archive_strcat(&shar->work, "SHAR_END\n");
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
archive_entry_free(shar->entry);
|
|
|
|
shar->entry = NULL;
|
2008-08-31 07:10:40 +00:00
|
|
|
|
|
|
|
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);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-03-03 07:37:37 +00:00
|
|
|
archive_write_shar_finish(struct archive_write *a)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
struct shar *shar;
|
2004-11-05 05:26:30 +00:00
|
|
|
int ret;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: Accumulate list of directory names/modes and
|
|
|
|
* fix them all up at end-of-archive.
|
|
|
|
*/
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
shar = (struct shar *)a->format_data;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only write the end-of-archive markers if the archive was
|
|
|
|
* actually started. This avoids problems if someone sets
|
|
|
|
* shar format, then sets another format (which would invoke
|
|
|
|
* shar_finish to free the format-specific data).
|
|
|
|
*/
|
2008-08-31 07:10:40 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2007-03-03 07:37:37 +00:00
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
archive_write_shar_destroy(struct archive_write *a)
|
|
|
|
{
|
|
|
|
struct shar *shar;
|
|
|
|
|
|
|
|
shar = (struct shar *)a->format_data;
|
2008-08-31 07:10:40 +00:00
|
|
|
if (shar == NULL)
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
|
|
|
|
archive_entry_free(shar->entry);
|
|
|
|
free(shar->last_dir);
|
2004-11-05 05:26:30 +00:00
|
|
|
archive_string_free(&(shar->work));
|
2008-08-31 07:10:40 +00:00
|
|
|
archive_string_free(&(shar->quoted_name));
|
2004-02-09 23:22:54 +00:00
|
|
|
free(shar);
|
|
|
|
a->format_data = NULL;
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|