2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2008 Joerg Sonnenberger
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_write_private.h"
49 struct archive_entry *entry;
53 /* Line buffer for uuencoded dump format */
58 struct archive_string work;
59 struct archive_string quoted_name;
62 static int archive_write_shar_close(struct archive_write *);
63 static int archive_write_shar_free(struct archive_write *);
64 static int archive_write_shar_header(struct archive_write *,
65 struct archive_entry *);
66 static ssize_t archive_write_shar_data_sed(struct archive_write *,
67 const void * buff, size_t);
68 static ssize_t archive_write_shar_data_uuencode(struct archive_write *,
69 const void * buff, size_t);
70 static int archive_write_shar_finish_entry(struct archive_write *);
73 * Copy the given string to the buffer, quoting all shell meta characters
77 shar_quote(struct archive_string *buf, const char *str, int in_shell)
79 static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
82 while (*str != '\0') {
83 if ((len = strcspn(str, meta)) != 0) {
84 archive_strncat(buf, str, len);
86 } else if (*str == '\n') {
88 archive_strcat(buf, "\"\n\"");
90 archive_strcat(buf, "\\n");
93 archive_strappend_char(buf, '\\');
94 archive_strappend_char(buf, *str);
101 * Set output format to 'shar' format.
104 archive_write_set_format_shar(struct archive *_a)
106 struct archive_write *a = (struct archive_write *)_a;
109 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
110 ARCHIVE_STATE_NEW, "archive_write_set_format_shar");
112 /* If someone else was already registered, unregister them. */
113 if (a->format_free != NULL)
116 shar = (struct shar *)calloc(1, sizeof(*shar));
118 archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data");
119 return (ARCHIVE_FATAL);
121 archive_string_init(&shar->work);
122 archive_string_init(&shar->quoted_name);
123 a->format_data = shar;
124 a->format_name = "shar";
125 a->format_write_header = archive_write_shar_header;
126 a->format_close = archive_write_shar_close;
127 a->format_free = archive_write_shar_free;
128 a->format_write_data = archive_write_shar_data_sed;
129 a->format_finish_entry = archive_write_shar_finish_entry;
130 a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE;
131 a->archive.archive_format_name = "shar";
136 * An alternate 'shar' that uses uudecode instead of 'sed' to encode
137 * file contents and can therefore be used to archive binary files.
138 * In addition, this variant also attempts to restore ownership, file modes,
139 * and other extended file information.
142 archive_write_set_format_shar_dump(struct archive *_a)
144 struct archive_write *a = (struct archive_write *)_a;
147 archive_write_set_format_shar(&a->archive);
148 shar = (struct shar *)a->format_data;
150 a->format_write_data = archive_write_shar_data_uuencode;
151 a->archive.archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
152 a->archive.archive_format_name = "shar dump";
157 archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
159 const char *linkname;
164 shar = (struct shar *)a->format_data;
165 if (!shar->wrote_header) {
166 archive_strcat(&shar->work, "#!/bin/sh\n");
167 archive_strcat(&shar->work, "# This is a shell archive\n");
168 shar->wrote_header = 1;
171 /* Save the entry for the closing. */
172 archive_entry_free(shar->entry);
173 shar->entry = archive_entry_clone(entry);
174 name = archive_entry_pathname(entry);
176 /* Handle some preparatory issues. */
177 switch(archive_entry_filetype(entry)) {
179 /* Only regular files have non-zero size. */
182 archive_entry_set_size(entry, 0);
183 /* Don't bother trying to recreate '.' */
184 if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0)
190 /* All other file types have zero size in the archive. */
191 archive_entry_set_size(entry, 0);
194 archive_entry_set_size(entry, 0);
195 if (archive_entry_hardlink(entry) == NULL &&
196 archive_entry_symlink(entry) == NULL) {
197 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
198 "shar format cannot archive this");
199 return (ARCHIVE_WARN);
203 archive_string_empty(&shar->quoted_name);
204 shar_quote(&shar->quoted_name, name, 1);
206 /* Stock preparation for all file types. */
207 archive_string_sprintf(&shar->work, "echo x %s\n", shar->quoted_name.s);
209 if (archive_entry_filetype(entry) != AE_IFDIR) {
210 /* Try to create the dir. */
212 pp = strrchr(p, '/');
213 /* If there is a / character, try to create the dir. */
217 /* Try to avoid a lot of redundant mkdir commands. */
218 if (strcmp(p, ".") == 0) {
219 /* Don't try to "mkdir ." */
221 } else if (shar->last_dir == NULL) {
222 archive_strcat(&shar->work, "mkdir -p ");
223 shar_quote(&shar->work, p, 1);
224 archive_strcat(&shar->work,
225 " > /dev/null 2>&1\n");
227 } else if (strcmp(p, shar->last_dir) == 0) {
228 /* We've already created this exact dir. */
230 } else if (strlen(p) < strlen(shar->last_dir) &&
231 strncmp(p, shar->last_dir, strlen(p)) == 0) {
232 /* We've already created a subdir. */
235 archive_strcat(&shar->work, "mkdir -p ");
236 shar_quote(&shar->work, p, 1);
237 archive_strcat(&shar->work,
238 " > /dev/null 2>&1\n");
246 /* Handle file-type specific issues. */
248 if ((linkname = archive_entry_hardlink(entry)) != NULL) {
249 archive_strcat(&shar->work, "ln -f ");
250 shar_quote(&shar->work, linkname, 1);
251 archive_string_sprintf(&shar->work, " %s\n",
252 shar->quoted_name.s);
253 } else if ((linkname = archive_entry_symlink(entry)) != NULL) {
254 archive_strcat(&shar->work, "ln -fs ");
255 shar_quote(&shar->work, linkname, 1);
256 archive_string_sprintf(&shar->work, " %s\n",
257 shar->quoted_name.s);
259 switch(archive_entry_filetype(entry)) {
261 if (archive_entry_size(entry) == 0) {
262 /* More portable than "touch." */
263 archive_string_sprintf(&shar->work,
264 "test -e \"%s\" || :> \"%s\"\n",
265 shar->quoted_name.s, shar->quoted_name.s);
268 unsigned int mode = archive_entry_mode(entry) & 0777;
269 archive_string_sprintf(&shar->work,
270 "uudecode -p > %s << 'SHAR_END'\n",
271 shar->quoted_name.s);
272 archive_string_sprintf(&shar->work,
274 shar_quote(&shar->work, name, 0);
275 archive_strcat(&shar->work, "\n");
277 archive_string_sprintf(&shar->work,
278 "sed 's/^X//' > %s << 'SHAR_END'\n",
279 shar->quoted_name.s);
282 shar->end_of_line = 1;
287 archive_string_sprintf(&shar->work,
288 "mkdir -p %s > /dev/null 2>&1\n",
289 shar->quoted_name.s);
290 /* Record that we just created this directory. */
291 free(shar->last_dir);
293 shar->last_dir = strdup(name);
294 /* Trim a trailing '/'. */
295 pp = strrchr(shar->last_dir, '/');
296 if (pp != NULL && pp[1] == '\0')
299 * TODO: Put dir name/mode on a list to be fixed
300 * up at end of archive.
304 archive_string_sprintf(&shar->work,
305 "mkfifo %s\n", shar->quoted_name.s);
308 archive_string_sprintf(&shar->work,
309 "mknod %s c %ju %ju\n", shar->quoted_name.s,
310 (uintmax_t)archive_entry_rdevmajor(entry),
311 (uintmax_t)archive_entry_rdevminor(entry));
314 archive_string_sprintf(&shar->work,
315 "mknod %s b %ju %ju\n", shar->quoted_name.s,
316 (uintmax_t)archive_entry_rdevmajor(entry),
317 (uintmax_t)archive_entry_rdevminor(entry));
320 return (ARCHIVE_WARN);
328 archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
330 static const size_t ensured = 65533;
337 shar = (struct shar *)a->format_data;
338 if (!shar->has_data || n == 0)
341 src = (const char *)buff;
344 * ensure is the number of bytes in buffer before expanding the
345 * current character. Each operation writes the current character
346 * and optionally the start-of-new-line marker. This can happen
347 * twice before entering the loop, so make sure three additional
348 * bytes can be written.
350 if (archive_string_ensure(&shar->work, ensured + 3) == NULL) {
351 archive_set_error(&a->archive, ENOMEM, "Out of memory");
352 return (ARCHIVE_FATAL);
355 if (shar->work.length > ensured) {
356 ret = __archive_write_output(a, shar->work.s,
358 if (ret != ARCHIVE_OK)
359 return (ARCHIVE_FATAL);
360 archive_string_empty(&shar->work);
362 buf = shar->work.s + shar->work.length;
363 buf_end = shar->work.s + ensured;
365 if (shar->end_of_line) {
367 shar->end_of_line = 0;
371 if ((*buf++ = *src++) == '\n') {
373 shar->end_of_line = 1;
378 if (buf >= buf_end) {
379 shar->work.length = buf - shar->work.s;
380 ret = __archive_write_output(a, shar->work.s,
382 if (ret != ARCHIVE_OK)
383 return (ARCHIVE_FATAL);
384 archive_string_empty(&shar->work);
389 shar->work.length = buf - shar->work.s;
394 #define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
397 uuencode_group(const char _in[3], char out[4])
399 const unsigned char *in = (const unsigned char *)_in;
402 t = (in[0] << 16) | (in[1] << 8) | in[2];
403 out[0] = UUENC( 0x3f & (t >> 18) );
404 out[1] = UUENC( 0x3f & (t >> 12) );
405 out[2] = UUENC( 0x3f & (t >> 6) );
406 out[3] = UUENC( 0x3f & t );
410 _uuencode_line(struct archive_write *a, struct shar *shar, const char *inbuf, size_t len)
415 /* len <= 45 -> expanded to 60 + len byte + new line */
416 alloc_len = shar->work.length + 62;
417 if (archive_string_ensure(&shar->work, alloc_len) == NULL) {
418 archive_set_error(&a->archive, ENOMEM, "Out of memory");
419 return (ARCHIVE_FATAL);
422 buf = shar->work.s + shar->work.length;
425 uuencode_group(inbuf, buf);
432 tmp_buf[0] = inbuf[0];
436 tmp_buf[1] = inbuf[1];
438 uuencode_group(tmp_buf, buf);
442 if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62)) {
443 archive_set_error(&a->archive,
444 ARCHIVE_ERRNO_MISC, "Buffer overflow");
445 return (ARCHIVE_FATAL);
447 shar->work.length = buf - shar->work.s;
451 #define uuencode_line(__a, __shar, __inbuf, __len) \
453 int r = _uuencode_line(__a, __shar, __inbuf, __len); \
454 if (r != ARCHIVE_OK) \
455 return (ARCHIVE_FATAL); \
459 archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
467 shar = (struct shar *)a->format_data;
470 src = (const char *)buff;
472 if (shar->outpos != 0) {
473 n = 45 - shar->outpos;
476 memcpy(shar->outbuff + shar->outpos, src, n);
477 if (shar->outpos + n < 45) {
481 uuencode_line(a, shar, shar->outbuff, 45);
489 uuencode_line(a, shar, src, 45);
493 if (shar->work.length < 65536)
495 ret = __archive_write_output(a, shar->work.s,
497 if (ret != ARCHIVE_OK)
498 return (ARCHIVE_FATAL);
499 archive_string_empty(&shar->work);
502 memcpy(shar->outbuff, src, n);
509 archive_write_shar_finish_entry(struct archive_write *a)
511 const char *g, *p, *u;
515 shar = (struct shar *)a->format_data;
516 if (shar->entry == NULL)
520 /* Finish uuencoded data. */
521 if (shar->has_data) {
522 if (shar->outpos > 0)
523 uuencode_line(a, shar, shar->outbuff,
525 archive_strcat(&shar->work, "`\nend\n");
526 archive_strcat(&shar->work, "SHAR_END\n");
528 /* Restore file mode, owner, flags. */
530 * TODO: Don't immediately restore mode for
531 * directories; defer that to end of script.
533 archive_string_sprintf(&shar->work, "chmod %o ",
534 (unsigned int)(archive_entry_mode(shar->entry) & 07777));
535 shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1);
536 archive_strcat(&shar->work, "\n");
538 u = archive_entry_uname(shar->entry);
539 g = archive_entry_gname(shar->entry);
540 if (u != NULL || g != NULL) {
541 archive_strcat(&shar->work, "chown ");
543 shar_quote(&shar->work, u, 1);
545 archive_strcat(&shar->work, ":");
546 shar_quote(&shar->work, g, 1);
548 archive_strcat(&shar->work, " ");
549 shar_quote(&shar->work,
550 archive_entry_pathname(shar->entry), 1);
551 archive_strcat(&shar->work, "\n");
554 if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
555 archive_string_sprintf(&shar->work, "chflags %s ", p);
556 shar_quote(&shar->work,
557 archive_entry_pathname(shar->entry), 1);
558 archive_strcat(&shar->work, "\n");
561 /* TODO: restore ACLs */
564 if (shar->has_data) {
565 /* Finish sed-encoded data: ensure last line ends. */
566 if (!shar->end_of_line)
567 archive_strappend_char(&shar->work, '\n');
568 archive_strcat(&shar->work, "SHAR_END\n");
572 archive_entry_free(shar->entry);
575 if (shar->work.length < 65536)
578 ret = __archive_write_output(a, shar->work.s, shar->work.length);
579 if (ret != ARCHIVE_OK)
580 return (ARCHIVE_FATAL);
581 archive_string_empty(&shar->work);
587 archive_write_shar_close(struct archive_write *a)
593 * TODO: Accumulate list of directory names/modes and
594 * fix them all up at end-of-archive.
597 shar = (struct shar *)a->format_data;
600 * Only write the end-of-archive markers if the archive was
601 * actually started. This avoids problems if someone sets
602 * shar format, then sets another format (which would invoke
603 * shar_finish to free the format-specific data).
605 if (shar->wrote_header == 0)
608 archive_strcat(&shar->work, "exit\n");
610 ret = __archive_write_output(a, shar->work.s, shar->work.length);
611 if (ret != ARCHIVE_OK)
612 return (ARCHIVE_FATAL);
614 /* Shar output is never padded. */
615 archive_write_set_bytes_in_last_block(&a->archive, 1);
617 * TODO: shar should also suppress padding of
618 * uncompressed data within gzip/bzip2 streams.
625 archive_write_shar_free(struct archive_write *a)
629 shar = (struct shar *)a->format_data;
633 archive_entry_free(shar->entry);
634 free(shar->last_dir);
635 archive_string_free(&(shar->work));
636 archive_string_free(&(shar->quoted_name));
638 a->format_data = NULL;