1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * Copyright (c) 2008 Joerg Sonnenberger
4228753Smm * All rights reserved.
5228753Smm *
6228753Smm * Redistribution and use in source and binary forms, with or without
7228753Smm * modification, are permitted provided that the following conditions
8228753Smm * are met:
9228753Smm * 1. Redistributions of source code must retain the above copyright
10228753Smm *    notice, this list of conditions and the following disclaimer.
11228753Smm * 2. Redistributions in binary form must reproduce the above copyright
12228753Smm *    notice, this list of conditions and the following disclaimer in the
13228753Smm *    documentation and/or other materials provided with the distribution.
14228753Smm *
15228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25228753Smm */
26228753Smm
27228753Smm#include "archive_platform.h"
28229592Smm__FBSDID("$FreeBSD$");
29228753Smm
30228753Smm#ifdef HAVE_ERRNO_H
31228753Smm#include <errno.h>
32228753Smm#endif
33228753Smm#include <stdio.h>
34228753Smm#ifdef HAVE_STDLIB_H
35228753Smm#include <stdlib.h>
36228753Smm#endif
37228753Smm#ifdef HAVE_STRING_H
38228753Smm#include <string.h>
39228753Smm#endif
40228753Smm
41228753Smm#include "archive.h"
42228753Smm#include "archive_entry.h"
43228753Smm#include "archive_private.h"
44228753Smm#include "archive_write_private.h"
45228753Smm
46228753Smmstruct shar {
47228753Smm	int			 dump;
48228753Smm	int			 end_of_line;
49228753Smm	struct archive_entry	*entry;
50228753Smm	int			 has_data;
51228753Smm	char			*last_dir;
52228753Smm
53228753Smm	/* Line buffer for uuencoded dump format */
54228753Smm	char			 outbuff[45];
55228753Smm	size_t			 outpos;
56228753Smm
57228753Smm	int			 wrote_header;
58228753Smm	struct archive_string	 work;
59228753Smm	struct archive_string	 quoted_name;
60228753Smm};
61228753Smm
62228753Smmstatic int	archive_write_shar_finish(struct archive_write *);
63228753Smmstatic int	archive_write_shar_destroy(struct archive_write *);
64228753Smmstatic int	archive_write_shar_header(struct archive_write *,
65228753Smm		    struct archive_entry *);
66228753Smmstatic ssize_t	archive_write_shar_data_sed(struct archive_write *,
67228753Smm		    const void * buff, size_t);
68228753Smmstatic ssize_t	archive_write_shar_data_uuencode(struct archive_write *,
69228753Smm		    const void * buff, size_t);
70228753Smmstatic int	archive_write_shar_finish_entry(struct archive_write *);
71228753Smm
72228753Smm/*
73228753Smm * Copy the given string to the buffer, quoting all shell meta characters
74228753Smm * found.
75228753Smm */
76228753Smmstatic void
77228753Smmshar_quote(struct archive_string *buf, const char *str, int in_shell)
78228753Smm{
79228753Smm	static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
80228753Smm	size_t len;
81228753Smm
82228753Smm	while (*str != '\0') {
83228753Smm		if ((len = strcspn(str, meta)) != 0) {
84228753Smm			archive_strncat(buf, str, len);
85228753Smm			str += len;
86228753Smm		} else if (*str == '\n') {
87228753Smm			if (in_shell)
88228753Smm				archive_strcat(buf, "\"\n\"");
89228753Smm			else
90228753Smm				archive_strcat(buf, "\\n");
91228753Smm			++str;
92228753Smm		} else {
93228753Smm			archive_strappend_char(buf, '\\');
94228753Smm			archive_strappend_char(buf, *str);
95228753Smm			++str;
96228753Smm		}
97228753Smm	}
98228753Smm}
99228753Smm
100228753Smm/*
101228753Smm * Set output format to 'shar' format.
102228753Smm */
103228753Smmint
104228753Smmarchive_write_set_format_shar(struct archive *_a)
105228753Smm{
106228753Smm	struct archive_write *a = (struct archive_write *)_a;
107228753Smm	struct shar *shar;
108228753Smm
109228753Smm	/* If someone else was already registered, unregister them. */
110228753Smm	if (a->format_destroy != NULL)
111228753Smm		(a->format_destroy)(a);
112228753Smm
113228753Smm	shar = (struct shar *)malloc(sizeof(*shar));
114228753Smm	if (shar == NULL) {
115228753Smm		archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data");
116228753Smm		return (ARCHIVE_FATAL);
117228753Smm	}
118228753Smm	memset(shar, 0, sizeof(*shar));
119228753Smm	archive_string_init(&shar->work);
120228753Smm	archive_string_init(&shar->quoted_name);
121228753Smm	a->format_data = shar;
122228753Smm
123228753Smm	a->pad_uncompressed = 0;
124228753Smm	a->format_name = "shar";
125228753Smm	a->format_write_header = archive_write_shar_header;
126228753Smm	a->format_finish = archive_write_shar_finish;
127228753Smm	a->format_destroy = archive_write_shar_destroy;
128228753Smm	a->format_write_data = archive_write_shar_data_sed;
129228753Smm	a->format_finish_entry = archive_write_shar_finish_entry;
130228753Smm	a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE;
131228753Smm	a->archive.archive_format_name = "shar";
132228753Smm	return (ARCHIVE_OK);
133228753Smm}
134228753Smm
135228753Smm/*
136228753Smm * An alternate 'shar' that uses uudecode instead of 'sed' to encode
137228753Smm * file contents and can therefore be used to archive binary files.
138228753Smm * In addition, this variant also attempts to restore ownership, file modes,
139228753Smm * and other extended file information.
140228753Smm */
141228753Smmint
142228753Smmarchive_write_set_format_shar_dump(struct archive *_a)
143228753Smm{
144228753Smm	struct archive_write *a = (struct archive_write *)_a;
145228753Smm	struct shar *shar;
146228753Smm
147228753Smm	archive_write_set_format_shar(&a->archive);
148228753Smm	shar = (struct shar *)a->format_data;
149228753Smm	shar->dump = 1;
150228753Smm	a->format_write_data = archive_write_shar_data_uuencode;
151228753Smm	a->archive.archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
152228753Smm	a->archive.archive_format_name = "shar dump";
153228753Smm	return (ARCHIVE_OK);
154228753Smm}
155228753Smm
156228753Smmstatic int
157228753Smmarchive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
158228753Smm{
159228753Smm	const char *linkname;
160228753Smm	const char *name;
161228753Smm	char *p, *pp;
162228753Smm	struct shar *shar;
163228753Smm
164228753Smm	shar = (struct shar *)a->format_data;
165228753Smm	if (!shar->wrote_header) {
166228753Smm		archive_strcat(&shar->work, "#!/bin/sh\n");
167228753Smm		archive_strcat(&shar->work, "# This is a shell archive\n");
168228753Smm		shar->wrote_header = 1;
169228753Smm	}
170228753Smm
171228753Smm	/* Save the entry for the closing. */
172228753Smm	if (shar->entry)
173228753Smm		archive_entry_free(shar->entry);
174228753Smm	shar->entry = archive_entry_clone(entry);
175228753Smm	name = archive_entry_pathname(entry);
176228753Smm
177228753Smm	/* Handle some preparatory issues. */
178228753Smm	switch(archive_entry_filetype(entry)) {
179228753Smm	case AE_IFREG:
180228753Smm		/* Only regular files have non-zero size. */
181228753Smm		break;
182228753Smm	case AE_IFDIR:
183228753Smm		archive_entry_set_size(entry, 0);
184228753Smm		/* Don't bother trying to recreate '.' */
185228753Smm		if (strcmp(name, ".") == 0  ||  strcmp(name, "./") == 0)
186228753Smm			return (ARCHIVE_OK);
187228753Smm		break;
188228753Smm	case AE_IFIFO:
189228753Smm	case AE_IFCHR:
190228753Smm	case AE_IFBLK:
191228753Smm		/* All other file types have zero size in the archive. */
192228753Smm		archive_entry_set_size(entry, 0);
193228753Smm		break;
194228753Smm	default:
195228753Smm		archive_entry_set_size(entry, 0);
196228753Smm		if (archive_entry_hardlink(entry) == NULL &&
197228753Smm		    archive_entry_symlink(entry) == NULL) {
198228753Smm			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
199228753Smm			    "shar format cannot archive this");
200228753Smm			return (ARCHIVE_WARN);
201228753Smm		}
202228753Smm	}
203228753Smm
204228753Smm	archive_string_empty(&shar->quoted_name);
205228753Smm	shar_quote(&shar->quoted_name, name, 1);
206228753Smm
207228753Smm	/* Stock preparation for all file types. */
208228753Smm	archive_string_sprintf(&shar->work, "echo x %s\n", shar->quoted_name.s);
209228753Smm
210228753Smm	if (archive_entry_filetype(entry) != AE_IFDIR) {
211228753Smm		/* Try to create the dir. */
212228753Smm		p = strdup(name);
213228753Smm		pp = strrchr(p, '/');
214228753Smm		/* If there is a / character, try to create the dir. */
215228753Smm		if (pp != NULL) {
216228753Smm			*pp = '\0';
217228753Smm
218228753Smm			/* Try to avoid a lot of redundant mkdir commands. */
219228753Smm			if (strcmp(p, ".") == 0) {
220228753Smm				/* Don't try to "mkdir ." */
221228753Smm				free(p);
222228753Smm			} else if (shar->last_dir == NULL) {
223228753Smm				archive_strcat(&shar->work, "mkdir -p ");
224228753Smm				shar_quote(&shar->work, p, 1);
225228753Smm				archive_strcat(&shar->work,
226228753Smm				    " > /dev/null 2>&1\n");
227228753Smm				shar->last_dir = p;
228228753Smm			} else if (strcmp(p, shar->last_dir) == 0) {
229228753Smm				/* We've already created this exact dir. */
230228753Smm				free(p);
231228753Smm			} else if (strlen(p) < strlen(shar->last_dir) &&
232228753Smm			    strncmp(p, shar->last_dir, strlen(p)) == 0) {
233228753Smm				/* We've already created a subdir. */
234228753Smm				free(p);
235228753Smm			} else {
236228753Smm				archive_strcat(&shar->work, "mkdir -p ");
237228753Smm				shar_quote(&shar->work, p, 1);
238228753Smm				archive_strcat(&shar->work,
239228753Smm				    " > /dev/null 2>&1\n");
240228753Smm				shar->last_dir = p;
241228753Smm			}
242228753Smm		} else {
243228753Smm			free(p);
244228753Smm		}
245228753Smm	}
246228753Smm
247228753Smm	/* Handle file-type specific issues. */
248228753Smm	shar->has_data = 0;
249228753Smm	if ((linkname = archive_entry_hardlink(entry)) != NULL) {
250228753Smm		archive_strcat(&shar->work, "ln -f ");
251228753Smm		shar_quote(&shar->work, linkname, 1);
252228753Smm		archive_string_sprintf(&shar->work, " %s\n",
253228753Smm		    shar->quoted_name.s);
254228753Smm	} else if ((linkname = archive_entry_symlink(entry)) != NULL) {
255228753Smm		archive_strcat(&shar->work, "ln -fs ");
256228753Smm		shar_quote(&shar->work, linkname, 1);
257228753Smm		archive_string_sprintf(&shar->work, " %s\n",
258228753Smm		    shar->quoted_name.s);
259228753Smm	} else {
260228753Smm		switch(archive_entry_filetype(entry)) {
261228753Smm		case AE_IFREG:
262228753Smm			if (archive_entry_size(entry) == 0) {
263228753Smm				/* More portable than "touch." */
264228753Smm				archive_string_sprintf(&shar->work,
265228753Smm				    "test -e \"%s\" || :> \"%s\"\n",
266228753Smm				    shar->quoted_name.s, shar->quoted_name.s);
267228753Smm			} else {
268228753Smm				if (shar->dump) {
269228753Smm					archive_string_sprintf(&shar->work,
270228753Smm					    "uudecode -p > %s << 'SHAR_END'\n",
271228753Smm					    shar->quoted_name.s);
272228753Smm					archive_string_sprintf(&shar->work,
273228753Smm					    "begin %o ",
274228753Smm					    archive_entry_mode(entry) & 0777);
275228753Smm					shar_quote(&shar->work, name, 0);
276228753Smm					archive_strcat(&shar->work, "\n");
277228753Smm				} else {
278228753Smm					archive_string_sprintf(&shar->work,
279228753Smm					    "sed 's/^X//' > %s << 'SHAR_END'\n",
280228753Smm					    shar->quoted_name.s);
281228753Smm				}
282228753Smm				shar->has_data = 1;
283228753Smm				shar->end_of_line = 1;
284228753Smm				shar->outpos = 0;
285228753Smm			}
286228753Smm			break;
287228753Smm		case AE_IFDIR:
288228753Smm			archive_string_sprintf(&shar->work,
289228753Smm			    "mkdir -p %s > /dev/null 2>&1\n",
290228753Smm			    shar->quoted_name.s);
291228753Smm			/* Record that we just created this directory. */
292228753Smm			if (shar->last_dir != NULL)
293228753Smm				free(shar->last_dir);
294228753Smm
295228753Smm			shar->last_dir = strdup(name);
296228753Smm			/* Trim a trailing '/'. */
297228753Smm			pp = strrchr(shar->last_dir, '/');
298228753Smm			if (pp != NULL && pp[1] == '\0')
299228753Smm				*pp = '\0';
300228753Smm			/*
301228753Smm			 * TODO: Put dir name/mode on a list to be fixed
302228753Smm			 * up at end of archive.
303228753Smm			 */
304228753Smm			break;
305228753Smm		case AE_IFIFO:
306228753Smm			archive_string_sprintf(&shar->work,
307228753Smm			    "mkfifo %s\n", shar->quoted_name.s);
308228753Smm			break;
309228753Smm		case AE_IFCHR:
310228753Smm			archive_string_sprintf(&shar->work,
311228753Smm			    "mknod %s c %d %d\n", shar->quoted_name.s,
312228753Smm			    archive_entry_rdevmajor(entry),
313228753Smm			    archive_entry_rdevminor(entry));
314228753Smm			break;
315228753Smm		case AE_IFBLK:
316228753Smm			archive_string_sprintf(&shar->work,
317228753Smm			    "mknod %s b %d %d\n", shar->quoted_name.s,
318228753Smm			    archive_entry_rdevmajor(entry),
319228753Smm			    archive_entry_rdevminor(entry));
320228753Smm			break;
321228753Smm		default:
322228753Smm			return (ARCHIVE_WARN);
323228753Smm		}
324228753Smm	}
325228753Smm
326228753Smm	return (ARCHIVE_OK);
327228753Smm}
328228753Smm
329228753Smmstatic ssize_t
330228753Smmarchive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
331228753Smm{
332228753Smm	static const size_t ensured = 65533;
333228753Smm	struct shar *shar;
334228753Smm	const char *src;
335228753Smm	char *buf, *buf_end;
336228753Smm	int ret;
337228753Smm	size_t written = n;
338228753Smm
339228753Smm	shar = (struct shar *)a->format_data;
340228753Smm	if (!shar->has_data || n == 0)
341228753Smm		return (0);
342228753Smm
343228753Smm	src = (const char *)buff;
344228753Smm
345228753Smm	/*
346228753Smm	 * ensure is the number of bytes in buffer before expanding the
347228753Smm	 * current character.  Each operation writes the current character
348228753Smm	 * and optionally the start-of-new-line marker.  This can happen
349228753Smm	 * twice before entering the loop, so make sure three additional
350228753Smm	 * bytes can be written.
351228753Smm	 */
352228753Smm	if (archive_string_ensure(&shar->work, ensured + 3) == NULL)
353228753Smm		__archive_errx(1, "Out of memory");
354228753Smm
355228753Smm	if (shar->work.length > ensured) {
356228753Smm		ret = (*a->compressor.write)(a, shar->work.s,
357228753Smm		    shar->work.length);
358228753Smm		if (ret != ARCHIVE_OK)
359228753Smm			return (ARCHIVE_FATAL);
360228753Smm		archive_string_empty(&shar->work);
361228753Smm	}
362228753Smm	buf = shar->work.s + shar->work.length;
363228753Smm	buf_end = shar->work.s + ensured;
364228753Smm
365228753Smm	if (shar->end_of_line) {
366228753Smm		*buf++ = 'X';
367228753Smm		shar->end_of_line = 0;
368228753Smm	}
369228753Smm
370228753Smm	while (n-- != 0) {
371228753Smm		if ((*buf++ = *src++) == '\n') {
372228753Smm			if (n == 0)
373228753Smm				shar->end_of_line = 1;
374228753Smm			else
375228753Smm				*buf++ = 'X';
376228753Smm		}
377228753Smm
378228753Smm		if (buf >= buf_end) {
379228753Smm			shar->work.length = buf - shar->work.s;
380228753Smm			ret = (*a->compressor.write)(a, shar->work.s,
381228753Smm			    shar->work.length);
382228753Smm			if (ret != ARCHIVE_OK)
383228753Smm				return (ARCHIVE_FATAL);
384228753Smm			archive_string_empty(&shar->work);
385228753Smm			buf = shar->work.s;
386228753Smm		}
387228753Smm	}
388228753Smm
389228753Smm	shar->work.length = buf - shar->work.s;
390228753Smm
391228753Smm	return (written);
392228753Smm}
393228753Smm
394228753Smm#define	UUENC(c)	(((c)!=0) ? ((c) & 077) + ' ': '`')
395228753Smm
396228753Smmstatic void
397228753Smmuuencode_group(const char _in[3], char out[4])
398228753Smm{
399228753Smm	const unsigned char *in = (const unsigned char *)_in;
400228753Smm	int t;
401228753Smm
402228753Smm	t = (in[0] << 16) | (in[1] << 8) | in[2];
403228753Smm	out[0] = UUENC( 0x3f & (t >> 18) );
404228753Smm	out[1] = UUENC( 0x3f & (t >> 12) );
405228753Smm	out[2] = UUENC( 0x3f & (t >> 6) );
406228753Smm	out[3] = UUENC( 0x3f & t );
407228753Smm}
408228753Smm
409228753Smmstatic void
410228753Smmuuencode_line(struct shar *shar, const char *inbuf, size_t len)
411228753Smm{
412228753Smm	char tmp_buf[3], *buf;
413228753Smm	size_t alloc_len;
414228753Smm
415228753Smm	/* len <= 45 -> expanded to 60 + len byte + new line */
416228753Smm	alloc_len = shar->work.length + 62;
417228753Smm	if (archive_string_ensure(&shar->work, alloc_len) == NULL)
418228753Smm		__archive_errx(1, "Out of memory");
419228753Smm
420228753Smm	buf = shar->work.s + shar->work.length;
421228753Smm	*buf++ = UUENC(len);
422228753Smm	while (len >= 3) {
423228753Smm		uuencode_group(inbuf, buf);
424228753Smm		len -= 3;
425228753Smm		inbuf += 3;
426228753Smm		buf += 4;
427228753Smm	}
428228753Smm	if (len != 0) {
429228753Smm		tmp_buf[0] = inbuf[0];
430228753Smm		if (len == 1)
431228753Smm			tmp_buf[1] = '\0';
432228753Smm		else
433228753Smm			tmp_buf[1] = inbuf[1];
434228753Smm		tmp_buf[2] = '\0';
435228753Smm		uuencode_group(inbuf, buf);
436228753Smm		buf += 4;
437228753Smm	}
438228753Smm	*buf++ = '\n';
439228753Smm	if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62))
440228753Smm		__archive_errx(1, "Buffer overflow");
441228753Smm	shar->work.length = buf - shar->work.s;
442228753Smm}
443228753Smm
444228753Smmstatic ssize_t
445228753Smmarchive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
446228753Smm    size_t length)
447228753Smm{
448228753Smm	struct shar *shar;
449228753Smm	const char *src;
450228753Smm	size_t n;
451228753Smm	int ret;
452228753Smm
453228753Smm	shar = (struct shar *)a->format_data;
454228753Smm	if (!shar->has_data)
455228753Smm		return (ARCHIVE_OK);
456228753Smm	src = (const char *)buff;
457228753Smm
458228753Smm	if (shar->outpos != 0) {
459228753Smm		n = 45 - shar->outpos;
460228753Smm		if (n > length)
461228753Smm			n = length;
462228753Smm		memcpy(shar->outbuff + shar->outpos, src, n);
463228753Smm		if (shar->outpos + n < 45) {
464228753Smm			shar->outpos += n;
465228753Smm			return length;
466228753Smm		}
467228753Smm		uuencode_line(shar, shar->outbuff, 45);
468228753Smm		src += n;
469228753Smm		n = length - n;
470228753Smm	} else {
471228753Smm		n = length;
472228753Smm	}
473228753Smm
474228753Smm	while (n >= 45) {
475228753Smm		uuencode_line(shar, src, 45);
476228753Smm		src += 45;
477228753Smm		n -= 45;
478228753Smm
479228753Smm		if (shar->work.length < 65536)
480228753Smm			continue;
481228753Smm		ret = (*a->compressor.write)(a, shar->work.s,
482228753Smm		    shar->work.length);
483228753Smm		if (ret != ARCHIVE_OK)
484228753Smm			return (ARCHIVE_FATAL);
485228753Smm		archive_string_empty(&shar->work);
486228753Smm	}
487228753Smm	if (n != 0) {
488228753Smm		memcpy(shar->outbuff, src, n);
489228753Smm		shar->outpos = n;
490228753Smm	}
491228753Smm	return (length);
492228753Smm}
493228753Smm
494228753Smmstatic int
495228753Smmarchive_write_shar_finish_entry(struct archive_write *a)
496228753Smm{
497228753Smm	const char *g, *p, *u;
498228753Smm	struct shar *shar;
499228753Smm	int ret;
500228753Smm
501228753Smm	shar = (struct shar *)a->format_data;
502228753Smm	if (shar->entry == NULL)
503228753Smm		return (0);
504228753Smm
505228753Smm	if (shar->dump) {
506228753Smm		/* Finish uuencoded data. */
507228753Smm		if (shar->has_data) {
508228753Smm			if (shar->outpos > 0)
509228753Smm				uuencode_line(shar, shar->outbuff,
510228753Smm				    shar->outpos);
511228753Smm			archive_strcat(&shar->work, "`\nend\n");
512228753Smm			archive_strcat(&shar->work, "SHAR_END\n");
513228753Smm		}
514228753Smm		/* Restore file mode, owner, flags. */
515228753Smm		/*
516228753Smm		 * TODO: Don't immediately restore mode for
517228753Smm		 * directories; defer that to end of script.
518228753Smm		 */
519228753Smm		archive_string_sprintf(&shar->work, "chmod %o ",
520228753Smm		    archive_entry_mode(shar->entry) & 07777);
521228753Smm		shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1);
522228753Smm		archive_strcat(&shar->work, "\n");
523228753Smm
524228753Smm		u = archive_entry_uname(shar->entry);
525228753Smm		g = archive_entry_gname(shar->entry);
526228753Smm		if (u != NULL || g != NULL) {
527228753Smm			archive_strcat(&shar->work, "chown ");
528228753Smm			if (u != NULL)
529228753Smm				shar_quote(&shar->work, u, 1);
530228753Smm			if (g != NULL) {
531228753Smm				archive_strcat(&shar->work, ":");
532228753Smm				shar_quote(&shar->work, g, 1);
533228753Smm			}
534228753Smm			shar_quote(&shar->work,
535228753Smm			    archive_entry_pathname(shar->entry), 1);
536228753Smm			archive_strcat(&shar->work, "\n");
537228753Smm		}
538228753Smm
539228753Smm		if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
540228753Smm			archive_string_sprintf(&shar->work, "chflags %s ", p);
541228753Smm			shar_quote(&shar->work,
542228753Smm			    archive_entry_pathname(shar->entry), 1);
543228753Smm			archive_strcat(&shar->work, "\n");
544228753Smm		}
545228753Smm
546228753Smm		/* TODO: restore ACLs */
547228753Smm
548228753Smm	} else {
549228753Smm		if (shar->has_data) {
550228753Smm			/* Finish sed-encoded data:  ensure last line ends. */
551228753Smm			if (!shar->end_of_line)
552228753Smm				archive_strappend_char(&shar->work, '\n');
553228753Smm			archive_strcat(&shar->work, "SHAR_END\n");
554228753Smm		}
555228753Smm	}
556228753Smm
557228753Smm	archive_entry_free(shar->entry);
558228753Smm	shar->entry = NULL;
559228753Smm
560228753Smm	if (shar->work.length < 65536)
561228753Smm		return (ARCHIVE_OK);
562228753Smm
563228753Smm	ret = (*a->compressor.write)(a, shar->work.s, shar->work.length);
564228753Smm	if (ret != ARCHIVE_OK)
565228753Smm		return (ARCHIVE_FATAL);
566228753Smm	archive_string_empty(&shar->work);
567228753Smm
568228753Smm	return (ARCHIVE_OK);
569228753Smm}
570228753Smm
571228753Smmstatic int
572228753Smmarchive_write_shar_finish(struct archive_write *a)
573228753Smm{
574228753Smm	struct shar *shar;
575228753Smm	int ret;
576228753Smm
577228753Smm	/*
578228753Smm	 * TODO: Accumulate list of directory names/modes and
579228753Smm	 * fix them all up at end-of-archive.
580228753Smm	 */
581228753Smm
582228753Smm	shar = (struct shar *)a->format_data;
583228753Smm
584228753Smm	/*
585228753Smm	 * Only write the end-of-archive markers if the archive was
586228753Smm	 * actually started.  This avoids problems if someone sets
587228753Smm	 * shar format, then sets another format (which would invoke
588228753Smm	 * shar_finish to free the format-specific data).
589228753Smm	 */
590228753Smm	if (shar->wrote_header == 0)
591228753Smm		return (ARCHIVE_OK);
592228753Smm
593228753Smm	archive_strcat(&shar->work, "exit\n");
594228753Smm
595228753Smm	ret = (*a->compressor.write)(a, shar->work.s, shar->work.length);
596228753Smm	if (ret != ARCHIVE_OK)
597228753Smm		return (ARCHIVE_FATAL);
598228753Smm
599228753Smm	/* Shar output is never padded. */
600228753Smm	archive_write_set_bytes_in_last_block(&a->archive, 1);
601228753Smm	/*
602228753Smm	 * TODO: shar should also suppress padding of
603228753Smm	 * uncompressed data within gzip/bzip2 streams.
604228753Smm	 */
605228753Smm
606228753Smm	return (ARCHIVE_OK);
607228753Smm}
608228753Smm
609228753Smmstatic int
610228753Smmarchive_write_shar_destroy(struct archive_write *a)
611228753Smm{
612228753Smm	struct shar *shar;
613228753Smm
614228753Smm	shar = (struct shar *)a->format_data;
615228753Smm	if (shar == NULL)
616228753Smm		return (ARCHIVE_OK);
617228753Smm
618228753Smm	archive_entry_free(shar->entry);
619228753Smm	free(shar->last_dir);
620228753Smm	archive_string_free(&(shar->work));
621228753Smm	archive_string_free(&(shar->quoted_name));
622228753Smm	free(shar);
623228753Smm	a->format_data = NULL;
624228753Smm	return (ARCHIVE_OK);
625228753Smm}
626