Merge from vendor branch AWK:
[dragonfly.git] / contrib / libarchive-1.3.1 / libarchive / archive_write_set_format_shar.c
1 /*-
2  * Copyright (c) 2003-2004 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  *    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.
14  *
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.
25  */
26
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_format_shar.c,v 1.11 2004/11/05 05:26:30 kientzle Exp $");
29
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "archive.h"
38 #include "archive_entry.h"
39 #include "archive_private.h"
40
41 struct shar {
42         int                      dump;
43         int                      end_of_line;
44         struct archive_entry    *entry;
45         int                      has_data;
46         char                    *last_dir;
47         char                     outbuff[1024];
48         size_t                   outbytes;
49         size_t                   outpos;
50         int                      uuavail;
51         char                     uubuffer[3];
52         int                      wrote_header;
53         struct archive_string    work;
54 };
55
56 static int      archive_write_shar_finish(struct archive *);
57 static int      archive_write_shar_header(struct archive *,
58                     struct archive_entry *);
59 static ssize_t  archive_write_shar_data_sed(struct archive *,
60                     const void * buff, size_t);
61 static ssize_t  archive_write_shar_data_uuencode(struct archive *,
62                     const void * buff, size_t);
63 static int      archive_write_shar_finish_entry(struct archive *);
64 static int      shar_printf(struct archive *, const char *fmt, ...);
65 static void     uuencode_group(struct shar *);
66
67 static int
68 shar_printf(struct archive *a, const char *fmt, ...)
69 {
70         struct shar *shar;
71         va_list ap;
72         int ret;
73
74         shar = a->format_data;
75         va_start(ap, fmt);
76         archive_string_empty(&(shar->work));
77         archive_string_vsprintf(&(shar->work), fmt, ap);
78         ret = ((a->compression_write)(a, shar->work.s, strlen(shar->work.s)));
79         va_end(ap);
80         return (ret);
81 }
82
83 /*
84  * Set output format to 'shar' format.
85  */
86 int
87 archive_write_set_format_shar(struct archive *a)
88 {
89         struct shar *shar;
90
91         /* If someone else was already registered, unregister them. */
92         if (a->format_finish != NULL)
93                 (a->format_finish)(a);
94
95         shar = malloc(sizeof(*shar));
96         if (shar == NULL) {
97                 archive_set_error(a, ENOMEM, "Can't allocate shar data");
98                 return (ARCHIVE_FATAL);
99         }
100         memset(shar, 0, sizeof(*shar));
101         a->format_data = shar;
102
103         a->pad_uncompressed = 0;
104         a->format_write_header = archive_write_shar_header;
105         a->format_finish = archive_write_shar_finish;
106         a->format_write_data = archive_write_shar_data_sed;
107         a->format_finish_entry = archive_write_shar_finish_entry;
108         a->archive_format = ARCHIVE_FORMAT_SHAR_BASE;
109         a->archive_format_name = "shar";
110         return (ARCHIVE_OK);
111 }
112
113 /*
114  * An alternate 'shar' that uses uudecode instead of 'sed' to encode
115  * file contents and can therefore be used to archive binary files.
116  * In addition, this variant also attempts to restore ownership, file modes,
117  * and other extended file information.
118  */
119 int
120 archive_write_set_format_shar_dump(struct archive *a)
121 {
122         struct shar *shar;
123
124         archive_write_set_format_shar(a);
125         shar = a->format_data;
126         shar->dump = 1;
127         a->format_write_data = archive_write_shar_data_uuencode;
128         a->archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
129         a->archive_format_name = "shar dump";
130         return (ARCHIVE_OK);
131 }
132
133 static int
134 archive_write_shar_header(struct archive *a, struct archive_entry *entry)
135 {
136         const char *linkname;
137         const char *name;
138         char *p, *pp;
139         struct shar *shar;
140         const struct stat *st;
141         int ret;
142
143         shar = a->format_data;
144         if (!shar->wrote_header) {
145                 ret = shar_printf(a, "#!/bin/sh\n");
146                 if (ret != ARCHIVE_OK)
147                         return (ret);
148                 ret = shar_printf(a, "# This is a shell archive\n");
149                 if (ret != ARCHIVE_OK)
150                         return (ret);
151                 shar->wrote_header = 1;
152         }
153
154         /* Save the entry for the closing. */
155         if (shar->entry)
156                 archive_entry_free(shar->entry);
157         shar->entry = archive_entry_clone(entry);
158         name = archive_entry_pathname(entry);
159         st = archive_entry_stat(entry);
160
161         /* Handle some preparatory issues. */
162         switch(st->st_mode & S_IFMT) {
163         case S_IFREG:
164                 /* Only regular files have non-zero size. */
165                 break;
166         case S_IFDIR:
167                 archive_entry_set_size(entry, 0);
168                 /* Don't bother trying to recreate '.' */
169                 if (strcmp(name, ".") == 0  ||  strcmp(name, "./") == 0)
170                         return (ARCHIVE_OK);
171                 break;
172         case S_IFIFO:
173         case S_IFCHR:
174         case S_IFBLK:
175                 /* All other file types have zero size in the archive. */
176                 archive_entry_set_size(entry, 0);
177                 break;
178         default:
179                 archive_entry_set_size(entry, 0);
180                 if (archive_entry_hardlink(entry) == NULL &&
181                     archive_entry_symlink(entry) == NULL) {
182                         archive_set_error(a, ARCHIVE_ERRNO_MISC,
183                             "shar format cannot archive this");
184                         return (ARCHIVE_WARN);
185                 }
186         }
187
188         /* Stock preparation for all file types. */
189         ret = shar_printf(a, "echo x %s\n", name);
190         if (ret != ARCHIVE_OK)
191                 return (ret);
192
193         if (!S_ISDIR(st->st_mode)) {
194                 /* Try to create the dir. */
195                 p = strdup(name);
196                 pp = strrchr(p, '/');
197                 /* If there is a / character, try to create the dir. */
198                 if (pp != NULL) {
199                         *pp = '\0';
200
201                         /* Try to avoid a lot of redundant mkdir commands. */
202                         if (strcmp(p, ".") == 0) {
203                                 /* Don't try to "mkdir ." */
204                         } else if (shar->last_dir == NULL) {
205                                 ret = shar_printf(a,
206                                     "mkdir -p %s > /dev/null 2>&1\n", p);
207                                 if (ret != ARCHIVE_OK)
208                                         return (ret);
209                                 shar->last_dir = p;
210                         } else if (strcmp(p, shar->last_dir) == 0) {
211                                 /* We've already created this exact dir. */
212                                 free(p);
213                         } else if (strlen(p) < strlen(shar->last_dir) &&
214                             strncmp(p, shar->last_dir, strlen(p)) == 0) {
215                                 /* We've already created a subdir. */
216                                 free(p);
217                         } else {
218                                 ret = shar_printf(a,
219                                     "mkdir -p %s > /dev/null 2>&1\n", p);
220                                 if (ret != ARCHIVE_OK)
221                                         return (ret);
222                                 free(shar->last_dir);
223                                 shar->last_dir = p;
224                         }
225                 }
226         }
227
228         /* Handle file-type specific issues. */
229         shar->has_data = 0;
230         if ((linkname = archive_entry_hardlink(entry)) != NULL) {
231                 ret = shar_printf(a, "ln -f %s %s\n", linkname, name);
232                 if (ret != ARCHIVE_OK)
233                         return (ret);
234         } else if ((linkname = archive_entry_symlink(entry)) != NULL) {
235                 ret = shar_printf(a, "ln -fs %s %s\n", linkname, name);
236                 if (ret != ARCHIVE_OK)
237                         return (ret);
238         } else {
239                 switch(st->st_mode & S_IFMT) {
240                 case S_IFREG:
241                         if (archive_entry_size(entry) == 0) {
242                                 /* More portable than "touch." */
243                                 ret = shar_printf(a, "test -e \"%s\" || :> \"%s\"\n", name, name);
244                                 if (ret != ARCHIVE_OK)
245                                         return (ret);
246                         } else {
247                                 if (shar->dump) {
248                                         ret = shar_printf(a,
249                                             "uudecode -o %s << 'SHAR_END'\n",
250                                             name);
251                                         if (ret != ARCHIVE_OK)
252                                                 return (ret);
253                                         ret = shar_printf(a, "begin %o %s\n",
254                                             archive_entry_mode(entry) & 0777,
255                                             name);
256                                         if (ret != ARCHIVE_OK)
257                                                 return (ret);
258                                 } else {
259                                         ret = shar_printf(a,
260                                             "sed 's/^X//' > %s << 'SHAR_END'\n",
261                                             name);
262                                         if (ret != ARCHIVE_OK)
263                                                 return (ret);
264                                 }
265                                 shar->has_data = 1;
266                                 shar->end_of_line = 1;
267                                 shar->outpos = 0;
268                                 shar->outbytes = 0;
269                         }
270                         break;
271                 case S_IFDIR:
272                         ret = shar_printf(a, "mkdir -p %s > /dev/null 2>&1\n",
273                             name);
274                         if (ret != ARCHIVE_OK)
275                                 return (ret);
276                         /* Record that we just created this directory. */
277                         if (shar->last_dir != NULL)
278                                 free(shar->last_dir);
279
280                         shar->last_dir = strdup(name);
281                         /* Trim a trailing '/'. */
282                         pp = strrchr(shar->last_dir, '/');
283                         if (pp != NULL && pp[1] == '\0')
284                                 *pp = '\0';
285                         /*
286                          * TODO: Put dir name/mode on a list to be fixed
287                          * up at end of archive.
288                          */
289                         break;
290                 case S_IFIFO:
291                         ret = shar_printf(a, "mkfifo %s\n", name);
292                         if (ret != ARCHIVE_OK)
293                                 return (ret);
294                         break;
295                 case S_IFCHR:
296                         ret = shar_printf(a, "mknod %s c %d %d\n", name,
297                             archive_entry_rdevmajor(entry),
298                             archive_entry_rdevminor(entry));
299                         if (ret != ARCHIVE_OK)
300                                 return (ret);
301                         break;
302                 case S_IFBLK:
303                         ret = shar_printf(a, "mknod %s b %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                 default:
310                         return (ARCHIVE_WARN);
311                 }
312         }
313
314         return (ARCHIVE_OK);
315 }
316
317 /* XXX TODO: This could be more efficient XXX */
318 static ssize_t
319 archive_write_shar_data_sed(struct archive *a, const void *buff, size_t n)
320 {
321         struct shar *shar;
322         const char *src;
323         int ret;
324         size_t written = n;
325
326         shar = a->format_data;
327         if (!shar->has_data)
328                 return (0);
329
330         src = buff;
331         ret = ARCHIVE_OK;
332         shar->outpos = 0;
333         while (n-- > 0) {
334                 if (shar->end_of_line) {
335                         shar->outbuff[shar->outpos++] = 'X';
336                         shar->end_of_line = 0;
337                 }
338                 if (*src == '\n')
339                         shar->end_of_line = 1;
340                 shar->outbuff[shar->outpos++] = *src++;
341
342                 if (shar->outpos > sizeof(shar->outbuff) - 2) {
343                         ret = (a->compression_write)(a, shar->outbuff,
344                             shar->outpos);
345                         if (ret != ARCHIVE_OK)
346                                 return (ret);
347                         shar->outpos = 0;
348                 }
349         }
350
351         if (shar->outpos > 0)
352                 ret = (a->compression_write)(a, shar->outbuff, shar->outpos);
353         if (ret != ARCHIVE_OK)
354                 return (ret);
355         return (written);
356 }
357
358 #define UUENC(c)        (((c)!=0) ? ((c) & 077) + ' ': '`')
359
360 /* XXX This could be a lot more efficient. XXX */
361 static void
362 uuencode_group(struct shar *shar)
363 {
364         int     t;
365
366         t = 0;
367         if (shar->uuavail > 0)
368                 t = 0xff0000 & (shar->uubuffer[0] << 16);
369         if (shar->uuavail > 1)
370                 t |= 0x00ff00 & (shar->uubuffer[1] << 8);
371         if (shar->uuavail > 2)
372                 t |= 0x0000ff & (shar->uubuffer[2]);
373         shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>18) );
374         shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>12) );
375         shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>6) );
376         shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t) );
377         shar->uuavail = 0;
378         shar->outbytes += shar->uuavail;
379         shar->outbuff[shar->outpos] = 0;
380 }
381
382 static ssize_t
383 archive_write_shar_data_uuencode(struct archive *a, const void *buff,
384     size_t length)
385 {
386         struct shar *shar;
387         const char *src;
388         size_t n;
389         int ret;
390
391         shar = a->format_data;
392         if (!shar->has_data)
393                 return (ARCHIVE_OK);
394         src = buff;
395         n = length;
396         while (n-- > 0) {
397                 if (shar->uuavail == 3)
398                         uuencode_group(shar);
399                 if (shar->outpos >= 60) {
400                         ret = shar_printf(a, "%c%s\n", UUENC(shar->outbytes),
401                             shar->outbuff);
402                         if (ret != ARCHIVE_OK)
403                                 return (ret);
404                         shar->outpos = 0;
405                         shar->outbytes = 0;
406                 }
407
408                 shar->uubuffer[shar->uuavail++] = *src++;
409                 shar->outbytes++;
410         }
411         return (length);
412 }
413
414 static int
415 archive_write_shar_finish_entry(struct archive *a)
416 {
417         const char *g, *p, *u;
418         struct shar *shar;
419         int ret;
420
421         shar = a->format_data;
422         if (shar->entry == NULL)
423                 return (0);
424
425         if (shar->dump) {
426                 /* Finish uuencoded data. */
427                 if (shar->has_data) {
428                         if (shar->uuavail > 0)
429                                 uuencode_group(shar);
430                         if (shar->outpos > 0) {
431                                 ret = shar_printf(a, "%c%s\n",
432                                     UUENC(shar->outbytes), shar->outbuff);
433                                 if (ret != ARCHIVE_OK)
434                                         return (ret);
435                                 shar->outpos = 0;
436                                 shar->uuavail = 0;
437                                 shar->outbytes = 0;
438                         }
439                         ret = shar_printf(a, "%c\n", UUENC(0));
440                         if (ret != ARCHIVE_OK)
441                                 return (ret);
442                         ret = shar_printf(a, "end\n", UUENC(0));
443                         if (ret != ARCHIVE_OK)
444                                 return (ret);
445                         ret = shar_printf(a, "SHAR_END\n");
446                         if (ret != ARCHIVE_OK)
447                                 return (ret);
448                 }
449                 /* Restore file mode, owner, flags. */
450                 /*
451                  * TODO: Don't immediately restore mode for
452                  * directories; defer that to end of script.
453                  */
454                 ret = shar_printf(a, "chmod %o %s\n",
455                     archive_entry_mode(shar->entry) & 07777,
456                     archive_entry_pathname(shar->entry));
457                 if (ret != ARCHIVE_OK)
458                         return (ret);
459
460                 u = archive_entry_uname(shar->entry);
461                 g = archive_entry_gname(shar->entry);
462                 if (u != NULL || g != NULL) {
463                         ret = shar_printf(a, "chown %s%s%s %s\n",
464                             (u != NULL) ? u : "",
465                             (g != NULL) ? ":" : "", (g != NULL) ? g : "",
466                             archive_entry_pathname(shar->entry));
467                         if (ret != ARCHIVE_OK)
468                                 return (ret);
469                 }
470
471                 if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
472                         ret = shar_printf(a, "chflags %s %s\n", p,
473                             archive_entry_pathname(shar->entry));
474                         if (ret != ARCHIVE_OK)
475                                 return (ret);
476                 }
477
478                 /* TODO: restore ACLs */
479
480         } else {
481                 if (shar->has_data) {
482                         /* Finish sed-encoded data:  ensure last line ends. */
483                         if (!shar->end_of_line) {
484                                 ret = shar_printf(a, "\n");
485                                 if (ret != ARCHIVE_OK)
486                                         return (ret);
487                         }
488                         ret = shar_printf(a, "SHAR_END\n");
489                         if (ret != ARCHIVE_OK)
490                                 return (ret);
491                 }
492         }
493
494         archive_entry_free(shar->entry);
495         shar->entry = NULL;
496         return (0);
497 }
498
499 static int
500 archive_write_shar_finish(struct archive *a)
501 {
502         struct shar *shar;
503         int ret;
504
505         /*
506          * TODO: Accumulate list of directory names/modes and
507          * fix them all up at end-of-archive.
508          */
509
510         shar = a->format_data;
511
512         /*
513          * Only write the end-of-archive markers if the archive was
514          * actually started.  This avoids problems if someone sets
515          * shar format, then sets another format (which would invoke
516          * shar_finish to free the format-specific data).
517          */
518         if (shar->wrote_header) {
519                 ret = shar_printf(a, "exit\n");
520                 if (ret != ARCHIVE_OK)
521                         return (ret);
522                 /* Shar output is never padded. */
523                 archive_write_set_bytes_in_last_block(a, 1);
524                 /*
525                  * TODO: shar should also suppress padding of
526                  * uncompressed data within gzip/bzip2 streams.
527                  */
528         }
529         if (shar->entry != NULL)
530                 archive_entry_free(shar->entry);
531         if (shar->last_dir != NULL)
532                 free(shar->last_dir);
533         archive_string_free(&(shar->work));
534         free(shar);
535         a->format_data = NULL;
536         return (ARCHIVE_OK);
537 }