archive_write_set_format_shar.c 17 KB

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