Import libarchive 2.1.9.
[dragonfly.git] / contrib / libarchive-2.1 / libarchive / archive_write_set_format_shar.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
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.
13  *
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.
24  */
25
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 $");
28
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
40
41 #include "archive.h"
42 #include "archive_entry.h"
43 #include "archive_private.h"
44 #include "archive_write_private.h"
45
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;
59 };
60
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 *);
72
73 static int
74 shar_printf(struct archive_write *a, const char *fmt, ...)
75 {
76         struct shar *shar;
77         va_list ap;
78         int ret;
79
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);
87 }
88
89 /*
90  * Set output format to 'shar' format.
91  */
92 int
93 archive_write_set_format_shar(struct archive *_a)
94 {
95         struct archive_write *a = (struct archive_write *)_a;
96         struct shar *shar;
97
98         /* If someone else was already registered, unregister them. */
99         if (a->format_destroy != NULL)
100                 (a->format_destroy)(a);
101
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);
106         }
107         memset(shar, 0, sizeof(*shar));
108         a->format_data = shar;
109
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";
118         return (ARCHIVE_OK);
119 }
120
121 /*
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.
126  */
127 int
128 archive_write_set_format_shar_dump(struct archive *_a)
129 {
130         struct archive_write *a = (struct archive_write *)_a;
131         struct shar *shar;
132
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_format = ARCHIVE_FORMAT_SHAR_DUMP;
138         a->archive_format_name = "shar dump";
139         return (ARCHIVE_OK);
140 }
141
142 static int
143 archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
144 {
145         const char *linkname;
146         const char *name;
147         char *p, *pp;
148         struct shar *shar;
149         int ret;
150
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;
160         }
161
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);
167
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);
192                 }
193         }
194
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);
199
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';
207
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) {
212                                 ret = shar_printf(a,
213                                     "mkdir -p %s > /dev/null 2>&1\n", p);
214                                 if (ret != ARCHIVE_OK)
215                                         return (ret);
216                                 shar->last_dir = p;
217                         } else if (strcmp(p, shar->last_dir) == 0) {
218                                 /* We've already created this exact dir. */
219                                 free(p);
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. */
223                                 free(p);
224                         } else {
225                                 ret = shar_printf(a,
226                                     "mkdir -p %s > /dev/null 2>&1\n", p);
227                                 if (ret != ARCHIVE_OK)
228                                         return (ret);
229                                 free(shar->last_dir);
230                                 shar->last_dir = p;
231                         }
232                 }
233         }
234
235         /* Handle file-type specific issues. */
236         shar->has_data = 0;
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)
240                         return (ret);
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)
244                         return (ret);
245         } else {
246                 switch(archive_entry_filetype(entry)) {
247                 case AE_IFREG:
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)
252                                         return (ret);
253                         } else {
254                                 if (shar->dump) {
255                                         ret = shar_printf(a,
256                                             "uudecode -o %s << 'SHAR_END'\n",
257                                             name);
258                                         if (ret != ARCHIVE_OK)
259                                                 return (ret);
260                                         ret = shar_printf(a, "begin %o %s\n",
261                                             archive_entry_mode(entry) & 0777,
262                                             name);
263                                         if (ret != ARCHIVE_OK)
264                                                 return (ret);
265                                 } else {
266                                         ret = shar_printf(a,
267                                             "sed 's/^X//' > %s << 'SHAR_END'\n",
268                                             name);
269                                         if (ret != ARCHIVE_OK)
270                                                 return (ret);
271                                 }
272                                 shar->has_data = 1;
273                                 shar->end_of_line = 1;
274                                 shar->outpos = 0;
275                                 shar->outbytes = 0;
276                         }
277                         break;
278                 case AE_IFDIR:
279                         ret = shar_printf(a, "mkdir -p %s > /dev/null 2>&1\n",
280                             name);
281                         if (ret != ARCHIVE_OK)
282                                 return (ret);
283                         /* Record that we just created this directory. */
284                         if (shar->last_dir != NULL)
285                                 free(shar->last_dir);
286
287                         shar->last_dir = strdup(name);
288                         /* Trim a trailing '/'. */
289                         pp = strrchr(shar->last_dir, '/');
290                         if (pp != NULL && pp[1] == '\0')
291                                 *pp = '\0';
292                         /*
293                          * TODO: Put dir name/mode on a list to be fixed
294                          * up at end of archive.
295                          */
296                         break;
297                 case AE_IFIFO:
298                         ret = shar_printf(a, "mkfifo %s\n", name);
299                         if (ret != ARCHIVE_OK)
300                                 return (ret);
301                         break;
302                 case AE_IFCHR:
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)
307                                 return (ret);
308                         break;
309                 case AE_IFBLK:
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)
314                                 return (ret);
315                         break;
316                 default:
317                         return (ARCHIVE_WARN);
318                 }
319         }
320
321         return (ARCHIVE_OK);
322 }
323
324 /* XXX TODO: This could be more efficient XXX */
325 static ssize_t
326 archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
327 {
328         struct shar *shar;
329         const char *src;
330         int ret;
331         size_t written = n;
332
333         shar = (struct shar *)a->format_data;
334         if (!shar->has_data)
335                 return (0);
336
337         src = (const char *)buff;
338         ret = ARCHIVE_OK;
339         shar->outpos = 0;
340         while (n-- > 0) {
341                 if (shar->end_of_line) {
342                         shar->outbuff[shar->outpos++] = 'X';
343                         shar->end_of_line = 0;
344                 }
345                 if (*src == '\n')
346                         shar->end_of_line = 1;
347                 shar->outbuff[shar->outpos++] = *src++;
348
349                 if (shar->outpos > sizeof(shar->outbuff) - 2) {
350                         ret = (a->compressor.write)(a, shar->outbuff,
351                             shar->outpos);
352                         if (ret != ARCHIVE_OK)
353                                 return (ret);
354                         shar->outpos = 0;
355                 }
356         }
357
358         if (shar->outpos > 0)
359                 ret = (a->compressor.write)(a, shar->outbuff, shar->outpos);
360         if (ret != ARCHIVE_OK)
361                 return (ret);
362         return (written);
363 }
364
365 #define UUENC(c)        (((c)!=0) ? ((c) & 077) + ' ': '`')
366
367 /* XXX This could be a lot more efficient. XXX */
368 static void
369 uuencode_group(struct shar *shar)
370 {
371         int     t;
372
373         t = 0;
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) );
384         shar->uuavail = 0;
385         shar->outbytes += shar->uuavail;
386         shar->outbuff[shar->outpos] = 0;
387 }
388
389 static ssize_t
390 archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
391     size_t length)
392 {
393         struct shar *shar;
394         const char *src;
395         size_t n;
396         int ret;
397
398         shar = (struct shar *)a->format_data;
399         if (!shar->has_data)
400                 return (ARCHIVE_OK);
401         src = (const char *)buff;
402         n = length;
403         while (n-- > 0) {
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),
408                             shar->outbuff);
409                         if (ret != ARCHIVE_OK)
410                                 return (ret);
411                         shar->outpos = 0;
412                         shar->outbytes = 0;
413                 }
414
415                 shar->uubuffer[shar->uuavail++] = *src++;
416                 shar->outbytes++;
417         }
418         return (length);
419 }
420
421 static int
422 archive_write_shar_finish_entry(struct archive_write *a)
423 {
424         const char *g, *p, *u;
425         struct shar *shar;
426         int ret;
427
428         shar = (struct shar *)a->format_data;
429         if (shar->entry == NULL)
430                 return (0);
431
432         if (shar->dump) {
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)
441                                         return (ret);
442                                 shar->outpos = 0;
443                                 shar->uuavail = 0;
444                                 shar->outbytes = 0;
445                         }
446                         ret = shar_printf(a, "%c\n", UUENC(0));
447                         if (ret != ARCHIVE_OK)
448                                 return (ret);
449                         ret = shar_printf(a, "end\n", UUENC(0));
450                         if (ret != ARCHIVE_OK)
451                                 return (ret);
452                         ret = shar_printf(a, "SHAR_END\n");
453                         if (ret != ARCHIVE_OK)
454                                 return (ret);
455                 }
456                 /* Restore file mode, owner, flags. */
457                 /*
458                  * TODO: Don't immediately restore mode for
459                  * directories; defer that to end of script.
460                  */
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)
465                         return (ret);
466
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)
475                                 return (ret);
476                 }
477
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)
482                                 return (ret);
483                 }
484
485                 /* TODO: restore ACLs */
486
487         } else {
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)
493                                         return (ret);
494                         }
495                         ret = shar_printf(a, "SHAR_END\n");
496                         if (ret != ARCHIVE_OK)
497                                 return (ret);
498                 }
499         }
500
501         archive_entry_free(shar->entry);
502         shar->entry = NULL;
503         return (0);
504 }
505
506 static int
507 archive_write_shar_finish(struct archive_write *a)
508 {
509         struct shar *shar;
510         int ret;
511
512         /*
513          * TODO: Accumulate list of directory names/modes and
514          * fix them all up at end-of-archive.
515          */
516
517         shar = (struct shar *)a->format_data;
518
519         /*
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).
524          */
525         if (shar->wrote_header) {
526                 ret = shar_printf(a, "exit\n");
527                 if (ret != ARCHIVE_OK)
528                         return (ret);
529                 /* Shar output is never padded. */
530                 archive_write_set_bytes_in_last_block(&a->archive, 1);
531                 /*
532                  * TODO: shar should also suppress padding of
533                  * uncompressed data within gzip/bzip2 streams.
534                  */
535         }
536         return (ARCHIVE_OK);
537 }
538
539 static int
540 archive_write_shar_destroy(struct archive_write *a)
541 {
542         struct shar *shar;
543
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));
550         free(shar);
551         a->format_data = NULL;
552         return (ARCHIVE_OK);
553 }