2 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
36 #if HAVE_LIBXML_XMLWRITER_H
37 #include <libxml/xmlwriter.h>
50 #include "archive_digest_private.h"
51 #include "archive_endian.h"
52 #include "archive_entry.h"
53 #include "archive_entry_locale.h"
54 #include "archive_private.h"
55 #include "archive_rb.h"
56 #include "archive_string.h"
57 #include "archive_write_private.h"
60 * Differences to xar utility.
61 * - Subdocument is not supported yet.
62 * - ACL is not supported yet.
63 * - When writing an XML element <link type="<file-type>">, <file-type>
64 * which is a file type a symbolic link is referencing is always marked
65 * as "broken". Xar utility uses stat(2) to get the file type, but, in
66 * libarchive format writer, we should not use it; if it is needed, we
67 * should get about it at archive_read_disk.c.
68 * - It is possible to appear both <flags> and <ext2> elements.
69 * Xar utility generates <flags> on BSD platform and <ext2> on Linux
74 #if !(defined(HAVE_LIBXML_XMLWRITER_H) && defined(LIBXML_VERSION) &&\
75 LIBXML_VERSION >= 20703) ||\
76 !defined(HAVE_ZLIB_H) || \
77 !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
79 * xar needs several external libraries.
81 * o openssl or MD5/SHA1 hash function
87 archive_write_set_format_xar(struct archive *_a)
89 struct archive_write *a = (struct archive_write *)_a;
91 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
92 "Xar not supported on this platform");
93 return (ARCHIVE_WARN);
96 #else /* Support xar format */
98 /*#define DEBUG_PRINT_TOC 1 */
100 #define BAD_CAST_CONST (const xmlChar *)
102 #define HEADER_MAGIC 0x78617221
103 #define HEADER_SIZE 28
104 #define HEADER_VERSION 1
114 #define MAX_SUM_SIZE 20
115 #define MD5_NAME "md5"
116 #define SHA1_NAME "sha1"
128 #ifdef ARCHIVE_HAS_MD5
129 archive_md5_ctx md5ctx;
131 #ifdef ARCHIVE_HAS_SHA1
132 archive_sha1_ctx sha1ctx;
145 const unsigned char *next_in;
149 unsigned char *next_out;
155 int (*code) (struct archive *a,
156 struct la_zstream *lastrm,
157 enum la_zaction action);
158 int (*end)(struct archive *a,
159 struct la_zstream *lastrm);
165 unsigned char val[MAX_SUM_SIZE];
170 struct heap_data *next;
171 uint64_t temp_offset;
172 uint64_t length; /* archived size. */
173 uint64_t size; /* extracted size. */
174 enum enctype compression;
175 struct chksumval a_sum; /* archived checksum. */
176 struct chksumval e_sum; /* extracted checksum. */
180 struct archive_rb_node rbnode;
183 struct archive_entry *entry;
185 struct archive_rb_tree rbtree;
189 /* For hardlinked files.
190 * Use only when archive_entry_nlink() > 1 */
191 struct file *hardlink_target;
192 struct file *parent; /* parent directory entry */
194 * To manage sub directory files.
195 * We use 'chnext' (a member of struct file) to chain.
202 /* For making a directory tree. */
203 struct archive_string parentdir;
204 struct archive_string basename;
205 struct archive_string symlink;
209 struct heap_data *first;
210 struct heap_data **last;
212 struct heap_data data;
213 struct archive_string script;
220 struct archive_rb_node rbnode;
230 uint64_t temp_offset;
234 struct file *cur_dirent;
235 struct archive_string cur_dirstr;
236 struct file *cur_file;
237 uint64_t bytes_remaining;
238 struct archive_string tstr;
239 struct archive_string vstr;
241 enum sumalg opt_toc_sumalg;
242 enum sumalg opt_sumalg;
243 enum enctype opt_compression;
244 int opt_compression_level;
245 uint32_t opt_threads;
247 struct chksumwork a_sumwrk; /* archived checksum. */
248 struct chksumwork e_sumwrk; /* extracted checksum. */
249 struct la_zstream stream;
250 struct archive_string_conv *sconv;
252 * Compressed data buffer.
254 unsigned char wbuff[1024 * 64];
255 size_t wbuff_remaining;
257 struct heap_data toc;
259 * The list of all file entries is used to manage struct file
261 * We use 'next' (a member of struct file) to chain.
268 * The list of hard-linked file entries.
269 * We use 'hlnext' (a member of struct file) to chain.
271 struct archive_rb_tree hardlink_rbtree;
274 static int xar_options(struct archive_write *,
275 const char *, const char *);
276 static int xar_write_header(struct archive_write *,
277 struct archive_entry *);
278 static ssize_t xar_write_data(struct archive_write *,
279 const void *, size_t);
280 static int xar_finish_entry(struct archive_write *);
281 static int xar_close(struct archive_write *);
282 static int xar_free(struct archive_write *);
284 static struct file *file_new(struct archive_write *a, struct archive_entry *);
285 static void file_free(struct file *);
286 static struct file *file_create_virtual_dir(struct archive_write *a, struct xar *,
288 static int file_add_child_tail(struct file *, struct file *);
289 static struct file *file_find_child(struct file *, const char *);
290 static int file_gen_utility_names(struct archive_write *,
292 static int get_path_component(char *, int, const char *);
293 static int file_tree(struct archive_write *, struct file **);
294 static void file_register(struct xar *, struct file *);
295 static void file_init_register(struct xar *);
296 static void file_free_register(struct xar *);
297 static int file_register_hardlink(struct archive_write *,
299 static void file_connect_hardlink_files(struct xar *);
300 static void file_init_hardlinks(struct xar *);
301 static void file_free_hardlinks(struct xar *);
303 static void checksum_init(struct chksumwork *, enum sumalg);
304 static void checksum_update(struct chksumwork *, const void *, size_t);
305 static void checksum_final(struct chksumwork *, struct chksumval *);
306 static int compression_init_encoder_gzip(struct archive *,
307 struct la_zstream *, int, int);
308 static int compression_code_gzip(struct archive *,
309 struct la_zstream *, enum la_zaction);
310 static int compression_end_gzip(struct archive *, struct la_zstream *);
311 static int compression_init_encoder_bzip2(struct archive *,
312 struct la_zstream *, int);
313 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
314 static int compression_code_bzip2(struct archive *,
315 struct la_zstream *, enum la_zaction);
316 static int compression_end_bzip2(struct archive *, struct la_zstream *);
318 static int compression_init_encoder_lzma(struct archive *,
319 struct la_zstream *, int);
320 static int compression_init_encoder_xz(struct archive *,
321 struct la_zstream *, int, int);
322 #if defined(HAVE_LZMA_H)
323 static int compression_code_lzma(struct archive *,
324 struct la_zstream *, enum la_zaction);
325 static int compression_end_lzma(struct archive *, struct la_zstream *);
327 static int xar_compression_init_encoder(struct archive_write *);
328 static int compression_code(struct archive *,
329 struct la_zstream *, enum la_zaction);
330 static int compression_end(struct archive *,
331 struct la_zstream *);
332 static int save_xattrs(struct archive_write *, struct file *);
333 static int getalgsize(enum sumalg);
334 static const char *getalgname(enum sumalg);
337 archive_write_set_format_xar(struct archive *_a)
339 struct archive_write *a = (struct archive_write *)_a;
342 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
343 ARCHIVE_STATE_NEW, "archive_write_set_format_xar");
345 /* If another format was already registered, unregister it. */
346 if (a->format_free != NULL)
349 xar = calloc(1, sizeof(*xar));
351 archive_set_error(&a->archive, ENOMEM,
352 "Can't allocate xar data");
353 return (ARCHIVE_FATAL);
356 file_init_register(xar);
357 file_init_hardlinks(xar);
358 archive_string_init(&(xar->tstr));
359 archive_string_init(&(xar->vstr));
362 * Create the root directory.
364 xar->root = file_create_virtual_dir(a, xar, "");
365 if (xar->root == NULL) {
367 archive_set_error(&a->archive, ENOMEM,
368 "Can't allocate xar data");
369 return (ARCHIVE_FATAL);
371 xar->root->parent = xar->root;
372 file_register(xar, xar->root);
373 xar->cur_dirent = xar->root;
374 archive_string_init(&(xar->cur_dirstr));
375 archive_string_ensure(&(xar->cur_dirstr), 1);
376 xar->cur_dirstr.s[0] = 0;
381 /* Set default checksum type. */
382 xar->opt_toc_sumalg = CKSUM_SHA1;
383 xar->opt_sumalg = CKSUM_SHA1;
384 /* Set default compression type, level, and number of threads. */
385 xar->opt_compression = GZIP;
386 xar->opt_compression_level = 6;
387 xar->opt_threads = 1;
389 a->format_data = xar;
391 a->format_name = "xar";
392 a->format_options = xar_options;
393 a->format_write_header = xar_write_header;
394 a->format_write_data = xar_write_data;
395 a->format_finish_entry = xar_finish_entry;
396 a->format_close = xar_close;
397 a->format_free = xar_free;
398 a->archive.archive_format = ARCHIVE_FORMAT_XAR;
399 a->archive.archive_format_name = "xar";
405 xar_options(struct archive_write *a, const char *key, const char *value)
409 xar = (struct xar *)a->format_data;
411 if (strcmp(key, "checksum") == 0) {
413 xar->opt_sumalg = CKSUM_NONE;
414 else if (strcmp(value, "sha1") == 0)
415 xar->opt_sumalg = CKSUM_SHA1;
416 else if (strcmp(value, "md5") == 0)
417 xar->opt_sumalg = CKSUM_MD5;
419 archive_set_error(&(a->archive),
421 "Unknown checksum name: `%s'",
423 return (ARCHIVE_FAILED);
427 if (strcmp(key, "compression") == 0) {
428 const char *name = NULL;
431 xar->opt_compression = NONE;
432 else if (strcmp(value, "gzip") == 0)
433 xar->opt_compression = GZIP;
434 else if (strcmp(value, "bzip2") == 0)
435 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
436 xar->opt_compression = BZIP2;
440 else if (strcmp(value, "lzma") == 0)
442 xar->opt_compression = LZMA;
446 else if (strcmp(value, "xz") == 0)
448 xar->opt_compression = XZ;
453 archive_set_error(&(a->archive),
455 "Unknown compression name: `%s'",
457 return (ARCHIVE_FAILED);
460 archive_set_error(&(a->archive),
462 "`%s' compression not supported "
465 return (ARCHIVE_FAILED);
469 if (strcmp(key, "compression-level") == 0) {
471 !(value[0] >= '0' && value[0] <= '9') ||
473 archive_set_error(&(a->archive),
475 "Illegal value `%s'",
477 return (ARCHIVE_FAILED);
479 xar->opt_compression_level = value[0] - '0';
482 if (strcmp(key, "toc-checksum") == 0) {
484 xar->opt_toc_sumalg = CKSUM_NONE;
485 else if (strcmp(value, "sha1") == 0)
486 xar->opt_toc_sumalg = CKSUM_SHA1;
487 else if (strcmp(value, "md5") == 0)
488 xar->opt_toc_sumalg = CKSUM_MD5;
490 archive_set_error(&(a->archive),
492 "Unknown checksum name: `%s'",
494 return (ARCHIVE_FAILED);
498 if (strcmp(key, "threads") == 0) {
502 return (ARCHIVE_FAILED);
504 xar->opt_threads = (int)strtoul(value, &endptr, 10);
505 if (errno != 0 || *endptr != '\0') {
506 xar->opt_threads = 1;
507 archive_set_error(&(a->archive),
509 "Illegal value `%s'",
511 return (ARCHIVE_FAILED);
513 if (xar->opt_threads == 0) {
514 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
515 xar->opt_threads = lzma_cputhreads();
517 xar->opt_threads = 1;
522 /* Note: The "warn" return is just to inform the options
523 * supervisor that we didn't handle it. It will generate
524 * a suitable error if no one used this option. */
525 return (ARCHIVE_WARN);
529 xar_write_header(struct archive_write *a, struct archive_entry *entry)
533 struct archive_entry *file_entry;
536 xar = (struct xar *)a->format_data;
537 xar->cur_file = NULL;
538 xar->bytes_remaining = 0;
540 if (xar->sconv == NULL) {
541 xar->sconv = archive_string_conversion_to_charset(
542 &a->archive, "UTF-8", 1);
543 if (xar->sconv == NULL)
544 return (ARCHIVE_FATAL);
547 file = file_new(a, entry);
549 archive_set_error(&a->archive, ENOMEM,
550 "Can't allocate data");
551 return (ARCHIVE_FATAL);
553 r2 = file_gen_utility_names(a, file);
554 if (r2 < ARCHIVE_WARN)
558 * Ignore a path which looks like the top of directory name
559 * since we have already made the root directory of an Xar archive.
561 if (archive_strlen(&(file->parentdir)) == 0 &&
562 archive_strlen(&(file->basename)) == 0) {
567 /* Add entry into tree */
568 file_entry = file->entry;
569 r = file_tree(a, &file);
572 /* There is the same file in tree and
573 * the current file is older than the file in tree.
574 * So we don't need the current file data anymore. */
575 if (file->entry != file_entry)
578 file_register(xar, file);
580 /* A virtual file, which is a directory, does not have
581 * any contents and we won't store it into a archive
582 * file other than its name. */
587 * Prepare to save the contents of the file.
589 if (xar->temp_fd == -1) {
591 xar->temp_offset = 0;
592 xar->temp_fd = __archive_mktemp(NULL);
593 if (xar->temp_fd < 0) {
594 archive_set_error(&a->archive, errno,
595 "Couldn't create temporary file");
596 return (ARCHIVE_FATAL);
598 algsize = getalgsize(xar->opt_toc_sumalg);
600 if (lseek(xar->temp_fd, algsize, SEEK_SET) < 0) {
601 archive_set_error(&(a->archive), errno,
603 return (ARCHIVE_FATAL);
605 xar->temp_offset = algsize;
609 if (archive_entry_hardlink(file->entry) == NULL) {
610 r = save_xattrs(a, file);
612 return (ARCHIVE_FATAL);
615 /* Non regular files contents are unneeded to be saved to
616 * a temporary file. */
617 if (archive_entry_filetype(file->entry) != AE_IFREG)
621 * Set the current file to cur_file to read its contents.
623 xar->cur_file = file;
625 if (archive_entry_nlink(file->entry) > 1) {
626 r = file_register_hardlink(a, file);
629 if (archive_entry_hardlink(file->entry) != NULL) {
630 archive_entry_unset_size(file->entry);
635 /* Save a offset of current file in temporary file. */
636 file->data.temp_offset = xar->temp_offset;
637 file->data.size = archive_entry_size(file->entry);
638 file->data.compression = xar->opt_compression;
639 xar->bytes_remaining = archive_entry_size(file->entry);
640 checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
641 checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
642 r = xar_compression_init_encoder(a);
651 write_to_temp(struct archive_write *a, const void *buff, size_t s)
654 const unsigned char *p;
657 xar = (struct xar *)a->format_data;
658 p = (const unsigned char *)buff;
660 ws = write(xar->temp_fd, p, s);
662 archive_set_error(&(a->archive), errno,
663 "fwrite function failed");
664 return (ARCHIVE_FATAL);
668 xar->temp_offset += ws;
674 xar_write_data(struct archive_write *a, const void *buff, size_t s)
681 xar = (struct xar *)a->format_data;
683 if (s > xar->bytes_remaining)
684 s = (size_t)xar->bytes_remaining;
685 if (s == 0 || xar->cur_file == NULL)
687 if (xar->cur_file->data.compression == NONE) {
688 checksum_update(&(xar->e_sumwrk), buff, s);
689 checksum_update(&(xar->a_sumwrk), buff, s);
692 xar->stream.next_in = (const unsigned char *)buff;
693 xar->stream.avail_in = s;
694 if (xar->bytes_remaining > s)
697 run = ARCHIVE_Z_FINISH;
698 /* Compress file data. */
699 r = compression_code(&(a->archive), &(xar->stream), run);
700 if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
701 return (ARCHIVE_FATAL);
702 rsize = s - xar->stream.avail_in;
703 checksum_update(&(xar->e_sumwrk), buff, rsize);
704 size = sizeof(xar->wbuff) - xar->stream.avail_out;
705 checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
707 #if !defined(_WIN32) || defined(__CYGWIN__)
708 if (xar->bytes_remaining ==
709 (uint64_t)archive_entry_size(xar->cur_file->entry)) {
711 * Get the path of a shell script if so.
713 const unsigned char *b = (const unsigned char *)buff;
715 archive_string_empty(&(xar->cur_file->script));
716 if (rsize > 2 && b[0] == '#' && b[1] == '!') {
723 if ((rsize - off) > PATH_MAX)
724 end = off + PATH_MAX;
728 /* Find the end of a script path. */
729 for (i = off; i < end && b[i] != '\0' &&
730 b[i] != '\n' && b[i] != '\r' &&
731 b[i] != ' ' && b[i] != '\t'; i++)
733 archive_strncpy(&(xar->cur_file->script), b + off,
739 if (xar->cur_file->data.compression == NONE) {
740 if (write_to_temp(a, buff, size) != ARCHIVE_OK)
741 return (ARCHIVE_FATAL);
743 if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
744 return (ARCHIVE_FATAL);
746 xar->bytes_remaining -= rsize;
747 xar->cur_file->data.length += size;
753 xar_finish_entry(struct archive_write *a)
760 xar = (struct xar *)a->format_data;
761 if (xar->cur_file == NULL)
764 while (xar->bytes_remaining > 0) {
765 s = (size_t)xar->bytes_remaining;
766 if (s > a->null_length)
768 w = xar_write_data(a, a->nulls, s);
770 xar->bytes_remaining -= w;
774 file = xar->cur_file;
775 checksum_final(&(xar->e_sumwrk), &(file->data.e_sum));
776 checksum_final(&(xar->a_sumwrk), &(file->data.a_sum));
777 xar->cur_file = NULL;
783 xmlwrite_string_attr(struct archive_write *a, xmlTextWriterPtr writer,
784 const char *key, const char *value,
785 const char *attrkey, const char *attrvalue)
789 r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(key));
791 archive_set_error(&a->archive,
793 "xmlTextWriterStartElement() failed: %d", r);
794 return (ARCHIVE_FATAL);
796 if (attrkey != NULL && attrvalue != NULL) {
797 r = xmlTextWriterWriteAttribute(writer,
798 BAD_CAST_CONST(attrkey), BAD_CAST_CONST(attrvalue));
800 archive_set_error(&a->archive,
802 "xmlTextWriterWriteAttribute() failed: %d", r);
803 return (ARCHIVE_FATAL);
807 r = xmlTextWriterWriteString(writer, BAD_CAST_CONST(value));
809 archive_set_error(&a->archive,
811 "xmlTextWriterWriteString() failed: %d", r);
812 return (ARCHIVE_FATAL);
815 r = xmlTextWriterEndElement(writer);
817 archive_set_error(&a->archive,
819 "xmlTextWriterEndElement() failed: %d", r);
820 return (ARCHIVE_FATAL);
826 xmlwrite_string(struct archive_write *a, xmlTextWriterPtr writer,
827 const char *key, const char *value)
834 r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(key));
836 archive_set_error(&a->archive,
838 "xmlTextWriterStartElement() failed: %d", r);
839 return (ARCHIVE_FATAL);
842 r = xmlTextWriterWriteString(writer, BAD_CAST_CONST(value));
844 archive_set_error(&a->archive,
846 "xmlTextWriterWriteString() failed: %d", r);
847 return (ARCHIVE_FATAL);
850 r = xmlTextWriterEndElement(writer);
852 archive_set_error(&a->archive,
854 "xmlTextWriterEndElement() failed: %d", r);
855 return (ARCHIVE_FATAL);
861 xmlwrite_fstring(struct archive_write *a, xmlTextWriterPtr writer,
862 const char *key, const char *fmt, ...)
867 xar = (struct xar *)a->format_data;
869 archive_string_empty(&xar->vstr);
870 archive_string_vsprintf(&xar->vstr, fmt, ap);
872 return (xmlwrite_string(a, writer, key, xar->vstr.s));
876 xmlwrite_time(struct archive_write *a, xmlTextWriterPtr writer,
877 const char *key, time_t t, int z)
882 #if defined(HAVE_GMTIME_R)
884 #elif defined(HAVE__GMTIME64_S)
885 _gmtime64_s(&tm, &t);
887 memcpy(&tm, gmtime(&t), sizeof(tm));
889 memset(×tr, 0, sizeof(timestr));
890 /* Do not use %F and %T for portability. */
891 strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &tm);
893 strcat(timestr, "Z");
894 return (xmlwrite_string(a, writer, key, timestr));
898 xmlwrite_mode(struct archive_write *a, xmlTextWriterPtr writer,
899 const char *key, mode_t mode)
904 ms[1] = '0' + ((mode >> 6) & 07);
905 ms[2] = '0' + ((mode >> 3) & 07);
906 ms[3] = '0' + (mode & 07);
909 return (xmlwrite_string(a, writer, key, ms));
913 xmlwrite_sum(struct archive_write *a, xmlTextWriterPtr writer,
914 const char *key, struct chksumval *sum)
918 char buff[MAX_SUM_SIZE*2 + 1];
924 algname = getalgname(sum->alg);
925 algsize = getalgsize(sum->alg);
926 if (algname != NULL) {
927 const char *hex = "0123456789abcdef";
930 for (i = 0; i < algsize; i++) {
931 *p++ = hex[(*s >> 4)];
932 *p++ = hex[(*s & 0x0f)];
936 r = xmlwrite_string_attr(a, writer,
940 return (ARCHIVE_FATAL);
947 xmlwrite_heap(struct archive_write *a, xmlTextWriterPtr writer,
948 struct heap_data *heap)
953 r = xmlwrite_fstring(a, writer, "length", "%ju", heap->length);
955 return (ARCHIVE_FATAL);
956 r = xmlwrite_fstring(a, writer, "offset", "%ju", heap->temp_offset);
958 return (ARCHIVE_FATAL);
959 r = xmlwrite_fstring(a, writer, "size", "%ju", heap->size);
961 return (ARCHIVE_FATAL);
962 switch (heap->compression) {
964 encname = "application/x-gzip"; break;
966 encname = "application/x-bzip2"; break;
968 encname = "application/x-lzma"; break;
970 encname = "application/x-xz"; break;
972 encname = "application/octet-stream"; break;
974 r = xmlwrite_string_attr(a, writer, "encoding", NULL,
977 return (ARCHIVE_FATAL);
978 r = xmlwrite_sum(a, writer, "archived-checksum", &(heap->a_sum));
980 return (ARCHIVE_FATAL);
981 r = xmlwrite_sum(a, writer, "extracted-checksum", &(heap->e_sum));
983 return (ARCHIVE_FATAL);
988 * xar utility records fflags as following xml elements:
998 * If xar is running on BSD platform, records <flags>..</flags>;
999 * if xar is running on linux platform, records <ext2>..</ext2>;
1000 * otherwise does not record.
1002 * Our implements records both <flags> and <ext2> if it's necessary.
1005 make_fflags_entry(struct archive_write *a, xmlTextWriterPtr writer,
1006 const char *element, const char *fflags_text)
1008 static const struct flagentry {
1010 const char *xarname;
1013 { "sappnd", "SystemAppend"},
1014 { "sappend", "SystemAppend"},
1015 { "arch", "SystemArchived"},
1016 { "archived", "SystemArchived"},
1017 { "schg", "SystemImmutable"},
1018 { "schange", "SystemImmutable"},
1019 { "simmutable", "SystemImmutable"},
1020 { "nosunlnk", "SystemNoUnlink"},
1021 { "nosunlink", "SystemNoUnlink"},
1022 { "snapshot", "SystemSnapshot"},
1023 { "uappnd", "UserAppend"},
1024 { "uappend", "UserAppend"},
1025 { "uchg", "UserImmutable"},
1026 { "uchange", "UserImmutable"},
1027 { "uimmutable", "UserImmutable"},
1028 { "nodump", "UserNoDump"},
1029 { "noopaque", "UserOpaque"},
1030 { "nouunlnk", "UserNoUnlink"},
1031 { "nouunlink", "UserNoUnlink"},
1035 { "sappnd", "AppendOnly"},
1036 { "sappend", "AppendOnly"},
1037 { "schg", "Immutable"},
1038 { "schange", "Immutable"},
1039 { "simmutable", "Immutable"},
1040 { "nodump", "NoDump"},
1041 { "nouunlnk", "Undelete"},
1042 { "nouunlink", "Undelete"},
1043 { "btree", "BTree"},
1044 { "comperr", "CompError"},
1045 { "compress", "Compress"},
1046 { "noatime", "NoAtime"},
1047 { "compdirty", "CompDirty"},
1048 { "comprblk", "CompBlock"},
1049 { "dirsync", "DirSync"},
1050 { "hashidx", "HashIndexed"},
1051 { "imagic", "iMagic"},
1052 { "journal", "Journaled"},
1053 { "securedeletion", "SecureDeletion"},
1054 { "sync", "Synchronous"},
1055 { "notail", "NoTail"},
1056 { "topdir", "TopDir"},
1057 { "reserved", "Reserved"},
1060 const struct flagentry *fe, *flagentry;
1061 #define FLAGENTRY_MAXSIZE ((sizeof(flagbsd)+sizeof(flagext2))/sizeof(flagbsd))
1062 const struct flagentry *avail[FLAGENTRY_MAXSIZE];
1066 if (strcmp(element, "ext2") == 0)
1067 flagentry = flagext2;
1069 flagentry = flagbsd;
1075 cp = strchr(p, ',');
1079 for (fe = flagentry; fe->name != NULL; fe++) {
1080 if (fe->name[cp - p] != '\0'
1081 || p[0] != fe->name[0])
1083 if (strncmp(p, fe->name, cp - p) == 0) {
1092 } while (p != NULL);
1095 r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(element));
1097 archive_set_error(&a->archive,
1099 "xmlTextWriterStartElement() failed: %d", r);
1100 return (ARCHIVE_FATAL);
1102 for (i = 0; i < n; i++) {
1103 r = xmlwrite_string(a, writer,
1104 avail[i]->xarname, NULL);
1105 if (r != ARCHIVE_OK)
1109 r = xmlTextWriterEndElement(writer);
1111 archive_set_error(&a->archive,
1113 "xmlTextWriterEndElement() failed: %d", r);
1114 return (ARCHIVE_FATAL);
1117 return (ARCHIVE_OK);
1121 make_file_entry(struct archive_write *a, xmlTextWriterPtr writer,
1125 const char *filetype, *filelink, *fflags;
1126 struct archive_string linkto;
1127 struct heap_data *heap;
1133 xar = (struct xar *)a->format_data;
1137 * Make a file name entry, "<name>".
1139 l = ll = archive_strlen(&(file->basename));
1142 archive_set_error(&a->archive, ENOMEM,
1143 "Can't allocate memory");
1144 return (ARCHIVE_FATAL);
1146 r = UTF8Toisolat1(tmp, &l, BAD_CAST(file->basename.s), &ll);
1149 r = xmlTextWriterStartElement(writer, BAD_CAST("name"));
1151 archive_set_error(&a->archive,
1153 "xmlTextWriterStartElement() failed: %d", r);
1154 return (ARCHIVE_FATAL);
1156 r = xmlTextWriterWriteAttribute(writer,
1157 BAD_CAST("enctype"), BAD_CAST("base64"));
1159 archive_set_error(&a->archive,
1161 "xmlTextWriterWriteAttribute() failed: %d", r);
1162 return (ARCHIVE_FATAL);
1164 r = xmlTextWriterWriteBase64(writer, file->basename.s,
1165 0, archive_strlen(&(file->basename)));
1167 archive_set_error(&a->archive,
1169 "xmlTextWriterWriteBase64() failed: %d", r);
1170 return (ARCHIVE_FATAL);
1172 r = xmlTextWriterEndElement(writer);
1174 archive_set_error(&a->archive,
1176 "xmlTextWriterEndElement() failed: %d", r);
1177 return (ARCHIVE_FATAL);
1180 r = xmlwrite_string(a, writer, "name", file->basename.s);
1182 return (ARCHIVE_FATAL);
1186 * Make a file type entry, "<type>".
1189 archive_string_init(&linkto);
1190 switch (archive_entry_filetype(file->entry)) {
1192 filetype = "directory"; break;
1194 filetype = "symlink"; break;
1196 filetype = "character special"; break;
1198 filetype = "block special"; break;
1200 filetype = "socket"; break;
1202 filetype = "fifo"; break;
1205 if (file->hardlink_target != NULL) {
1206 filetype = "hardlink";
1208 if (file->hardlink_target == file)
1209 archive_strcpy(&linkto, "original");
1211 archive_string_sprintf(&linkto, "%d",
1212 file->hardlink_target->id);
1217 r = xmlwrite_string_attr(a, writer, "type", filetype,
1218 filelink, linkto.s);
1219 archive_string_free(&linkto);
1221 return (ARCHIVE_FATAL);
1224 * On a virtual directory, we record "name" and "type" only.
1227 return (ARCHIVE_OK);
1229 switch (archive_entry_filetype(file->entry)) {
1232 * xar utility has checked a file type, which
1233 * a symbolic-link file has referenced.
1235 * <link type="directory">../ref/</link>
1236 * The symlink target file is "../ref/" and its
1237 * file type is a directory.
1239 * <link type="file">../f</link>
1240 * The symlink target file is "../f" and its
1241 * file type is a regular file.
1243 * But our implementation cannot do it, and then we
1244 * always record that a attribute "type" is "broken",
1246 * <link type="broken">foo/bar</link>
1247 * It means "foo/bar" is not reachable.
1249 r = xmlwrite_string_attr(a, writer, "link",
1253 return (ARCHIVE_FATAL);
1257 r = xmlTextWriterStartElement(writer, BAD_CAST("device"));
1259 archive_set_error(&a->archive,
1261 "xmlTextWriterStartElement() failed: %d", r);
1262 return (ARCHIVE_FATAL);
1264 r = xmlwrite_fstring(a, writer, "major",
1265 "%d", archive_entry_rdevmajor(file->entry));
1267 return (ARCHIVE_FATAL);
1268 r = xmlwrite_fstring(a, writer, "minor",
1269 "%d", archive_entry_rdevminor(file->entry));
1271 return (ARCHIVE_FATAL);
1272 r = xmlTextWriterEndElement(writer);
1274 archive_set_error(&a->archive,
1276 "xmlTextWriterEndElement() failed: %d", r);
1277 return (ARCHIVE_FATAL);
1285 * Make a inode entry, "<inode>".
1287 r = xmlwrite_fstring(a, writer, "inode",
1288 "%jd", archive_entry_ino64(file->entry));
1290 return (ARCHIVE_FATAL);
1291 if (archive_entry_dev(file->entry) != 0) {
1292 r = xmlwrite_fstring(a, writer, "deviceno",
1293 "%d", archive_entry_dev(file->entry));
1295 return (ARCHIVE_FATAL);
1299 * Make a file mode entry, "<mode>".
1301 r = xmlwrite_mode(a, writer, "mode",
1302 archive_entry_mode(file->entry));
1304 return (ARCHIVE_FATAL);
1307 * Make a user entry, "<uid>" and "<user>.
1309 r = xmlwrite_fstring(a, writer, "uid",
1310 "%d", archive_entry_uid(file->entry));
1312 return (ARCHIVE_FATAL);
1313 r = archive_entry_uname_l(file->entry, &p, &len, xar->sconv);
1315 if (errno == ENOMEM) {
1316 archive_set_error(&a->archive, ENOMEM,
1317 "Can't allocate memory for Uname");
1318 return (ARCHIVE_FATAL);
1320 archive_set_error(&a->archive,
1321 ARCHIVE_ERRNO_FILE_FORMAT,
1322 "Can't translate uname '%s' to UTF-8",
1323 archive_entry_uname(file->entry));
1327 r = xmlwrite_string(a, writer, "user", p);
1329 return (ARCHIVE_FATAL);
1333 * Make a group entry, "<gid>" and "<group>.
1335 r = xmlwrite_fstring(a, writer, "gid",
1336 "%d", archive_entry_gid(file->entry));
1338 return (ARCHIVE_FATAL);
1339 r = archive_entry_gname_l(file->entry, &p, &len, xar->sconv);
1341 if (errno == ENOMEM) {
1342 archive_set_error(&a->archive, ENOMEM,
1343 "Can't allocate memory for Gname");
1344 return (ARCHIVE_FATAL);
1346 archive_set_error(&a->archive,
1347 ARCHIVE_ERRNO_FILE_FORMAT,
1348 "Can't translate gname '%s' to UTF-8",
1349 archive_entry_gname(file->entry));
1353 r = xmlwrite_string(a, writer, "group", p);
1355 return (ARCHIVE_FATAL);
1359 * Make a ctime entry, "<ctime>".
1361 if (archive_entry_ctime_is_set(file->entry)) {
1362 r = xmlwrite_time(a, writer, "ctime",
1363 archive_entry_ctime(file->entry), 1);
1365 return (ARCHIVE_FATAL);
1369 * Make a mtime entry, "<mtime>".
1371 if (archive_entry_mtime_is_set(file->entry)) {
1372 r = xmlwrite_time(a, writer, "mtime",
1373 archive_entry_mtime(file->entry), 1);
1375 return (ARCHIVE_FATAL);
1379 * Make a atime entry, "<atime>".
1381 if (archive_entry_atime_is_set(file->entry)) {
1382 r = xmlwrite_time(a, writer, "atime",
1383 archive_entry_atime(file->entry), 1);
1385 return (ARCHIVE_FATAL);
1389 * Make fflags entries, "<flags>" and "<ext2>".
1391 fflags = archive_entry_fflags_text(file->entry);
1392 if (fflags != NULL) {
1393 r = make_fflags_entry(a, writer, "flags", fflags);
1396 r = make_fflags_entry(a, writer, "ext2", fflags);
1402 * Make extended attribute entries, "<ea>".
1404 archive_entry_xattr_reset(file->entry);
1405 for (heap = file->xattr.first; heap != NULL; heap = heap->next) {
1410 archive_entry_xattr_next(file->entry,
1411 &name, &value, &size);
1412 r = xmlTextWriterStartElement(writer, BAD_CAST("ea"));
1414 archive_set_error(&a->archive,
1416 "xmlTextWriterStartElement() failed: %d", r);
1417 return (ARCHIVE_FATAL);
1419 r = xmlTextWriterWriteFormatAttribute(writer,
1420 BAD_CAST("id"), "%d", heap->id);
1422 archive_set_error(&a->archive,
1424 "xmlTextWriterWriteAttribute() failed: %d", r);
1425 return (ARCHIVE_FATAL);
1427 r = xmlwrite_heap(a, writer, heap);
1429 return (ARCHIVE_FATAL);
1430 r = xmlwrite_string(a, writer, "name", name);
1432 return (ARCHIVE_FATAL);
1434 r = xmlTextWriterEndElement(writer);
1436 archive_set_error(&a->archive,
1438 "xmlTextWriterEndElement() failed: %d", r);
1439 return (ARCHIVE_FATAL);
1444 * Make a file data entry, "<data>".
1446 if (file->data.length > 0) {
1447 r = xmlTextWriterStartElement(writer, BAD_CAST("data"));
1449 archive_set_error(&a->archive,
1451 "xmlTextWriterStartElement() failed: %d", r);
1452 return (ARCHIVE_FATAL);
1455 r = xmlwrite_heap(a, writer, &(file->data));
1457 return (ARCHIVE_FATAL);
1459 r = xmlTextWriterEndElement(writer);
1461 archive_set_error(&a->archive,
1463 "xmlTextWriterEndElement() failed: %d", r);
1464 return (ARCHIVE_FATAL);
1468 if (archive_strlen(&file->script) > 0) {
1469 r = xmlTextWriterStartElement(writer, BAD_CAST("content"));
1471 archive_set_error(&a->archive,
1473 "xmlTextWriterStartElement() failed: %d", r);
1474 return (ARCHIVE_FATAL);
1477 r = xmlwrite_string(a, writer,
1478 "interpreter", file->script.s);
1480 return (ARCHIVE_FATAL);
1482 r = xmlwrite_string(a, writer, "type", "script");
1484 return (ARCHIVE_FATAL);
1486 r = xmlTextWriterEndElement(writer);
1488 archive_set_error(&a->archive,
1490 "xmlTextWriterEndElement() failed: %d", r);
1491 return (ARCHIVE_FATAL);
1502 make_toc(struct archive_write *a)
1507 xmlTextWriterPtr writer;
1511 xar = (struct xar *)a->format_data;
1513 ret = ARCHIVE_FATAL;
1516 * Initialize xml writer.
1519 bp = xmlBufferCreate();
1521 archive_set_error(&a->archive, ENOMEM,
1522 "xmlBufferCreate() "
1523 "couldn't create xml buffer");
1526 writer = xmlNewTextWriterMemory(bp, 0);
1527 if (writer == NULL) {
1528 archive_set_error(&a->archive,
1530 "xmlNewTextWriterMemory() "
1531 "couldn't create xml writer");
1534 r = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);
1536 archive_set_error(&a->archive,
1538 "xmlTextWriterStartDocument() failed: %d", r);
1541 r = xmlTextWriterSetIndent(writer, 4);
1543 archive_set_error(&a->archive,
1545 "xmlTextWriterSetIndent() failed: %d", r);
1550 * Start recording TOC
1552 r = xmlTextWriterStartElement(writer, BAD_CAST("xar"));
1554 archive_set_error(&a->archive,
1556 "xmlTextWriterStartElement() failed: %d", r);
1559 r = xmlTextWriterStartElement(writer, BAD_CAST("toc"));
1561 archive_set_error(&a->archive,
1563 "xmlTextWriterStartDocument() failed: %d", r);
1568 * Record the creation time of the archive file.
1570 r = xmlwrite_time(a, writer, "creation-time", time(NULL), 0);
1575 * Record the checksum value of TOC
1577 algsize = getalgsize(xar->opt_toc_sumalg);
1580 * Record TOC checksum
1582 r = xmlTextWriterStartElement(writer, BAD_CAST("checksum"));
1584 archive_set_error(&a->archive,
1586 "xmlTextWriterStartElement() failed: %d", r);
1589 r = xmlTextWriterWriteAttribute(writer, BAD_CAST("style"),
1590 BAD_CAST_CONST(getalgname(xar->opt_toc_sumalg)));
1592 archive_set_error(&a->archive,
1594 "xmlTextWriterWriteAttribute() failed: %d", r);
1599 * Record the offset of the value of checksum of TOC
1601 r = xmlwrite_string(a, writer, "offset", "0");
1606 * Record the size of the value of checksum of TOC
1608 r = xmlwrite_fstring(a, writer, "size", "%d", algsize);
1612 r = xmlTextWriterEndElement(writer);
1614 archive_set_error(&a->archive,
1616 "xmlTextWriterEndElement() failed: %d", r);
1623 if (np != np->parent) {
1624 r = make_file_entry(a, writer, np);
1625 if (r != ARCHIVE_OK)
1629 if (np->dir && np->children.first != NULL) {
1630 /* Enter to sub directories. */
1631 np = np->children.first;
1632 r = xmlTextWriterStartElement(writer,
1635 archive_set_error(&a->archive,
1637 "xmlTextWriterStartElement() "
1641 r = xmlTextWriterWriteFormatAttribute(
1642 writer, BAD_CAST("id"), "%d", np->id);
1644 archive_set_error(&a->archive,
1646 "xmlTextWriterWriteAttribute() "
1652 while (np != np->parent) {
1653 r = xmlTextWriterEndElement(writer);
1655 archive_set_error(&a->archive,
1657 "xmlTextWriterEndElement() "
1661 if (np->chnext == NULL) {
1662 /* Return to the parent directory. */
1666 r = xmlTextWriterStartElement(writer,
1669 archive_set_error(&a->archive,
1671 "xmlTextWriterStartElement() "
1675 r = xmlTextWriterWriteFormatAttribute(
1676 writer, BAD_CAST("id"), "%d", np->id);
1678 archive_set_error(&a->archive,
1680 "xmlTextWriterWriteAttribute() "
1687 } while (np != np->parent);
1689 r = xmlTextWriterEndDocument(writer);
1691 archive_set_error(&a->archive,
1693 "xmlTextWriterEndDocument() failed: %d", r);
1697 fprintf(stderr, "\n---TOC-- %d bytes --\n%s\n",
1698 strlen((const char *)bp->content), bp->content);
1702 * Compress the TOC and calculate the sum of the TOC.
1704 xar->toc.temp_offset = xar->temp_offset;
1705 xar->toc.size = bp->use;
1706 checksum_init(&(xar->a_sumwrk), xar->opt_toc_sumalg);
1708 r = compression_init_encoder_gzip(&(a->archive),
1709 &(xar->stream), 6, 1);
1710 if (r != ARCHIVE_OK)
1712 xar->stream.next_in = bp->content;
1713 xar->stream.avail_in = bp->use;
1714 xar->stream.total_in = 0;
1715 xar->stream.next_out = xar->wbuff;
1716 xar->stream.avail_out = sizeof(xar->wbuff);
1717 xar->stream.total_out = 0;
1721 r = compression_code(&(a->archive),
1722 &(xar->stream), ARCHIVE_Z_FINISH);
1723 if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
1725 size = sizeof(xar->wbuff) - xar->stream.avail_out;
1726 checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
1727 if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
1729 if (r == ARCHIVE_EOF)
1731 xar->stream.next_out = xar->wbuff;
1732 xar->stream.avail_out = sizeof(xar->wbuff);
1734 r = compression_end(&(a->archive), &(xar->stream));
1735 if (r != ARCHIVE_OK)
1737 xar->toc.length = xar->stream.total_out;
1738 xar->toc.compression = GZIP;
1739 checksum_final(&(xar->a_sumwrk), &(xar->toc.a_sum));
1744 xmlFreeTextWriter(writer);
1752 flush_wbuff(struct archive_write *a)
1758 xar = (struct xar *)a->format_data;
1759 s = sizeof(xar->wbuff) - xar->wbuff_remaining;
1760 r = __archive_write_output(a, xar->wbuff, s);
1761 if (r != ARCHIVE_OK)
1763 xar->wbuff_remaining = sizeof(xar->wbuff);
1768 copy_out(struct archive_write *a, uint64_t offset, uint64_t length)
1773 xar = (struct xar *)a->format_data;
1774 if (lseek(xar->temp_fd, offset, SEEK_SET) < 0) {
1775 archive_set_error(&(a->archive), errno, "lseek failed");
1776 return (ARCHIVE_FATAL);
1783 if (length > xar->wbuff_remaining)
1784 rsize = xar->wbuff_remaining;
1786 rsize = (size_t)length;
1787 wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
1788 rs = read(xar->temp_fd, wb, rsize);
1790 archive_set_error(&(a->archive), errno,
1791 "Can't read temporary file(%jd)",
1793 return (ARCHIVE_FATAL);
1796 archive_set_error(&(a->archive), 0,
1797 "Truncated xar archive");
1798 return (ARCHIVE_FATAL);
1800 xar->wbuff_remaining -= rs;
1802 if (xar->wbuff_remaining == 0) {
1804 if (r != ARCHIVE_OK)
1808 return (ARCHIVE_OK);
1812 xar_close(struct archive_write *a)
1819 xar = (struct xar *)a->format_data;
1822 if (xar->root->children.first == NULL)
1823 return (ARCHIVE_OK);
1825 /* Save the length of all file extended attributes and contents. */
1826 length = xar->temp_offset;
1828 /* Connect hardlinked files */
1829 file_connect_hardlink_files(xar);
1833 if (r != ARCHIVE_OK)
1836 * Make the xar header on wbuff(write buffer).
1839 xar->wbuff_remaining = sizeof(xar->wbuff);
1840 archive_be32enc(&wb[0], HEADER_MAGIC);
1841 archive_be16enc(&wb[4], HEADER_SIZE);
1842 archive_be16enc(&wb[6], HEADER_VERSION);
1843 archive_be64enc(&wb[8], xar->toc.length);
1844 archive_be64enc(&wb[16], xar->toc.size);
1845 archive_be32enc(&wb[24], xar->toc.a_sum.alg);
1846 xar->wbuff_remaining -= HEADER_SIZE;
1851 r = copy_out(a, xar->toc.temp_offset, xar->toc.length);
1852 if (r != ARCHIVE_OK)
1855 /* Write the checksum value of the TOC. */
1856 if (xar->toc.a_sum.len) {
1857 if (xar->wbuff_remaining < xar->toc.a_sum.len) {
1859 if (r != ARCHIVE_OK)
1862 wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
1863 memcpy(wb, xar->toc.a_sum.val, xar->toc.a_sum.len);
1864 xar->wbuff_remaining -= xar->toc.a_sum.len;
1868 * Write all file extended attributes and contents.
1870 r = copy_out(a, xar->toc.a_sum.len, length);
1871 if (r != ARCHIVE_OK)
1878 xar_free(struct archive_write *a)
1882 xar = (struct xar *)a->format_data;
1884 /* Close the temporary file. */
1885 if (xar->temp_fd >= 0)
1886 close(xar->temp_fd);
1888 archive_string_free(&(xar->cur_dirstr));
1889 archive_string_free(&(xar->tstr));
1890 archive_string_free(&(xar->vstr));
1891 file_free_hardlinks(xar);
1892 file_free_register(xar);
1893 compression_end(&(a->archive), &(xar->stream));
1896 return (ARCHIVE_OK);
1900 file_cmp_node(const struct archive_rb_node *n1,
1901 const struct archive_rb_node *n2)
1903 const struct file *f1 = (const struct file *)n1;
1904 const struct file *f2 = (const struct file *)n2;
1906 return (strcmp(f1->basename.s, f2->basename.s));
1910 file_cmp_key(const struct archive_rb_node *n, const void *key)
1912 const struct file *f = (const struct file *)n;
1914 return (strcmp(f->basename.s, (const char *)key));
1917 static struct file *
1918 file_new(struct archive_write *a, struct archive_entry *entry)
1921 static const struct archive_rb_tree_ops rb_ops = {
1922 file_cmp_node, file_cmp_key
1925 file = calloc(1, sizeof(*file));
1930 file->entry = archive_entry_clone(entry);
1932 file->entry = archive_entry_new2(&a->archive);
1933 if (file->entry == NULL) {
1937 __archive_rb_tree_init(&(file->rbtree), &rb_ops);
1938 file->children.first = NULL;
1939 file->children.last = &(file->children.first);
1940 file->xattr.first = NULL;
1941 file->xattr.last = &(file->xattr.first);
1942 archive_string_init(&(file->parentdir));
1943 archive_string_init(&(file->basename));
1944 archive_string_init(&(file->symlink));
1945 archive_string_init(&(file->script));
1946 if (entry != NULL && archive_entry_filetype(entry) == AE_IFDIR)
1953 file_free(struct file *file)
1955 struct heap_data *heap, *next_heap;
1957 heap = file->xattr.first;
1958 while (heap != NULL) {
1959 next_heap = heap->next;
1963 archive_string_free(&(file->parentdir));
1964 archive_string_free(&(file->basename));
1965 archive_string_free(&(file->symlink));
1966 archive_string_free(&(file->script));
1967 archive_entry_free(file->entry);
1971 static struct file *
1972 file_create_virtual_dir(struct archive_write *a, struct xar *xar,
1973 const char *pathname)
1977 (void)xar; /* UNUSED */
1979 file = file_new(a, NULL);
1982 archive_entry_set_pathname(file->entry, pathname);
1983 archive_entry_set_mode(file->entry, 0555 | AE_IFDIR);
1992 file_add_child_tail(struct file *parent, struct file *child)
1994 if (!__archive_rb_tree_insert_node(
1995 &(parent->rbtree), (struct archive_rb_node *)child))
1997 child->chnext = NULL;
1998 *parent->children.last = child;
1999 parent->children.last = &(child->chnext);
2000 child->parent = parent;
2005 * Find a entry from `parent'
2007 static struct file *
2008 file_find_child(struct file *parent, const char *child_name)
2012 np = (struct file *)__archive_rb_tree_find_node(
2013 &(parent->rbtree), child_name);
2017 #if defined(_WIN32) || defined(__CYGWIN__)
2019 cleanup_backslash(char *utf8, size_t len)
2022 /* Convert a path-separator from '\' to '/' */
2023 while (*utf8 != '\0' && len) {
2031 #define cleanup_backslash(p, len) /* nop */
2035 * Generate a parent directory name and a base name from a pathname.
2038 file_gen_utility_names(struct archive_write *a, struct file *file)
2042 char *p, *dirname, *slash;
2046 xar = (struct xar *)a->format_data;
2047 archive_string_empty(&(file->parentdir));
2048 archive_string_empty(&(file->basename));
2049 archive_string_empty(&(file->symlink));
2051 if (file->parent == file)/* virtual root */
2052 return (ARCHIVE_OK);
2054 if (archive_entry_pathname_l(file->entry, &pp, &len, xar->sconv)
2056 if (errno == ENOMEM) {
2057 archive_set_error(&a->archive, ENOMEM,
2058 "Can't allocate memory for Pathname");
2059 return (ARCHIVE_FATAL);
2061 archive_set_error(&a->archive,
2062 ARCHIVE_ERRNO_FILE_FORMAT,
2063 "Can't translate pathname '%s' to UTF-8",
2064 archive_entry_pathname(file->entry));
2067 archive_strncpy(&(file->parentdir), pp, len);
2068 len = file->parentdir.length;
2069 p = dirname = file->parentdir.s;
2071 * Convert a path-separator from '\' to '/'
2073 cleanup_backslash(p, len);
2076 * Remove leading '/', '../' and './' elements
2082 } else if (p[0] != '.')
2084 else if (p[1] == '.' && p[2] == '/') {
2087 } else if (p[1] == '/' || (p[1] == '.' && p[2] == '\0')) {
2090 } else if (p[1] == '\0') {
2097 memmove(dirname, p, len+1);
2101 * Remove "/","/." and "/.." elements from tail.
2106 if (len > 0 && p[len-1] == '/') {
2110 if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
2114 if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
2125 /* Convert '//' --> '/' */
2127 else if (p[1] == '.' && p[2] == '/')
2128 /* Convert '/./' --> '/' */
2130 else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
2131 /* Convert 'dir/dir1/../dir2/'
2135 while (rp >= dirname) {
2144 strcpy(dirname, p+4);
2155 if (archive_entry_filetype(file->entry) == AE_IFLNK) {
2157 /* Convert symlink name too. */
2158 if (archive_entry_symlink_l(file->entry, &pp, &len2,
2160 if (errno == ENOMEM) {
2161 archive_set_error(&a->archive, ENOMEM,
2162 "Can't allocate memory for Linkname");
2163 return (ARCHIVE_FATAL);
2165 archive_set_error(&a->archive,
2166 ARCHIVE_ERRNO_FILE_FORMAT,
2167 "Can't translate symlink '%s' to UTF-8",
2168 archive_entry_symlink(file->entry));
2171 archive_strncpy(&(file->symlink), pp, len2);
2172 cleanup_backslash(file->symlink.s, file->symlink.length);
2175 * - Count up directory elements.
2176 * - Find out the position which points the last position of
2177 * path separator('/').
2180 for (; *p != '\0'; p++)
2183 if (slash == NULL) {
2184 /* The pathname doesn't have a parent directory. */
2185 file->parentdir.length = len;
2186 archive_string_copy(&(file->basename), &(file->parentdir));
2187 archive_string_empty(&(file->parentdir));
2188 *file->parentdir.s = '\0';
2192 /* Make a basename from dirname and slash */
2194 file->parentdir.length = slash - dirname;
2195 archive_strcpy(&(file->basename), slash + 1);
2200 get_path_component(char *name, int n, const char *fn)
2205 p = strchr(fn, '/');
2207 if ((l = strlen(fn)) == 0)
2213 memcpy(name, fn, l);
2220 * Add a new entry into the tree.
2223 file_tree(struct archive_write *a, struct file **filepp)
2225 #if defined(_WIN32) && !defined(__CYGWIN__)
2226 char name[_MAX_FNAME];/* Included null terminator size. */
2227 #elif defined(NAME_MAX) && NAME_MAX >= 255
2228 char name[NAME_MAX+1];
2232 struct xar *xar = (struct xar *)a->format_data;
2233 struct file *dent, *file, *np;
2234 struct archive_entry *ent;
2240 if (file->parentdir.length > 0)
2241 fn = p = file->parentdir.s;
2246 * If the path of the parent directory of `file' entry is
2247 * the same as the path of `cur_dirent', add isoent to
2250 if (archive_strlen(&(xar->cur_dirstr))
2251 == archive_strlen(&(file->parentdir)) &&
2252 strcmp(xar->cur_dirstr.s, fn) == 0) {
2253 if (!file_add_child_tail(xar->cur_dirent, file)) {
2254 np = (struct file *)__archive_rb_tree_find_node(
2255 &(xar->cur_dirent->rbtree),
2259 return (ARCHIVE_OK);
2263 l = get_path_component(name, sizeof(name), fn);
2269 archive_set_error(&a->archive,
2271 "A name buffer is too small");
2274 return (ARCHIVE_FATAL);
2277 np = file_find_child(dent, name);
2278 if (np == NULL || fn[0] == '\0')
2281 /* Find next subdirectory. */
2283 /* NOT Directory! */
2284 archive_set_error(&a->archive,
2286 "`%s' is not directory, we cannot insert `%s' ",
2287 archive_entry_pathname(np->entry),
2288 archive_entry_pathname(file->entry));
2291 return (ARCHIVE_FAILED);
2300 * Create virtual parent directories.
2302 while (fn[0] != '\0') {
2304 struct archive_string as;
2306 archive_string_init(&as);
2307 archive_strncat(&as, p, fn - p + l);
2308 if (as.s[as.length-1] == '/') {
2309 as.s[as.length-1] = '\0';
2312 vp = file_create_virtual_dir(a, xar, as.s);
2314 archive_string_free(&as);
2315 archive_set_error(&a->archive, ENOMEM,
2316 "Can't allocate memory");
2319 return (ARCHIVE_FATAL);
2321 archive_string_free(&as);
2322 if (file_gen_utility_names(a, vp) <= ARCHIVE_FAILED)
2323 return (ARCHIVE_FATAL);
2324 file_add_child_tail(dent, vp);
2325 file_register(xar, vp);
2331 l = get_path_component(name, sizeof(name), fn);
2333 archive_string_free(&as);
2334 archive_set_error(&a->archive,
2336 "A name buffer is too small");
2339 return (ARCHIVE_FATAL);
2344 /* Found out the parent directory where isoent can be
2346 xar->cur_dirent = dent;
2347 archive_string_empty(&(xar->cur_dirstr));
2348 archive_string_ensure(&(xar->cur_dirstr),
2349 archive_strlen(&(dent->parentdir)) +
2350 archive_strlen(&(dent->basename)) + 2);
2351 if (archive_strlen(&(dent->parentdir)) +
2352 archive_strlen(&(dent->basename)) == 0)
2353 xar->cur_dirstr.s[0] = 0;
2355 if (archive_strlen(&(dent->parentdir)) > 0) {
2356 archive_string_copy(&(xar->cur_dirstr),
2357 &(dent->parentdir));
2358 archive_strappend_char(&(xar->cur_dirstr), '/');
2360 archive_string_concat(&(xar->cur_dirstr),
2364 if (!file_add_child_tail(dent, file)) {
2365 np = (struct file *)__archive_rb_tree_find_node(
2366 &(dent->rbtree), file->basename.s);
2369 return (ARCHIVE_OK);
2374 * We have already has the entry the filename of which is
2377 if (archive_entry_filetype(np->entry) !=
2378 archive_entry_filetype(file->entry)) {
2379 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2380 "Found duplicate entries `%s' and its file type is "
2382 archive_entry_pathname(np->entry));
2385 return (ARCHIVE_FAILED);
2390 np->entry = file->entry;
2396 return (ARCHIVE_OK);
2400 file_register(struct xar *xar, struct file *file)
2402 file->id = xar->file_idx++;
2404 *xar->file_list.last = file;
2405 xar->file_list.last = &(file->next);
2409 file_init_register(struct xar *xar)
2411 xar->file_list.first = NULL;
2412 xar->file_list.last = &(xar->file_list.first);
2416 file_free_register(struct xar *xar)
2418 struct file *file, *file_next;
2420 file = xar->file_list.first;
2421 while (file != NULL) {
2422 file_next = file->next;
2429 * Register entry to get a hardlink target.
2432 file_register_hardlink(struct archive_write *a, struct file *file)
2434 struct xar *xar = (struct xar *)a->format_data;
2435 struct hardlink *hl;
2436 const char *pathname;
2438 archive_entry_set_nlink(file->entry, 1);
2439 pathname = archive_entry_hardlink(file->entry);
2440 if (pathname == NULL) {
2441 /* This `file` is a hardlink target. */
2442 hl = malloc(sizeof(*hl));
2444 archive_set_error(&a->archive, ENOMEM,
2445 "Can't allocate memory");
2446 return (ARCHIVE_FATAL);
2449 /* A hardlink target must be the first position. */
2450 file->hlnext = NULL;
2451 hl->file_list.first = file;
2452 hl->file_list.last = &(file->hlnext);
2453 __archive_rb_tree_insert_node(&(xar->hardlink_rbtree),
2454 (struct archive_rb_node *)hl);
2456 hl = (struct hardlink *)__archive_rb_tree_find_node(
2457 &(xar->hardlink_rbtree), pathname);
2459 /* Insert `file` entry into the tail. */
2460 file->hlnext = NULL;
2461 *hl->file_list.last = file;
2462 hl->file_list.last = &(file->hlnext);
2465 archive_entry_unset_size(file->entry);
2468 return (ARCHIVE_OK);
2472 * Hardlinked files have to have the same location of extent.
2473 * We have to find out hardlink target entries for entries which
2474 * have a hardlink target name.
2477 file_connect_hardlink_files(struct xar *xar)
2479 struct archive_rb_node *n;
2480 struct hardlink *hl;
2481 struct file *target, *nf;
2483 ARCHIVE_RB_TREE_FOREACH(n, &(xar->hardlink_rbtree)) {
2484 hl = (struct hardlink *)n;
2486 /* The first entry must be a hardlink target. */
2487 target = hl->file_list.first;
2488 archive_entry_set_nlink(target->entry, hl->nlink);
2490 /* It means this file is a hardlink
2492 target->hardlink_target = target;
2493 for (nf = target->hlnext;
2494 nf != NULL; nf = nf->hlnext) {
2495 nf->hardlink_target = target;
2496 archive_entry_set_nlink(nf->entry, hl->nlink);
2502 file_hd_cmp_node(const struct archive_rb_node *n1,
2503 const struct archive_rb_node *n2)
2505 const struct hardlink *h1 = (const struct hardlink *)n1;
2506 const struct hardlink *h2 = (const struct hardlink *)n2;
2508 return (strcmp(archive_entry_pathname(h1->file_list.first->entry),
2509 archive_entry_pathname(h2->file_list.first->entry)));
2513 file_hd_cmp_key(const struct archive_rb_node *n, const void *key)
2515 const struct hardlink *h = (const struct hardlink *)n;
2517 return (strcmp(archive_entry_pathname(h->file_list.first->entry),
2518 (const char *)key));
2523 file_init_hardlinks(struct xar *xar)
2525 static const struct archive_rb_tree_ops rb_ops = {
2526 file_hd_cmp_node, file_hd_cmp_key,
2529 __archive_rb_tree_init(&(xar->hardlink_rbtree), &rb_ops);
2533 file_free_hardlinks(struct xar *xar)
2535 struct archive_rb_node *n, *next;
2537 for (n = ARCHIVE_RB_TREE_MIN(&(xar->hardlink_rbtree)); n;) {
2538 next = __archive_rb_tree_iterate(&(xar->hardlink_rbtree),
2539 n, ARCHIVE_RB_DIR_RIGHT);
2546 checksum_init(struct chksumwork *sumwrk, enum sumalg sum_alg)
2548 sumwrk->alg = sum_alg;
2553 archive_sha1_init(&(sumwrk->sha1ctx));
2556 archive_md5_init(&(sumwrk->md5ctx));
2562 checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
2565 switch (sumwrk->alg) {
2569 archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
2572 archive_md5_update(&(sumwrk->md5ctx), buff, size);
2578 checksum_final(struct chksumwork *sumwrk, struct chksumval *sumval)
2581 switch (sumwrk->alg) {
2586 archive_sha1_final(&(sumwrk->sha1ctx), sumval->val);
2587 sumval->len = SHA1_SIZE;
2590 archive_md5_final(&(sumwrk->md5ctx), sumval->val);
2591 sumval->len = MD5_SIZE;
2594 sumval->alg = sumwrk->alg;
2597 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
2599 compression_unsupported_encoder(struct archive *a,
2600 struct la_zstream *lastrm, const char *name)
2603 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2604 "%s compression not supported on this platform", name);
2606 lastrm->real_stream = NULL;
2607 return (ARCHIVE_FAILED);
2612 compression_init_encoder_gzip(struct archive *a,
2613 struct la_zstream *lastrm, int level, int withheader)
2618 compression_end(a, lastrm);
2619 strm = calloc(1, sizeof(*strm));
2621 archive_set_error(a, ENOMEM,
2622 "Can't allocate memory for gzip stream");
2623 return (ARCHIVE_FATAL);
2625 /* zlib.h is not const-correct, so we need this one bit
2626 * of ugly hackery to convert a const * pointer to
2627 * a non-const pointer. */
2628 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2629 strm->avail_in = lastrm->avail_in;
2630 strm->total_in = (uLong)lastrm->total_in;
2631 strm->next_out = lastrm->next_out;
2632 strm->avail_out = lastrm->avail_out;
2633 strm->total_out = (uLong)lastrm->total_out;
2634 if (deflateInit2(strm, level, Z_DEFLATED,
2635 (withheader)?15:-15,
2636 8, Z_DEFAULT_STRATEGY) != Z_OK) {
2638 lastrm->real_stream = NULL;
2639 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2640 "Internal error initializing compression library");
2641 return (ARCHIVE_FATAL);
2643 lastrm->real_stream = strm;
2645 lastrm->code = compression_code_gzip;
2646 lastrm->end = compression_end_gzip;
2647 return (ARCHIVE_OK);
2651 compression_code_gzip(struct archive *a,
2652 struct la_zstream *lastrm, enum la_zaction action)
2657 strm = (z_stream *)lastrm->real_stream;
2658 /* zlib.h is not const-correct, so we need this one bit
2659 * of ugly hackery to convert a const * pointer to
2660 * a non-const pointer. */
2661 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2662 strm->avail_in = lastrm->avail_in;
2663 strm->total_in = (uLong)lastrm->total_in;
2664 strm->next_out = lastrm->next_out;
2665 strm->avail_out = lastrm->avail_out;
2666 strm->total_out = (uLong)lastrm->total_out;
2668 (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
2669 lastrm->next_in = strm->next_in;
2670 lastrm->avail_in = strm->avail_in;
2671 lastrm->total_in = strm->total_in;
2672 lastrm->next_out = strm->next_out;
2673 lastrm->avail_out = strm->avail_out;
2674 lastrm->total_out = strm->total_out;
2677 return (ARCHIVE_OK);
2679 return (ARCHIVE_EOF);
2681 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2682 "GZip compression failed:"
2683 " deflate() call returned status %d", r);
2684 return (ARCHIVE_FATAL);
2689 compression_end_gzip(struct archive *a, struct la_zstream *lastrm)
2694 strm = (z_stream *)lastrm->real_stream;
2695 r = deflateEnd(strm);
2697 lastrm->real_stream = NULL;
2700 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2701 "Failed to clean up compressor");
2702 return (ARCHIVE_FATAL);
2704 return (ARCHIVE_OK);
2707 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
2709 compression_init_encoder_bzip2(struct archive *a,
2710 struct la_zstream *lastrm, int level)
2715 compression_end(a, lastrm);
2716 strm = calloc(1, sizeof(*strm));
2718 archive_set_error(a, ENOMEM,
2719 "Can't allocate memory for bzip2 stream");
2720 return (ARCHIVE_FATAL);
2722 /* bzlib.h is not const-correct, so we need this one bit
2723 * of ugly hackery to convert a const * pointer to
2724 * a non-const pointer. */
2725 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2726 strm->avail_in = lastrm->avail_in;
2727 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2728 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2729 strm->next_out = (char *)lastrm->next_out;
2730 strm->avail_out = lastrm->avail_out;
2731 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2732 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2733 if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
2735 lastrm->real_stream = NULL;
2736 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2737 "Internal error initializing compression library");
2738 return (ARCHIVE_FATAL);
2740 lastrm->real_stream = strm;
2742 lastrm->code = compression_code_bzip2;
2743 lastrm->end = compression_end_bzip2;
2744 return (ARCHIVE_OK);
2748 compression_code_bzip2(struct archive *a,
2749 struct la_zstream *lastrm, enum la_zaction action)
2754 strm = (bz_stream *)lastrm->real_stream;
2755 /* bzlib.h is not const-correct, so we need this one bit
2756 * of ugly hackery to convert a const * pointer to
2757 * a non-const pointer. */
2758 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2759 strm->avail_in = lastrm->avail_in;
2760 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2761 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2762 strm->next_out = (char *)lastrm->next_out;
2763 strm->avail_out = lastrm->avail_out;
2764 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2765 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2766 r = BZ2_bzCompress(strm,
2767 (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
2768 lastrm->next_in = (const unsigned char *)strm->next_in;
2769 lastrm->avail_in = strm->avail_in;
2771 (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
2772 + (uint64_t)(uint32_t)strm->total_in_lo32;
2773 lastrm->next_out = (unsigned char *)strm->next_out;
2774 lastrm->avail_out = strm->avail_out;
2776 (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
2777 + (uint64_t)(uint32_t)strm->total_out_lo32;
2779 case BZ_RUN_OK: /* Non-finishing */
2780 case BZ_FINISH_OK: /* Finishing: There's more work to do */
2781 return (ARCHIVE_OK);
2782 case BZ_STREAM_END: /* Finishing: all done */
2783 /* Only occurs in finishing case */
2784 return (ARCHIVE_EOF);
2786 /* Any other return value indicates an error */
2787 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2788 "Bzip2 compression failed:"
2789 " BZ2_bzCompress() call returned status %d", r);
2790 return (ARCHIVE_FATAL);
2795 compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
2800 strm = (bz_stream *)lastrm->real_stream;
2801 r = BZ2_bzCompressEnd(strm);
2803 lastrm->real_stream = NULL;
2806 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2807 "Failed to clean up compressor");
2808 return (ARCHIVE_FATAL);
2810 return (ARCHIVE_OK);
2815 compression_init_encoder_bzip2(struct archive *a,
2816 struct la_zstream *lastrm, int level)
2819 (void) level; /* UNUSED */
2821 compression_end(a, lastrm);
2822 return (compression_unsupported_encoder(a, lastrm, "bzip2"));
2826 #if defined(HAVE_LZMA_H)
2828 compression_init_encoder_lzma(struct archive *a,
2829 struct la_zstream *lastrm, int level)
2831 static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
2833 lzma_options_lzma lzma_opt;
2837 compression_end(a, lastrm);
2838 if (lzma_lzma_preset(&lzma_opt, level)) {
2839 lastrm->real_stream = NULL;
2840 archive_set_error(a, ENOMEM,
2841 "Internal error initializing compression library");
2842 return (ARCHIVE_FATAL);
2844 strm = calloc(1, sizeof(*strm));
2846 archive_set_error(a, ENOMEM,
2847 "Can't allocate memory for lzma stream");
2848 return (ARCHIVE_FATAL);
2850 *strm = lzma_init_data;
2851 r = lzma_alone_encoder(strm, &lzma_opt);
2854 lastrm->real_stream = strm;
2856 lastrm->code = compression_code_lzma;
2857 lastrm->end = compression_end_lzma;
2860 case LZMA_MEM_ERROR:
2862 lastrm->real_stream = NULL;
2863 archive_set_error(a, ENOMEM,
2864 "Internal error initializing compression library: "
2865 "Cannot allocate memory");
2870 lastrm->real_stream = NULL;
2871 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2872 "Internal error initializing compression library: "
2873 "It's a bug in liblzma");
2881 compression_init_encoder_xz(struct archive *a,
2882 struct la_zstream *lastrm, int level, int threads)
2884 static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
2886 lzma_filter *lzmafilters;
2887 lzma_options_lzma lzma_opt;
2889 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
2893 (void)threads; /* UNUSED (if multi-threaded LZMA library not avail) */
2896 compression_end(a, lastrm);
2897 strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
2899 archive_set_error(a, ENOMEM,
2900 "Can't allocate memory for xz stream");
2901 return (ARCHIVE_FATAL);
2903 lzmafilters = (lzma_filter *)(strm+1);
2906 if (lzma_lzma_preset(&lzma_opt, level)) {
2908 lastrm->real_stream = NULL;
2909 archive_set_error(a, ENOMEM,
2910 "Internal error initializing compression library");
2911 return (ARCHIVE_FATAL);
2913 lzmafilters[0].id = LZMA_FILTER_LZMA2;
2914 lzmafilters[0].options = &lzma_opt;
2915 lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
2917 *strm = lzma_init_data;
2918 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
2920 memset(&mt_options, 0, sizeof(mt_options));
2921 mt_options.threads = threads;
2922 mt_options.timeout = 300;
2923 mt_options.filters = lzmafilters;
2924 mt_options.check = LZMA_CHECK_CRC64;
2925 r = lzma_stream_encoder_mt(strm, &mt_options);
2928 r = lzma_stream_encoder(strm, lzmafilters, LZMA_CHECK_CRC64);
2931 lastrm->real_stream = strm;
2933 lastrm->code = compression_code_lzma;
2934 lastrm->end = compression_end_lzma;
2937 case LZMA_MEM_ERROR:
2939 lastrm->real_stream = NULL;
2940 archive_set_error(a, ENOMEM,
2941 "Internal error initializing compression library: "
2942 "Cannot allocate memory");
2947 lastrm->real_stream = NULL;
2948 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2949 "Internal error initializing compression library: "
2950 "It's a bug in liblzma");
2958 compression_code_lzma(struct archive *a,
2959 struct la_zstream *lastrm, enum la_zaction action)
2964 strm = (lzma_stream *)lastrm->real_stream;
2965 strm->next_in = lastrm->next_in;
2966 strm->avail_in = lastrm->avail_in;
2967 strm->total_in = lastrm->total_in;
2968 strm->next_out = lastrm->next_out;
2969 strm->avail_out = lastrm->avail_out;
2970 strm->total_out = lastrm->total_out;
2972 (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
2973 lastrm->next_in = strm->next_in;
2974 lastrm->avail_in = strm->avail_in;
2975 lastrm->total_in = strm->total_in;
2976 lastrm->next_out = strm->next_out;
2977 lastrm->avail_out = strm->avail_out;
2978 lastrm->total_out = strm->total_out;
2981 /* Non-finishing case */
2982 return (ARCHIVE_OK);
2983 case LZMA_STREAM_END:
2984 /* This return can only occur in finishing case. */
2985 return (ARCHIVE_EOF);
2986 case LZMA_MEMLIMIT_ERROR:
2987 archive_set_error(a, ENOMEM,
2988 "lzma compression error:"
2989 " %ju MiB would have been needed",
2990 (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
2992 return (ARCHIVE_FATAL);
2994 /* Any other return value indicates an error */
2995 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2996 "lzma compression failed:"
2997 " lzma_code() call returned status %d", r);
2998 return (ARCHIVE_FATAL);
3003 compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
3007 (void)a; /* UNUSED */
3008 strm = (lzma_stream *)lastrm->real_stream;
3012 lastrm->real_stream = NULL;
3013 return (ARCHIVE_OK);
3017 compression_init_encoder_lzma(struct archive *a,
3018 struct la_zstream *lastrm, int level)
3021 (void) level; /* UNUSED */
3023 compression_end(a, lastrm);
3024 return (compression_unsupported_encoder(a, lastrm, "lzma"));
3027 compression_init_encoder_xz(struct archive *a,
3028 struct la_zstream *lastrm, int level, int threads)
3031 (void) level; /* UNUSED */
3032 (void) threads; /* UNUSED */
3034 compression_end(a, lastrm);
3035 return (compression_unsupported_encoder(a, lastrm, "xz"));
3040 xar_compression_init_encoder(struct archive_write *a)
3045 xar = (struct xar *)a->format_data;
3046 switch (xar->opt_compression) {
3048 r = compression_init_encoder_gzip(
3049 &(a->archive), &(xar->stream),
3050 xar->opt_compression_level, 1);
3053 r = compression_init_encoder_bzip2(
3054 &(a->archive), &(xar->stream),
3055 xar->opt_compression_level);
3058 r = compression_init_encoder_lzma(
3059 &(a->archive), &(xar->stream),
3060 xar->opt_compression_level);
3063 r = compression_init_encoder_xz(
3064 &(a->archive), &(xar->stream),
3065 xar->opt_compression_level, xar->opt_threads);
3071 if (r == ARCHIVE_OK) {
3072 xar->stream.total_in = 0;
3073 xar->stream.next_out = xar->wbuff;
3074 xar->stream.avail_out = sizeof(xar->wbuff);
3075 xar->stream.total_out = 0;
3082 compression_code(struct archive *a, struct la_zstream *lastrm,
3083 enum la_zaction action)
3086 return (lastrm->code(a, lastrm, action));
3087 return (ARCHIVE_OK);
3091 compression_end(struct archive *a, struct la_zstream *lastrm)
3094 return (lastrm->end(a, lastrm));
3095 return (ARCHIVE_OK);
3100 save_xattrs(struct archive_write *a, struct file *file)
3105 struct heap_data *heap;
3109 xar = (struct xar *)a->format_data;
3110 count = archive_entry_xattr_reset(file->entry);
3112 return (ARCHIVE_OK);
3114 archive_entry_xattr_next(file->entry,
3115 &name, &value, &size);
3116 checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
3117 checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
3119 heap = calloc(1, sizeof(*heap));
3121 archive_set_error(&a->archive, ENOMEM,
3122 "Can't allocate memory for xattr");
3123 return (ARCHIVE_FATAL);
3125 heap->id = file->ea_idx++;
3126 heap->temp_offset = xar->temp_offset;
3127 heap->size = size;/* save a extracted size */
3128 heap->compression = xar->opt_compression;
3129 /* Get a extracted sumcheck value. */
3130 checksum_update(&(xar->e_sumwrk), value, size);
3131 checksum_final(&(xar->e_sumwrk), &(heap->e_sum));
3134 * Not compression to xattr is simple way.
3136 if (heap->compression == NONE) {
3137 checksum_update(&(xar->a_sumwrk), value, size);
3138 checksum_final(&(xar->a_sumwrk), &(heap->a_sum));
3139 if (write_to_temp(a, value, size)
3142 return (ARCHIVE_FATAL);
3144 heap->length = size;
3145 /* Add heap to the tail of file->xattr. */
3147 *file->xattr.last = heap;
3148 file->xattr.last = &(heap->next);
3154 * Init compression library.
3156 r = xar_compression_init_encoder(a);
3157 if (r != ARCHIVE_OK) {
3159 return (ARCHIVE_FATAL);
3162 xar->stream.next_in = (const unsigned char *)value;
3163 xar->stream.avail_in = size;
3165 r = compression_code(&(a->archive),
3166 &(xar->stream), ARCHIVE_Z_FINISH);
3167 if (r != ARCHIVE_OK && r != ARCHIVE_EOF) {
3169 return (ARCHIVE_FATAL);
3171 size = sizeof(xar->wbuff) - xar->stream.avail_out;
3172 checksum_update(&(xar->a_sumwrk),
3174 if (write_to_temp(a, xar->wbuff, size)
3176 return (ARCHIVE_FATAL);
3177 if (r == ARCHIVE_OK) {
3178 xar->stream.next_out = xar->wbuff;
3179 xar->stream.avail_out = sizeof(xar->wbuff);
3181 checksum_final(&(xar->a_sumwrk),
3183 heap->length = xar->stream.total_out;
3184 /* Add heap to the tail of file->xattr. */
3186 *file->xattr.last = heap;
3187 file->xattr.last = &(heap->next);
3191 /* Clean up compression library. */
3192 r = compression_end(&(a->archive), &(xar->stream));
3193 if (r != ARCHIVE_OK)
3194 return (ARCHIVE_FATAL);
3196 return (ARCHIVE_OK);
3200 getalgsize(enum sumalg sumalg)
3214 getalgname(enum sumalg sumalg)
3227 #endif /* Support xar format */