2 * Copyright (c) 2003-2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
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$");
30 #ifdef HAVE_SYS_STAT_H
46 #include "archive_entry.h"
47 #include "archive_private.h"
52 struct archive_entry *entry;
61 struct archive_string work;
64 static int archive_write_shar_finish(struct archive *);
65 static int archive_write_shar_header(struct archive *,
66 struct archive_entry *);
67 static ssize_t archive_write_shar_data_sed(struct archive *,
68 const void * buff, size_t);
69 static ssize_t archive_write_shar_data_uuencode(struct archive *,
70 const void * buff, size_t);
71 static int archive_write_shar_finish_entry(struct archive *);
72 static int shar_printf(struct archive *, const char *fmt, ...);
73 static void uuencode_group(struct shar *);
76 shar_printf(struct archive *a, const char *fmt, ...)
82 shar = (struct shar *)a->format_data;
84 archive_string_empty(&(shar->work));
85 archive_string_vsprintf(&(shar->work), fmt, ap);
86 ret = ((a->compression_write)(a, shar->work.s, strlen(shar->work.s)));
92 * Set output format to 'shar' format.
95 archive_write_set_format_shar(struct archive *a)
99 /* If someone else was already registered, unregister them. */
100 if (a->format_finish != NULL)
101 (a->format_finish)(a);
103 shar = (struct shar *)malloc(sizeof(*shar));
105 archive_set_error(a, ENOMEM, "Can't allocate shar data");
106 return (ARCHIVE_FATAL);
108 memset(shar, 0, sizeof(*shar));
109 a->format_data = shar;
111 a->pad_uncompressed = 0;
112 a->format_write_header = archive_write_shar_header;
113 a->format_finish = archive_write_shar_finish;
114 a->format_write_data = archive_write_shar_data_sed;
115 a->format_finish_entry = archive_write_shar_finish_entry;
116 a->archive_format = ARCHIVE_FORMAT_SHAR_BASE;
117 a->archive_format_name = "shar";
122 * An alternate 'shar' that uses uudecode instead of 'sed' to encode
123 * file contents and can therefore be used to archive binary files.
124 * In addition, this variant also attempts to restore ownership, file modes,
125 * and other extended file information.
128 archive_write_set_format_shar_dump(struct archive *a)
132 archive_write_set_format_shar(a);
133 shar = (struct shar *)a->format_data;
135 a->format_write_data = archive_write_shar_data_uuencode;
136 a->archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
137 a->archive_format_name = "shar dump";
142 archive_write_shar_header(struct archive *a, struct archive_entry *entry)
144 const char *linkname;
148 const struct stat *st;
151 shar = (struct shar *)a->format_data;
152 if (!shar->wrote_header) {
153 ret = shar_printf(a, "#!/bin/sh\n");
154 if (ret != ARCHIVE_OK)
156 ret = shar_printf(a, "# This is a shell archive\n");
157 if (ret != ARCHIVE_OK)
159 shar->wrote_header = 1;
162 /* Save the entry for the closing. */
164 archive_entry_free(shar->entry);
165 shar->entry = archive_entry_clone(entry);
166 name = archive_entry_pathname(entry);
167 st = archive_entry_stat(entry);
169 /* Handle some preparatory issues. */
170 switch(st->st_mode & S_IFMT) {
172 /* Only regular files have non-zero size. */
175 archive_entry_set_size(entry, 0);
176 /* Don't bother trying to recreate '.' */
177 if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0)
183 /* All other file types have zero size in the archive. */
184 archive_entry_set_size(entry, 0);
187 archive_entry_set_size(entry, 0);
188 if (archive_entry_hardlink(entry) == NULL &&
189 archive_entry_symlink(entry) == NULL) {
190 archive_set_error(a, ARCHIVE_ERRNO_MISC,
191 "shar format cannot archive this");
192 return (ARCHIVE_WARN);
196 /* Stock preparation for all file types. */
197 ret = shar_printf(a, "echo x %s\n", name);
198 if (ret != ARCHIVE_OK)
201 if (!S_ISDIR(st->st_mode)) {
202 /* Try to create the dir. */
204 pp = strrchr(p, '/');
205 /* If there is a / character, try to create the dir. */
209 /* Try to avoid a lot of redundant mkdir commands. */
210 if (strcmp(p, ".") == 0) {
211 /* Don't try to "mkdir ." */
212 } else if (shar->last_dir == NULL) {
214 "mkdir -p %s > /dev/null 2>&1\n", p);
215 if (ret != ARCHIVE_OK)
218 } else if (strcmp(p, shar->last_dir) == 0) {
219 /* We've already created this exact dir. */
221 } else if (strlen(p) < strlen(shar->last_dir) &&
222 strncmp(p, shar->last_dir, strlen(p)) == 0) {
223 /* We've already created a subdir. */
227 "mkdir -p %s > /dev/null 2>&1\n", p);
228 if (ret != ARCHIVE_OK)
230 free(shar->last_dir);
236 /* Handle file-type specific issues. */
238 if ((linkname = archive_entry_hardlink(entry)) != NULL) {
239 ret = shar_printf(a, "ln -f %s %s\n", linkname, name);
240 if (ret != ARCHIVE_OK)
242 } else if ((linkname = archive_entry_symlink(entry)) != NULL) {
243 ret = shar_printf(a, "ln -fs %s %s\n", linkname, name);
244 if (ret != ARCHIVE_OK)
247 switch(st->st_mode & S_IFMT) {
249 if (archive_entry_size(entry) == 0) {
250 /* More portable than "touch." */
251 ret = shar_printf(a, "test -e \"%s\" || :> \"%s\"\n", name, name);
252 if (ret != ARCHIVE_OK)
257 "uudecode -o %s << 'SHAR_END'\n",
259 if (ret != ARCHIVE_OK)
261 ret = shar_printf(a, "begin %o %s\n",
262 archive_entry_mode(entry) & 0777,
264 if (ret != ARCHIVE_OK)
268 "sed 's/^X//' > %s << 'SHAR_END'\n",
270 if (ret != ARCHIVE_OK)
274 shar->end_of_line = 1;
280 ret = shar_printf(a, "mkdir -p %s > /dev/null 2>&1\n",
282 if (ret != ARCHIVE_OK)
284 /* Record that we just created this directory. */
285 if (shar->last_dir != NULL)
286 free(shar->last_dir);
288 shar->last_dir = strdup(name);
289 /* Trim a trailing '/'. */
290 pp = strrchr(shar->last_dir, '/');
291 if (pp != NULL && pp[1] == '\0')
294 * TODO: Put dir name/mode on a list to be fixed
295 * up at end of archive.
299 ret = shar_printf(a, "mkfifo %s\n", name);
300 if (ret != ARCHIVE_OK)
304 ret = shar_printf(a, "mknod %s c %d %d\n", name,
305 archive_entry_rdevmajor(entry),
306 archive_entry_rdevminor(entry));
307 if (ret != ARCHIVE_OK)
311 ret = shar_printf(a, "mknod %s b %d %d\n", name,
312 archive_entry_rdevmajor(entry),
313 archive_entry_rdevminor(entry));
314 if (ret != ARCHIVE_OK)
318 return (ARCHIVE_WARN);
325 /* XXX TODO: This could be more efficient XXX */
327 archive_write_shar_data_sed(struct archive *a, const void *buff, size_t n)
334 shar = (struct shar *)a->format_data;
338 src = (const char *)buff;
342 if (shar->end_of_line) {
343 shar->outbuff[shar->outpos++] = 'X';
344 shar->end_of_line = 0;
347 shar->end_of_line = 1;
348 shar->outbuff[shar->outpos++] = *src++;
350 if (shar->outpos > sizeof(shar->outbuff) - 2) {
351 ret = (a->compression_write)(a, shar->outbuff,
353 if (ret != ARCHIVE_OK)
359 if (shar->outpos > 0)
360 ret = (a->compression_write)(a, shar->outbuff, shar->outpos);
361 if (ret != ARCHIVE_OK)
366 #define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
368 /* XXX This could be a lot more efficient. XXX */
370 uuencode_group(struct shar *shar)
375 if (shar->uuavail > 0)
376 t = 0xff0000 & (shar->uubuffer[0] << 16);
377 if (shar->uuavail > 1)
378 t |= 0x00ff00 & (shar->uubuffer[1] << 8);
379 if (shar->uuavail > 2)
380 t |= 0x0000ff & (shar->uubuffer[2]);
381 shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>18) );
382 shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>12) );
383 shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>6) );
384 shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t) );
386 shar->outbytes += shar->uuavail;
387 shar->outbuff[shar->outpos] = 0;
391 archive_write_shar_data_uuencode(struct archive *a, const void *buff,
399 shar = (struct shar *)a->format_data;
402 src = (const char *)buff;
405 if (shar->uuavail == 3)
406 uuencode_group(shar);
407 if (shar->outpos >= 60) {
408 ret = shar_printf(a, "%c%s\n", UUENC(shar->outbytes),
410 if (ret != ARCHIVE_OK)
416 shar->uubuffer[shar->uuavail++] = *src++;
423 archive_write_shar_finish_entry(struct archive *a)
425 const char *g, *p, *u;
429 shar = (struct shar *)a->format_data;
430 if (shar->entry == NULL)
434 /* Finish uuencoded data. */
435 if (shar->has_data) {
436 if (shar->uuavail > 0)
437 uuencode_group(shar);
438 if (shar->outpos > 0) {
439 ret = shar_printf(a, "%c%s\n",
440 UUENC(shar->outbytes), shar->outbuff);
441 if (ret != ARCHIVE_OK)
447 ret = shar_printf(a, "%c\n", UUENC(0));
448 if (ret != ARCHIVE_OK)
450 ret = shar_printf(a, "end\n", UUENC(0));
451 if (ret != ARCHIVE_OK)
453 ret = shar_printf(a, "SHAR_END\n");
454 if (ret != ARCHIVE_OK)
457 /* Restore file mode, owner, flags. */
459 * TODO: Don't immediately restore mode for
460 * directories; defer that to end of script.
462 ret = shar_printf(a, "chmod %o %s\n",
463 archive_entry_mode(shar->entry) & 07777,
464 archive_entry_pathname(shar->entry));
465 if (ret != ARCHIVE_OK)
468 u = archive_entry_uname(shar->entry);
469 g = archive_entry_gname(shar->entry);
470 if (u != NULL || g != NULL) {
471 ret = shar_printf(a, "chown %s%s%s %s\n",
472 (u != NULL) ? u : "",
473 (g != NULL) ? ":" : "", (g != NULL) ? g : "",
474 archive_entry_pathname(shar->entry));
475 if (ret != ARCHIVE_OK)
479 if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
480 ret = shar_printf(a, "chflags %s %s\n", p,
481 archive_entry_pathname(shar->entry));
482 if (ret != ARCHIVE_OK)
486 /* TODO: restore ACLs */
489 if (shar->has_data) {
490 /* Finish sed-encoded data: ensure last line ends. */
491 if (!shar->end_of_line) {
492 ret = shar_printf(a, "\n");
493 if (ret != ARCHIVE_OK)
496 ret = shar_printf(a, "SHAR_END\n");
497 if (ret != ARCHIVE_OK)
502 archive_entry_free(shar->entry);
508 archive_write_shar_finish(struct archive *a)
514 * TODO: Accumulate list of directory names/modes and
515 * fix them all up at end-of-archive.
518 shar = (struct shar *)a->format_data;
521 * Only write the end-of-archive markers if the archive was
522 * actually started. This avoids problems if someone sets
523 * shar format, then sets another format (which would invoke
524 * shar_finish to free the format-specific data).
526 if (shar->wrote_header) {
527 ret = shar_printf(a, "exit\n");
528 if (ret != ARCHIVE_OK)
530 /* Shar output is never padded. */
531 archive_write_set_bytes_in_last_block(a, 1);
533 * TODO: shar should also suppress padding of
534 * uncompressed data within gzip/bzip2 streams.
537 if (shar->entry != NULL)
538 archive_entry_free(shar->entry);
539 if (shar->last_dir != NULL)
540 free(shar->last_dir);
541 archive_string_free(&(shar->work));
543 a->format_data = NULL;