archive_write_set_format_shar.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. #include "archive_write_set_format_private.h"
  43. struct shar {
  44. int dump;
  45. int end_of_line;
  46. struct archive_entry *entry;
  47. int has_data;
  48. char *last_dir;
  49. /* Line buffer for uuencoded dump format */
  50. char outbuff[45];
  51. size_t outpos;
  52. int wrote_header;
  53. struct archive_string work;
  54. struct archive_string quoted_name;
  55. };
  56. static int archive_write_shar_close(struct archive_write *);
  57. static int archive_write_shar_free(struct archive_write *);
  58. static int archive_write_shar_header(struct archive_write *,
  59. struct archive_entry *);
  60. static ssize_t archive_write_shar_data_sed(struct archive_write *,
  61. const void * buff, size_t);
  62. static ssize_t archive_write_shar_data_uuencode(struct archive_write *,
  63. const void * buff, size_t);
  64. static int archive_write_shar_finish_entry(struct archive_write *);
  65. /*
  66. * Copy the given string to the buffer, quoting all shell meta characters
  67. * found.
  68. */
  69. static void
  70. shar_quote(struct archive_string *buf, const char *str, int in_shell)
  71. {
  72. static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
  73. size_t len;
  74. while (*str != '\0') {
  75. if ((len = strcspn(str, meta)) != 0) {
  76. archive_strncat(buf, str, len);
  77. str += len;
  78. } else if (*str == '\n') {
  79. if (in_shell)
  80. archive_strcat(buf, "\"\n\"");
  81. else
  82. archive_strcat(buf, "\\n");
  83. ++str;
  84. } else {
  85. archive_strappend_char(buf, '\\');
  86. archive_strappend_char(buf, *str);
  87. ++str;
  88. }
  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 archive_write *a = (struct archive_write *)_a;
  98. struct shar *shar;
  99. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  100. ARCHIVE_STATE_NEW, "archive_write_set_format_shar");
  101. /* If someone else was already registered, unregister them. */
  102. if (a->format_free != NULL)
  103. (a->format_free)(a);
  104. shar = (struct shar *)calloc(1, sizeof(*shar));
  105. if (shar == NULL) {
  106. archive_set_error(&a->archive, ENOMEM, "Can't allocate shar data");
  107. return (ARCHIVE_FATAL);
  108. }
  109. archive_string_init(&shar->work);
  110. archive_string_init(&shar->quoted_name);
  111. a->format_data = shar;
  112. a->format_name = "shar";
  113. a->format_write_header = archive_write_shar_header;
  114. a->format_close = archive_write_shar_close;
  115. a->format_free = archive_write_shar_free;
  116. a->format_write_data = archive_write_shar_data_sed;
  117. a->format_finish_entry = archive_write_shar_finish_entry;
  118. a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE;
  119. a->archive.archive_format_name = "shar";
  120. return (ARCHIVE_OK);
  121. }
  122. /*
  123. * An alternate 'shar' that uses uudecode instead of 'sed' to encode
  124. * file contents and can therefore be used to archive binary files.
  125. * In addition, this variant also attempts to restore ownership, file modes,
  126. * and other extended file information.
  127. */
  128. int
  129. archive_write_set_format_shar_dump(struct archive *_a)
  130. {
  131. struct archive_write *a = (struct archive_write *)_a;
  132. struct shar *shar;
  133. archive_write_set_format_shar(&a->archive);
  134. shar = (struct shar *)a->format_data;
  135. shar->dump = 1;
  136. a->format_write_data = archive_write_shar_data_uuencode;
  137. a->archive.archive_format = ARCHIVE_FORMAT_SHAR_DUMP;
  138. a->archive.archive_format_name = "shar dump";
  139. return (ARCHIVE_OK);
  140. }
  141. static int
  142. archive_write_shar_header(struct archive_write *a, struct archive_entry *entry)
  143. {
  144. const char *linkname;
  145. const char *name;
  146. char *p, *pp;
  147. struct shar *shar;
  148. shar = (struct shar *)a->format_data;
  149. if (!shar->wrote_header) {
  150. archive_strcat(&shar->work, "#!/bin/sh\n");
  151. archive_strcat(&shar->work, "# This is a shell archive\n");
  152. shar->wrote_header = 1;
  153. }
  154. /* Save the entry for the closing. */
  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_write_entry_filetype_unsupported(
  180. &a->archive, entry, "shar");
  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. free(shar->last_dir);
  269. shar->last_dir = strdup(name);
  270. /* Trim a trailing '/'. */
  271. pp = strrchr(shar->last_dir, '/');
  272. if (pp != NULL && pp[1] == '\0')
  273. *pp = '\0';
  274. /*
  275. * TODO: Put dir name/mode on a list to be fixed
  276. * up at end of archive.
  277. */
  278. break;
  279. case AE_IFIFO:
  280. archive_string_sprintf(&shar->work,
  281. "mkfifo %s\n", shar->quoted_name.s);
  282. break;
  283. case AE_IFCHR:
  284. archive_string_sprintf(&shar->work,
  285. "mknod %s c %ju %ju\n", shar->quoted_name.s,
  286. (uintmax_t)archive_entry_rdevmajor(entry),
  287. (uintmax_t)archive_entry_rdevminor(entry));
  288. break;
  289. case AE_IFBLK:
  290. archive_string_sprintf(&shar->work,
  291. "mknod %s b %ju %ju\n", shar->quoted_name.s,
  292. (uintmax_t)archive_entry_rdevmajor(entry),
  293. (uintmax_t)archive_entry_rdevminor(entry));
  294. break;
  295. default:
  296. return (ARCHIVE_WARN);
  297. }
  298. }
  299. return (ARCHIVE_OK);
  300. }
  301. static ssize_t
  302. archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n)
  303. {
  304. static const size_t ensured = 65533;
  305. struct shar *shar;
  306. const char *src;
  307. char *buf, *buf_end;
  308. int ret;
  309. size_t written = n;
  310. shar = (struct shar *)a->format_data;
  311. if (!shar->has_data || n == 0)
  312. return (0);
  313. src = (const char *)buff;
  314. /*
  315. * ensure is the number of bytes in buffer before expanding the
  316. * current character. Each operation writes the current character
  317. * and optionally the start-of-new-line marker. This can happen
  318. * twice before entering the loop, so make sure three additional
  319. * bytes can be written.
  320. */
  321. if (archive_string_ensure(&shar->work, ensured + 3) == NULL) {
  322. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  323. return (ARCHIVE_FATAL);
  324. }
  325. if (shar->work.length > ensured) {
  326. ret = __archive_write_output(a, shar->work.s,
  327. shar->work.length);
  328. if (ret != ARCHIVE_OK)
  329. return (ARCHIVE_FATAL);
  330. archive_string_empty(&shar->work);
  331. }
  332. buf = shar->work.s + shar->work.length;
  333. buf_end = shar->work.s + ensured;
  334. if (shar->end_of_line) {
  335. *buf++ = 'X';
  336. shar->end_of_line = 0;
  337. }
  338. while (n-- != 0) {
  339. if ((*buf++ = *src++) == '\n') {
  340. if (n == 0)
  341. shar->end_of_line = 1;
  342. else
  343. *buf++ = 'X';
  344. }
  345. if (buf >= buf_end) {
  346. shar->work.length = buf - shar->work.s;
  347. ret = __archive_write_output(a, shar->work.s,
  348. shar->work.length);
  349. if (ret != ARCHIVE_OK)
  350. return (ARCHIVE_FATAL);
  351. archive_string_empty(&shar->work);
  352. buf = shar->work.s;
  353. }
  354. }
  355. shar->work.length = buf - shar->work.s;
  356. return (written);
  357. }
  358. #define UUENC(c) (((c)!=0) ? ((c) & 077) + ' ': '`')
  359. static void
  360. uuencode_group(const char _in[3], char out[4])
  361. {
  362. const unsigned char *in = (const unsigned char *)_in;
  363. int t;
  364. t = (in[0] << 16) | (in[1] << 8) | in[2];
  365. out[0] = UUENC( 0x3f & (t >> 18) );
  366. out[1] = UUENC( 0x3f & (t >> 12) );
  367. out[2] = UUENC( 0x3f & (t >> 6) );
  368. out[3] = UUENC( 0x3f & t );
  369. }
  370. static int
  371. _uuencode_line(struct archive_write *a, struct shar *shar, const char *inbuf, size_t len)
  372. {
  373. char *buf;
  374. size_t alloc_len;
  375. /* len <= 45 -> expanded to 60 + len byte + new line */
  376. alloc_len = shar->work.length + 62;
  377. if (archive_string_ensure(&shar->work, alloc_len) == NULL) {
  378. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  379. return (ARCHIVE_FATAL);
  380. }
  381. buf = shar->work.s + shar->work.length;
  382. *buf++ = UUENC(len);
  383. while (len >= 3) {
  384. uuencode_group(inbuf, buf);
  385. len -= 3;
  386. inbuf += 3;
  387. buf += 4;
  388. }
  389. if (len != 0) {
  390. char tmp_buf[3];
  391. tmp_buf[0] = inbuf[0];
  392. if (len == 1)
  393. tmp_buf[1] = '\0';
  394. else
  395. tmp_buf[1] = inbuf[1];
  396. tmp_buf[2] = '\0';
  397. uuencode_group(tmp_buf, buf);
  398. buf += 4;
  399. }
  400. *buf++ = '\n';
  401. if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62)) {
  402. archive_set_error(&a->archive,
  403. ARCHIVE_ERRNO_MISC, "Buffer overflow");
  404. return (ARCHIVE_FATAL);
  405. }
  406. shar->work.length = buf - shar->work.s;
  407. return (ARCHIVE_OK);
  408. }
  409. #define uuencode_line(__a, __shar, __inbuf, __len) \
  410. do { \
  411. int r = _uuencode_line(__a, __shar, __inbuf, __len); \
  412. if (r != ARCHIVE_OK) \
  413. return (ARCHIVE_FATAL); \
  414. } while (0)
  415. static ssize_t
  416. archive_write_shar_data_uuencode(struct archive_write *a, const void *buff,
  417. size_t length)
  418. {
  419. struct shar *shar;
  420. const char *src;
  421. size_t n;
  422. int ret;
  423. shar = (struct shar *)a->format_data;
  424. if (!shar->has_data)
  425. return (ARCHIVE_OK);
  426. src = (const char *)buff;
  427. if (shar->outpos != 0) {
  428. n = 45 - shar->outpos;
  429. if (n > length)
  430. n = length;
  431. memcpy(shar->outbuff + shar->outpos, src, n);
  432. if (shar->outpos + n < 45) {
  433. shar->outpos += n;
  434. return length;
  435. }
  436. uuencode_line(a, shar, shar->outbuff, 45);
  437. src += n;
  438. n = length - n;
  439. } else {
  440. n = length;
  441. }
  442. while (n >= 45) {
  443. uuencode_line(a, shar, src, 45);
  444. src += 45;
  445. n -= 45;
  446. if (shar->work.length < 65536)
  447. continue;
  448. ret = __archive_write_output(a, shar->work.s,
  449. shar->work.length);
  450. if (ret != ARCHIVE_OK)
  451. return (ARCHIVE_FATAL);
  452. archive_string_empty(&shar->work);
  453. }
  454. if (n != 0) {
  455. memcpy(shar->outbuff, src, n);
  456. shar->outpos = n;
  457. }
  458. return (length);
  459. }
  460. static int
  461. archive_write_shar_finish_entry(struct archive_write *a)
  462. {
  463. const char *g, *p, *u;
  464. struct shar *shar;
  465. int ret;
  466. shar = (struct shar *)a->format_data;
  467. if (shar->entry == NULL)
  468. return (0);
  469. if (shar->dump) {
  470. /* Finish uuencoded data. */
  471. if (shar->has_data) {
  472. if (shar->outpos > 0)
  473. uuencode_line(a, shar, shar->outbuff,
  474. shar->outpos);
  475. archive_strcat(&shar->work, "`\nend\n");
  476. archive_strcat(&shar->work, "SHAR_END\n");
  477. }
  478. /* Restore file mode, owner, flags. */
  479. /*
  480. * TODO: Don't immediately restore mode for
  481. * directories; defer that to end of script.
  482. */
  483. archive_string_sprintf(&shar->work, "chmod %o ",
  484. (unsigned int)(archive_entry_mode(shar->entry) & 07777));
  485. shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1);
  486. archive_strcat(&shar->work, "\n");
  487. u = archive_entry_uname(shar->entry);
  488. g = archive_entry_gname(shar->entry);
  489. if (u != NULL || g != NULL) {
  490. archive_strcat(&shar->work, "chown ");
  491. if (u != NULL)
  492. shar_quote(&shar->work, u, 1);
  493. if (g != NULL) {
  494. archive_strcat(&shar->work, ":");
  495. shar_quote(&shar->work, g, 1);
  496. }
  497. archive_strcat(&shar->work, " ");
  498. shar_quote(&shar->work,
  499. archive_entry_pathname(shar->entry), 1);
  500. archive_strcat(&shar->work, "\n");
  501. }
  502. if ((p = archive_entry_fflags_text(shar->entry)) != NULL) {
  503. archive_string_sprintf(&shar->work, "chflags %s ", p);
  504. shar_quote(&shar->work,
  505. archive_entry_pathname(shar->entry), 1);
  506. archive_strcat(&shar->work, "\n");
  507. }
  508. /* TODO: restore ACLs */
  509. } else {
  510. if (shar->has_data) {
  511. /* Finish sed-encoded data: ensure last line ends. */
  512. if (!shar->end_of_line)
  513. archive_strappend_char(&shar->work, '\n');
  514. archive_strcat(&shar->work, "SHAR_END\n");
  515. }
  516. }
  517. archive_entry_free(shar->entry);
  518. shar->entry = NULL;
  519. if (shar->work.length < 65536)
  520. return (ARCHIVE_OK);
  521. ret = __archive_write_output(a, shar->work.s, shar->work.length);
  522. if (ret != ARCHIVE_OK)
  523. return (ARCHIVE_FATAL);
  524. archive_string_empty(&shar->work);
  525. return (ARCHIVE_OK);
  526. }
  527. static int
  528. archive_write_shar_close(struct archive_write *a)
  529. {
  530. struct shar *shar;
  531. int ret;
  532. /*
  533. * TODO: Accumulate list of directory names/modes and
  534. * fix them all up at end-of-archive.
  535. */
  536. shar = (struct shar *)a->format_data;
  537. /*
  538. * Only write the end-of-archive markers if the archive was
  539. * actually started. This avoids problems if someone sets
  540. * shar format, then sets another format (which would invoke
  541. * shar_finish to free the format-specific data).
  542. */
  543. if (shar->wrote_header == 0)
  544. return (ARCHIVE_OK);
  545. archive_strcat(&shar->work, "exit\n");
  546. ret = __archive_write_output(a, shar->work.s, shar->work.length);
  547. if (ret != ARCHIVE_OK)
  548. return (ARCHIVE_FATAL);
  549. /* Shar output is never padded. */
  550. archive_write_set_bytes_in_last_block(&a->archive, 1);
  551. /*
  552. * TODO: shar should also suppress padding of
  553. * uncompressed data within gzip/bzip2 streams.
  554. */
  555. return (ARCHIVE_OK);
  556. }
  557. static int
  558. archive_write_shar_free(struct archive_write *a)
  559. {
  560. struct shar *shar;
  561. shar = (struct shar *)a->format_data;
  562. if (shar == NULL)
  563. return (ARCHIVE_OK);
  564. archive_entry_free(shar->entry);
  565. free(shar->last_dir);
  566. archive_string_free(&(shar->work));
  567. archive_string_free(&(shar->quoted_name));
  568. free(shar);
  569. a->format_data = NULL;
  570. return (ARCHIVE_OK);
  571. }