Make sure UFS disallows mknod()'s with type VDIR.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_write_set_format_shar.c
blobb5d16e09d132026ce36d96ba04fca2c7be4a8f89
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 $");
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #include <stdarg.h>
33 #include <stdio.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
41 #include "archive.h"
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_write_private.h"
46 struct shar {
47 int dump;
48 int end_of_line;
49 struct archive_entry *entry;
50 int has_data;
51 char *last_dir;
52 char outbuff[1024];
53 size_t outbytes;
54 size_t outpos;
55 int uuavail;
56 char uubuffer[3];
57 int wrote_header;
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 *);
73 static int
74 shar_printf(struct archive_write *a, const char *fmt, ...)
76 struct shar *shar;
77 va_list ap;
78 int ret;
80 shar = (struct shar *)a->format_data;
81 va_start(ap, fmt);
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)));
85 va_end(ap);
86 return (ret);
90 * Set output format to 'shar' format.
92 int
93 archive_write_set_format_shar(struct archive *_a)
95 struct archive_write *a = (struct archive_write *)_a;
96 struct shar *shar;
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));
103 if (shar == NULL) {
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";
118 return (ARCHIVE_OK);
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;
131 struct shar *shar;
133 archive_write_set_format_shar(&a->archive);
134 shar = (struct shar *)a->format_data;
135 shar->dump = 1;
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";
139 return (ARCHIVE_OK);
142 static int
143 archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
145 const char *linkname;
146 const char *name;
147 char *p, *pp;
148 struct shar *shar;
149 int ret;
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)
155 return (ret);
156 ret = shar_printf(a, "# This is a shell archive\n");
157 if (ret != ARCHIVE_OK)
158 return (ret);
159 shar->wrote_header = 1;
162 /* Save the entry for the closing. */
163 if (shar->entry)
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)) {
170 case AE_IFREG:
171 /* Only regular files have non-zero size. */
172 break;
173 case AE_IFDIR:
174 archive_entry_set_size(entry, 0);
175 /* Don't bother trying to recreate '.' */
176 if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0)
177 return (ARCHIVE_OK);
178 break;
179 case AE_IFIFO:
180 case AE_IFCHR:
181 case AE_IFBLK:
182 /* All other file types have zero size in the archive. */
183 archive_entry_set_size(entry, 0);
184 break;
185 default:
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)
198 return (ret);
200 if (archive_entry_filetype(entry) != AE_IFDIR) {
201 /* Try to create the dir. */
202 p = strdup(name);
203 pp = strrchr(p, '/');
204 /* If there is a / character, try to create the dir. */
205 if (pp != NULL) {
206 *pp = '\0';
208 /* Try to avoid a lot of redundant mkdir commands. */
209 if (strcmp(p, ".") == 0) {
210 /* Don't try to "mkdir ." */
211 free(p);
212 } else if (shar->last_dir == NULL) {
213 ret = shar_printf(a,
214 "mkdir -p %s > /dev/null 2>&1\n", p);
215 if (ret != ARCHIVE_OK) {
216 free(p);
217 return (ret);
219 shar->last_dir = p;
220 } else if (strcmp(p, shar->last_dir) == 0) {
221 /* We've already created this exact dir. */
222 free(p);
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. */
226 free(p);
227 } else {
228 ret = shar_printf(a,
229 "mkdir -p %s > /dev/null 2>&1\n", p);
230 if (ret != ARCHIVE_OK) {
231 free(p);
232 return (ret);
234 free(shar->last_dir);
235 shar->last_dir = p;
237 } else {
238 free(p);
242 /* Handle file-type specific issues. */
243 shar->has_data = 0;
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)
247 return (ret);
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)
251 return (ret);
252 } else {
253 switch(archive_entry_filetype(entry)) {
254 case AE_IFREG:
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)
259 return (ret);
260 } else {
261 if (shar->dump) {
262 ret = shar_printf(a,
263 "uudecode -o %s << 'SHAR_END'\n",
264 name);
265 if (ret != ARCHIVE_OK)
266 return (ret);
267 ret = shar_printf(a, "begin %o %s\n",
268 archive_entry_mode(entry) & 0777,
269 name);
270 if (ret != ARCHIVE_OK)
271 return (ret);
272 } else {
273 ret = shar_printf(a,
274 "sed 's/^X//' > %s << 'SHAR_END'\n",
275 name);
276 if (ret != ARCHIVE_OK)
277 return (ret);
279 shar->has_data = 1;
280 shar->end_of_line = 1;
281 shar->outpos = 0;
282 shar->outbytes = 0;
284 break;
285 case AE_IFDIR:
286 ret = shar_printf(a, "mkdir -p %s > /dev/null 2>&1\n",
287 name);
288 if (ret != ARCHIVE_OK)
289 return (ret);
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')
298 *pp = '\0';
300 * TODO: Put dir name/mode on a list to be fixed
301 * up at end of archive.
303 break;
304 case AE_IFIFO:
305 ret = shar_printf(a, "mkfifo %s\n", name);
306 if (ret != ARCHIVE_OK)
307 return (ret);
308 break;
309 case AE_IFCHR:
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)
314 return (ret);
315 break;
316 case AE_IFBLK:
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)
321 return (ret);
322 break;
323 default:
324 return (ARCHIVE_WARN);
328 return (ARCHIVE_OK);
331 /* XXX TODO: This could be more efficient XXX */
332 static ssize_t
333 archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
335 struct shar *shar;
336 const char *src;
337 int ret;
338 size_t written = n;
340 shar = (struct shar *)a->format_data;
341 if (!shar->has_data)
342 return (0);
344 src = (const char *)buff;
345 ret = ARCHIVE_OK;
346 shar->outpos = 0;
347 while (n-- > 0) {
348 if (shar->end_of_line) {
349 shar->outbuff[shar->outpos++] = 'X';
350 shar->end_of_line = 0;
352 if (*src == '\n')
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,
358 shar->outpos);
359 if (ret != ARCHIVE_OK)
360 return (ret);
361 shar->outpos = 0;
365 if (shar->outpos > 0)
366 ret = (a->compressor.write)(a, shar->outbuff, shar->outpos);
367 if (ret != ARCHIVE_OK)
368 return (ret);
369 return (written);
372 #define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
374 /* XXX This could be a lot more efficient. XXX */
375 static void
376 uuencode_group(struct shar *shar)
378 int t;
380 t = 0;
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) );
391 shar->uuavail = 0;
392 shar->outbytes += shar->uuavail;
393 shar->outbuff[shar->outpos] = 0;
396 static ssize_t
397 archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
398 size_t length)
400 struct shar *shar;
401 const char *src;
402 size_t n;
403 int ret;
405 shar = (struct shar *)a->format_data;
406 if (!shar->has_data)
407 return (ARCHIVE_OK);
408 src = (const char *)buff;
409 n = length;
410 while (n-- > 0) {
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),
415 shar->outbuff);
416 if (ret != ARCHIVE_OK)
417 return (ret);
418 shar->outpos = 0;
419 shar->outbytes = 0;
422 shar->uubuffer[shar->uuavail++] = *src++;
423 shar->outbytes++;
425 return (length);
428 static int
429 archive_write_shar_finish_entry(struct archive_write *a)
431 const char *g, *p, *u;
432 struct shar *shar;
433 int ret;
435 shar = (struct shar *)a->format_data;
436 if (shar->entry == NULL)
437 return (0);
439 if (shar->dump) {
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)
448 return (ret);
449 shar->outpos = 0;
450 shar->uuavail = 0;
451 shar->outbytes = 0;
453 ret = shar_printf(a, "%c\n", UUENC(0));
454 if (ret != ARCHIVE_OK)
455 return (ret);
456 ret = shar_printf(a, "end\n", UUENC(0));
457 if (ret != ARCHIVE_OK)
458 return (ret);
459 ret = shar_printf(a, "SHAR_END\n");
460 if (ret != ARCHIVE_OK)
461 return (ret);
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)
472 return (ret);
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)
482 return (ret);
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)
489 return (ret);
492 /* TODO: restore ACLs */
494 } else {
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)
500 return (ret);
502 ret = shar_printf(a, "SHAR_END\n");
503 if (ret != ARCHIVE_OK)
504 return (ret);
508 archive_entry_free(shar->entry);
509 shar->entry = NULL;
510 return (0);
513 static int
514 archive_write_shar_finish(struct archive_write *a)
516 struct shar *shar;
517 int ret;
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)
535 return (ret);
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.
543 return (ARCHIVE_OK);
546 static int
547 archive_write_shar_destroy(struct archive_write *a)
549 struct shar *shar;
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));
557 free(shar);
558 a->format_data = NULL;
559 return (ARCHIVE_OK);