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 * libarcive 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 menber 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 menber of struct file to chain.
268 * The list of hard-linked file entries.
269 * We use 'hlnext' a menber 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 symblic-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 implemention cannot do it, and then we
1241 * always record that a attribute "type" is "borken",
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 recoding 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));
1967 static struct file *
1968 file_create_virtual_dir(struct archive_write *a, struct xar *xar,
1969 const char *pathname)
1973 (void)xar; /* UNUSED */
1975 file = file_new(a, NULL);
1978 archive_entry_set_pathname(file->entry, pathname);
1979 archive_entry_set_mode(file->entry, 0555 | AE_IFDIR);
1988 file_add_child_tail(struct file *parent, struct file *child)
1990 if (!__archive_rb_tree_insert_node(
1991 &(parent->rbtree), (struct archive_rb_node *)child))
1993 child->chnext = NULL;
1994 *parent->children.last = child;
1995 parent->children.last = &(child->chnext);
1996 child->parent = parent;
2001 * Find a entry from `parent'
2003 static struct file *
2004 file_find_child(struct file *parent, const char *child_name)
2008 np = (struct file *)__archive_rb_tree_find_node(
2009 &(parent->rbtree), child_name);
2013 #if defined(_WIN32) || defined(__CYGWIN__)
2015 cleanup_backslash(char *utf8, size_t len)
2018 /* Convert a path-separator from '\' to '/' */
2019 while (*utf8 != '\0' && len) {
2027 #define cleanup_backslash(p, len) /* nop */
2031 * Generate a parent directory name and a base name from a pathname.
2034 file_gen_utility_names(struct archive_write *a, struct file *file)
2038 char *p, *dirname, *slash;
2042 xar = (struct xar *)a->format_data;
2043 archive_string_empty(&(file->parentdir));
2044 archive_string_empty(&(file->basename));
2045 archive_string_empty(&(file->symlink));
2047 if (file->parent == file)/* virtual root */
2048 return (ARCHIVE_OK);
2050 if (archive_entry_pathname_l(file->entry, &pp, &len, xar->sconv)
2052 if (errno == ENOMEM) {
2053 archive_set_error(&a->archive, ENOMEM,
2054 "Can't allocate memory for Pathname");
2055 return (ARCHIVE_FATAL);
2057 archive_set_error(&a->archive,
2058 ARCHIVE_ERRNO_FILE_FORMAT,
2059 "Can't translate pathname '%s' to UTF-8",
2060 archive_entry_pathname(file->entry));
2063 archive_strncpy(&(file->parentdir), pp, len);
2064 len = file->parentdir.length;
2065 p = dirname = file->parentdir.s;
2067 * Convert a path-separator from '\' to '/'
2069 cleanup_backslash(p, len);
2072 * Remove leading '/', '../' and './' elements
2078 } else if (p[0] != '.')
2080 else if (p[1] == '.' && p[2] == '/') {
2083 } else if (p[1] == '/' || (p[1] == '.' && p[2] == '\0')) {
2086 } else if (p[1] == '\0') {
2093 memmove(dirname, p, len+1);
2097 * Remove "/","/." and "/.." elements from tail.
2102 if (len > 0 && p[len-1] == '/') {
2106 if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
2110 if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
2121 /* Convert '//' --> '/' */
2123 else if (p[1] == '.' && p[2] == '/')
2124 /* Convert '/./' --> '/' */
2126 else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
2127 /* Convert 'dir/dir1/../dir2/'
2131 while (rp >= dirname) {
2140 strcpy(dirname, p+4);
2151 if (archive_entry_filetype(file->entry) == AE_IFLNK) {
2153 /* Convert symlink name too. */
2154 if (archive_entry_symlink_l(file->entry, &pp, &len2,
2156 if (errno == ENOMEM) {
2157 archive_set_error(&a->archive, ENOMEM,
2158 "Can't allocate memory for Linkname");
2159 return (ARCHIVE_FATAL);
2161 archive_set_error(&a->archive,
2162 ARCHIVE_ERRNO_FILE_FORMAT,
2163 "Can't translate symlink '%s' to UTF-8",
2164 archive_entry_symlink(file->entry));
2167 archive_strncpy(&(file->symlink), pp, len2);
2168 cleanup_backslash(file->symlink.s, file->symlink.length);
2171 * - Count up directory elements.
2172 * - Find out the position which points the last position of
2173 * path separator('/').
2176 for (; *p != '\0'; p++)
2179 if (slash == NULL) {
2180 /* The pathname doesn't have a parent directory. */
2181 file->parentdir.length = len;
2182 archive_string_copy(&(file->basename), &(file->parentdir));
2183 archive_string_empty(&(file->parentdir));
2184 *file->parentdir.s = '\0';
2188 /* Make a basename from dirname and slash */
2190 file->parentdir.length = slash - dirname;
2191 archive_strcpy(&(file->basename), slash + 1);
2196 get_path_component(char *name, int n, const char *fn)
2201 p = strchr(fn, '/');
2203 if ((l = strlen(fn)) == 0)
2209 memcpy(name, fn, l);
2216 * Add a new entry into the tree.
2219 file_tree(struct archive_write *a, struct file **filepp)
2221 #if defined(_WIN32) && !defined(__CYGWIN__)
2222 char name[_MAX_FNAME];/* Included null terminator size. */
2223 #elif defined(NAME_MAX) && NAME_MAX >= 255
2224 char name[NAME_MAX+1];
2228 struct xar *xar = (struct xar *)a->format_data;
2229 struct file *dent, *file, *np;
2230 struct archive_entry *ent;
2236 if (file->parentdir.length > 0)
2237 fn = p = file->parentdir.s;
2242 * If the path of the parent directory of `file' entry is
2243 * the same as the path of `cur_dirent', add isoent to
2246 if (archive_strlen(&(xar->cur_dirstr))
2247 == archive_strlen(&(file->parentdir)) &&
2248 strcmp(xar->cur_dirstr.s, fn) == 0) {
2249 if (!file_add_child_tail(xar->cur_dirent, file)) {
2250 np = (struct file *)__archive_rb_tree_find_node(
2251 &(xar->cur_dirent->rbtree),
2255 return (ARCHIVE_OK);
2259 l = get_path_component(name, sizeof(name), fn);
2265 archive_set_error(&a->archive,
2267 "A name buffer is too small");
2270 return (ARCHIVE_FATAL);
2273 np = file_find_child(dent, name);
2274 if (np == NULL || fn[0] == '\0')
2277 /* Find next subdirectory. */
2279 /* NOT Directory! */
2280 archive_set_error(&a->archive,
2282 "`%s' is not directory, we cannot insert `%s' ",
2283 archive_entry_pathname(np->entry),
2284 archive_entry_pathname(file->entry));
2287 return (ARCHIVE_FAILED);
2296 * Create virtual parent directories.
2298 while (fn[0] != '\0') {
2300 struct archive_string as;
2302 archive_string_init(&as);
2303 archive_strncat(&as, p, fn - p + l);
2304 if (as.s[as.length-1] == '/') {
2305 as.s[as.length-1] = '\0';
2308 vp = file_create_virtual_dir(a, xar, as.s);
2310 archive_string_free(&as);
2311 archive_set_error(&a->archive, ENOMEM,
2312 "Can't allocate memory");
2315 return (ARCHIVE_FATAL);
2317 archive_string_free(&as);
2318 if (file_gen_utility_names(a, vp) <= ARCHIVE_FAILED)
2319 return (ARCHIVE_FATAL);
2320 file_add_child_tail(dent, vp);
2321 file_register(xar, vp);
2327 l = get_path_component(name, sizeof(name), fn);
2329 archive_string_free(&as);
2330 archive_set_error(&a->archive,
2332 "A name buffer is too small");
2335 return (ARCHIVE_FATAL);
2340 /* Found out the parent directory where isoent can be
2342 xar->cur_dirent = dent;
2343 archive_string_empty(&(xar->cur_dirstr));
2344 archive_string_ensure(&(xar->cur_dirstr),
2345 archive_strlen(&(dent->parentdir)) +
2346 archive_strlen(&(dent->basename)) + 2);
2347 if (archive_strlen(&(dent->parentdir)) +
2348 archive_strlen(&(dent->basename)) == 0)
2349 xar->cur_dirstr.s[0] = 0;
2351 if (archive_strlen(&(dent->parentdir)) > 0) {
2352 archive_string_copy(&(xar->cur_dirstr),
2353 &(dent->parentdir));
2354 archive_strappend_char(&(xar->cur_dirstr), '/');
2356 archive_string_concat(&(xar->cur_dirstr),
2360 if (!file_add_child_tail(dent, file)) {
2361 np = (struct file *)__archive_rb_tree_find_node(
2362 &(dent->rbtree), file->basename.s);
2365 return (ARCHIVE_OK);
2370 * We have already has the entry the filename of which is
2373 if (archive_entry_filetype(np->entry) !=
2374 archive_entry_filetype(file->entry)) {
2375 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2376 "Found duplicate entries `%s' and its file type is "
2378 archive_entry_pathname(np->entry));
2381 return (ARCHIVE_FAILED);
2386 np->entry = file->entry;
2392 return (ARCHIVE_OK);
2396 file_register(struct xar *xar, struct file *file)
2398 file->id = xar->file_idx++;
2400 *xar->file_list.last = file;
2401 xar->file_list.last = &(file->next);
2405 file_init_register(struct xar *xar)
2407 xar->file_list.first = NULL;
2408 xar->file_list.last = &(xar->file_list.first);
2412 file_free_register(struct xar *xar)
2414 struct file *file, *file_next;
2416 file = xar->file_list.first;
2417 while (file != NULL) {
2418 file_next = file->next;
2425 * Register entry to get a hardlink target.
2428 file_register_hardlink(struct archive_write *a, struct file *file)
2430 struct xar *xar = (struct xar *)a->format_data;
2431 struct hardlink *hl;
2432 const char *pathname;
2434 archive_entry_set_nlink(file->entry, 1);
2435 pathname = archive_entry_hardlink(file->entry);
2436 if (pathname == NULL) {
2437 /* This `file` is a hardlink target. */
2438 hl = malloc(sizeof(*hl));
2440 archive_set_error(&a->archive, ENOMEM,
2441 "Can't allocate memory");
2442 return (ARCHIVE_FATAL);
2445 /* A hardlink target must be the first position. */
2446 file->hlnext = NULL;
2447 hl->file_list.first = file;
2448 hl->file_list.last = &(file->hlnext);
2449 __archive_rb_tree_insert_node(&(xar->hardlink_rbtree),
2450 (struct archive_rb_node *)hl);
2452 hl = (struct hardlink *)__archive_rb_tree_find_node(
2453 &(xar->hardlink_rbtree), pathname);
2455 /* Insert `file` entry into the tail. */
2456 file->hlnext = NULL;
2457 *hl->file_list.last = file;
2458 hl->file_list.last = &(file->hlnext);
2461 archive_entry_unset_size(file->entry);
2464 return (ARCHIVE_OK);
2468 * Hardlinked files have to have the same location of extent.
2469 * We have to find out hardlink target entries for entries which
2470 * have a hardlink target name.
2473 file_connect_hardlink_files(struct xar *xar)
2475 struct archive_rb_node *n;
2476 struct hardlink *hl;
2477 struct file *target, *nf;
2479 ARCHIVE_RB_TREE_FOREACH(n, &(xar->hardlink_rbtree)) {
2480 hl = (struct hardlink *)n;
2482 /* The first entry must be a hardlink target. */
2483 target = hl->file_list.first;
2484 archive_entry_set_nlink(target->entry, hl->nlink);
2486 /* It means this file is a hardlink
2488 target->hardlink_target = target;
2489 for (nf = target->hlnext;
2490 nf != NULL; nf = nf->hlnext) {
2491 nf->hardlink_target = target;
2492 archive_entry_set_nlink(nf->entry, hl->nlink);
2498 file_hd_cmp_node(const struct archive_rb_node *n1,
2499 const struct archive_rb_node *n2)
2501 const struct hardlink *h1 = (const struct hardlink *)n1;
2502 const struct hardlink *h2 = (const struct hardlink *)n2;
2504 return (strcmp(archive_entry_pathname(h1->file_list.first->entry),
2505 archive_entry_pathname(h2->file_list.first->entry)));
2509 file_hd_cmp_key(const struct archive_rb_node *n, const void *key)
2511 const struct hardlink *h = (const struct hardlink *)n;
2513 return (strcmp(archive_entry_pathname(h->file_list.first->entry),
2514 (const char *)key));
2519 file_init_hardlinks(struct xar *xar)
2521 static const struct archive_rb_tree_ops rb_ops = {
2522 file_hd_cmp_node, file_hd_cmp_key,
2525 __archive_rb_tree_init(&(xar->hardlink_rbtree), &rb_ops);
2529 file_free_hardlinks(struct xar *xar)
2531 struct archive_rb_node *n, *next;
2533 for (n = ARCHIVE_RB_TREE_MIN(&(xar->hardlink_rbtree)); n;) {
2534 next = __archive_rb_tree_iterate(&(xar->hardlink_rbtree),
2535 n, ARCHIVE_RB_DIR_RIGHT);
2542 checksum_init(struct chksumwork *sumwrk, enum sumalg sum_alg)
2544 sumwrk->alg = sum_alg;
2549 archive_sha1_init(&(sumwrk->sha1ctx));
2552 archive_md5_init(&(sumwrk->md5ctx));
2558 checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
2561 switch (sumwrk->alg) {
2565 archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
2568 archive_md5_update(&(sumwrk->md5ctx), buff, size);
2574 checksum_final(struct chksumwork *sumwrk, struct chksumval *sumval)
2577 switch (sumwrk->alg) {
2582 archive_sha1_final(&(sumwrk->sha1ctx), sumval->val);
2583 sumval->len = SHA1_SIZE;
2586 archive_md5_final(&(sumwrk->md5ctx), sumval->val);
2587 sumval->len = MD5_SIZE;
2590 sumval->alg = sumwrk->alg;
2593 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
2595 compression_unsupported_encoder(struct archive *a,
2596 struct la_zstream *lastrm, const char *name)
2599 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2600 "%s compression not supported on this platform", name);
2602 lastrm->real_stream = NULL;
2603 return (ARCHIVE_FAILED);
2608 compression_init_encoder_gzip(struct archive *a,
2609 struct la_zstream *lastrm, int level, int withheader)
2614 compression_end(a, lastrm);
2615 strm = calloc(1, sizeof(*strm));
2617 archive_set_error(a, ENOMEM,
2618 "Can't allocate memory for gzip stream");
2619 return (ARCHIVE_FATAL);
2621 /* zlib.h is not const-correct, so we need this one bit
2622 * of ugly hackery to convert a const * pointer to
2623 * a non-const pointer. */
2624 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2625 strm->avail_in = lastrm->avail_in;
2626 strm->total_in = (uLong)lastrm->total_in;
2627 strm->next_out = lastrm->next_out;
2628 strm->avail_out = lastrm->avail_out;
2629 strm->total_out = (uLong)lastrm->total_out;
2630 if (deflateInit2(strm, level, Z_DEFLATED,
2631 (withheader)?15:-15,
2632 8, Z_DEFAULT_STRATEGY) != Z_OK) {
2634 lastrm->real_stream = NULL;
2635 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2636 "Internal error initializing compression library");
2637 return (ARCHIVE_FATAL);
2639 lastrm->real_stream = strm;
2641 lastrm->code = compression_code_gzip;
2642 lastrm->end = compression_end_gzip;
2643 return (ARCHIVE_OK);
2647 compression_code_gzip(struct archive *a,
2648 struct la_zstream *lastrm, enum la_zaction action)
2653 strm = (z_stream *)lastrm->real_stream;
2654 /* zlib.h is not const-correct, so we need this one bit
2655 * of ugly hackery to convert a const * pointer to
2656 * a non-const pointer. */
2657 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2658 strm->avail_in = lastrm->avail_in;
2659 strm->total_in = (uLong)lastrm->total_in;
2660 strm->next_out = lastrm->next_out;
2661 strm->avail_out = lastrm->avail_out;
2662 strm->total_out = (uLong)lastrm->total_out;
2664 (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
2665 lastrm->next_in = strm->next_in;
2666 lastrm->avail_in = strm->avail_in;
2667 lastrm->total_in = strm->total_in;
2668 lastrm->next_out = strm->next_out;
2669 lastrm->avail_out = strm->avail_out;
2670 lastrm->total_out = strm->total_out;
2673 return (ARCHIVE_OK);
2675 return (ARCHIVE_EOF);
2677 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2678 "GZip compression failed:"
2679 " deflate() call returned status %d", r);
2680 return (ARCHIVE_FATAL);
2685 compression_end_gzip(struct archive *a, struct la_zstream *lastrm)
2690 strm = (z_stream *)lastrm->real_stream;
2691 r = deflateEnd(strm);
2693 lastrm->real_stream = NULL;
2696 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2697 "Failed to clean up compressor");
2698 return (ARCHIVE_FATAL);
2700 return (ARCHIVE_OK);
2703 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
2705 compression_init_encoder_bzip2(struct archive *a,
2706 struct la_zstream *lastrm, int level)
2711 compression_end(a, lastrm);
2712 strm = calloc(1, sizeof(*strm));
2714 archive_set_error(a, ENOMEM,
2715 "Can't allocate memory for bzip2 stream");
2716 return (ARCHIVE_FATAL);
2718 /* bzlib.h is not const-correct, so we need this one bit
2719 * of ugly hackery to convert a const * pointer to
2720 * a non-const pointer. */
2721 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2722 strm->avail_in = lastrm->avail_in;
2723 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2724 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2725 strm->next_out = (char *)lastrm->next_out;
2726 strm->avail_out = lastrm->avail_out;
2727 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2728 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2729 if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
2731 lastrm->real_stream = NULL;
2732 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2733 "Internal error initializing compression library");
2734 return (ARCHIVE_FATAL);
2736 lastrm->real_stream = strm;
2738 lastrm->code = compression_code_bzip2;
2739 lastrm->end = compression_end_bzip2;
2740 return (ARCHIVE_OK);
2744 compression_code_bzip2(struct archive *a,
2745 struct la_zstream *lastrm, enum la_zaction action)
2750 strm = (bz_stream *)lastrm->real_stream;
2751 /* bzlib.h is not const-correct, so we need this one bit
2752 * of ugly hackery to convert a const * pointer to
2753 * a non-const pointer. */
2754 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2755 strm->avail_in = lastrm->avail_in;
2756 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2757 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2758 strm->next_out = (char *)lastrm->next_out;
2759 strm->avail_out = lastrm->avail_out;
2760 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2761 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2762 r = BZ2_bzCompress(strm,
2763 (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
2764 lastrm->next_in = (const unsigned char *)strm->next_in;
2765 lastrm->avail_in = strm->avail_in;
2767 (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
2768 + (uint64_t)(uint32_t)strm->total_in_lo32;
2769 lastrm->next_out = (unsigned char *)strm->next_out;
2770 lastrm->avail_out = strm->avail_out;
2772 (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
2773 + (uint64_t)(uint32_t)strm->total_out_lo32;
2775 case BZ_RUN_OK: /* Non-finishing */
2776 case BZ_FINISH_OK: /* Finishing: There's more work to do */
2777 return (ARCHIVE_OK);
2778 case BZ_STREAM_END: /* Finishing: all done */
2779 /* Only occurs in finishing case */
2780 return (ARCHIVE_EOF);
2782 /* Any other return value indicates an error */
2783 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2784 "Bzip2 compression failed:"
2785 " BZ2_bzCompress() call returned status %d", r);
2786 return (ARCHIVE_FATAL);
2791 compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
2796 strm = (bz_stream *)lastrm->real_stream;
2797 r = BZ2_bzCompressEnd(strm);
2799 lastrm->real_stream = NULL;
2802 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2803 "Failed to clean up compressor");
2804 return (ARCHIVE_FATAL);
2806 return (ARCHIVE_OK);
2811 compression_init_encoder_bzip2(struct archive *a,
2812 struct la_zstream *lastrm, int level)
2815 (void) level; /* UNUSED */
2817 compression_end(a, lastrm);
2818 return (compression_unsupported_encoder(a, lastrm, "bzip2"));
2822 #if defined(HAVE_LZMA_H)
2824 compression_init_encoder_lzma(struct archive *a,
2825 struct la_zstream *lastrm, int level)
2827 static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
2829 lzma_options_lzma lzma_opt;
2833 compression_end(a, lastrm);
2834 if (lzma_lzma_preset(&lzma_opt, level)) {
2835 lastrm->real_stream = NULL;
2836 archive_set_error(a, ENOMEM,
2837 "Internal error initializing compression library");
2838 return (ARCHIVE_FATAL);
2840 strm = calloc(1, sizeof(*strm));
2842 archive_set_error(a, ENOMEM,
2843 "Can't allocate memory for lzma stream");
2844 return (ARCHIVE_FATAL);
2846 *strm = lzma_init_data;
2847 r = lzma_alone_encoder(strm, &lzma_opt);
2850 lastrm->real_stream = strm;
2852 lastrm->code = compression_code_lzma;
2853 lastrm->end = compression_end_lzma;
2856 case LZMA_MEM_ERROR:
2858 lastrm->real_stream = NULL;
2859 archive_set_error(a, ENOMEM,
2860 "Internal error initializing compression library: "
2861 "Cannot allocate memory");
2866 lastrm->real_stream = NULL;
2867 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2868 "Internal error initializing compression library: "
2869 "It's a bug in liblzma");
2877 compression_init_encoder_xz(struct archive *a,
2878 struct la_zstream *lastrm, int level, int threads)
2880 static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
2882 lzma_filter *lzmafilters;
2883 lzma_options_lzma lzma_opt;
2885 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
2889 (void)threads; /* UNUSED (if multi-threaded LZMA library not avail) */
2892 compression_end(a, lastrm);
2893 strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
2895 archive_set_error(a, ENOMEM,
2896 "Can't allocate memory for xz stream");
2897 return (ARCHIVE_FATAL);
2899 lzmafilters = (lzma_filter *)(strm+1);
2902 if (lzma_lzma_preset(&lzma_opt, level)) {
2904 lastrm->real_stream = NULL;
2905 archive_set_error(a, ENOMEM,
2906 "Internal error initializing compression library");
2907 return (ARCHIVE_FATAL);
2909 lzmafilters[0].id = LZMA_FILTER_LZMA2;
2910 lzmafilters[0].options = &lzma_opt;
2911 lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
2913 *strm = lzma_init_data;
2914 #ifdef HAVE_LZMA_STREAM_ENCODER_MT
2916 bzero(&mt_options, sizeof(mt_options));
2917 mt_options.threads = threads;
2918 mt_options.timeout = 300;
2919 mt_options.filters = lzmafilters;
2920 mt_options.check = LZMA_CHECK_CRC64;
2921 r = lzma_stream_encoder_mt(strm, &mt_options);
2924 r = lzma_stream_encoder(strm, lzmafilters, LZMA_CHECK_CRC64);
2927 lastrm->real_stream = strm;
2929 lastrm->code = compression_code_lzma;
2930 lastrm->end = compression_end_lzma;
2933 case LZMA_MEM_ERROR:
2935 lastrm->real_stream = NULL;
2936 archive_set_error(a, ENOMEM,
2937 "Internal error initializing compression library: "
2938 "Cannot allocate memory");
2943 lastrm->real_stream = NULL;
2944 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2945 "Internal error initializing compression library: "
2946 "It's a bug in liblzma");
2954 compression_code_lzma(struct archive *a,
2955 struct la_zstream *lastrm, enum la_zaction action)
2960 strm = (lzma_stream *)lastrm->real_stream;
2961 strm->next_in = lastrm->next_in;
2962 strm->avail_in = lastrm->avail_in;
2963 strm->total_in = lastrm->total_in;
2964 strm->next_out = lastrm->next_out;
2965 strm->avail_out = lastrm->avail_out;
2966 strm->total_out = lastrm->total_out;
2968 (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
2969 lastrm->next_in = strm->next_in;
2970 lastrm->avail_in = strm->avail_in;
2971 lastrm->total_in = strm->total_in;
2972 lastrm->next_out = strm->next_out;
2973 lastrm->avail_out = strm->avail_out;
2974 lastrm->total_out = strm->total_out;
2977 /* Non-finishing case */
2978 return (ARCHIVE_OK);
2979 case LZMA_STREAM_END:
2980 /* This return can only occur in finishing case. */
2981 return (ARCHIVE_EOF);
2982 case LZMA_MEMLIMIT_ERROR:
2983 archive_set_error(a, ENOMEM,
2984 "lzma compression error:"
2985 " %ju MiB would have been needed",
2986 (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
2988 return (ARCHIVE_FATAL);
2990 /* Any other return value indicates an error */
2991 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2992 "lzma compression failed:"
2993 " lzma_code() call returned status %d", r);
2994 return (ARCHIVE_FATAL);
2999 compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
3003 (void)a; /* UNUSED */
3004 strm = (lzma_stream *)lastrm->real_stream;
3008 lastrm->real_stream = NULL;
3009 return (ARCHIVE_OK);
3013 compression_init_encoder_lzma(struct archive *a,
3014 struct la_zstream *lastrm, int level)
3017 (void) level; /* UNUSED */
3019 compression_end(a, lastrm);
3020 return (compression_unsupported_encoder(a, lastrm, "lzma"));
3023 compression_init_encoder_xz(struct archive *a,
3024 struct la_zstream *lastrm, int level, int threads)
3027 (void) level; /* UNUSED */
3028 (void) threads; /* UNUSED */
3030 compression_end(a, lastrm);
3031 return (compression_unsupported_encoder(a, lastrm, "xz"));
3036 xar_compression_init_encoder(struct archive_write *a)
3041 xar = (struct xar *)a->format_data;
3042 switch (xar->opt_compression) {
3044 r = compression_init_encoder_gzip(
3045 &(a->archive), &(xar->stream),
3046 xar->opt_compression_level, 1);
3049 r = compression_init_encoder_bzip2(
3050 &(a->archive), &(xar->stream),
3051 xar->opt_compression_level);
3054 r = compression_init_encoder_lzma(
3055 &(a->archive), &(xar->stream),
3056 xar->opt_compression_level);
3059 r = compression_init_encoder_xz(
3060 &(a->archive), &(xar->stream),
3061 xar->opt_compression_level, xar->opt_threads);
3067 if (r == ARCHIVE_OK) {
3068 xar->stream.total_in = 0;
3069 xar->stream.next_out = xar->wbuff;
3070 xar->stream.avail_out = sizeof(xar->wbuff);
3071 xar->stream.total_out = 0;
3078 compression_code(struct archive *a, struct la_zstream *lastrm,
3079 enum la_zaction action)
3082 return (lastrm->code(a, lastrm, action));
3083 return (ARCHIVE_OK);
3087 compression_end(struct archive *a, struct la_zstream *lastrm)
3090 return (lastrm->end(a, lastrm));
3091 return (ARCHIVE_OK);
3096 save_xattrs(struct archive_write *a, struct file *file)
3101 struct heap_data *heap;
3105 xar = (struct xar *)a->format_data;
3106 count = archive_entry_xattr_reset(file->entry);
3108 return (ARCHIVE_OK);
3110 archive_entry_xattr_next(file->entry,
3111 &name, &value, &size);
3112 checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
3113 checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
3115 heap = calloc(1, sizeof(*heap));
3117 archive_set_error(&a->archive, ENOMEM,
3118 "Can't allocate memory for xattr");
3119 return (ARCHIVE_FATAL);
3121 heap->id = file->ea_idx++;
3122 heap->temp_offset = xar->temp_offset;
3123 heap->size = size;/* save a extracted size */
3124 heap->compression = xar->opt_compression;
3125 /* Get a extracted sumcheck value. */
3126 checksum_update(&(xar->e_sumwrk), value, size);
3127 checksum_final(&(xar->e_sumwrk), &(heap->e_sum));
3130 * Not compression to xattr is simple way.
3132 if (heap->compression == NONE) {
3133 checksum_update(&(xar->a_sumwrk), value, size);
3134 checksum_final(&(xar->a_sumwrk), &(heap->a_sum));
3135 if (write_to_temp(a, value, size)
3138 return (ARCHIVE_FATAL);
3140 heap->length = size;
3141 /* Add heap to the tail of file->xattr. */
3143 *file->xattr.last = heap;
3144 file->xattr.last = &(heap->next);
3150 * Init compression library.
3152 r = xar_compression_init_encoder(a);
3153 if (r != ARCHIVE_OK) {
3155 return (ARCHIVE_FATAL);
3158 xar->stream.next_in = (const unsigned char *)value;
3159 xar->stream.avail_in = size;
3161 r = compression_code(&(a->archive),
3162 &(xar->stream), ARCHIVE_Z_FINISH);
3163 if (r != ARCHIVE_OK && r != ARCHIVE_EOF) {
3165 return (ARCHIVE_FATAL);
3167 size = sizeof(xar->wbuff) - xar->stream.avail_out;
3168 checksum_update(&(xar->a_sumwrk),
3170 if (write_to_temp(a, xar->wbuff, size)
3172 return (ARCHIVE_FATAL);
3173 if (r == ARCHIVE_OK) {
3174 xar->stream.next_out = xar->wbuff;
3175 xar->stream.avail_out = sizeof(xar->wbuff);
3177 checksum_final(&(xar->a_sumwrk),
3179 heap->length = xar->stream.total_out;
3180 /* Add heap to the tail of file->xattr. */
3182 *file->xattr.last = heap;
3183 file->xattr.last = &(heap->next);
3187 /* Clean up compression library. */
3188 r = compression_end(&(a->archive), &(xar->stream));
3189 if (r != ARCHIVE_OK)
3190 return (ARCHIVE_FATAL);
3192 return (ARCHIVE_OK);
3196 getalgsize(enum sumalg sumalg)
3210 getalgname(enum sumalg sumalg)
3223 #endif /* Support xar format */