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) {
500 return (ARCHIVE_FAILED);
501 xar->opt_threads = (int)strtoul(value, NULL, 10);
502 if (xar->opt_threads == 0 && errno != 0) {
503 xar->opt_threads = 1;
504 archive_set_error(&(a->archive),
506 "Illegal value `%s'",
508 return (ARCHIVE_FAILED);
510 if (xar->opt_threads == 0) {
511 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
512 xar->opt_threads = lzma_cputhreads();
514 xar->opt_threads = 1;
519 /* Note: The "warn" return is just to inform the options
520 * supervisor that we didn't handle it. It will generate
521 * a suitable error if no one used this option. */
522 return (ARCHIVE_WARN);
526 xar_write_header(struct archive_write *a, struct archive_entry *entry)
530 struct archive_entry *file_entry;
533 xar = (struct xar *)a->format_data;
534 xar->cur_file = NULL;
535 xar->bytes_remaining = 0;
537 if (xar->sconv == NULL) {
538 xar->sconv = archive_string_conversion_to_charset(
539 &a->archive, "UTF-8", 1);
540 if (xar->sconv == NULL)
541 return (ARCHIVE_FATAL);
544 file = file_new(a, entry);
546 archive_set_error(&a->archive, ENOMEM,
547 "Can't allocate data");
548 return (ARCHIVE_FATAL);
550 r2 = file_gen_utility_names(a, file);
551 if (r2 < ARCHIVE_WARN)
555 * Ignore a path which looks like the top of directory name
556 * since we have already made the root directory of an Xar archive.
558 if (archive_strlen(&(file->parentdir)) == 0 &&
559 archive_strlen(&(file->basename)) == 0) {
564 /* Add entry into tree */
565 file_entry = file->entry;
566 r = file_tree(a, &file);
569 /* There is the same file in tree and
570 * the current file is older than the file in tree.
571 * So we don't need the current file data anymore. */
572 if (file->entry != file_entry)
575 file_register(xar, file);
577 /* A virtual file, which is a directory, does not have
578 * any contents and we won't store it into a archive
579 * file other than its name. */
584 * Prepare to save the contents of the file.
586 if (xar->temp_fd == -1) {
588 xar->temp_offset = 0;
589 xar->temp_fd = __archive_mktemp(NULL);
590 if (xar->temp_fd < 0) {
591 archive_set_error(&a->archive, errno,
592 "Couldn't create temporary file");
593 return (ARCHIVE_FATAL);
595 algsize = getalgsize(xar->opt_toc_sumalg);
597 if (lseek(xar->temp_fd, algsize, SEEK_SET) < 0) {
598 archive_set_error(&(a->archive), errno,
600 return (ARCHIVE_FATAL);
602 xar->temp_offset = algsize;
606 if (archive_entry_hardlink(file->entry) == NULL) {
607 r = save_xattrs(a, file);
609 return (ARCHIVE_FATAL);
612 /* Non regular files contents are unneeded to be saved to
613 * a temporary file. */
614 if (archive_entry_filetype(file->entry) != AE_IFREG)
618 * Set the current file to cur_file to read its contents.
620 xar->cur_file = file;
622 if (archive_entry_nlink(file->entry) > 1) {
623 r = file_register_hardlink(a, file);
626 if (archive_entry_hardlink(file->entry) != NULL) {
627 archive_entry_unset_size(file->entry);
632 /* Save a offset of current file in temporary file. */
633 file->data.temp_offset = xar->temp_offset;
634 file->data.size = archive_entry_size(file->entry);
635 file->data.compression = xar->opt_compression;
636 xar->bytes_remaining = archive_entry_size(file->entry);
637 checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
638 checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
639 r = xar_compression_init_encoder(a);
648 write_to_temp(struct archive_write *a, const void *buff, size_t s)
651 const unsigned char *p;
654 xar = (struct xar *)a->format_data;
655 p = (const unsigned char *)buff;
657 ws = write(xar->temp_fd, p, s);
659 archive_set_error(&(a->archive), errno,
660 "fwrite function failed");
661 return (ARCHIVE_FATAL);
665 xar->temp_offset += ws;
671 xar_write_data(struct archive_write *a, const void *buff, size_t s)
678 xar = (struct xar *)a->format_data;
680 if (s > xar->bytes_remaining)
681 s = (size_t)xar->bytes_remaining;
682 if (s == 0 || xar->cur_file == NULL)
684 if (xar->cur_file->data.compression == NONE) {
685 checksum_update(&(xar->e_sumwrk), buff, s);
686 checksum_update(&(xar->a_sumwrk), buff, s);
689 xar->stream.next_in = (const unsigned char *)buff;
690 xar->stream.avail_in = s;
691 if (xar->bytes_remaining > s)
694 run = ARCHIVE_Z_FINISH;
695 /* Compress file data. */
696 r = compression_code(&(a->archive), &(xar->stream), run);
697 if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
698 return (ARCHIVE_FATAL);
699 rsize = s - xar->stream.avail_in;
700 checksum_update(&(xar->e_sumwrk), buff, rsize);
701 size = sizeof(xar->wbuff) - xar->stream.avail_out;
702 checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
704 #if !defined(_WIN32) || defined(__CYGWIN__)
705 if (xar->bytes_remaining ==
706 (uint64_t)archive_entry_size(xar->cur_file->entry)) {
708 * Get the path of a shell script if so.
710 const unsigned char *b = (const unsigned char *)buff;
712 archive_string_empty(&(xar->cur_file->script));
713 if (rsize > 2 && b[0] == '#' && b[1] == '!') {
720 if ((rsize - off) > PATH_MAX)
721 end = off + PATH_MAX;
725 /* Find the end of a script path. */
726 for (i = off; i < end && b[i] != '\0' &&
727 b[i] != '\n' && b[i] != '\r' &&
728 b[i] != ' ' && b[i] != '\t'; i++)
730 archive_strncpy(&(xar->cur_file->script), b + off,
736 if (xar->cur_file->data.compression == NONE) {
737 if (write_to_temp(a, buff, size) != ARCHIVE_OK)
738 return (ARCHIVE_FATAL);
740 if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
741 return (ARCHIVE_FATAL);
743 xar->bytes_remaining -= rsize;
744 xar->cur_file->data.length += size;
750 xar_finish_entry(struct archive_write *a)
757 xar = (struct xar *)a->format_data;
758 if (xar->cur_file == NULL)
761 while (xar->bytes_remaining > 0) {
762 s = (size_t)xar->bytes_remaining;
763 if (s > a->null_length)
765 w = xar_write_data(a, a->nulls, s);
767 xar->bytes_remaining -= w;
771 file = xar->cur_file;
772 checksum_final(&(xar->e_sumwrk), &(file->data.e_sum));
773 checksum_final(&(xar->a_sumwrk), &(file->data.a_sum));
774 xar->cur_file = NULL;
780 xmlwrite_string_attr(struct archive_write *a, xmlTextWriterPtr writer,
781 const char *key, const char *value,
782 const char *attrkey, const char *attrvalue)
786 r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(key));
788 archive_set_error(&a->archive,
790 "xmlTextWriterStartElement() failed: %d", r);
791 return (ARCHIVE_FATAL);
793 if (attrkey != NULL && attrvalue != NULL) {
794 r = xmlTextWriterWriteAttribute(writer,
795 BAD_CAST_CONST(attrkey), BAD_CAST_CONST(attrvalue));
797 archive_set_error(&a->archive,
799 "xmlTextWriterWriteAttribute() failed: %d", r);
800 return (ARCHIVE_FATAL);
804 r = xmlTextWriterWriteString(writer, BAD_CAST_CONST(value));
806 archive_set_error(&a->archive,
808 "xmlTextWriterWriteString() failed: %d", r);
809 return (ARCHIVE_FATAL);
812 r = xmlTextWriterEndElement(writer);
814 archive_set_error(&a->archive,
816 "xmlTextWriterEndElement() failed: %d", r);
817 return (ARCHIVE_FATAL);
823 xmlwrite_string(struct archive_write *a, xmlTextWriterPtr writer,
824 const char *key, const char *value)
831 r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(key));
833 archive_set_error(&a->archive,
835 "xmlTextWriterStartElement() failed: %d", r);
836 return (ARCHIVE_FATAL);
839 r = xmlTextWriterWriteString(writer, BAD_CAST_CONST(value));
841 archive_set_error(&a->archive,
843 "xmlTextWriterWriteString() failed: %d", r);
844 return (ARCHIVE_FATAL);
847 r = xmlTextWriterEndElement(writer);
849 archive_set_error(&a->archive,
851 "xmlTextWriterEndElement() failed: %d", r);
852 return (ARCHIVE_FATAL);
858 xmlwrite_fstring(struct archive_write *a, xmlTextWriterPtr writer,
859 const char *key, const char *fmt, ...)
864 xar = (struct xar *)a->format_data;
866 archive_string_empty(&xar->vstr);
867 archive_string_vsprintf(&xar->vstr, fmt, ap);
869 return (xmlwrite_string(a, writer, key, xar->vstr.s));
873 xmlwrite_time(struct archive_write *a, xmlTextWriterPtr writer,
874 const char *key, time_t t, int z)
879 #if defined(HAVE_GMTIME_R)
881 #elif defined(HAVE__GMTIME64_S)
882 _gmtime64_s(&tm, &t);
884 memcpy(&tm, gmtime(&t), sizeof(tm));
886 memset(×tr, 0, sizeof(timestr));
887 /* Do not use %F and %T for portability. */
888 strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &tm);
890 strcat(timestr, "Z");
891 return (xmlwrite_string(a, writer, key, timestr));
895 xmlwrite_mode(struct archive_write *a, xmlTextWriterPtr writer,
896 const char *key, mode_t mode)
901 ms[1] = '0' + ((mode >> 6) & 07);
902 ms[2] = '0' + ((mode >> 3) & 07);
903 ms[3] = '0' + (mode & 07);
906 return (xmlwrite_string(a, writer, key, ms));
910 xmlwrite_sum(struct archive_write *a, xmlTextWriterPtr writer,
911 const char *key, struct chksumval *sum)
915 char buff[MAX_SUM_SIZE*2 + 1];
921 algname = getalgname(sum->alg);
922 algsize = getalgsize(sum->alg);
923 if (algname != NULL) {
924 const char *hex = "0123456789abcdef";
927 for (i = 0; i < algsize; i++) {
928 *p++ = hex[(*s >> 4)];
929 *p++ = hex[(*s & 0x0f)];
933 r = xmlwrite_string_attr(a, writer,
937 return (ARCHIVE_FATAL);
944 xmlwrite_heap(struct archive_write *a, xmlTextWriterPtr writer,
945 struct heap_data *heap)
950 r = xmlwrite_fstring(a, writer, "length", "%ju", heap->length);
952 return (ARCHIVE_FATAL);
953 r = xmlwrite_fstring(a, writer, "offset", "%ju", heap->temp_offset);
955 return (ARCHIVE_FATAL);
956 r = xmlwrite_fstring(a, writer, "size", "%ju", heap->size);
958 return (ARCHIVE_FATAL);
959 switch (heap->compression) {
961 encname = "application/x-gzip"; break;
963 encname = "application/x-bzip2"; break;
965 encname = "application/x-lzma"; break;
967 encname = "application/x-xz"; break;
969 encname = "application/octet-stream"; break;
971 r = xmlwrite_string_attr(a, writer, "encoding", NULL,
974 return (ARCHIVE_FATAL);
975 r = xmlwrite_sum(a, writer, "archived-checksum", &(heap->a_sum));
977 return (ARCHIVE_FATAL);
978 r = xmlwrite_sum(a, writer, "extracted-checksum", &(heap->e_sum));
980 return (ARCHIVE_FATAL);
985 * xar utility records fflags as following xml elements:
995 * If xar is running on BSD platform, records <flags>..</flags>;
996 * if xar is running on linux platform, records <ext2>..</ext2>;
997 * otherwise does not record.
999 * Our implements records both <flags> and <ext2> if it's necessary.
1002 make_fflags_entry(struct archive_write *a, xmlTextWriterPtr writer,
1003 const char *element, const char *fflags_text)
1005 static const struct flagentry {
1007 const char *xarname;
1010 { "sappnd", "SystemAppend"},
1011 { "sappend", "SystemAppend"},
1012 { "arch", "SystemArchived"},
1013 { "archived", "SystemArchived"},
1014 { "schg", "SystemImmutable"},
1015 { "schange", "SystemImmutable"},
1016 { "simmutable", "SystemImmutable"},
1017 { "nosunlnk", "SystemNoUnlink"},
1018 { "nosunlink", "SystemNoUnlink"},
1019 { "snapshot", "SystemSnapshot"},
1020 { "uappnd", "UserAppend"},
1021 { "uappend", "UserAppend"},
1022 { "uchg", "UserImmutable"},
1023 { "uchange", "UserImmutable"},
1024 { "uimmutable", "UserImmutable"},
1025 { "nodump", "UserNoDump"},
1026 { "noopaque", "UserOpaque"},
1027 { "nouunlnk", "UserNoUnlink"},
1028 { "nouunlink", "UserNoUnlink"},
1032 { "sappnd", "AppendOnly"},
1033 { "sappend", "AppendOnly"},
1034 { "schg", "Immutable"},
1035 { "schange", "Immutable"},
1036 { "simmutable", "Immutable"},
1037 { "nodump", "NoDump"},
1038 { "nouunlnk", "Undelete"},
1039 { "nouunlink", "Undelete"},
1040 { "btree", "BTree"},
1041 { "comperr", "CompError"},
1042 { "compress", "Compress"},
1043 { "noatime", "NoAtime"},
1044 { "compdirty", "CompDirty"},
1045 { "comprblk", "CompBlock"},
1046 { "dirsync", "DirSync"},
1047 { "hashidx", "HashIndexed"},
1048 { "imagic", "iMagic"},
1049 { "journal", "Journaled"},
1050 { "securedeletion", "SecureDeletion"},
1051 { "sync", "Synchronous"},
1052 { "notail", "NoTail"},
1053 { "topdir", "TopDir"},
1054 { "reserved", "Reserved"},
1057 const struct flagentry *fe, *flagentry;
1058 #define FLAGENTRY_MAXSIZE ((sizeof(flagbsd)+sizeof(flagext2))/sizeof(flagbsd))
1059 const struct flagentry *avail[FLAGENTRY_MAXSIZE];
1063 if (strcmp(element, "ext2") == 0)
1064 flagentry = flagext2;
1066 flagentry = flagbsd;
1072 cp = strchr(p, ',');
1076 for (fe = flagentry; fe->name != NULL; fe++) {
1077 if (fe->name[cp - p] != '\0'
1078 || p[0] != fe->name[0])
1080 if (strncmp(p, fe->name, cp - p) == 0) {
1089 } while (p != NULL);
1092 r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(element));
1094 archive_set_error(&a->archive,
1096 "xmlTextWriterStartElement() failed: %d", r);
1097 return (ARCHIVE_FATAL);
1099 for (i = 0; i < n; i++) {
1100 r = xmlwrite_string(a, writer,
1101 avail[i]->xarname, NULL);
1102 if (r != ARCHIVE_OK)
1106 r = xmlTextWriterEndElement(writer);
1108 archive_set_error(&a->archive,
1110 "xmlTextWriterEndElement() failed: %d", r);
1111 return (ARCHIVE_FATAL);
1114 return (ARCHIVE_OK);
1118 make_file_entry(struct archive_write *a, xmlTextWriterPtr writer,
1122 const char *filetype, *filelink, *fflags;
1123 struct archive_string linkto;
1124 struct heap_data *heap;
1130 xar = (struct xar *)a->format_data;
1134 * Make a file name entry, "<name>".
1136 l = ll = archive_strlen(&(file->basename));
1139 archive_set_error(&a->archive, ENOMEM,
1140 "Can't allocate memory");
1141 return (ARCHIVE_FATAL);
1143 r = UTF8Toisolat1(tmp, &l, BAD_CAST(file->basename.s), &ll);
1146 r = xmlTextWriterStartElement(writer, BAD_CAST("name"));
1148 archive_set_error(&a->archive,
1150 "xmlTextWriterStartElement() failed: %d", r);
1151 return (ARCHIVE_FATAL);
1153 r = xmlTextWriterWriteAttribute(writer,
1154 BAD_CAST("enctype"), BAD_CAST("base64"));
1156 archive_set_error(&a->archive,
1158 "xmlTextWriterWriteAttribute() failed: %d", r);
1159 return (ARCHIVE_FATAL);
1161 r = xmlTextWriterWriteBase64(writer, file->basename.s,
1162 0, archive_strlen(&(file->basename)));
1164 archive_set_error(&a->archive,
1166 "xmlTextWriterWriteBase64() failed: %d", r);
1167 return (ARCHIVE_FATAL);
1169 r = xmlTextWriterEndElement(writer);
1171 archive_set_error(&a->archive,
1173 "xmlTextWriterEndElement() failed: %d", r);
1174 return (ARCHIVE_FATAL);
1177 r = xmlwrite_string(a, writer, "name", file->basename.s);
1179 return (ARCHIVE_FATAL);
1183 * Make a file type entry, "<type>".
1186 archive_string_init(&linkto);
1187 switch (archive_entry_filetype(file->entry)) {
1189 filetype = "directory"; break;
1191 filetype = "symlink"; break;
1193 filetype = "character special"; break;
1195 filetype = "block special"; break;
1197 filetype = "socket"; break;
1199 filetype = "fifo"; break;
1202 if (file->hardlink_target != NULL) {
1203 filetype = "hardlink";
1205 if (file->hardlink_target == file)
1206 archive_strcpy(&linkto, "original");
1208 archive_string_sprintf(&linkto, "%d",
1209 file->hardlink_target->id);
1214 r = xmlwrite_string_attr(a, writer, "type", filetype,
1215 filelink, linkto.s);
1216 archive_string_free(&linkto);
1218 return (ARCHIVE_FATAL);
1221 * On a virtual directory, we record "name" and "type" only.
1224 return (ARCHIVE_OK);
1226 switch (archive_entry_filetype(file->entry)) {
1229 * xar utility has checked a file type, which
1230 * a symbolic-link file has referenced.
1232 * <link type="directory">../ref/</link>
1233 * The symlink target file is "../ref/" and its
1234 * file type is a directory.
1236 * <link type="file">../f</link>
1237 * The symlink target file is "../f" and its
1238 * file type is a regular file.
1240 * But our implementation cannot do it, and then we
1241 * always record that a attribute "type" is "broken",
1243 * <link type="broken">foo/bar</link>
1244 * It means "foo/bar" is not reachable.
1246 r = xmlwrite_string_attr(a, writer, "link",
1250 return (ARCHIVE_FATAL);
1254 r = xmlTextWriterStartElement(writer, BAD_CAST("device"));
1256 archive_set_error(&a->archive,
1258 "xmlTextWriterStartElement() failed: %d", r);
1259 return (ARCHIVE_FATAL);
1261 r = xmlwrite_fstring(a, writer, "major",
1262 "%d", archive_entry_rdevmajor(file->entry));
1264 return (ARCHIVE_FATAL);
1265 r = xmlwrite_fstring(a, writer, "minor",
1266 "%d", archive_entry_rdevminor(file->entry));
1268 return (ARCHIVE_FATAL);
1269 r = xmlTextWriterEndElement(writer);
1271 archive_set_error(&a->archive,
1273 "xmlTextWriterEndElement() failed: %d", r);
1274 return (ARCHIVE_FATAL);
1282 * Make a inode entry, "<inode>".
1284 r = xmlwrite_fstring(a, writer, "inode",
1285 "%jd", archive_entry_ino64(file->entry));
1287 return (ARCHIVE_FATAL);
1288 if (archive_entry_dev(file->entry) != 0) {
1289 r = xmlwrite_fstring(a, writer, "deviceno",
1290 "%d", archive_entry_dev(file->entry));
1292 return (ARCHIVE_FATAL);
1296 * Make a file mode entry, "<mode>".
1298 r = xmlwrite_mode(a, writer, "mode",
1299 archive_entry_mode(file->entry));
1301 return (ARCHIVE_FATAL);
1304 * Make a user entry, "<uid>" and "<user>.
1306 r = xmlwrite_fstring(a, writer, "uid",
1307 "%d", archive_entry_uid(file->entry));
1309 return (ARCHIVE_FATAL);
1310 r = archive_entry_uname_l(file->entry, &p, &len, xar->sconv);
1312 if (errno == ENOMEM) {
1313 archive_set_error(&a->archive, ENOMEM,
1314 "Can't allocate memory for Uname");
1315 return (ARCHIVE_FATAL);
1317 archive_set_error(&a->archive,
1318 ARCHIVE_ERRNO_FILE_FORMAT,
1319 "Can't translate uname '%s' to UTF-8",
1320 archive_entry_uname(file->entry));
1324 r = xmlwrite_string(a, writer, "user", p);
1326 return (ARCHIVE_FATAL);
1330 * Make a group entry, "<gid>" and "<group>.
1332 r = xmlwrite_fstring(a, writer, "gid",
1333 "%d", archive_entry_gid(file->entry));
1335 return (ARCHIVE_FATAL);
1336 r = archive_entry_gname_l(file->entry, &p, &len, xar->sconv);
1338 if (errno == ENOMEM) {
1339 archive_set_error(&a->archive, ENOMEM,
1340 "Can't allocate memory for Gname");
1341 return (ARCHIVE_FATAL);
1343 archive_set_error(&a->archive,
1344 ARCHIVE_ERRNO_FILE_FORMAT,
1345 "Can't translate gname '%s' to UTF-8",
1346 archive_entry_gname(file->entry));
1350 r = xmlwrite_string(a, writer, "group", p);
1352 return (ARCHIVE_FATAL);
1356 * Make a ctime entry, "<ctime>".
1358 if (archive_entry_ctime_is_set(file->entry)) {
1359 r = xmlwrite_time(a, writer, "ctime",
1360 archive_entry_ctime(file->entry), 1);
1362 return (ARCHIVE_FATAL);
1366 * Make a mtime entry, "<mtime>".
1368 if (archive_entry_mtime_is_set(file->entry)) {
1369 r = xmlwrite_time(a, writer, "mtime",
1370 archive_entry_mtime(file->entry), 1);
1372 return (ARCHIVE_FATAL);
1376 * Make a atime entry, "<atime>".
1378 if (archive_entry_atime_is_set(file->entry)) {
1379 r = xmlwrite_time(a, writer, "atime",
1380 archive_entry_atime(file->entry), 1);
1382 return (ARCHIVE_FATAL);
1386 * Make fflags entries, "<flags>" and "<ext2>".
1388 fflags = archive_entry_fflags_text(file->entry);
1389 if (fflags != NULL) {
1390 r = make_fflags_entry(a, writer, "flags", fflags);
1393 r = make_fflags_entry(a, writer, "ext2", fflags);
1399 * Make extended attribute entries, "<ea>".
1401 archive_entry_xattr_reset(file->entry);
1402 for (heap = file->xattr.first; heap != NULL; heap = heap->next) {
1407 archive_entry_xattr_next(file->entry,
1408 &name, &value, &size);
1409 r = xmlTextWriterStartElement(writer, BAD_CAST("ea"));
1411 archive_set_error(&a->archive,
1413 "xmlTextWriterStartElement() failed: %d", r);
1414 return (ARCHIVE_FATAL);
1416 r = xmlTextWriterWriteFormatAttribute(writer,
1417 BAD_CAST("id"), "%d", heap->id);
1419 archive_set_error(&a->archive,
1421 "xmlTextWriterWriteAttribute() failed: %d", r);
1422 return (ARCHIVE_FATAL);
1424 r = xmlwrite_heap(a, writer, heap);
1426 return (ARCHIVE_FATAL);
1427 r = xmlwrite_string(a, writer, "name", name);
1429 return (ARCHIVE_FATAL);
1431 r = xmlTextWriterEndElement(writer);
1433 archive_set_error(&a->archive,
1435 "xmlTextWriterEndElement() failed: %d", r);
1436 return (ARCHIVE_FATAL);
1441 * Make a file data entry, "<data>".
1443 if (file->data.length > 0) {
1444 r = xmlTextWriterStartElement(writer, BAD_CAST("data"));
1446 archive_set_error(&a->archive,
1448 "xmlTextWriterStartElement() failed: %d", r);
1449 return (ARCHIVE_FATAL);
1452 r = xmlwrite_heap(a, writer, &(file->data));
1454 return (ARCHIVE_FATAL);
1456 r = xmlTextWriterEndElement(writer);
1458 archive_set_error(&a->archive,
1460 "xmlTextWriterEndElement() failed: %d", r);
1461 return (ARCHIVE_FATAL);
1465 if (archive_strlen(&file->script) > 0) {
1466 r = xmlTextWriterStartElement(writer, BAD_CAST("content"));
1468 archive_set_error(&a->archive,
1470 "xmlTextWriterStartElement() failed: %d", r);
1471 return (ARCHIVE_FATAL);
1474 r = xmlwrite_string(a, writer,
1475 "interpreter", file->script.s);
1477 return (ARCHIVE_FATAL);
1479 r = xmlwrite_string(a, writer, "type", "script");
1481 return (ARCHIVE_FATAL);
1483 r = xmlTextWriterEndElement(writer);
1485 archive_set_error(&a->archive,
1487 "xmlTextWriterEndElement() failed: %d", r);
1488 return (ARCHIVE_FATAL);
1499 make_toc(struct archive_write *a)
1504 xmlTextWriterPtr writer;
1508 xar = (struct xar *)a->format_data;
1510 ret = ARCHIVE_FATAL;
1513 * Initialize xml writer.
1516 bp = xmlBufferCreate();
1518 archive_set_error(&a->archive, ENOMEM,
1519 "xmlBufferCreate() "
1520 "couldn't create xml buffer");
1523 writer = xmlNewTextWriterMemory(bp, 0);
1524 if (writer == NULL) {
1525 archive_set_error(&a->archive,
1527 "xmlNewTextWriterMemory() "
1528 "couldn't create xml writer");
1531 r = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);
1533 archive_set_error(&a->archive,
1535 "xmlTextWriterStartDocument() failed: %d", r);
1538 r = xmlTextWriterSetIndent(writer, 4);
1540 archive_set_error(&a->archive,
1542 "xmlTextWriterSetIndent() failed: %d", r);
1547 * Start recording TOC
1549 r = xmlTextWriterStartElement(writer, BAD_CAST("xar"));
1551 archive_set_error(&a->archive,
1553 "xmlTextWriterStartElement() failed: %d", r);
1556 r = xmlTextWriterStartElement(writer, BAD_CAST("toc"));
1558 archive_set_error(&a->archive,
1560 "xmlTextWriterStartDocument() failed: %d", r);
1565 * Record the creation time of the archive file.
1567 r = xmlwrite_time(a, writer, "creation-time", time(NULL), 0);
1572 * Record the checksum value of TOC
1574 algsize = getalgsize(xar->opt_toc_sumalg);
1577 * Record TOC checksum
1579 r = xmlTextWriterStartElement(writer, BAD_CAST("checksum"));
1581 archive_set_error(&a->archive,
1583 "xmlTextWriterStartElement() failed: %d", r);
1586 r = xmlTextWriterWriteAttribute(writer, BAD_CAST("style"),
1587 BAD_CAST_CONST(getalgname(xar->opt_toc_sumalg)));
1589 archive_set_error(&a->archive,
1591 "xmlTextWriterWriteAttribute() failed: %d", r);
1596 * Record the offset of the value of checksum of TOC
1598 r = xmlwrite_string(a, writer, "offset", "0");
1603 * Record the size of the value of checksum of TOC
1605 r = xmlwrite_fstring(a, writer, "size", "%d", algsize);
1609 r = xmlTextWriterEndElement(writer);
1611 archive_set_error(&a->archive,
1613 "xmlTextWriterEndElement() failed: %d", r);
1620 if (np != np->parent) {
1621 r = make_file_entry(a, writer, np);
1622 if (r != ARCHIVE_OK)
1626 if (np->dir && np->children.first != NULL) {
1627 /* Enter to sub directories. */
1628 np = np->children.first;
1629 r = xmlTextWriterStartElement(writer,
1632 archive_set_error(&a->archive,
1634 "xmlTextWriterStartElement() "
1638 r = xmlTextWriterWriteFormatAttribute(
1639 writer, BAD_CAST("id"), "%d", np->id);
1641 archive_set_error(&a->archive,
1643 "xmlTextWriterWriteAttribute() "
1649 while (np != np->parent) {
1650 r = xmlTextWriterEndElement(writer);
1652 archive_set_error(&a->archive,
1654 "xmlTextWriterEndElement() "
1658 if (np->chnext == NULL) {
1659 /* Return to the parent directory. */
1663 r = xmlTextWriterStartElement(writer,
1666 archive_set_error(&a->archive,
1668 "xmlTextWriterStartElement() "
1672 r = xmlTextWriterWriteFormatAttribute(
1673 writer, BAD_CAST("id"), "%d", np->id);
1675 archive_set_error(&a->archive,
1677 "xmlTextWriterWriteAttribute() "
1684 } while (np != np->parent);
1686 r = xmlTextWriterEndDocument(writer);
1688 archive_set_error(&a->archive,
1690 "xmlTextWriterEndDocument() failed: %d", r);
1694 fprintf(stderr, "\n---TOC-- %d bytes --\n%s\n",
1695 strlen((const char *)bp->content), bp->content);
1699 * Compress the TOC and calculate the sum of the TOC.
1701 xar->toc.temp_offset = xar->temp_offset;
1702 xar->toc.size = bp->use;
1703 checksum_init(&(xar->a_sumwrk), xar->opt_toc_sumalg);
1705 r = compression_init_encoder_gzip(&(a->archive),
1706 &(xar->stream), 6, 1);
1707 if (r != ARCHIVE_OK)
1709 xar->stream.next_in = bp->content;
1710 xar->stream.avail_in = bp->use;
1711 xar->stream.total_in = 0;
1712 xar->stream.next_out = xar->wbuff;
1713 xar->stream.avail_out = sizeof(xar->wbuff);
1714 xar->stream.total_out = 0;
1718 r = compression_code(&(a->archive),
1719 &(xar->stream), ARCHIVE_Z_FINISH);
1720 if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
1722 size = sizeof(xar->wbuff) - xar->stream.avail_out;
1723 checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
1724 if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
1726 if (r == ARCHIVE_EOF)
1728 xar->stream.next_out = xar->wbuff;
1729 xar->stream.avail_out = sizeof(xar->wbuff);
1731 r = compression_end(&(a->archive), &(xar->stream));
1732 if (r != ARCHIVE_OK)
1734 xar->toc.length = xar->stream.total_out;
1735 xar->toc.compression = GZIP;
1736 checksum_final(&(xar->a_sumwrk), &(xar->toc.a_sum));
1741 xmlFreeTextWriter(writer);
1749 flush_wbuff(struct archive_write *a)
1755 xar = (struct xar *)a->format_data;
1756 s = sizeof(xar->wbuff) - xar->wbuff_remaining;
1757 r = __archive_write_output(a, xar->wbuff, s);
1758 if (r != ARCHIVE_OK)
1760 xar->wbuff_remaining = sizeof(xar->wbuff);
1765 copy_out(struct archive_write *a, uint64_t offset, uint64_t length)
1770 xar = (struct xar *)a->format_data;
1771 if (lseek(xar->temp_fd, offset, SEEK_SET) < 0) {
1772 archive_set_error(&(a->archive), errno, "lseek failed");
1773 return (ARCHIVE_FATAL);
1780 if (length > xar->wbuff_remaining)
1781 rsize = xar->wbuff_remaining;
1783 rsize = (size_t)length;
1784 wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
1785 rs = read(xar->temp_fd, wb, rsize);
1787 archive_set_error(&(a->archive), errno,
1788 "Can't read temporary file(%jd)",
1790 return (ARCHIVE_FATAL);
1793 archive_set_error(&(a->archive), 0,
1794 "Truncated xar archive");
1795 return (ARCHIVE_FATAL);
1797 xar->wbuff_remaining -= rs;
1799 if (xar->wbuff_remaining == 0) {
1801 if (r != ARCHIVE_OK)
1805 return (ARCHIVE_OK);
1809 xar_close(struct archive_write *a)
1816 xar = (struct xar *)a->format_data;
1819 if (xar->root->children.first == NULL)
1820 return (ARCHIVE_OK);
1822 /* Save the length of all file extended attributes and contents. */
1823 length = xar->temp_offset;
1825 /* Connect hardlinked files */
1826 file_connect_hardlink_files(xar);
1830 if (r != ARCHIVE_OK)
1833 * Make the xar header on wbuff(write buffer).
1836 xar->wbuff_remaining = sizeof(xar->wbuff);
1837 archive_be32enc(&wb[0], HEADER_MAGIC);
1838 archive_be16enc(&wb[4], HEADER_SIZE);
1839 archive_be16enc(&wb[6], HEADER_VERSION);
1840 archive_be64enc(&wb[8], xar->toc.length);
1841 archive_be64enc(&wb[16], xar->toc.size);
1842 archive_be32enc(&wb[24], xar->toc.a_sum.alg);
1843 xar->wbuff_remaining -= HEADER_SIZE;
1848 r = copy_out(a, xar->toc.temp_offset, xar->toc.length);
1849 if (r != ARCHIVE_OK)
1852 /* Write the checksum value of the TOC. */
1853 if (xar->toc.a_sum.len) {
1854 if (xar->wbuff_remaining < xar->toc.a_sum.len) {
1856 if (r != ARCHIVE_OK)
1859 wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
1860 memcpy(wb, xar->toc.a_sum.val, xar->toc.a_sum.len);
1861 xar->wbuff_remaining -= xar->toc.a_sum.len;
1865 * Write all file extended attributes and contents.
1867 r = copy_out(a, xar->toc.a_sum.len, length);
1868 if (r != ARCHIVE_OK)
1875 xar_free(struct archive_write *a)
1879 xar = (struct xar *)a->format_data;
1881 /* Close the temporary file. */
1882 if (xar->temp_fd >= 0)
1883 close(xar->temp_fd);
1885 archive_string_free(&(xar->cur_dirstr));
1886 archive_string_free(&(xar->tstr));
1887 archive_string_free(&(xar->vstr));
1888 file_free_hardlinks(xar);
1889 file_free_register(xar);
1890 compression_end(&(a->archive), &(xar->stream));
1893 return (ARCHIVE_OK);
1897 file_cmp_node(const struct archive_rb_node *n1,
1898 const struct archive_rb_node *n2)
1900 const struct file *f1 = (const struct file *)n1;
1901 const struct file *f2 = (const struct file *)n2;
1903 return (strcmp(f1->basename.s, f2->basename.s));
1907 file_cmp_key(const struct archive_rb_node *n, const void *key)
1909 const struct file *f = (const struct file *)n;
1911 return (strcmp(f->basename.s, (const char *)key));
1914 static struct file *
1915 file_new(struct archive_write *a, struct archive_entry *entry)
1918 static const struct archive_rb_tree_ops rb_ops = {
1919 file_cmp_node, file_cmp_key
1922 file = calloc(1, sizeof(*file));
1927 file->entry = archive_entry_clone(entry);
1929 file->entry = archive_entry_new2(&a->archive);
1930 if (file->entry == NULL) {
1934 __archive_rb_tree_init(&(file->rbtree), &rb_ops);
1935 file->children.first = NULL;
1936 file->children.last = &(file->children.first);
1937 file->xattr.first = NULL;
1938 file->xattr.last = &(file->xattr.first);
1939 archive_string_init(&(file->parentdir));
1940 archive_string_init(&(file->basename));
1941 archive_string_init(&(file->symlink));
1942 archive_string_init(&(file->script));
1943 if (entry != NULL && archive_entry_filetype(entry) == AE_IFDIR)
1950 file_free(struct file *file)
1952 struct heap_data *heap, *next_heap;
1954 heap = file->xattr.first;
1955 while (heap != NULL) {
1956 next_heap = heap->next;
1960 archive_string_free(&(file->parentdir));
1961 archive_string_free(&(file->basename));
1962 archive_string_free(&(file->symlink));
1963 archive_string_free(&(file->script));
1964 archive_entry_free(file->entry);
1968 static struct file *
1969 file_create_virtual_dir(struct archive_write *a, struct xar *xar,
1970 const char *pathname)
1974 (void)xar; /* UNUSED */
1976 file = file_new(a, NULL);
1979 archive_entry_set_pathname(file->entry, pathname);
1980 archive_entry_set_mode(file->entry, 0555 | AE_IFDIR);
1989 file_add_child_tail(struct file *parent, struct file *child)
1991 if (!__archive_rb_tree_insert_node(
1992 &(parent->rbtree), (struct archive_rb_node *)child))
1994 child->chnext = NULL;
1995 *parent->children.last = child;
1996 parent->children.last = &(child->chnext);
1997 child->parent = parent;
2002 * Find a entry from `parent'
2004 static struct file *
2005 file_find_child(struct file *parent, const char *child_name)
2009 np = (struct file *)__archive_rb_tree_find_node(
2010 &(parent->rbtree), child_name);
2014 #if defined(_WIN32) || defined(__CYGWIN__)
2016 cleanup_backslash(char *utf8, size_t len)
2019 /* Convert a path-separator from '\' to '/' */
2020 while (*utf8 != '\0' && len) {
2028 #define cleanup_backslash(p, len) /* nop */
2032 * Generate a parent directory name and a base name from a pathname.
2035 file_gen_utility_names(struct archive_write *a, struct file *file)
2039 char *p, *dirname, *slash;
2043 xar = (struct xar *)a->format_data;
2044 archive_string_empty(&(file->parentdir));
2045 archive_string_empty(&(file->basename));
2046 archive_string_empty(&(file->symlink));
2048 if (file->parent == file)/* virtual root */
2049 return (ARCHIVE_OK);
2051 if (archive_entry_pathname_l(file->entry, &pp, &len, xar->sconv)
2053 if (errno == ENOMEM) {
2054 archive_set_error(&a->archive, ENOMEM,
2055 "Can't allocate memory for Pathname");
2056 return (ARCHIVE_FATAL);
2058 archive_set_error(&a->archive,
2059 ARCHIVE_ERRNO_FILE_FORMAT,
2060 "Can't translate pathname '%s' to UTF-8",
2061 archive_entry_pathname(file->entry));
2064 archive_strncpy(&(file->parentdir), pp, len);
2065 len = file->parentdir.length;
2066 p = dirname = file->parentdir.s;
2068 * Convert a path-separator from '\' to '/'
2070 cleanup_backslash(p, len);
2073 * Remove leading '/', '../' and './' elements
2079 } else if (p[0] != '.')
2081 else if (p[1] == '.' && p[2] == '/') {
2084 } else if (p[1] == '/' || (p[1] == '.' && p[2] == '\0')) {
2087 } else if (p[1] == '\0') {
2094 memmove(dirname, p, len+1);
2098 * Remove "/","/." and "/.." elements from tail.
2103 if (len > 0 && p[len-1] == '/') {
2107 if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
2111 if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
2122 /* Convert '//' --> '/' */
2124 else if (p[1] == '.' && p[2] == '/')
2125 /* Convert '/./' --> '/' */
2127 else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
2128 /* Convert 'dir/dir1/../dir2/'
2132 while (rp >= dirname) {
2141 strcpy(dirname, p+4);
2152 if (archive_entry_filetype(file->entry) == AE_IFLNK) {
2154 /* Convert symlink name too. */
2155 if (archive_entry_symlink_l(file->entry, &pp, &len2,
2157 if (errno == ENOMEM) {
2158 archive_set_error(&a->archive, ENOMEM,
2159 "Can't allocate memory for Linkname");
2160 return (ARCHIVE_FATAL);
2162 archive_set_error(&a->archive,
2163 ARCHIVE_ERRNO_FILE_FORMAT,
2164 "Can't translate symlink '%s' to UTF-8",
2165 archive_entry_symlink(file->entry));
2168 archive_strncpy(&(file->symlink), pp, len2);
2169 cleanup_backslash(file->symlink.s, file->symlink.length);
2172 * - Count up directory elements.
2173 * - Find out the position which points the last position of
2174 * path separator('/').
2177 for (; *p != '\0'; p++)
2180 if (slash == NULL) {
2181 /* The pathname doesn't have a parent directory. */
2182 file->parentdir.length = len;
2183 archive_string_copy(&(file->basename), &(file->parentdir));
2184 archive_string_empty(&(file->parentdir));
2185 *file->parentdir.s = '\0';
2189 /* Make a basename from dirname and slash */
2191 file->parentdir.length = slash - dirname;
2192 archive_strcpy(&(file->basename), slash + 1);
2197 get_path_component(char *name, int n, const char *fn)
2202 p = strchr(fn, '/');
2204 if ((l = strlen(fn)) == 0)
2210 memcpy(name, fn, l);
2217 * Add a new entry into the tree.
2220 file_tree(struct archive_write *a, struct file **filepp)
2222 #if defined(_WIN32) && !defined(__CYGWIN__)
2223 char name[_MAX_FNAME];/* Included null terminator size. */
2224 #elif defined(NAME_MAX) && NAME_MAX >= 255
2225 char name[NAME_MAX+1];
2229 struct xar *xar = (struct xar *)a->format_data;
2230 struct file *dent, *file, *np;
2231 struct archive_entry *ent;
2237 if (file->parentdir.length > 0)
2238 fn = p = file->parentdir.s;
2243 * If the path of the parent directory of `file' entry is
2244 * the same as the path of `cur_dirent', add isoent to
2247 if (archive_strlen(&(xar->cur_dirstr))
2248 == archive_strlen(&(file->parentdir)) &&
2249 strcmp(xar->cur_dirstr.s, fn) == 0) {
2250 if (!file_add_child_tail(xar->cur_dirent, file)) {
2251 np = (struct file *)__archive_rb_tree_find_node(
2252 &(xar->cur_dirent->rbtree),
2256 return (ARCHIVE_OK);
2260 l = get_path_component(name, sizeof(name), fn);
2266 archive_set_error(&a->archive,
2268 "A name buffer is too small");
2271 return (ARCHIVE_FATAL);
2274 np = file_find_child(dent, name);
2275 if (np == NULL || fn[0] == '\0')
2278 /* Find next subdirectory. */
2280 /* NOT Directory! */
2281 archive_set_error(&a->archive,
2283 "`%s' is not directory, we cannot insert `%s' ",
2284 archive_entry_pathname(np->entry),
2285 archive_entry_pathname(file->entry));
2288 return (ARCHIVE_FAILED);
2297 * Create virtual parent directories.
2299 while (fn[0] != '\0') {
2301 struct archive_string as;
2303 archive_string_init(&as);
2304 archive_strncat(&as, p, fn - p + l);
2305 if (as.s[as.length-1] == '/') {
2306 as.s[as.length-1] = '\0';
2309 vp = file_create_virtual_dir(a, xar, as.s);
2311 archive_string_free(&as);
2312 archive_set_error(&a->archive, ENOMEM,
2313 "Can't allocate memory");
2316 return (ARCHIVE_FATAL);
2318 archive_string_free(&as);
2319 if (file_gen_utility_names(a, vp) <= ARCHIVE_FAILED)
2320 return (ARCHIVE_FATAL);
2321 file_add_child_tail(dent, vp);
2322 file_register(xar, vp);
2328 l = get_path_component(name, sizeof(name), fn);
2330 archive_string_free(&as);
2331 archive_set_error(&a->archive,
2333 "A name buffer is too small");
2336 return (ARCHIVE_FATAL);
2341 /* Found out the parent directory where isoent can be
2343 xar->cur_dirent = dent;
2344 archive_string_empty(&(xar->cur_dirstr));
2345 archive_string_ensure(&(xar->cur_dirstr),
2346 archive_strlen(&(dent->parentdir)) +
2347 archive_strlen(&(dent->basename)) + 2);
2348 if (archive_strlen(&(dent->parentdir)) +
2349 archive_strlen(&(dent->basename)) == 0)
2350 xar->cur_dirstr.s[0] = 0;
2352 if (archive_strlen(&(dent->parentdir)) > 0) {
2353 archive_string_copy(&(xar->cur_dirstr),
2354 &(dent->parentdir));
2355 archive_strappend_char(&(xar->cur_dirstr), '/');
2357 archive_string_concat(&(xar->cur_dirstr),
2361 if (!file_add_child_tail(dent, file)) {
2362 np = (struct file *)__archive_rb_tree_find_node(
2363 &(dent->rbtree), file->basename.s);
2366 return (ARCHIVE_OK);
2371 * We have already has the entry the filename of which is
2374 if (archive_entry_filetype(np->entry) !=
2375 archive_entry_filetype(file->entry)) {
2376 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2377 "Found duplicate entries `%s' and its file type is "
2379 archive_entry_pathname(np->entry));
2382 return (ARCHIVE_FAILED);
2387 np->entry = file->entry;
2393 return (ARCHIVE_OK);
2397 file_register(struct xar *xar, struct file *file)
2399 file->id = xar->file_idx++;
2401 *xar->file_list.last = file;
2402 xar->file_list.last = &(file->next);
2406 file_init_register(struct xar *xar)
2408 xar->file_list.first = NULL;
2409 xar->file_list.last = &(xar->file_list.first);
2413 file_free_register(struct xar *xar)
2415 struct file *file, *file_next;
2417 file = xar->file_list.first;
2418 while (file != NULL) {
2419 file_next = file->next;
2426 * Register entry to get a hardlink target.
2429 file_register_hardlink(struct archive_write *a, struct file *file)
2431 struct xar *xar = (struct xar *)a->format_data;
2432 struct hardlink *hl;
2433 const char *pathname;
2435 archive_entry_set_nlink(file->entry, 1);
2436 pathname = archive_entry_hardlink(file->entry);
2437 if (pathname == NULL) {
2438 /* This `file` is a hardlink target. */
2439 hl = malloc(sizeof(*hl));
2441 archive_set_error(&a->archive, ENOMEM,
2442 "Can't allocate memory");
2443 return (ARCHIVE_FATAL);
2446 /* A hardlink target must be the first position. */
2447 file->hlnext = NULL;
2448 hl->file_list.first = file;
2449 hl->file_list.last = &(file->hlnext);
2450 __archive_rb_tree_insert_node(&(xar->hardlink_rbtree),
2451 (struct archive_rb_node *)hl);
2453 hl = (struct hardlink *)__archive_rb_tree_find_node(
2454 &(xar->hardlink_rbtree), pathname);
2456 /* Insert `file` entry into the tail. */
2457 file->hlnext = NULL;
2458 *hl->file_list.last = file;
2459 hl->file_list.last = &(file->hlnext);
2462 archive_entry_unset_size(file->entry);
2465 return (ARCHIVE_OK);
2469 * Hardlinked files have to have the same location of extent.
2470 * We have to find out hardlink target entries for entries which
2471 * have a hardlink target name.
2474 file_connect_hardlink_files(struct xar *xar)
2476 struct archive_rb_node *n;
2477 struct hardlink *hl;
2478 struct file *target, *nf;
2480 ARCHIVE_RB_TREE_FOREACH(n, &(xar->hardlink_rbtree)) {
2481 hl = (struct hardlink *)n;
2483 /* The first entry must be a hardlink target. */
2484 target = hl->file_list.first;
2485 archive_entry_set_nlink(target->entry, hl->nlink);
2487 /* It means this file is a hardlink
2489 target->hardlink_target = target;
2490 for (nf = target->hlnext;
2491 nf != NULL; nf = nf->hlnext) {
2492 nf->hardlink_target = target;
2493 archive_entry_set_nlink(nf->entry, hl->nlink);
2499 file_hd_cmp_node(const struct archive_rb_node *n1,
2500 const struct archive_rb_node *n2)
2502 const struct hardlink *h1 = (const struct hardlink *)n1;
2503 const struct hardlink *h2 = (const struct hardlink *)n2;
2505 return (strcmp(archive_entry_pathname(h1->file_list.first->entry),
2506 archive_entry_pathname(h2->file_list.first->entry)));
2510 file_hd_cmp_key(const struct archive_rb_node *n, const void *key)
2512 const struct hardlink *h = (const struct hardlink *)n;
2514 return (strcmp(archive_entry_pathname(h->file_list.first->entry),
2515 (const char *)key));
2520 file_init_hardlinks(struct xar *xar)
2522 static const struct archive_rb_tree_ops rb_ops = {
2523 file_hd_cmp_node, file_hd_cmp_key,
2526 __archive_rb_tree_init(&(xar->hardlink_rbtree), &rb_ops);
2530 file_free_hardlinks(struct xar *xar)
2532 struct archive_rb_node *n, *next;
2534 for (n = ARCHIVE_RB_TREE_MIN(&(xar->hardlink_rbtree)); n;) {
2535 next = __archive_rb_tree_iterate(&(xar->hardlink_rbtree),
2536 n, ARCHIVE_RB_DIR_RIGHT);
2543 checksum_init(struct chksumwork *sumwrk, enum sumalg sum_alg)
2545 sumwrk->alg = sum_alg;
2550 archive_sha1_init(&(sumwrk->sha1ctx));
2553 archive_md5_init(&(sumwrk->md5ctx));
2559 checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
2562 switch (sumwrk->alg) {
2566 archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
2569 archive_md5_update(&(sumwrk->md5ctx), buff, size);
2575 checksum_final(struct chksumwork *sumwrk, struct chksumval *sumval)
2578 switch (sumwrk->alg) {
2583 archive_sha1_final(&(sumwrk->sha1ctx), sumval->val);
2584 sumval->len = SHA1_SIZE;
2587 archive_md5_final(&(sumwrk->md5ctx), sumval->val);
2588 sumval->len = MD5_SIZE;
2591 sumval->alg = sumwrk->alg;
2594 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
2596 compression_unsupported_encoder(struct archive *a,
2597 struct la_zstream *lastrm, const char *name)
2600 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2601 "%s compression not supported on this platform", name);
2603 lastrm->real_stream = NULL;
2604 return (ARCHIVE_FAILED);
2609 compression_init_encoder_gzip(struct archive *a,
2610 struct la_zstream *lastrm, int level, int withheader)
2615 compression_end(a, lastrm);
2616 strm = calloc(1, sizeof(*strm));
2618 archive_set_error(a, ENOMEM,
2619 "Can't allocate memory for gzip stream");
2620 return (ARCHIVE_FATAL);
2622 /* zlib.h is not const-correct, so we need this one bit
2623 * of ugly hackery to convert a const * pointer to
2624 * a non-const pointer. */
2625 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2626 strm->avail_in = lastrm->avail_in;
2627 strm->total_in = (uLong)lastrm->total_in;
2628 strm->next_out = lastrm->next_out;
2629 strm->avail_out = lastrm->avail_out;
2630 strm->total_out = (uLong)lastrm->total_out;
2631 if (deflateInit2(strm, level, Z_DEFLATED,
2632 (withheader)?15:-15,
2633 8, Z_DEFAULT_STRATEGY) != Z_OK) {
2635 lastrm->real_stream = NULL;
2636 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2637 "Internal error initializing compression library");
2638 return (ARCHIVE_FATAL);
2640 lastrm->real_stream = strm;
2642 lastrm->code = compression_code_gzip;
2643 lastrm->end = compression_end_gzip;
2644 return (ARCHIVE_OK);
2648 compression_code_gzip(struct archive *a,
2649 struct la_zstream *lastrm, enum la_zaction action)
2654 strm = (z_stream *)lastrm->real_stream;
2655 /* zlib.h is not const-correct, so we need this one bit
2656 * of ugly hackery to convert a const * pointer to
2657 * a non-const pointer. */
2658 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2659 strm->avail_in = lastrm->avail_in;
2660 strm->total_in = (uLong)lastrm->total_in;
2661 strm->next_out = lastrm->next_out;
2662 strm->avail_out = lastrm->avail_out;
2663 strm->total_out = (uLong)lastrm->total_out;
2665 (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
2666 lastrm->next_in = strm->next_in;
2667 lastrm->avail_in = strm->avail_in;
2668 lastrm->total_in = strm->total_in;
2669 lastrm->next_out = strm->next_out;
2670 lastrm->avail_out = strm->avail_out;
2671 lastrm->total_out = strm->total_out;
2674 return (ARCHIVE_OK);
2676 return (ARCHIVE_EOF);
2678 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2679 "GZip compression failed:"
2680 " deflate() call returned status %d", r);
2681 return (ARCHIVE_FATAL);
2686 compression_end_gzip(struct archive *a, struct la_zstream *lastrm)
2691 strm = (z_stream *)lastrm->real_stream;
2692 r = deflateEnd(strm);
2694 lastrm->real_stream = NULL;
2697 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2698 "Failed to clean up compressor");
2699 return (ARCHIVE_FATAL);
2701 return (ARCHIVE_OK);
2704 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
2706 compression_init_encoder_bzip2(struct archive *a,
2707 struct la_zstream *lastrm, int level)
2712 compression_end(a, lastrm);
2713 strm = calloc(1, sizeof(*strm));
2715 archive_set_error(a, ENOMEM,
2716 "Can't allocate memory for bzip2 stream");
2717 return (ARCHIVE_FATAL);
2719 /* bzlib.h is not const-correct, so we need this one bit
2720 * of ugly hackery to convert a const * pointer to
2721 * a non-const pointer. */
2722 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2723 strm->avail_in = lastrm->avail_in;
2724 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2725 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2726 strm->next_out = (char *)lastrm->next_out;
2727 strm->avail_out = lastrm->avail_out;
2728 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2729 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2730 if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
2732 lastrm->real_stream = NULL;
2733 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2734 "Internal error initializing compression library");
2735 return (ARCHIVE_FATAL);
2737 lastrm->real_stream = strm;
2739 lastrm->code = compression_code_bzip2;
2740 lastrm->end = compression_end_bzip2;
2741 return (ARCHIVE_OK);
2745 compression_code_bzip2(struct archive *a,
2746 struct la_zstream *lastrm, enum la_zaction action)
2751 strm = (bz_stream *)lastrm->real_stream;
2752 /* bzlib.h is not const-correct, so we need this one bit
2753 * of ugly hackery to convert a const * pointer to
2754 * a non-const pointer. */
2755 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2756 strm->avail_in = lastrm->avail_in;
2757 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2758 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2759 strm->next_out = (char *)lastrm->next_out;
2760 strm->avail_out = lastrm->avail_out;
2761 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2762 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2763 r = BZ2_bzCompress(strm,
2764 (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
2765 lastrm->next_in = (const unsigned char *)strm->next_in;
2766 lastrm->avail_in = strm->avail_in;
2768 (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
2769 + (uint64_t)(uint32_t)strm->total_in_lo32;
2770 lastrm->next_out = (unsigned char *)strm->next_out;
2771 lastrm->avail_out = strm->avail_out;
2773 (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
2774 + (uint64_t)(uint32_t)strm->total_out_lo32;
2776 case BZ_RUN_OK: /* Non-finishing */
2777 case BZ_FINISH_OK: /* Finishing: There's more work to do */
2778 return (ARCHIVE_OK);
2779 case BZ_STREAM_END: /* Finishing: all done */
2780 /* Only occurs in finishing case */
2781 return (ARCHIVE_EOF);
2783 /* Any other return value indicates an error */
2784 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2785 "Bzip2 compression failed:"
2786 " BZ2_bzCompress() call returned status %d", r);
2787 return (ARCHIVE_FATAL);
2792 compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
2797 strm = (bz_stream *)lastrm->real_stream;
2798 r = BZ2_bzCompressEnd(strm);
2800 lastrm->real_stream = NULL;
2803 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2804 "Failed to clean up compressor");
2805 return (ARCHIVE_FATAL);
2807 return (ARCHIVE_OK);
2812 compression_init_encoder_bzip2(struct archive *a,
2813 struct la_zstream *lastrm, int level)
2816 (void) level; /* UNUSED */
2818 compression_end(a, lastrm);
2819 return (compression_unsupported_encoder(a, lastrm, "bzip2"));
2823 #if defined(HAVE_LZMA_H)
2825 compression_init_encoder_lzma(struct archive *a,
2826 struct la_zstream *lastrm, int level)
2828 static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
2830 lzma_options_lzma lzma_opt;
2834 compression_end(a, lastrm);
2835 if (lzma_lzma_preset(&lzma_opt, level)) {
2836 lastrm->real_stream = NULL;
2837 archive_set_error(a, ENOMEM,
2838 "Internal error initializing compression library");
2839 return (ARCHIVE_FATAL);
2841 strm = calloc(1, sizeof(*strm));
2843 archive_set_error(a, ENOMEM,
2844 "Can't allocate memory for lzma stream");
2845 return (ARCHIVE_FATAL);
2847 *strm = lzma_init_data;
2848 r = lzma_alone_encoder(strm, &lzma_opt);
2851 lastrm->real_stream = strm;
2853 lastrm->code = compression_code_lzma;
2854 lastrm->end = compression_end_lzma;
2857 case LZMA_MEM_ERROR:
2859 lastrm->real_stream = NULL;
2860 archive_set_error(a, ENOMEM,
2861 "Internal error initializing compression library: "
2862 "Cannot allocate memory");
2867 lastrm->real_stream = NULL;
2868 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2869 "Internal error initializing compression library: "
2870 "It's a bug in liblzma");
2878 compression_init_encoder_xz(struct archive *a,
2879 struct la_zstream *lastrm, int level, int threads)
2881 static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
2883 lzma_filter *lzmafilters;
2884 lzma_options_lzma lzma_opt;
2886 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
2890 (void)threads; /* UNUSED (if multi-threaded LZMA library not avail) */
2893 compression_end(a, lastrm);
2894 strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
2896 archive_set_error(a, ENOMEM,
2897 "Can't allocate memory for xz stream");
2898 return (ARCHIVE_FATAL);
2900 lzmafilters = (lzma_filter *)(strm+1);
2903 if (lzma_lzma_preset(&lzma_opt, level)) {
2905 lastrm->real_stream = NULL;
2906 archive_set_error(a, ENOMEM,
2907 "Internal error initializing compression library");
2908 return (ARCHIVE_FATAL);
2910 lzmafilters[0].id = LZMA_FILTER_LZMA2;
2911 lzmafilters[0].options = &lzma_opt;
2912 lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
2914 *strm = lzma_init_data;
2915 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
2917 memset(&mt_options, 0, sizeof(mt_options));
2918 mt_options.threads = threads;
2919 mt_options.timeout = 300;
2920 mt_options.filters = lzmafilters;
2921 mt_options.check = LZMA_CHECK_CRC64;
2922 r = lzma_stream_encoder_mt(strm, &mt_options);
2925 r = lzma_stream_encoder(strm, lzmafilters, LZMA_CHECK_CRC64);
2928 lastrm->real_stream = strm;
2930 lastrm->code = compression_code_lzma;
2931 lastrm->end = compression_end_lzma;
2934 case LZMA_MEM_ERROR:
2936 lastrm->real_stream = NULL;
2937 archive_set_error(a, ENOMEM,
2938 "Internal error initializing compression library: "
2939 "Cannot allocate memory");
2944 lastrm->real_stream = NULL;
2945 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2946 "Internal error initializing compression library: "
2947 "It's a bug in liblzma");
2955 compression_code_lzma(struct archive *a,
2956 struct la_zstream *lastrm, enum la_zaction action)
2961 strm = (lzma_stream *)lastrm->real_stream;
2962 strm->next_in = lastrm->next_in;
2963 strm->avail_in = lastrm->avail_in;
2964 strm->total_in = lastrm->total_in;
2965 strm->next_out = lastrm->next_out;
2966 strm->avail_out = lastrm->avail_out;
2967 strm->total_out = lastrm->total_out;
2969 (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
2970 lastrm->next_in = strm->next_in;
2971 lastrm->avail_in = strm->avail_in;
2972 lastrm->total_in = strm->total_in;
2973 lastrm->next_out = strm->next_out;
2974 lastrm->avail_out = strm->avail_out;
2975 lastrm->total_out = strm->total_out;
2978 /* Non-finishing case */
2979 return (ARCHIVE_OK);
2980 case LZMA_STREAM_END:
2981 /* This return can only occur in finishing case. */
2982 return (ARCHIVE_EOF);
2983 case LZMA_MEMLIMIT_ERROR:
2984 archive_set_error(a, ENOMEM,
2985 "lzma compression error:"
2986 " %ju MiB would have been needed",
2987 (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
2989 return (ARCHIVE_FATAL);
2991 /* Any other return value indicates an error */
2992 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2993 "lzma compression failed:"
2994 " lzma_code() call returned status %d", r);
2995 return (ARCHIVE_FATAL);
3000 compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
3004 (void)a; /* UNUSED */
3005 strm = (lzma_stream *)lastrm->real_stream;
3009 lastrm->real_stream = NULL;
3010 return (ARCHIVE_OK);
3014 compression_init_encoder_lzma(struct archive *a,
3015 struct la_zstream *lastrm, int level)
3018 (void) level; /* UNUSED */
3020 compression_end(a, lastrm);
3021 return (compression_unsupported_encoder(a, lastrm, "lzma"));
3024 compression_init_encoder_xz(struct archive *a,
3025 struct la_zstream *lastrm, int level, int threads)
3028 (void) level; /* UNUSED */
3029 (void) threads; /* UNUSED */
3031 compression_end(a, lastrm);
3032 return (compression_unsupported_encoder(a, lastrm, "xz"));
3037 xar_compression_init_encoder(struct archive_write *a)
3042 xar = (struct xar *)a->format_data;
3043 switch (xar->opt_compression) {
3045 r = compression_init_encoder_gzip(
3046 &(a->archive), &(xar->stream),
3047 xar->opt_compression_level, 1);
3050 r = compression_init_encoder_bzip2(
3051 &(a->archive), &(xar->stream),
3052 xar->opt_compression_level);
3055 r = compression_init_encoder_lzma(
3056 &(a->archive), &(xar->stream),
3057 xar->opt_compression_level);
3060 r = compression_init_encoder_xz(
3061 &(a->archive), &(xar->stream),
3062 xar->opt_compression_level, xar->opt_threads);
3068 if (r == ARCHIVE_OK) {
3069 xar->stream.total_in = 0;
3070 xar->stream.next_out = xar->wbuff;
3071 xar->stream.avail_out = sizeof(xar->wbuff);
3072 xar->stream.total_out = 0;
3079 compression_code(struct archive *a, struct la_zstream *lastrm,
3080 enum la_zaction action)
3083 return (lastrm->code(a, lastrm, action));
3084 return (ARCHIVE_OK);
3088 compression_end(struct archive *a, struct la_zstream *lastrm)
3091 return (lastrm->end(a, lastrm));
3092 return (ARCHIVE_OK);
3097 save_xattrs(struct archive_write *a, struct file *file)
3102 struct heap_data *heap;
3106 xar = (struct xar *)a->format_data;
3107 count = archive_entry_xattr_reset(file->entry);
3109 return (ARCHIVE_OK);
3111 archive_entry_xattr_next(file->entry,
3112 &name, &value, &size);
3113 checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
3114 checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
3116 heap = calloc(1, sizeof(*heap));
3118 archive_set_error(&a->archive, ENOMEM,
3119 "Can't allocate memory for xattr");
3120 return (ARCHIVE_FATAL);
3122 heap->id = file->ea_idx++;
3123 heap->temp_offset = xar->temp_offset;
3124 heap->size = size;/* save a extracted size */
3125 heap->compression = xar->opt_compression;
3126 /* Get a extracted sumcheck value. */
3127 checksum_update(&(xar->e_sumwrk), value, size);
3128 checksum_final(&(xar->e_sumwrk), &(heap->e_sum));
3131 * Not compression to xattr is simple way.
3133 if (heap->compression == NONE) {
3134 checksum_update(&(xar->a_sumwrk), value, size);
3135 checksum_final(&(xar->a_sumwrk), &(heap->a_sum));
3136 if (write_to_temp(a, value, size)
3139 return (ARCHIVE_FATAL);
3141 heap->length = size;
3142 /* Add heap to the tail of file->xattr. */
3144 *file->xattr.last = heap;
3145 file->xattr.last = &(heap->next);
3151 * Init compression library.
3153 r = xar_compression_init_encoder(a);
3154 if (r != ARCHIVE_OK) {
3156 return (ARCHIVE_FATAL);
3159 xar->stream.next_in = (const unsigned char *)value;
3160 xar->stream.avail_in = size;
3162 r = compression_code(&(a->archive),
3163 &(xar->stream), ARCHIVE_Z_FINISH);
3164 if (r != ARCHIVE_OK && r != ARCHIVE_EOF) {
3166 return (ARCHIVE_FATAL);
3168 size = sizeof(xar->wbuff) - xar->stream.avail_out;
3169 checksum_update(&(xar->a_sumwrk),
3171 if (write_to_temp(a, xar->wbuff, size)
3173 return (ARCHIVE_FATAL);
3174 if (r == ARCHIVE_OK) {
3175 xar->stream.next_out = xar->wbuff;
3176 xar->stream.avail_out = sizeof(xar->wbuff);
3178 checksum_final(&(xar->a_sumwrk),
3180 heap->length = xar->stream.total_out;
3181 /* Add heap to the tail of file->xattr. */
3183 *file->xattr.last = heap;
3184 file->xattr.last = &(heap->next);
3188 /* Clean up compression library. */
3189 r = compression_end(&(a->archive), &(xar->stream));
3190 if (r != ARCHIVE_OK)
3191 return (ARCHIVE_FATAL);
3193 return (ARCHIVE_OK);
3197 getalgsize(enum sumalg sumalg)
3211 getalgname(enum sumalg sumalg)
3224 #endif /* Support xar format */