Merge from vendor branch NTPD:
[dragonfly.git] / contrib / 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 int      archive_write_shar_data_sed(struct archive *,
60                     const void * buff, size_t);
61 static int      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 int
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
325         shar = a->format_data;
326         if (!shar->has_data)
327                 return (0);
328
329         src = buff;
330         ret = ARCHIVE_OK;
331         shar->outpos = 0;
332         while (n-- > 0) {
333                 if (shar->end_of_line) {
334                         shar->outbuff[shar->outpos++] = 'X';
335                         shar->end_of_line = 0;
336                 }
337                 if (*src == '\n')
338                         shar->end_of_line = 1;
339                 shar->outbuff[shar->outpos++] = *src++;
340
341                 if (shar->outpos > sizeof(shar->outbuff) - 2) {
342                         ret = (a->compression_write)(a, shar->outbuff,
343                             shar->outpos);
344                         if (ret != ARCHIVE_OK)
345                                 return (ret);
346                         shar->outpos = 0;
347                 }
348         }
349
350         if (shar->outpos > 0)
351                 ret = (a->compression_write)(a, shar->outbuff, shar->outpos);
352         return (ret);
353 }
354
355 #define UUENC(c)        (((c)!=0) ? ((c) & 077) + ' ': '`')
356
357 /* XXX This could be a lot more efficient. XXX */
358 static void
359 uuencode_group(struct shar *shar)
360 {
361         int     t;
362
363         t = 0;
364         if (shar->uuavail > 0)
365                 t = 0xff0000 & (shar->uubuffer[0] << 16);
366         if (shar->uuavail > 1)
367                 t |= 0x00ff00 & (shar->uubuffer[1] << 8);
368         if (shar->uuavail > 2)
369                 t |= 0x0000ff & (shar->uubuffer[2]);
370         shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>18) );
371         shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>12) );
372         shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t>>6) );
373         shar->outbuff[shar->outpos++] = UUENC( 0x3f & (t) );
374         shar->uuavail = 0;
375         shar->outbytes += shar->uuavail;
376         shar->outbuff[shar->outpos] = 0;
377 }
378
379 static int
380 archive_write_shar_data_uuencode(struct archive *a, const void *buff,
381     size_t length)
382 {
383         struct shar *shar;
384         const char *src;
385         size_t n;
386         int ret;
387
388         shar = a->format_data;
389         if (!shar->has_data)
390                 return (ARCHIVE_OK);
391         src = buff;
392         n = length;
393         while (n-- > 0) {
394                 if (shar->uuavail == 3)
395                         uuencode_group(shar);
396                 if (shar->outpos >= 60) {
397                         ret = shar_printf(a, "%c%s\n", UUENC(shar->outbytes),
398                             shar->outbuff);
399                         if (ret != ARCHIVE_OK)
400                                 return (ret);
401                         shar->outpos = 0;
402                         shar->outbytes = 0;
403                 }
404
405                 shar->uubuffer[shar->uuavail++] = *src++;
406                 shar->outbytes++;
407         }
408         return (ARCHIVE_OK);
409 }
410
411 static int
412 archive_write_shar_finish_entry(struct archive *a)
413 {
414         const char *g, *p, *u;
415         struct shar *shar;
416         int ret;
417
418         shar = a->format_data;
419         if (shar->entry == NULL)
420                 return (0);
421
422         if (shar->dump) {
423                 /* Finish uuencoded data. */
424                 if (shar->has_data) {
425                         if (shar->uuavail > 0)
426                                 uuencode_group(shar);
427                         if (shar->outpos > 0) {
428                                 ret = shar_printf(a, "%c%s\n",
429                                     UUENC(shar->outbytes), shar->outbuff);
430                                 if (ret != ARCHIVE_OK)
431                                         return (ret);
432                                 shar->outpos = 0;
433                                 shar->uuavail = 0;
434                                 shar->outbytes = 0;
435                         }
436                         ret = shar_printf(a, "%c\n", UUENC(0));
437                         if (ret != ARCHIVE_OK)
438                                 return (ret);
439                         ret = shar_printf(a, "end\n", UUENC(0));
440                         if (ret != ARCHIVE_OK)
441                                 return (ret);
442                         ret = shar_printf(a, "SHAR_END\n");
443                         if (ret != ARCHIVE_OK)
444                                 return (ret);
445                 }
446                 /* Restore file mode, owner, flags. */
447                 /*
448                  * TODO: Don't immediately restore mode for
449                  * directories; defer that to end of script.
450                  */
451                 ret = shar_printf(a, "chmod %o %s\n",
452                     archive_entry_mode(shar->entry) & 07777,
453                     archive_entry_pathname(shar->entry));
454                 if (ret != ARCHIVE_OK)
455                         return (ret);
456
457                 u = archive_entry_uname(shar->entry);
458                 g = archive_entry_gname(shar->entry);
459                 if (u != NULL || g != NULL) {
460                         ret = shar_printf(a, "chown %s%s%s %s\n",
461                             (u != NULL) ? u : "",
462                             (g != NULL) ? ":" : "", (g != NULL) ? g : "",
463                             archive_entry_pathname(shar->entry));
464                         if (ret != ARCHIVE_OK)
465                                 return (ret);
466                 }
467
468                 if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
469                         ret = shar_printf(a, "chflags %s %s\n", p,
470                             archive_entry_pathname(shar->entry));
471                         if (ret != ARCHIVE_OK)
472                                 return (ret);
473                 }
474
475                 /* TODO: restore ACLs */
476
477         } else {
478                 if (shar->has_data) {
479                         /* Finish sed-encoded data:  ensure last line ends. */
480                         if (!shar->end_of_line) {
481                                 ret = shar_printf(a, "\n");
482                                 if (ret != ARCHIVE_OK)
483                                         return (ret);
484                         }
485                         ret = shar_printf(a, "SHAR_END\n");
486                         if (ret != ARCHIVE_OK)
487                                 return (ret);
488                 }
489         }
490
491         archive_entry_free(shar->entry);
492         shar->entry = NULL;
493         return (0);
494 }
495
496 static int
497 archive_write_shar_finish(struct archive *a)
498 {
499         struct shar *shar;
500         int ret;
501
502         /*
503          * TODO: Accumulate list of directory names/modes and
504          * fix them all up at end-of-archive.
505          */
506
507         shar = a->format_data;
508
509         /*
510          * Only write the end-of-archive markers if the archive was
511          * actually started.  This avoids problems if someone sets
512          * shar format, then sets another format (which would invoke
513          * shar_finish to free the format-specific data).
514          */
515         if (shar->wrote_header) {
516                 ret = shar_printf(a, "exit\n");
517                 if (ret != ARCHIVE_OK)
518                         return (ret);
519                 /* Shar output is never padded. */
520                 archive_write_set_bytes_in_last_block(a, 1);
521                 /*
522                  * TODO: shar should also suppress padding of
523                  * uncompressed data within gzip/bzip2 streams.
524                  */
525         }
526         if (shar->entry != NULL)
527                 archive_entry_free(shar->entry);
528         if (shar->last_dir != NULL)
529                 free(shar->last_dir);
530         archive_string_free(&(shar->work));
531         free(shar);
532         a->format_data = NULL;
533         return (ARCHIVE_OK);
534 }