1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3228753Smm * Copyright (c) 2008 Joerg Sonnenberger 4228753Smm * All rights reserved. 5228753Smm * 6228753Smm * Redistribution and use in source and binary forms, with or without 7228753Smm * modification, are permitted provided that the following conditions 8228753Smm * are met: 9228753Smm * 1. Redistributions of source code must retain the above copyright 10228753Smm * notice, this list of conditions and the following disclaimer. 11228753Smm * 2. Redistributions in binary form must reproduce the above copyright 12228753Smm * notice, this list of conditions and the following disclaimer in the 13228753Smm * documentation and/or other materials provided with the distribution. 14228753Smm * 15228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25228753Smm */ 26228753Smm 27228753Smm#include "archive_platform.h" 28228763Smm__FBSDID("$FreeBSD$"); 29228753Smm 30228753Smm#ifdef HAVE_ERRNO_H 31228753Smm#include <errno.h> 32228753Smm#endif 33228753Smm#include <stdio.h> 34228753Smm#ifdef HAVE_STDLIB_H 35228753Smm#include <stdlib.h> 36228753Smm#endif 37228753Smm#ifdef HAVE_STRING_H 38228753Smm#include <string.h> 39228753Smm#endif 40228753Smm 41228753Smm#include "archive.h" 42228753Smm#include "archive_entry.h" 43228753Smm#include "archive_private.h" 44228753Smm#include "archive_write_private.h" 45228753Smm 46228753Smmstruct shar { 47228753Smm int dump; 48228753Smm int end_of_line; 49228753Smm struct archive_entry *entry; 50228753Smm int has_data; 51228753Smm char *last_dir; 52228753Smm 53228753Smm /* Line buffer for uuencoded dump format */ 54228753Smm char outbuff[45]; 55228753Smm size_t outpos; 56228753Smm 57228753Smm int wrote_header; 58228753Smm struct archive_string work; 59228753Smm struct archive_string quoted_name; 60228753Smm}; 61228753Smm 62232153Smmstatic int archive_write_shar_close(struct archive_write *); 63232153Smmstatic int archive_write_shar_free(struct archive_write *); 64228753Smmstatic int archive_write_shar_header(struct archive_write *, 65228753Smm struct archive_entry *); 66228753Smmstatic ssize_t archive_write_shar_data_sed(struct archive_write *, 67228753Smm const void * buff, size_t); 68228753Smmstatic ssize_t archive_write_shar_data_uuencode(struct archive_write *, 69228753Smm const void * buff, size_t); 70228753Smmstatic int archive_write_shar_finish_entry(struct archive_write *); 71228753Smm 72228753Smm/* 73228753Smm * Copy the given string to the buffer, quoting all shell meta characters 74228753Smm * found. 75228753Smm */ 76228753Smmstatic void 77228753Smmshar_quote(struct archive_string *buf, const char *str, int in_shell) 78228753Smm{ 79228753Smm static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~"; 80228753Smm size_t len; 81228753Smm 82228753Smm while (*str != '\0') { 83228753Smm if ((len = strcspn(str, meta)) != 0) { 84228753Smm archive_strncat(buf, str, len); 85228753Smm str += len; 86228753Smm } else if (*str == '\n') { 87228753Smm if (in_shell) 88228753Smm archive_strcat(buf, "\"\n\""); 89228753Smm else 90228753Smm archive_strcat(buf, "\\n"); 91228753Smm ++str; 92228753Smm } else { 93228753Smm archive_strappend_char(buf, '\\'); 94228753Smm archive_strappend_char(buf, *str); 95228753Smm ++str; 96228753Smm } 97228753Smm } 98228753Smm} 99228753Smm 100228753Smm/* 101228753Smm * Set output format to 'shar' format. 102228753Smm */ 103228753Smmint 104228753Smmarchive_write_set_format_shar(struct archive *_a) 105228753Smm{ 106228753Smm struct archive_write *a = (struct archive_write *)_a; 107228753Smm struct shar *shar; 108228753Smm 109232153Smm archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, 110232153Smm ARCHIVE_STATE_NEW, "archive_write_set_format_shar"); 111232153Smm 112228753Smm /* If someone else was already registered, unregister them. */ 113232153Smm if (a->format_free != NULL) 114232153Smm (a->format_free)(a); 115228753Smm 116228753Smm shar = (struct shar *)malloc(sizeof(*shar)); 117228753Smm if (shar == NULL) { 118228753Smm archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data"); 119228753Smm return (ARCHIVE_FATAL); 120228753Smm } 121228753Smm memset(shar, 0, sizeof(*shar)); 122228753Smm archive_string_init(&shar->work); 123228753Smm archive_string_init(&shar->quoted_name); 124228753Smm a->format_data = shar; 125228753Smm a->format_name = "shar"; 126228753Smm a->format_write_header = archive_write_shar_header; 127232153Smm a->format_close = archive_write_shar_close; 128232153Smm a->format_free = archive_write_shar_free; 129228753Smm a->format_write_data = archive_write_shar_data_sed; 130228753Smm a->format_finish_entry = archive_write_shar_finish_entry; 131228753Smm a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE; 132228753Smm a->archive.archive_format_name = "shar"; 133228753Smm return (ARCHIVE_OK); 134228753Smm} 135228753Smm 136228753Smm/* 137228753Smm * An alternate 'shar' that uses uudecode instead of 'sed' to encode 138228753Smm * file contents and can therefore be used to archive binary files. 139228753Smm * In addition, this variant also attempts to restore ownership, file modes, 140228753Smm * and other extended file information. 141228753Smm */ 142228753Smmint 143228753Smmarchive_write_set_format_shar_dump(struct archive *_a) 144228753Smm{ 145228753Smm struct archive_write *a = (struct archive_write *)_a; 146228753Smm struct shar *shar; 147228753Smm 148228753Smm archive_write_set_format_shar(&a->archive); 149228753Smm shar = (struct shar *)a->format_data; 150228753Smm shar->dump = 1; 151228753Smm a->format_write_data = archive_write_shar_data_uuencode; 152228753Smm a->archive.archive_format = ARCHIVE_FORMAT_SHAR_DUMP; 153228753Smm a->archive.archive_format_name = "shar dump"; 154228753Smm return (ARCHIVE_OK); 155228753Smm} 156228753Smm 157228753Smmstatic int 158228753Smmarchive_write_shar_header(struct archive_write *a, struct archive_entry *entry) 159228753Smm{ 160228753Smm const char *linkname; 161228753Smm const char *name; 162228753Smm char *p, *pp; 163228753Smm struct shar *shar; 164228753Smm 165228753Smm shar = (struct shar *)a->format_data; 166228753Smm if (!shar->wrote_header) { 167228753Smm archive_strcat(&shar->work, "#!/bin/sh\n"); 168228753Smm archive_strcat(&shar->work, "# This is a shell archive\n"); 169228753Smm shar->wrote_header = 1; 170228753Smm } 171228753Smm 172228753Smm /* Save the entry for the closing. */ 173228753Smm if (shar->entry) 174228753Smm archive_entry_free(shar->entry); 175228753Smm shar->entry = archive_entry_clone(entry); 176228753Smm name = archive_entry_pathname(entry); 177228753Smm 178228753Smm /* Handle some preparatory issues. */ 179228753Smm switch(archive_entry_filetype(entry)) { 180228753Smm case AE_IFREG: 181228753Smm /* Only regular files have non-zero size. */ 182228753Smm break; 183228753Smm case AE_IFDIR: 184228753Smm archive_entry_set_size(entry, 0); 185228753Smm /* Don't bother trying to recreate '.' */ 186228753Smm if (strcmp(name, ".") == 0 || strcmp(name, "./") == 0) 187228753Smm return (ARCHIVE_OK); 188228753Smm break; 189228753Smm case AE_IFIFO: 190228753Smm case AE_IFCHR: 191228753Smm case AE_IFBLK: 192228753Smm /* All other file types have zero size in the archive. */ 193228753Smm archive_entry_set_size(entry, 0); 194228753Smm break; 195228753Smm default: 196228753Smm archive_entry_set_size(entry, 0); 197228753Smm if (archive_entry_hardlink(entry) == NULL && 198228753Smm archive_entry_symlink(entry) == NULL) { 199228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 200228753Smm "shar format cannot archive this"); 201228753Smm return (ARCHIVE_WARN); 202228753Smm } 203228753Smm } 204228753Smm 205228753Smm archive_string_empty(&shar->quoted_name); 206228753Smm shar_quote(&shar->quoted_name, name, 1); 207228753Smm 208228753Smm /* Stock preparation for all file types. */ 209228753Smm archive_string_sprintf(&shar->work, "echo x %s\n", shar->quoted_name.s); 210228753Smm 211228753Smm if (archive_entry_filetype(entry) != AE_IFDIR) { 212228753Smm /* Try to create the dir. */ 213228753Smm p = strdup(name); 214228753Smm pp = strrchr(p, '/'); 215228753Smm /* If there is a / character, try to create the dir. */ 216228753Smm if (pp != NULL) { 217228753Smm *pp = '\0'; 218228753Smm 219228753Smm /* Try to avoid a lot of redundant mkdir commands. */ 220228753Smm if (strcmp(p, ".") == 0) { 221228753Smm /* Don't try to "mkdir ." */ 222228753Smm free(p); 223228753Smm } else if (shar->last_dir == NULL) { 224228753Smm archive_strcat(&shar->work, "mkdir -p "); 225228753Smm shar_quote(&shar->work, p, 1); 226228753Smm archive_strcat(&shar->work, 227228753Smm " > /dev/null 2>&1\n"); 228228753Smm shar->last_dir = p; 229228753Smm } else if (strcmp(p, shar->last_dir) == 0) { 230228753Smm /* We've already created this exact dir. */ 231228753Smm free(p); 232228753Smm } else if (strlen(p) < strlen(shar->last_dir) && 233228753Smm strncmp(p, shar->last_dir, strlen(p)) == 0) { 234228753Smm /* We've already created a subdir. */ 235228753Smm free(p); 236228753Smm } else { 237228753Smm archive_strcat(&shar->work, "mkdir -p "); 238228753Smm shar_quote(&shar->work, p, 1); 239228753Smm archive_strcat(&shar->work, 240228753Smm " > /dev/null 2>&1\n"); 241228753Smm shar->last_dir = p; 242228753Smm } 243228753Smm } else { 244228753Smm free(p); 245228753Smm } 246228753Smm } 247228753Smm 248228753Smm /* Handle file-type specific issues. */ 249228753Smm shar->has_data = 0; 250228753Smm if ((linkname = archive_entry_hardlink(entry)) != NULL) { 251228753Smm archive_strcat(&shar->work, "ln -f "); 252228753Smm shar_quote(&shar->work, linkname, 1); 253228753Smm archive_string_sprintf(&shar->work, " %s\n", 254228753Smm shar->quoted_name.s); 255228753Smm } else if ((linkname = archive_entry_symlink(entry)) != NULL) { 256228753Smm archive_strcat(&shar->work, "ln -fs "); 257228753Smm shar_quote(&shar->work, linkname, 1); 258228753Smm archive_string_sprintf(&shar->work, " %s\n", 259228753Smm shar->quoted_name.s); 260228753Smm } else { 261228753Smm switch(archive_entry_filetype(entry)) { 262228753Smm case AE_IFREG: 263228753Smm if (archive_entry_size(entry) == 0) { 264228753Smm /* More portable than "touch." */ 265228753Smm archive_string_sprintf(&shar->work, 266228753Smm "test -e \"%s\" || :> \"%s\"\n", 267228753Smm shar->quoted_name.s, shar->quoted_name.s); 268228753Smm } else { 269228753Smm if (shar->dump) { 270232153Smm unsigned int mode = archive_entry_mode(entry) & 0777; 271228753Smm archive_string_sprintf(&shar->work, 272228753Smm "uudecode -p > %s << 'SHAR_END'\n", 273228753Smm shar->quoted_name.s); 274228753Smm archive_string_sprintf(&shar->work, 275232153Smm "begin %o ", mode); 276228753Smm shar_quote(&shar->work, name, 0); 277228753Smm archive_strcat(&shar->work, "\n"); 278228753Smm } else { 279228753Smm archive_string_sprintf(&shar->work, 280228753Smm "sed 's/^X//' > %s << 'SHAR_END'\n", 281228753Smm shar->quoted_name.s); 282228753Smm } 283228753Smm shar->has_data = 1; 284228753Smm shar->end_of_line = 1; 285228753Smm shar->outpos = 0; 286228753Smm } 287228753Smm break; 288228753Smm case AE_IFDIR: 289228753Smm archive_string_sprintf(&shar->work, 290228753Smm "mkdir -p %s > /dev/null 2>&1\n", 291228753Smm shar->quoted_name.s); 292228753Smm /* Record that we just created this directory. */ 293228753Smm if (shar->last_dir != NULL) 294228753Smm free(shar->last_dir); 295228753Smm 296228753Smm shar->last_dir = strdup(name); 297228753Smm /* Trim a trailing '/'. */ 298228753Smm pp = strrchr(shar->last_dir, '/'); 299228753Smm if (pp != NULL && pp[1] == '\0') 300228753Smm *pp = '\0'; 301228753Smm /* 302228753Smm * TODO: Put dir name/mode on a list to be fixed 303228753Smm * up at end of archive. 304228753Smm */ 305228753Smm break; 306228753Smm case AE_IFIFO: 307228753Smm archive_string_sprintf(&shar->work, 308228753Smm "mkfifo %s\n", shar->quoted_name.s); 309228753Smm break; 310228753Smm case AE_IFCHR: 311228753Smm archive_string_sprintf(&shar->work, 312232153Smm "mknod %s c %ju %ju\n", shar->quoted_name.s, 313232153Smm (uintmax_t)archive_entry_rdevmajor(entry), 314232153Smm (uintmax_t)archive_entry_rdevminor(entry)); 315228753Smm break; 316228753Smm case AE_IFBLK: 317228753Smm archive_string_sprintf(&shar->work, 318232153Smm "mknod %s b %ju %ju\n", shar->quoted_name.s, 319232153Smm (uintmax_t)archive_entry_rdevmajor(entry), 320232153Smm (uintmax_t)archive_entry_rdevminor(entry)); 321228753Smm break; 322228753Smm default: 323228753Smm return (ARCHIVE_WARN); 324228753Smm } 325228753Smm } 326228753Smm 327228753Smm return (ARCHIVE_OK); 328228753Smm} 329228753Smm 330228753Smmstatic ssize_t 331228753Smmarchive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n) 332228753Smm{ 333228753Smm static const size_t ensured = 65533; 334228753Smm struct shar *shar; 335228753Smm const char *src; 336228753Smm char *buf, *buf_end; 337228753Smm int ret; 338228753Smm size_t written = n; 339228753Smm 340228753Smm shar = (struct shar *)a->format_data; 341228753Smm if (!shar->has_data || n == 0) 342228753Smm return (0); 343228753Smm 344228753Smm src = (const char *)buff; 345228753Smm 346228753Smm /* 347228753Smm * ensure is the number of bytes in buffer before expanding the 348228753Smm * current character. Each operation writes the current character 349228753Smm * and optionally the start-of-new-line marker. This can happen 350228753Smm * twice before entering the loop, so make sure three additional 351228753Smm * bytes can be written. 352228753Smm */ 353232153Smm if (archive_string_ensure(&shar->work, ensured + 3) == NULL) { 354232153Smm archive_set_error(&a->archive, ENOMEM, "Out of memory"); 355232153Smm return (ARCHIVE_FATAL); 356232153Smm } 357228753Smm 358228753Smm if (shar->work.length > ensured) { 359232153Smm ret = __archive_write_output(a, shar->work.s, 360228753Smm shar->work.length); 361228753Smm if (ret != ARCHIVE_OK) 362228753Smm return (ARCHIVE_FATAL); 363228753Smm archive_string_empty(&shar->work); 364228753Smm } 365228753Smm buf = shar->work.s + shar->work.length; 366228753Smm buf_end = shar->work.s + ensured; 367228753Smm 368228753Smm if (shar->end_of_line) { 369228753Smm *buf++ = 'X'; 370228753Smm shar->end_of_line = 0; 371228753Smm } 372228753Smm 373228753Smm while (n-- != 0) { 374228753Smm if ((*buf++ = *src++) == '\n') { 375228753Smm if (n == 0) 376228753Smm shar->end_of_line = 1; 377228753Smm else 378228753Smm *buf++ = 'X'; 379228753Smm } 380228753Smm 381228753Smm if (buf >= buf_end) { 382228753Smm shar->work.length = buf - shar->work.s; 383232153Smm ret = __archive_write_output(a, shar->work.s, 384228753Smm shar->work.length); 385228753Smm if (ret != ARCHIVE_OK) 386228753Smm return (ARCHIVE_FATAL); 387228753Smm archive_string_empty(&shar->work); 388228753Smm buf = shar->work.s; 389228753Smm } 390228753Smm } 391228753Smm 392228753Smm shar->work.length = buf - shar->work.s; 393228753Smm 394228753Smm return (written); 395228753Smm} 396228753Smm 397228753Smm#define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`') 398228753Smm 399228753Smmstatic void 400228753Smmuuencode_group(const char _in[3], char out[4]) 401228753Smm{ 402228753Smm const unsigned char *in = (const unsigned char *)_in; 403228753Smm int t; 404228753Smm 405228753Smm t = (in[0] << 16) | (in[1] << 8) | in[2]; 406228753Smm out[0] = UUENC( 0x3f & (t >> 18) ); 407228753Smm out[1] = UUENC( 0x3f & (t >> 12) ); 408228753Smm out[2] = UUENC( 0x3f & (t >> 6) ); 409228753Smm out[3] = UUENC( 0x3f & t ); 410228753Smm} 411228753Smm 412232153Smmstatic int 413232153Smm_uuencode_line(struct archive_write *a, struct shar *shar, const char *inbuf, size_t len) 414228753Smm{ 415232153Smm char *buf; 416228753Smm size_t alloc_len; 417228753Smm 418228753Smm /* len <= 45 -> expanded to 60 + len byte + new line */ 419228753Smm alloc_len = shar->work.length + 62; 420232153Smm if (archive_string_ensure(&shar->work, alloc_len) == NULL) { 421232153Smm archive_set_error(&a->archive, ENOMEM, "Out of memory"); 422232153Smm return (ARCHIVE_FATAL); 423232153Smm } 424228753Smm 425228753Smm buf = shar->work.s + shar->work.length; 426228753Smm *buf++ = UUENC(len); 427228753Smm while (len >= 3) { 428228753Smm uuencode_group(inbuf, buf); 429228753Smm len -= 3; 430228753Smm inbuf += 3; 431228753Smm buf += 4; 432228753Smm } 433228753Smm if (len != 0) { 434232153Smm char tmp_buf[3]; 435228753Smm tmp_buf[0] = inbuf[0]; 436228753Smm if (len == 1) 437228753Smm tmp_buf[1] = '\0'; 438228753Smm else 439228753Smm tmp_buf[1] = inbuf[1]; 440228753Smm tmp_buf[2] = '\0'; 441232153Smm uuencode_group(tmp_buf, buf); 442228753Smm buf += 4; 443228753Smm } 444228753Smm *buf++ = '\n'; 445232153Smm if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62)) { 446232153Smm archive_set_error(&a->archive, 447232153Smm ARCHIVE_ERRNO_MISC, "Buffer overflow"); 448232153Smm return (ARCHIVE_FATAL); 449232153Smm } 450228753Smm shar->work.length = buf - shar->work.s; 451232153Smm return (ARCHIVE_OK); 452228753Smm} 453228753Smm 454232153Smm#define uuencode_line(__a, __shar, __inbuf, __len) \ 455232153Smm do { \ 456232153Smm int r = _uuencode_line(__a, __shar, __inbuf, __len); \ 457232153Smm if (r != ARCHIVE_OK) \ 458232153Smm return (ARCHIVE_FATAL); \ 459232153Smm } while (0) 460232153Smm 461228753Smmstatic ssize_t 462228753Smmarchive_write_shar_data_uuencode(struct archive_write *a, const void *buff, 463228753Smm size_t length) 464228753Smm{ 465228753Smm struct shar *shar; 466228753Smm const char *src; 467228753Smm size_t n; 468228753Smm int ret; 469228753Smm 470228753Smm shar = (struct shar *)a->format_data; 471228753Smm if (!shar->has_data) 472228753Smm return (ARCHIVE_OK); 473228753Smm src = (const char *)buff; 474228753Smm 475228753Smm if (shar->outpos != 0) { 476228753Smm n = 45 - shar->outpos; 477228753Smm if (n > length) 478228753Smm n = length; 479228753Smm memcpy(shar->outbuff + shar->outpos, src, n); 480228753Smm if (shar->outpos + n < 45) { 481228753Smm shar->outpos += n; 482228753Smm return length; 483228753Smm } 484232153Smm uuencode_line(a, shar, shar->outbuff, 45); 485228753Smm src += n; 486228753Smm n = length - n; 487228753Smm } else { 488228753Smm n = length; 489228753Smm } 490228753Smm 491228753Smm while (n >= 45) { 492232153Smm uuencode_line(a, shar, src, 45); 493228753Smm src += 45; 494228753Smm n -= 45; 495228753Smm 496228753Smm if (shar->work.length < 65536) 497228753Smm continue; 498232153Smm ret = __archive_write_output(a, shar->work.s, 499228753Smm shar->work.length); 500228753Smm if (ret != ARCHIVE_OK) 501228753Smm return (ARCHIVE_FATAL); 502228753Smm archive_string_empty(&shar->work); 503228753Smm } 504228753Smm if (n != 0) { 505228753Smm memcpy(shar->outbuff, src, n); 506228753Smm shar->outpos = n; 507228753Smm } 508228753Smm return (length); 509228753Smm} 510228753Smm 511228753Smmstatic int 512228753Smmarchive_write_shar_finish_entry(struct archive_write *a) 513228753Smm{ 514228753Smm const char *g, *p, *u; 515228753Smm struct shar *shar; 516228753Smm int ret; 517228753Smm 518228753Smm shar = (struct shar *)a->format_data; 519228753Smm if (shar->entry == NULL) 520228753Smm return (0); 521228753Smm 522228753Smm if (shar->dump) { 523228753Smm /* Finish uuencoded data. */ 524228753Smm if (shar->has_data) { 525228753Smm if (shar->outpos > 0) 526232153Smm uuencode_line(a, shar, shar->outbuff, 527228753Smm shar->outpos); 528228753Smm archive_strcat(&shar->work, "`\nend\n"); 529228753Smm archive_strcat(&shar->work, "SHAR_END\n"); 530228753Smm } 531228753Smm /* Restore file mode, owner, flags. */ 532228753Smm /* 533228753Smm * TODO: Don't immediately restore mode for 534228753Smm * directories; defer that to end of script. 535228753Smm */ 536228753Smm archive_string_sprintf(&shar->work, "chmod %o ", 537232153Smm (unsigned int)(archive_entry_mode(shar->entry) & 07777)); 538228753Smm shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1); 539228753Smm archive_strcat(&shar->work, "\n"); 540228753Smm 541228753Smm u = archive_entry_uname(shar->entry); 542228753Smm g = archive_entry_gname(shar->entry); 543228753Smm if (u != NULL || g != NULL) { 544228753Smm archive_strcat(&shar->work, "chown "); 545228753Smm if (u != NULL) 546228753Smm shar_quote(&shar->work, u, 1); 547228753Smm if (g != NULL) { 548228753Smm archive_strcat(&shar->work, ":"); 549228753Smm shar_quote(&shar->work, g, 1); 550228753Smm } 551228753Smm shar_quote(&shar->work, 552228753Smm archive_entry_pathname(shar->entry), 1); 553228753Smm archive_strcat(&shar->work, "\n"); 554228753Smm } 555228753Smm 556228753Smm if ((p = archive_entry_fflags_text(shar->entry)) != NULL) { 557228753Smm archive_string_sprintf(&shar->work, "chflags %s ", p); 558228753Smm shar_quote(&shar->work, 559228753Smm archive_entry_pathname(shar->entry), 1); 560228753Smm archive_strcat(&shar->work, "\n"); 561228753Smm } 562228753Smm 563228753Smm /* TODO: restore ACLs */ 564228753Smm 565228753Smm } else { 566228753Smm if (shar->has_data) { 567228753Smm /* Finish sed-encoded data: ensure last line ends. */ 568228753Smm if (!shar->end_of_line) 569228753Smm archive_strappend_char(&shar->work, '\n'); 570228753Smm archive_strcat(&shar->work, "SHAR_END\n"); 571228753Smm } 572228753Smm } 573228753Smm 574228753Smm archive_entry_free(shar->entry); 575228753Smm shar->entry = NULL; 576228753Smm 577228753Smm if (shar->work.length < 65536) 578228753Smm return (ARCHIVE_OK); 579228753Smm 580232153Smm ret = __archive_write_output(a, shar->work.s, shar->work.length); 581228753Smm if (ret != ARCHIVE_OK) 582228753Smm return (ARCHIVE_FATAL); 583228753Smm archive_string_empty(&shar->work); 584228753Smm 585228753Smm return (ARCHIVE_OK); 586228753Smm} 587228753Smm 588228753Smmstatic int 589232153Smmarchive_write_shar_close(struct archive_write *a) 590228753Smm{ 591228753Smm struct shar *shar; 592228753Smm int ret; 593228753Smm 594228753Smm /* 595228753Smm * TODO: Accumulate list of directory names/modes and 596228753Smm * fix them all up at end-of-archive. 597228753Smm */ 598228753Smm 599228753Smm shar = (struct shar *)a->format_data; 600228753Smm 601228753Smm /* 602228753Smm * Only write the end-of-archive markers if the archive was 603228753Smm * actually started. This avoids problems if someone sets 604228753Smm * shar format, then sets another format (which would invoke 605228753Smm * shar_finish to free the format-specific data). 606228753Smm */ 607228753Smm if (shar->wrote_header == 0) 608228753Smm return (ARCHIVE_OK); 609228753Smm 610228753Smm archive_strcat(&shar->work, "exit\n"); 611228753Smm 612232153Smm ret = __archive_write_output(a, shar->work.s, shar->work.length); 613228753Smm if (ret != ARCHIVE_OK) 614228753Smm return (ARCHIVE_FATAL); 615228753Smm 616228753Smm /* Shar output is never padded. */ 617228753Smm archive_write_set_bytes_in_last_block(&a->archive, 1); 618228753Smm /* 619228753Smm * TODO: shar should also suppress padding of 620228753Smm * uncompressed data within gzip/bzip2 streams. 621228753Smm */ 622228753Smm 623228753Smm return (ARCHIVE_OK); 624228753Smm} 625228753Smm 626228753Smmstatic int 627232153Smmarchive_write_shar_free(struct archive_write *a) 628228753Smm{ 629228753Smm struct shar *shar; 630228753Smm 631228753Smm shar = (struct shar *)a->format_data; 632228753Smm if (shar == NULL) 633228753Smm return (ARCHIVE_OK); 634228753Smm 635228753Smm archive_entry_free(shar->entry); 636228753Smm free(shar->last_dir); 637228753Smm archive_string_free(&(shar->work)); 638228753Smm archive_string_free(&(shar->quoted_name)); 639228753Smm free(shar); 640228753Smm a->format_data = NULL; 641228753Smm return (ARCHIVE_OK); 642228753Smm} 643