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"
45 #include "archive_write_set_format_private.h"
50 struct archive_entry *entry;
54 /* Line buffer for uuencoded dump format */
59 struct archive_string work;
60 struct archive_string quoted_name;
63 static int archive_write_shar_close(struct archive_write *);
64 static int archive_write_shar_free(struct archive_write *);
65 static int archive_write_shar_header(struct archive_write *,
66 struct archive_entry *);
67 static ssize_t archive_write_shar_data_sed(struct archive_write *,
68 const void * buff, size_t);
69 static ssize_t archive_write_shar_data_uuencode(struct archive_write *,
70 const void * buff, size_t);
71 static int archive_write_shar_finish_entry(struct archive_write *);
74 * Copy the given string to the buffer, quoting all shell meta characters
78 shar_quote(struct archive_string *buf, const char *str, int in_shell)
80 static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
83 while (*str != '\0') {
84 if ((len = strcspn(str, meta)) != 0) {
85 archive_strncat(buf, str, len);
87 } else if (*str == '\n') {
89 archive_strcat(buf, "\"\n\"");
91 archive_strcat(buf, "\\n");
94 archive_strappend_char(buf, '\\');
95 archive_strappend_char(buf, *str);
102 * Set output format to 'shar' format.
105 archive_write_set_format_shar(struct archive *_a)
107 struct archive_write *a = (struct archive_write *)_a;
110 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
111 ARCHIVE_STATE_NEW, "archive_write_set_format_shar");
113 /* If someone else was already registered, unregister them. */
114 if (a->format_free != NULL)
117 shar = (struct shar *)calloc(1, sizeof(*shar));
119 archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data");
120 return (ARCHIVE_FATAL);
122 archive_string_init(&shar->work);
123 archive_string_init(&shar->quoted_name);
124 a->format_data = shar;
125 a->format_name = "shar";
126 a->format_write_header = archive_write_shar_header;
127 a->format_close = archive_write_shar_close;
128 a->format_free = archive_write_shar_free;
129 a->format_write_data = archive_write_shar_data_sed;
130 a->format_finish_entry = archive_write_shar_finish_entry;
131 a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE;
132 a->archive.archive_format_name = "shar";
137 * An alternate 'shar' that uses uudecode instead of 'sed' to encode
138 * file contents and can therefore be used to archive binary files.
139 * In addition, this variant also attempts to restore ownership, file modes,
140 * and other extended file information.
143 archive_write_set_format_shar_dump(struct archive *_a)
145 struct archive_write *a = (struct archive_write *)_a;
148 archive_write_set_format_shar(&a->archive);
149 shar = (struct shar *)a->format_data;
151 a->format_write_data = archive_write_shar_data_uuencode;
152 a->archive.archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
153 a->archive.archive_format_name = "shar dump";
158 archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
160 const char *linkname;
165 shar = (struct shar *)a->format_data;
166 if (!shar->wrote_header) {
167 archive_strcat(&shar->work, "#!/bin/sh\n");
168 archive_strcat(&shar->work, "# This is a shell archive\n");
169 shar->wrote_header = 1;
172 /* Save the entry for the closing. */
173 archive_entry_free(shar->entry);
174 shar->entry = archive_entry_clone(entry);
175 name = archive_entry_pathname(entry);
177 /* Handle some preparatory issues. */
178 switch(archive_entry_filetype(entry)) {
180 /* Only regular files have non-zero size. */
183 archive_entry_set_size(entry, 0);
184 /* Don't bother trying to recreate '.' */
185 if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0)
191 /* All other file types have zero size in the archive. */
192 archive_entry_set_size(entry, 0);
195 archive_entry_set_size(entry, 0);
196 if (archive_entry_hardlink(entry) == NULL &&
197 archive_entry_symlink(entry) == NULL) {
198 __archive_write_entry_filetype_unsupported(
199 &a->archive, entry, "shar");
200 return (ARCHIVE_WARN);
204 archive_string_empty(&shar->quoted_name);
205 shar_quote(&shar->quoted_name, name, 1);
207 /* Stock preparation for all file types. */
208 archive_string_sprintf(&shar->work, "echo x %s\n", shar->quoted_name.s);
210 if (archive_entry_filetype(entry) != AE_IFDIR) {
211 /* Try to create the dir. */
213 pp = strrchr(p, '/');
214 /* If there is a / character, try to create the dir. */
218 /* Try to avoid a lot of redundant mkdir commands. */
219 if (strcmp(p, ".") == 0) {
220 /* Don't try to "mkdir ." */
222 } else if (shar->last_dir == NULL) {
223 archive_strcat(&shar->work, "mkdir -p ");
224 shar_quote(&shar->work, p, 1);
225 archive_strcat(&shar->work,
226 " > /dev/null 2>&1\n");
228 } else if (strcmp(p, shar->last_dir) == 0) {
229 /* We've already created this exact dir. */
231 } else if (strlen(p) < strlen(shar->last_dir) &&
232 strncmp(p, shar->last_dir, strlen(p)) == 0) {
233 /* We've already created a subdir. */
236 archive_strcat(&shar->work, "mkdir -p ");
237 shar_quote(&shar->work, p, 1);
238 archive_strcat(&shar->work,
239 " > /dev/null 2>&1\n");
247 /* Handle file-type specific issues. */
249 if ((linkname = archive_entry_hardlink(entry)) != NULL) {
250 archive_strcat(&shar->work, "ln -f ");
251 shar_quote(&shar->work, linkname, 1);
252 archive_string_sprintf(&shar->work, " %s\n",
253 shar->quoted_name.s);
254 } else if ((linkname = archive_entry_symlink(entry)) != NULL) {
255 archive_strcat(&shar->work, "ln -fs ");
256 shar_quote(&shar->work, linkname, 1);
257 archive_string_sprintf(&shar->work, " %s\n",
258 shar->quoted_name.s);
260 switch(archive_entry_filetype(entry)) {
262 if (archive_entry_size(entry) == 0) {
263 /* More portable than "touch." */
264 archive_string_sprintf(&shar->work,
265 "test -e \"%s\" || :> \"%s\"\n",
266 shar->quoted_name.s, shar->quoted_name.s);
269 unsigned int mode = archive_entry_mode(entry) & 0777;
270 archive_string_sprintf(&shar->work,
271 "uudecode -p > %s << 'SHAR_END'\n",
272 shar->quoted_name.s);
273 archive_string_sprintf(&shar->work,
275 shar_quote(&shar->work, name, 0);
276 archive_strcat(&shar->work, "\n");
278 archive_string_sprintf(&shar->work,
279 "sed 's/^X//' > %s << 'SHAR_END'\n",
280 shar->quoted_name.s);
283 shar->end_of_line = 1;
288 archive_string_sprintf(&shar->work,
289 "mkdir -p %s > /dev/null 2>&1\n",
290 shar->quoted_name.s);
291 /* Record that we just created this directory. */
292 free(shar->last_dir);
294 shar->last_dir = strdup(name);
295 /* Trim a trailing '/'. */
296 pp = strrchr(shar->last_dir, '/');
297 if (pp != NULL && pp[1] == '\0')
300 * TODO: Put dir name/mode on a list to be fixed
301 * up at end of archive.
305 archive_string_sprintf(&shar->work,
306 "mkfifo %s\n", shar->quoted_name.s);
309 archive_string_sprintf(&shar->work,
310 "mknod %s c %ju %ju\n", shar->quoted_name.s,
311 (uintmax_t)archive_entry_rdevmajor(entry),
312 (uintmax_t)archive_entry_rdevminor(entry));
315 archive_string_sprintf(&shar->work,
316 "mknod %s b %ju %ju\n", shar->quoted_name.s,
317 (uintmax_t)archive_entry_rdevmajor(entry),
318 (uintmax_t)archive_entry_rdevminor(entry));
321 return (ARCHIVE_WARN);
329 archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
331 static const size_t ensured = 65533;
338 shar = (struct shar *)a->format_data;
339 if (!shar->has_data || n == 0)
342 src = (const char *)buff;
345 * ensure is the number of bytes in buffer before expanding the
346 * current character. Each operation writes the current character
347 * and optionally the start-of-new-line marker. This can happen
348 * twice before entering the loop, so make sure three additional
349 * bytes can be written.
351 if (archive_string_ensure(&shar->work, ensured + 3) == NULL) {
352 archive_set_error(&a->archive, ENOMEM, "Out of memory");
353 return (ARCHIVE_FATAL);
356 if (shar->work.length > ensured) {
357 ret = __archive_write_output(a, shar->work.s,
359 if (ret != ARCHIVE_OK)
360 return (ARCHIVE_FATAL);
361 archive_string_empty(&shar->work);
363 buf = shar->work.s + shar->work.length;
364 buf_end = shar->work.s + ensured;
366 if (shar->end_of_line) {
368 shar->end_of_line = 0;
372 if ((*buf++ = *src++) == '\n') {
374 shar->end_of_line = 1;
379 if (buf >= buf_end) {
380 shar->work.length = buf - shar->work.s;
381 ret = __archive_write_output(a, shar->work.s,
383 if (ret != ARCHIVE_OK)
384 return (ARCHIVE_FATAL);
385 archive_string_empty(&shar->work);
390 shar->work.length = buf - shar->work.s;
395 #define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
398 uuencode_group(const char _in[3], char out[4])
400 const unsigned char *in = (const unsigned char *)_in;
403 t = (in[0] << 16) | (in[1] << 8) | in[2];
404 out[0] = UUENC( 0x3f & (t >> 18) );
405 out[1] = UUENC( 0x3f & (t >> 12) );
406 out[2] = UUENC( 0x3f & (t >> 6) );
407 out[3] = UUENC( 0x3f & t );
411 _uuencode_line(struct archive_write *a, struct shar *shar, const char *inbuf, size_t len)
416 /* len <= 45 -> expanded to 60 + len byte + new line */
417 alloc_len = shar->work.length + 62;
418 if (archive_string_ensure(&shar->work, alloc_len) == NULL) {
419 archive_set_error(&a->archive, ENOMEM, "Out of memory");
420 return (ARCHIVE_FATAL);
423 buf = shar->work.s + shar->work.length;
426 uuencode_group(inbuf, buf);
433 tmp_buf[0] = inbuf[0];
437 tmp_buf[1] = inbuf[1];
439 uuencode_group(tmp_buf, buf);
443 if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62)) {
444 archive_set_error(&a->archive,
445 ARCHIVE_ERRNO_MISC, "Buffer overflow");
446 return (ARCHIVE_FATAL);
448 shar->work.length = buf - shar->work.s;
452 #define uuencode_line(__a, __shar, __inbuf, __len) \
454 int r = _uuencode_line(__a, __shar, __inbuf, __len); \
455 if (r != ARCHIVE_OK) \
456 return (ARCHIVE_FATAL); \
460 archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
468 shar = (struct shar *)a->format_data;
471 src = (const char *)buff;
473 if (shar->outpos != 0) {
474 n = 45 - shar->outpos;
477 memcpy(shar->outbuff + shar->outpos, src, n);
478 if (shar->outpos + n < 45) {
482 uuencode_line(a, shar, shar->outbuff, 45);
490 uuencode_line(a, shar, src, 45);
494 if (shar->work.length < 65536)
496 ret = __archive_write_output(a, shar->work.s,
498 if (ret != ARCHIVE_OK)
499 return (ARCHIVE_FATAL);
500 archive_string_empty(&shar->work);
503 memcpy(shar->outbuff, src, n);
510 archive_write_shar_finish_entry(struct archive_write *a)
512 const char *g, *p, *u;
516 shar = (struct shar *)a->format_data;
517 if (shar->entry == NULL)
521 /* Finish uuencoded data. */
522 if (shar->has_data) {
523 if (shar->outpos > 0)
524 uuencode_line(a, shar, shar->outbuff,
526 archive_strcat(&shar->work, "`\nend\n");
527 archive_strcat(&shar->work, "SHAR_END\n");
529 /* Restore file mode, owner, flags. */
531 * TODO: Don't immediately restore mode for
532 * directories; defer that to end of script.
534 archive_string_sprintf(&shar->work, "chmod %o ",
535 (unsigned int)(archive_entry_mode(shar->entry) & 07777));
536 shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1);
537 archive_strcat(&shar->work, "\n");
539 u = archive_entry_uname(shar->entry);
540 g = archive_entry_gname(shar->entry);
541 if (u != NULL || g != NULL) {
542 archive_strcat(&shar->work, "chown ");
544 shar_quote(&shar->work, u, 1);
546 archive_strcat(&shar->work, ":");
547 shar_quote(&shar->work, g, 1);
549 archive_strcat(&shar->work, " ");
550 shar_quote(&shar->work,
551 archive_entry_pathname(shar->entry), 1);
552 archive_strcat(&shar->work, "\n");
555 if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
556 archive_string_sprintf(&shar->work, "chflags %s ", p);
557 shar_quote(&shar->work,
558 archive_entry_pathname(shar->entry), 1);
559 archive_strcat(&shar->work, "\n");
562 /* TODO: restore ACLs */
565 if (shar->has_data) {
566 /* Finish sed-encoded data: ensure last line ends. */
567 if (!shar->end_of_line)
568 archive_strappend_char(&shar->work, '\n');
569 archive_strcat(&shar->work, "SHAR_END\n");
573 archive_entry_free(shar->entry);
576 if (shar->work.length < 65536)
579 ret = __archive_write_output(a, shar->work.s, shar->work.length);
580 if (ret != ARCHIVE_OK)
581 return (ARCHIVE_FATAL);
582 archive_string_empty(&shar->work);
588 archive_write_shar_close(struct archive_write *a)
594 * TODO: Accumulate list of directory names/modes and
595 * fix them all up at end-of-archive.
598 shar = (struct shar *)a->format_data;
601 * Only write the end-of-archive markers if the archive was
602 * actually started. This avoids problems if someone sets
603 * shar format, then sets another format (which would invoke
604 * shar_finish to free the format-specific data).
606 if (shar->wrote_header == 0)
609 archive_strcat(&shar->work, "exit\n");
611 ret = __archive_write_output(a, shar->work.s, shar->work.length);
612 if (ret != ARCHIVE_OK)
613 return (ARCHIVE_FATAL);
615 /* Shar output is never padded. */
616 archive_write_set_bytes_in_last_block(&a->archive, 1);
618 * TODO: shar should also suppress padding of
619 * uncompressed data within gzip/bzip2 streams.
626 archive_write_shar_free(struct archive_write *a)
630 shar = (struct shar *)a->format_data;
634 archive_entry_free(shar->entry);
635 free(shar->last_dir);
636 archive_string_free(&(shar->work));
637 archive_string_free(&(shar->quoted_name));
639 a->format_data = NULL;