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.19 2008/03/15 11:04:45 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
.archive_format
= ARCHIVE_FORMAT_SHAR_BASE
;
117 a
->archive
.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
.archive_format
= ARCHIVE_FORMAT_SHAR_DUMP
;
138 a
->archive
.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 ." */
212 } else if (shar
->last_dir
== NULL
) {
214 "mkdir -p %s > /dev/null 2>&1\n", p
);
215 if (ret
!= ARCHIVE_OK
) {
220 } else if (strcmp(p
, shar
->last_dir
) == 0) {
221 /* We've already created this exact dir. */
223 } else if (strlen(p
) < strlen(shar
->last_dir
) &&
224 strncmp(p
, shar
->last_dir
, strlen(p
)) == 0) {
225 /* We've already created a subdir. */
229 "mkdir -p %s > /dev/null 2>&1\n", p
);
230 if (ret
!= ARCHIVE_OK
) {
234 free(shar
->last_dir
);
242 /* Handle file-type specific issues. */
244 if ((linkname
= archive_entry_hardlink(entry
)) != NULL
) {
245 ret
= shar_printf(a
, "ln -f %s %s\n", linkname
, name
);
246 if (ret
!= ARCHIVE_OK
)
248 } else if ((linkname
= archive_entry_symlink(entry
)) != NULL
) {
249 ret
= shar_printf(a
, "ln -fs %s %s\n", linkname
, name
);
250 if (ret
!= ARCHIVE_OK
)
253 switch(archive_entry_filetype(entry
)) {
255 if (archive_entry_size(entry
) == 0) {
256 /* More portable than "touch." */
257 ret
= shar_printf(a
, "test -e \"%s\" || :> \"%s\"\n", name
, name
);
258 if (ret
!= ARCHIVE_OK
)
263 "uudecode -o %s << 'SHAR_END'\n",
265 if (ret
!= ARCHIVE_OK
)
267 ret
= shar_printf(a
, "begin %o %s\n",
268 archive_entry_mode(entry
) & 0777,
270 if (ret
!= ARCHIVE_OK
)
274 "sed 's/^X//' > %s << 'SHAR_END'\n",
276 if (ret
!= ARCHIVE_OK
)
280 shar
->end_of_line
= 1;
286 ret
= shar_printf(a
, "mkdir -p %s > /dev/null 2>&1\n",
288 if (ret
!= ARCHIVE_OK
)
290 /* Record that we just created this directory. */
291 if (shar
->last_dir
!= NULL
)
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 ret
= shar_printf(a
, "mkfifo %s\n", name
);
306 if (ret
!= ARCHIVE_OK
)
310 ret
= shar_printf(a
, "mknod %s c %d %d\n", name
,
311 archive_entry_rdevmajor(entry
),
312 archive_entry_rdevminor(entry
));
313 if (ret
!= ARCHIVE_OK
)
317 ret
= shar_printf(a
, "mknod %s b %d %d\n", name
,
318 archive_entry_rdevmajor(entry
),
319 archive_entry_rdevminor(entry
));
320 if (ret
!= ARCHIVE_OK
)
324 return (ARCHIVE_WARN
);
331 /* XXX TODO: This could be more efficient XXX */
333 archive_write_shar_data_sed(struct archive_write
*a
, const void *buff
, size_t n
)
340 shar
= (struct shar
*)a
->format_data
;
344 src
= (const char *)buff
;
348 if (shar
->end_of_line
) {
349 shar
->outbuff
[shar
->outpos
++] = 'X';
350 shar
->end_of_line
= 0;
353 shar
->end_of_line
= 1;
354 shar
->outbuff
[shar
->outpos
++] = *src
++;
356 if (shar
->outpos
> sizeof(shar
->outbuff
) - 2) {
357 ret
= (a
->compressor
.write
)(a
, shar
->outbuff
,
359 if (ret
!= ARCHIVE_OK
)
365 if (shar
->outpos
> 0)
366 ret
= (a
->compressor
.write
)(a
, shar
->outbuff
, shar
->outpos
);
367 if (ret
!= ARCHIVE_OK
)
372 #define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
374 /* XXX This could be a lot more efficient. XXX */
376 uuencode_group(struct shar
*shar
)
381 if (shar
->uuavail
> 0)
382 t
= 0xff0000 & (shar
->uubuffer
[0] << 16);
383 if (shar
->uuavail
> 1)
384 t
|= 0x00ff00 & (shar
->uubuffer
[1] << 8);
385 if (shar
->uuavail
> 2)
386 t
|= 0x0000ff & (shar
->uubuffer
[2]);
387 shar
->outbuff
[shar
->outpos
++] = UUENC( 0x3f & (t
>>18) );
388 shar
->outbuff
[shar
->outpos
++] = UUENC( 0x3f & (t
>>12) );
389 shar
->outbuff
[shar
->outpos
++] = UUENC( 0x3f & (t
>>6) );
390 shar
->outbuff
[shar
->outpos
++] = UUENC( 0x3f & (t
) );
392 shar
->outbytes
+= shar
->uuavail
;
393 shar
->outbuff
[shar
->outpos
] = 0;
397 archive_write_shar_data_uuencode(struct archive_write
*a
, const void *buff
,
405 shar
= (struct shar
*)a
->format_data
;
408 src
= (const char *)buff
;
411 if (shar
->uuavail
== 3)
412 uuencode_group(shar
);
413 if (shar
->outpos
>= 60) {
414 ret
= shar_printf(a
, "%c%s\n", UUENC(shar
->outbytes
),
416 if (ret
!= ARCHIVE_OK
)
422 shar
->uubuffer
[shar
->uuavail
++] = *src
++;
429 archive_write_shar_finish_entry(struct archive_write
*a
)
431 const char *g
, *p
, *u
;
435 shar
= (struct shar
*)a
->format_data
;
436 if (shar
->entry
== NULL
)
440 /* Finish uuencoded data. */
441 if (shar
->has_data
) {
442 if (shar
->uuavail
> 0)
443 uuencode_group(shar
);
444 if (shar
->outpos
> 0) {
445 ret
= shar_printf(a
, "%c%s\n",
446 UUENC(shar
->outbytes
), shar
->outbuff
);
447 if (ret
!= ARCHIVE_OK
)
453 ret
= shar_printf(a
, "%c\n", UUENC(0));
454 if (ret
!= ARCHIVE_OK
)
456 ret
= shar_printf(a
, "end\n", UUENC(0));
457 if (ret
!= ARCHIVE_OK
)
459 ret
= shar_printf(a
, "SHAR_END\n");
460 if (ret
!= ARCHIVE_OK
)
463 /* Restore file mode, owner, flags. */
465 * TODO: Don't immediately restore mode for
466 * directories; defer that to end of script.
468 ret
= shar_printf(a
, "chmod %o %s\n",
469 archive_entry_mode(shar
->entry
) & 07777,
470 archive_entry_pathname(shar
->entry
));
471 if (ret
!= ARCHIVE_OK
)
474 u
= archive_entry_uname(shar
->entry
);
475 g
= archive_entry_gname(shar
->entry
);
476 if (u
!= NULL
|| g
!= NULL
) {
477 ret
= shar_printf(a
, "chown %s%s%s %s\n",
478 (u
!= NULL
) ? u
: "",
479 (g
!= NULL
) ? ":" : "", (g
!= NULL
) ? g
: "",
480 archive_entry_pathname(shar
->entry
));
481 if (ret
!= ARCHIVE_OK
)
485 if ((p
= archive_entry_fflags_text(shar
->entry
)) != NULL
) {
486 ret
= shar_printf(a
, "chflags %s %s\n", p
,
487 archive_entry_pathname(shar
->entry
));
488 if (ret
!= ARCHIVE_OK
)
492 /* TODO: restore ACLs */
495 if (shar
->has_data
) {
496 /* Finish sed-encoded data: ensure last line ends. */
497 if (!shar
->end_of_line
) {
498 ret
= shar_printf(a
, "\n");
499 if (ret
!= ARCHIVE_OK
)
502 ret
= shar_printf(a
, "SHAR_END\n");
503 if (ret
!= ARCHIVE_OK
)
508 archive_entry_free(shar
->entry
);
514 archive_write_shar_finish(struct archive_write
*a
)
520 * TODO: Accumulate list of directory names/modes and
521 * fix them all up at end-of-archive.
524 shar
= (struct shar
*)a
->format_data
;
527 * Only write the end-of-archive markers if the archive was
528 * actually started. This avoids problems if someone sets
529 * shar format, then sets another format (which would invoke
530 * shar_finish to free the format-specific data).
532 if (shar
->wrote_header
) {
533 ret
= shar_printf(a
, "exit\n");
534 if (ret
!= ARCHIVE_OK
)
536 /* Shar output is never padded. */
537 archive_write_set_bytes_in_last_block(&a
->archive
, 1);
539 * TODO: shar should also suppress padding of
540 * uncompressed data within gzip/bzip2 streams.
547 archive_write_shar_destroy(struct archive_write
*a
)
551 shar
= (struct shar
*)a
->format_data
;
552 if (shar
->entry
!= NULL
)
553 archive_entry_free(shar
->entry
);
554 if (shar
->last_dir
!= NULL
)
555 free(shar
->last_dir
);
556 archive_string_free(&(shar
->work
));
558 a
->format_data
= NULL
;