archive_write_set_format_shar.c 17 KB

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