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