2 * Copyright (c) 2003-2007 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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_format_shar.c,v 1.16 2007/03/03 07:37:36 kientzle Exp $");
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_write_private.h"
49 struct archive_entry *entry;
58 struct archive_string work;
61 static int archive_write_shar_finish(struct archive_write *);
62 static int archive_write_shar_destroy(struct archive_write *);
63 static int archive_write_shar_header(struct archive_write *,
64 struct archive_entry *);
65 static ssize_t archive_write_shar_data_sed(struct archive_write *,
66 const void * buff, size_t);
67 static ssize_t archive_write_shar_data_uuencode(struct archive_write *,
68 const void * buff, size_t);
69 static int archive_write_shar_finish_entry(struct archive_write *);
70 static int shar_printf(struct archive_write *, const char *fmt, ...);
71 static void uuencode_group(struct shar *);
74 shar_printf(struct archive_write *a, const char *fmt, ...)
80 shar = (struct shar *)a->format_data;
82 archive_string_empty(&(shar->work));
83 archive_string_vsprintf(&(shar->work), fmt, ap);
84 ret = ((a->compressor.write)(a, shar->work.s, strlen(shar->work.s)));
90 * Set output format to 'shar' format.
93 archive_write_set_format_shar(struct archive *_a)
95 struct archive_write *a = (struct archive_write *)_a;
98 /* If someone else was already registered, unregister them. */
99 if (a->format_destroy != NULL)
100 (a->format_destroy)(a);
102 shar = (struct shar *)malloc(sizeof(*shar));
104 archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data");
105 return (ARCHIVE_FATAL);
107 memset(shar, 0, sizeof(*shar));
108 a->format_data = shar;
110 a->pad_uncompressed = 0;
111 a->format_write_header = archive_write_shar_header;
112 a->format_finish = archive_write_shar_finish;
113 a->format_destroy = archive_write_shar_destroy;
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)
130 struct archive_write *a = (struct archive_write *)_a;
133 archive_write_set_format_shar(&a->archive);
134 shar = (struct shar *)a->format_data;
136 a->format_write_data = archive_write_shar_data_uuencode;
137 a->archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
138 a->archive_format_name = "shar dump";
143 archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
145 const char *linkname;
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);
168 /* Handle some preparatory issues. */
169 switch(archive_entry_filetype(entry)) {
171 /* Only regular files have non-zero size. */
174 archive_entry_set_size(entry, 0);
175 /* Don't bother trying to recreate '.' */
176 if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0)
182 /* All other file types have zero size in the archive. */
183 archive_entry_set_size(entry, 0);
186 archive_entry_set_size(entry, 0);
187 if (archive_entry_hardlink(entry) == NULL &&
188 archive_entry_symlink(entry) == NULL) {
189 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
190 "shar format cannot archive this");
191 return (ARCHIVE_WARN);
195 /* Stock preparation for all file types. */
196 ret = shar_printf(a, "echo x %s\n", name);
197 if (ret != ARCHIVE_OK)
200 if (archive_entry_filetype(entry) != AE_IFDIR) {
201 /* Try to create the dir. */
203 pp = strrchr(p, '/');
204 /* If there is a / character, try to create the dir. */
208 /* Try to avoid a lot of redundant mkdir commands. */
209 if (strcmp(p, ".") == 0) {
210 /* Don't try to "mkdir ." */
211 } else if (shar->last_dir == NULL) {
213 "mkdir -p %s > /dev/null 2>&1\n", p);
214 if (ret != ARCHIVE_OK)
217 } else if (strcmp(p, shar->last_dir) == 0) {
218 /* We've already created this exact dir. */
220 } else if (strlen(p) < strlen(shar->last_dir) &&
221 strncmp(p, shar->last_dir, strlen(p)) == 0) {
222 /* We've already created a subdir. */
226 "mkdir -p %s > /dev/null 2>&1\n", p);
227 if (ret != ARCHIVE_OK)
229 free(shar->last_dir);
235 /* Handle file-type specific issues. */
237 if ((linkname = archive_entry_hardlink(entry)) != NULL) {
238 ret = shar_printf(a, "ln -f %s %s\n", linkname, name);
239 if (ret != ARCHIVE_OK)
241 } else if ((linkname = archive_entry_symlink(entry)) != NULL) {
242 ret = shar_printf(a, "ln -fs %s %s\n", linkname, name);
243 if (ret != ARCHIVE_OK)
246 switch(archive_entry_filetype(entry)) {
248 if (archive_entry_size(entry) == 0) {
249 /* More portable than "touch." */
250 ret = shar_printf(a, "test -e \"%s\" || :> \"%s\"\n", name, name);
251 if (ret != ARCHIVE_OK)
256 "uudecode -o %s << 'SHAR_END'\n",
258 if (ret != ARCHIVE_OK)
260 ret = shar_printf(a, "begin %o %s\n",
261 archive_entry_mode(entry) & 0777,
263 if (ret != ARCHIVE_OK)
267 "sed 's/^X//' > %s << 'SHAR_END'\n",
269 if (ret != ARCHIVE_OK)
273 shar->end_of_line = 1;
279 ret = shar_printf(a, "mkdir -p %s > /dev/null 2>&1\n",
281 if (ret != ARCHIVE_OK)
283 /* Record that we just created this directory. */
284 if (shar->last_dir != NULL)
285 free(shar->last_dir);
287 shar->last_dir = strdup(name);
288 /* Trim a trailing '/'. */
289 pp = strrchr(shar->last_dir, '/');
290 if (pp != NULL && pp[1] == '\0')
293 * TODO: Put dir name/mode on a list to be fixed
294 * up at end of archive.
298 ret = shar_printf(a, "mkfifo %s\n", name);
299 if (ret != ARCHIVE_OK)
303 ret = shar_printf(a, "mknod %s c %d %d\n", name,
304 archive_entry_rdevmajor(entry),
305 archive_entry_rdevminor(entry));
306 if (ret != ARCHIVE_OK)
310 ret = shar_printf(a, "mknod %s b %d %d\n", name,
311 archive_entry_rdevmajor(entry),
312 archive_entry_rdevminor(entry));
313 if (ret != ARCHIVE_OK)
317 return (ARCHIVE_WARN);
324 /* XXX TODO: This could be more efficient XXX */
326 archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
333 shar = (struct shar *)a->format_data;
337 src = (const char *)buff;
341 if (shar->end_of_line) {
342 shar->outbuff[shar->outpos++] = 'X';
343 shar->end_of_line = 0;
346 shar->end_of_line = 1;
347 shar->outbuff[shar->outpos++] = *src++;
349 if (shar->outpos > sizeof(shar->outbuff) - 2) {
350 ret = (a->compressor.write)(a, shar->outbuff,
352 if (ret != ARCHIVE_OK)
358 if (shar->outpos > 0)
359 ret = (a->compressor.write)(a, shar->outbuff, shar->outpos);
360 if (ret != ARCHIVE_OK)
365 #define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
367 /* XXX This could be a lot more efficient. XXX */
369 uuencode_group(struct shar *shar)
374 if (shar->uuavail > 0)
375 t = 0xff0000 & (shar->uubuffer[0] << 16);
376 if (shar->uuavail > 1)
377 t |= 0x00ff00 & (shar->uubuffer[1] << 8);
378 if (shar->uuavail > 2)
379 t |= 0x0000ff & (shar->uubuffer[2]);
380 shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>18) );
381 shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>12) );
382 shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>6) );
383 shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t) );
385 shar->outbytes += shar->uuavail;
386 shar->outbuff[shar->outpos] = 0;
390 archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
398 shar = (struct shar *)a->format_data;
401 src = (const char *)buff;
404 if (shar->uuavail == 3)
405 uuencode_group(shar);
406 if (shar->outpos >= 60) {
407 ret = shar_printf(a, "%c%s\n", UUENC(shar->outbytes),
409 if (ret != ARCHIVE_OK)
415 shar->uubuffer[shar->uuavail++] = *src++;
422 archive_write_shar_finish_entry(struct archive_write *a)
424 const char *g, *p, *u;
428 shar = (struct shar *)a->format_data;
429 if (shar->entry == NULL)
433 /* Finish uuencoded data. */
434 if (shar->has_data) {
435 if (shar->uuavail > 0)
436 uuencode_group(shar);
437 if (shar->outpos > 0) {
438 ret = shar_printf(a, "%c%s\n",
439 UUENC(shar->outbytes), shar->outbuff);
440 if (ret != ARCHIVE_OK)
446 ret = shar_printf(a, "%c\n", UUENC(0));
447 if (ret != ARCHIVE_OK)
449 ret = shar_printf(a, "end\n", UUENC(0));
450 if (ret != ARCHIVE_OK)
452 ret = shar_printf(a, "SHAR_END\n");
453 if (ret != ARCHIVE_OK)
456 /* Restore file mode, owner, flags. */
458 * TODO: Don't immediately restore mode for
459 * directories; defer that to end of script.
461 ret = shar_printf(a, "chmod %o %s\n",
462 archive_entry_mode(shar->entry) & 07777,
463 archive_entry_pathname(shar->entry));
464 if (ret != ARCHIVE_OK)
467 u = archive_entry_uname(shar->entry);
468 g = archive_entry_gname(shar->entry);
469 if (u != NULL || g != NULL) {
470 ret = shar_printf(a, "chown %s%s%s %s\n",
471 (u != NULL) ? u : "",
472 (g != NULL) ? ":" : "", (g != NULL) ? g : "",
473 archive_entry_pathname(shar->entry));
474 if (ret != ARCHIVE_OK)
478 if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
479 ret = shar_printf(a, "chflags %s %s\n", p,
480 archive_entry_pathname(shar->entry));
481 if (ret != ARCHIVE_OK)
485 /* TODO: restore ACLs */
488 if (shar->has_data) {
489 /* Finish sed-encoded data: ensure last line ends. */
490 if (!shar->end_of_line) {
491 ret = shar_printf(a, "\n");
492 if (ret != ARCHIVE_OK)
495 ret = shar_printf(a, "SHAR_END\n");
496 if (ret != ARCHIVE_OK)
501 archive_entry_free(shar->entry);
507 archive_write_shar_finish(struct archive_write *a)
513 * TODO: Accumulate list of directory names/modes and
514 * fix them all up at end-of-archive.
517 shar = (struct shar *)a->format_data;
520 * Only write the end-of-archive markers if the archive was
521 * actually started. This avoids problems if someone sets
522 * shar format, then sets another format (which would invoke
523 * shar_finish to free the format-specific data).
525 if (shar->wrote_header) {
526 ret = shar_printf(a, "exit\n");
527 if (ret != ARCHIVE_OK)
529 /* Shar output is never padded. */
530 archive_write_set_bytes_in_last_block(&a->archive, 1);
532 * TODO: shar should also suppress padding of
533 * uncompressed data within gzip/bzip2 streams.
540 archive_write_shar_destroy(struct archive_write *a)
544 shar = (struct shar *)a->format_data;
545 if (shar->entry != NULL)
546 archive_entry_free(shar->entry);
547 if (shar->last_dir != NULL)
548 free(shar->last_dir);
549 archive_string_free(&(shar->work));
551 a->format_data = NULL;