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