]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libarchive/archive_write_set_format_shar.c
import ath hal
[FreeBSD/FreeBSD.git] / lib / libarchive / archive_write_set_format_shar.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2008 Joerg Sonnenberger
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
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$");
29
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
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
53         /* Line buffer for uuencoded dump format */
54         char                     outbuff[45];
55         size_t                   outpos;
56
57         int                      wrote_header;
58         struct archive_string    work;
59         struct archive_string    quoted_name;
60 };
61
62 static int      archive_write_shar_finish(struct archive_write *);
63 static int      archive_write_shar_destroy(struct archive_write *);
64 static int      archive_write_shar_header(struct archive_write *,
65                     struct archive_entry *);
66 static ssize_t  archive_write_shar_data_sed(struct archive_write *,
67                     const void * buff, size_t);
68 static ssize_t  archive_write_shar_data_uuencode(struct archive_write *,
69                     const void * buff, size_t);
70 static int      archive_write_shar_finish_entry(struct archive_write *);
71
72 /*
73  * Copy the given string to the buffer, quoting all shell meta characters
74  * found.
75  */
76 static void
77 shar_quote(struct archive_string *buf, const char *str, int in_shell)
78 {
79         static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
80         size_t len;
81
82         while (*str != '\0') {
83                 if ((len = strcspn(str, meta)) != 0) {
84                         archive_strncat(buf, str, len);
85                         str += len;
86                 } else if (*str == '\n') {
87                         if (in_shell)
88                                 archive_strcat(buf, "\"\n\"");
89                         else
90                                 archive_strcat(buf, "\\n");
91                         ++str;
92                 } else {
93                         archive_strappend_char(buf, '\\');
94                         archive_strappend_char(buf, *str);
95                         ++str;
96                 }
97         }
98 }
99
100 /*
101  * Set output format to 'shar' format.
102  */
103 int
104 archive_write_set_format_shar(struct archive *_a)
105 {
106         struct archive_write *a = (struct archive_write *)_a;
107         struct shar *shar;
108
109         /* If someone else was already registered, unregister them. */
110         if (a->format_destroy != NULL)
111                 (a->format_destroy)(a);
112
113         shar = (struct shar *)malloc(sizeof(*shar));
114         if (shar == NULL) {
115                 archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data");
116                 return (ARCHIVE_FATAL);
117         }
118         memset(shar, 0, sizeof(*shar));
119         archive_string_init(&shar->work);
120         archive_string_init(&shar->quoted_name);
121         a->format_data = shar;
122
123         a->pad_uncompressed = 0;
124         a->format_write_header = archive_write_shar_header;
125         a->format_finish = archive_write_shar_finish;
126         a->format_destroy = archive_write_shar_destroy;
127         a->format_write_data = archive_write_shar_data_sed;
128         a->format_finish_entry = archive_write_shar_finish_entry;
129         a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE;
130         a->archive.archive_format_name = "shar";
131         return (ARCHIVE_OK);
132 }
133
134 /*
135  * An alternate 'shar' that uses uudecode instead of 'sed' to encode
136  * file contents and can therefore be used to archive binary files.
137  * In addition, this variant also attempts to restore ownership, file modes,
138  * and other extended file information.
139  */
140 int
141 archive_write_set_format_shar_dump(struct archive *_a)
142 {
143         struct archive_write *a = (struct archive_write *)_a;
144         struct shar *shar;
145
146         archive_write_set_format_shar(&a->archive);
147         shar = (struct shar *)a->format_data;
148         shar->dump = 1;
149         a->format_write_data = archive_write_shar_data_uuencode;
150         a->archive.archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
151         a->archive.archive_format_name = "shar dump";
152         return (ARCHIVE_OK);
153 }
154
155 static int
156 archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
157 {
158         const char *linkname;
159         const char *name;
160         char *p, *pp;
161         struct shar *shar;
162
163         shar = (struct shar *)a->format_data;
164         if (!shar->wrote_header) {
165                 archive_strcat(&shar->work, "#!/bin/sh\n");
166                 archive_strcat(&shar->work, "# This is a shell archive\n");
167                 shar->wrote_header = 1;
168         }
169
170         /* Save the entry for the closing. */
171         if (shar->entry)
172                 archive_entry_free(shar->entry);
173         shar->entry = archive_entry_clone(entry);
174         name = archive_entry_pathname(entry);
175
176         /* Handle some preparatory issues. */
177         switch(archive_entry_filetype(entry)) {
178         case AE_IFREG:
179                 /* Only regular files have non-zero size. */
180                 break;
181         case AE_IFDIR:
182                 archive_entry_set_size(entry, 0);
183                 /* Don't bother trying to recreate '.' */
184                 if (strcmp(name, ".") == 0  ||  strcmp(name, "./") == 0)
185                         return (ARCHIVE_OK);
186                 break;
187         case AE_IFIFO:
188         case AE_IFCHR:
189         case AE_IFBLK:
190                 /* All other file types have zero size in the archive. */
191                 archive_entry_set_size(entry, 0);
192                 break;
193         default:
194                 archive_entry_set_size(entry, 0);
195                 if (archive_entry_hardlink(entry) == NULL &&
196                     archive_entry_symlink(entry) == NULL) {
197                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
198                             "shar format cannot archive this");
199                         return (ARCHIVE_WARN);
200                 }
201         }
202
203         archive_string_empty(&shar->quoted_name);
204         shar_quote(&shar->quoted_name, name, 1);
205
206         /* Stock preparation for all file types. */
207         archive_string_sprintf(&shar->work, "echo x %s\n", shar->quoted_name.s);
208
209         if (archive_entry_filetype(entry) != AE_IFDIR) {
210                 /* Try to create the dir. */
211                 p = strdup(name);
212                 pp = strrchr(p, '/');
213                 /* If there is a / character, try to create the dir. */
214                 if (pp != NULL) {
215                         *pp = '\0';
216
217                         /* Try to avoid a lot of redundant mkdir commands. */
218                         if (strcmp(p, ".") == 0) {
219                                 /* Don't try to "mkdir ." */
220                                 free(p);
221                         } else if (shar->last_dir == NULL) {
222                                 archive_strcat(&shar->work, "mkdir -p ");
223                                 shar_quote(&shar->work, p, 1);
224                                 archive_strcat(&shar->work,
225                                     " > /dev/null 2>&1\n");
226                                 shar->last_dir = p;
227                         } else if (strcmp(p, shar->last_dir) == 0) {
228                                 /* We've already created this exact dir. */
229                                 free(p);
230                         } else if (strlen(p) < strlen(shar->last_dir) &&
231                             strncmp(p, shar->last_dir, strlen(p)) == 0) {
232                                 /* We've already created a subdir. */
233                                 free(p);
234                         } else {
235                                 archive_strcat(&shar->work, "mkdir -p ");
236                                 shar_quote(&shar->work, p, 1);
237                                 archive_strcat(&shar->work,
238                                     " > /dev/null 2>&1\n");
239                                 shar->last_dir = p;
240                         }
241                 } else {
242                         free(p);
243                 }
244         }
245
246         /* Handle file-type specific issues. */
247         shar->has_data = 0;
248         if ((linkname = archive_entry_hardlink(entry)) != NULL) {
249                 archive_strcat(&shar->work, "ln -f ");
250                 shar_quote(&shar->work, linkname, 1);
251                 archive_string_sprintf(&shar->work, " %s\n",
252                     shar->quoted_name.s);
253         } else if ((linkname = archive_entry_symlink(entry)) != NULL) {
254                 archive_strcat(&shar->work, "ln -fs ");
255                 shar_quote(&shar->work, linkname, 1);
256                 archive_string_sprintf(&shar->work, " %s\n",
257                     shar->quoted_name.s);
258         } else {
259                 switch(archive_entry_filetype(entry)) {
260                 case AE_IFREG:
261                         if (archive_entry_size(entry) == 0) {
262                                 /* More portable than "touch." */
263                                 archive_string_sprintf(&shar->work,
264                                     "test -e \"%s\" || :> \"%s\"\n",
265                                     shar->quoted_name.s, shar->quoted_name.s);
266                         } else {
267                                 if (shar->dump) {
268                                         archive_string_sprintf(&shar->work,
269                                             "uudecode -p > %s << 'SHAR_END'\n",
270                                             shar->quoted_name.s);
271                                         archive_string_sprintf(&shar->work,
272                                             "begin %o ",
273                                             archive_entry_mode(entry) & 0777);
274                                         shar_quote(&shar->work, name, 0);
275                                         archive_strcat(&shar->work, "\n");
276                                 } else {
277                                         archive_string_sprintf(&shar->work,
278                                             "sed 's/^X//' > %s << 'SHAR_END'\n",
279                                             shar->quoted_name.s);
280                                 }
281                                 shar->has_data = 1;
282                                 shar->end_of_line = 1;
283                                 shar->outpos = 0;
284                         }
285                         break;
286                 case AE_IFDIR:
287                         archive_string_sprintf(&shar->work,
288                             "mkdir -p %s > /dev/null 2>&1\n",
289                             shar->quoted_name.s);
290                         /* Record that we just created this directory. */
291                         if (shar->last_dir != NULL)
292                                 free(shar->last_dir);
293
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';
299                         /*
300                          * TODO: Put dir name/mode on a list to be fixed
301                          * up at end of archive.
302                          */
303                         break;
304                 case AE_IFIFO:
305                         archive_string_sprintf(&shar->work,
306                             "mkfifo %s\n", shar->quoted_name.s);
307                         break;
308                 case AE_IFCHR:
309                         archive_string_sprintf(&shar->work,
310                             "mknod %s c %d %d\n", shar->quoted_name.s,
311                             archive_entry_rdevmajor(entry),
312                             archive_entry_rdevminor(entry));
313                         break;
314                 case AE_IFBLK:
315                         archive_string_sprintf(&shar->work,
316                             "mknod %s b %d %d\n", shar->quoted_name.s,
317                             archive_entry_rdevmajor(entry),
318                             archive_entry_rdevminor(entry));
319                         break;
320                 default:
321                         return (ARCHIVE_WARN);
322                 }
323         }
324
325         return (ARCHIVE_OK);
326 }
327
328 static ssize_t
329 archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
330 {
331         static const size_t ensured = 65533;
332         struct shar *shar;
333         const char *src;
334         char *buf, *buf_end;
335         int ret;
336         size_t written = n;
337
338         shar = (struct shar *)a->format_data;
339         if (!shar->has_data || n == 0)
340                 return (0);
341
342         src = (const char *)buff;
343
344         /*
345          * ensure is the number of bytes in buffer before expanding the
346          * current character.  Each operation writes the current character
347          * and optionally the start-of-new-line marker.  This can happen
348          * twice before entering the loop, so make sure three additional
349          * bytes can be written.
350          */
351         if (archive_string_ensure(&shar->work, ensured + 3) == NULL)
352                 __archive_errx(1, "Out of memory");
353
354         if (shar->work.length > ensured) {
355                 ret = (*a->compressor.write)(a, shar->work.s,
356                     shar->work.length);
357                 if (ret != ARCHIVE_OK)
358                         return (ARCHIVE_FATAL);
359                 archive_string_empty(&shar->work);
360         }
361         buf = shar->work.s + shar->work.length;
362         buf_end = shar->work.s + ensured;
363
364         if (shar->end_of_line) {
365                 *buf++ = 'X';
366                 shar->end_of_line = 0;
367         }
368
369         while (n-- != 0) {
370                 if ((*buf++ = *src++) == '\n') {
371                         if (n == 0)
372                                 shar->end_of_line = 1;
373                         else
374                                 *buf++ = 'X';
375                 }
376
377                 if (buf >= buf_end) {
378                         shar->work.length = buf - shar->work.s;
379                         ret = (*a->compressor.write)(a, shar->work.s,
380                             shar->work.length);
381                         if (ret != ARCHIVE_OK)
382                                 return (ARCHIVE_FATAL);
383                         archive_string_empty(&shar->work);
384                         buf = shar->work.s;
385                 }
386         }
387
388         shar->work.length = buf - shar->work.s;
389
390         return (written);
391 }
392
393 #define UUENC(c)        (((c)!=0) ? ((c) & 077) + ' ': '`')
394
395 static void
396 uuencode_group(const char _in[3], char out[4])
397 {
398         const unsigned char *in = (const unsigned char *)_in;
399         int t;
400
401         t = (in[0] << 16) | (in[1] << 8) | in[2];
402         out[0] = UUENC( 0x3f & (t >> 18) );
403         out[1] = UUENC( 0x3f & (t >> 12) );
404         out[2] = UUENC( 0x3f & (t >> 6) );
405         out[3] = UUENC( 0x3f & t );
406 }
407
408 static void
409 uuencode_line(struct shar *shar, const char *inbuf, size_t len)
410 {
411         char tmp_buf[3], *buf;
412         size_t alloc_len;
413
414         /* len <= 45 -> expanded to 60 + len byte + new line */
415         alloc_len = shar->work.length + 62;
416         if (archive_string_ensure(&shar->work, alloc_len) == NULL)
417                 __archive_errx(1, "Out of memory");
418
419         buf = shar->work.s + shar->work.length;
420         *buf++ = UUENC(len);
421         while (len >= 3) {
422                 uuencode_group(inbuf, buf);
423                 len -= 3;
424                 inbuf += 3;
425                 buf += 4;
426         }
427         if (len != 0) {
428                 tmp_buf[0] = inbuf[0];
429                 if (len == 1)
430                         tmp_buf[1] = '\0';
431                 else
432                         tmp_buf[1] = inbuf[1];
433                 tmp_buf[2] = '\0';
434                 uuencode_group(inbuf, buf);
435                 buf += 4;
436         }
437         *buf++ = '\n';
438         if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62))
439                 __archive_errx(1, "Buffer overflow");
440         shar->work.length = buf - shar->work.s;
441 }
442
443 static ssize_t
444 archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
445     size_t length)
446 {
447         struct shar *shar;
448         const char *src;
449         size_t n;
450         int ret;
451
452         shar = (struct shar *)a->format_data;
453         if (!shar->has_data)
454                 return (ARCHIVE_OK);
455         src = (const char *)buff;
456
457         if (shar->outpos != 0) {
458                 n = 45 - shar->outpos;
459                 if (n > length)
460                         n = length;
461                 memcpy(shar->outbuff + shar->outpos, src, n);
462                 if (shar->outpos + n < 45) {
463                         shar->outpos += n;
464                         return length;
465                 }
466                 uuencode_line(shar, shar->outbuff, 45);
467                 src += n;
468                 n = length - n;
469         } else {
470                 n = length;
471         }
472
473         while (n >= 45) {
474                 uuencode_line(shar, src, 45);
475                 src += 45;
476                 n -= 45;
477
478                 if (shar->work.length < 65536)
479                         continue;
480                 ret = (*a->compressor.write)(a, shar->work.s,
481                     shar->work.length);
482                 if (ret != ARCHIVE_OK)
483                         return (ARCHIVE_FATAL);
484                 archive_string_empty(&shar->work);
485         }
486         if (n != 0) {
487                 memcpy(shar->outbuff, src, n);
488                 shar->outpos = n;
489         }
490         return (length);
491 }
492
493 static int
494 archive_write_shar_finish_entry(struct archive_write *a)
495 {
496         const char *g, *p, *u;
497         struct shar *shar;
498         int ret;
499
500         shar = (struct shar *)a->format_data;
501         if (shar->entry == NULL)
502                 return (0);
503
504         if (shar->dump) {
505                 /* Finish uuencoded data. */
506                 if (shar->has_data) {
507                         if (shar->outpos > 0)
508                                 uuencode_line(shar, shar->outbuff,
509                                     shar->outpos);
510                         archive_strcat(&shar->work, "`\nend\n");
511                         archive_strcat(&shar->work, "SHAR_END\n");
512                 }
513                 /* Restore file mode, owner, flags. */
514                 /*
515                  * TODO: Don't immediately restore mode for
516                  * directories; defer that to end of script.
517                  */
518                 archive_string_sprintf(&shar->work, "chmod %o ",
519                     archive_entry_mode(shar->entry) & 07777);
520                 shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1);
521                 archive_strcat(&shar->work, "\n");
522
523                 u = archive_entry_uname(shar->entry);
524                 g = archive_entry_gname(shar->entry);
525                 if (u != NULL || g != NULL) {
526                         archive_strcat(&shar->work, "chown ");
527                         if (u != NULL)
528                                 shar_quote(&shar->work, u, 1);
529                         if (g != NULL) {
530                                 archive_strcat(&shar->work, ":");
531                                 shar_quote(&shar->work, g, 1);
532                         }
533                         shar_quote(&shar->work,
534                             archive_entry_pathname(shar->entry), 1);
535                         archive_strcat(&shar->work, "\n");
536                 }
537
538                 if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
539                         archive_string_sprintf(&shar->work, "chflags %s ",
540                             p, archive_entry_pathname(shar->entry));
541                         shar_quote(&shar->work,
542                             archive_entry_pathname(shar->entry), 1);
543                         archive_strcat(&shar->work, "\n");
544                 }
545
546                 /* TODO: restore ACLs */
547
548         } else {
549                 if (shar->has_data) {
550                         /* Finish sed-encoded data:  ensure last line ends. */
551                         if (!shar->end_of_line)
552                                 archive_strappend_char(&shar->work, '\n');
553                         archive_strcat(&shar->work, "SHAR_END\n");
554                 }
555         }
556
557         archive_entry_free(shar->entry);
558         shar->entry = NULL;
559
560         if (shar->work.length < 65536)
561                 return (ARCHIVE_OK);
562
563         ret = (*a->compressor.write)(a, shar->work.s, shar->work.length);
564         if (ret != ARCHIVE_OK)
565                 return (ARCHIVE_FATAL);
566         archive_string_empty(&shar->work);
567
568         return (ARCHIVE_OK);
569 }
570
571 static int
572 archive_write_shar_finish(struct archive_write *a)
573 {
574         struct shar *shar;
575         int ret;
576
577         /*
578          * TODO: Accumulate list of directory names/modes and
579          * fix them all up at end-of-archive.
580          */
581
582         shar = (struct shar *)a->format_data;
583
584         /*
585          * Only write the end-of-archive markers if the archive was
586          * actually started.  This avoids problems if someone sets
587          * shar format, then sets another format (which would invoke
588          * shar_finish to free the format-specific data).
589          */
590         if (shar->wrote_header == 0)
591                 return (ARCHIVE_OK);
592
593         archive_strcat(&shar->work, "exit\n");
594
595         ret = (*a->compressor.write)(a, shar->work.s, shar->work.length);
596         if (ret != ARCHIVE_OK)
597                 return (ARCHIVE_FATAL);
598
599         /* Shar output is never padded. */
600         archive_write_set_bytes_in_last_block(&a->archive, 1);
601         /*
602          * TODO: shar should also suppress padding of
603          * uncompressed data within gzip/bzip2 streams.
604          */
605
606         return (ARCHIVE_OK);
607 }
608
609 static int
610 archive_write_shar_destroy(struct archive_write *a)
611 {
612         struct shar *shar;
613
614         shar = (struct shar *)a->format_data;
615         if (shar == NULL)
616                 return (ARCHIVE_OK);
617
618         archive_entry_free(shar->entry);
619         free(shar->last_dir);
620         archive_string_free(&(shar->work));
621         archive_string_free(&(shar->quoted_name));
622         free(shar);
623         a->format_data = NULL;
624         return (ARCHIVE_OK);
625 }