2 * Copyright (c) 2010-2011 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>
54 #include "archive_crypto_private.h"
55 #include "archive_endian.h"
56 #include "archive_entry.h"
57 #include "archive_entry_locale.h"
58 #include "archive_private.h"
59 #include "archive_rb.h"
60 #include "archive_string.h"
61 #include "archive_write_private.h"
64 * Differences to xar utility.
65 * - Subdocument is not supported yet.
66 * - ACL is not supported yet.
67 * - When writing an XML element <link type="<file-type>">, <file-type>
68 * which is a file type a symbolic link is referencing is always marked
69 * as "broken". Xar utility uses stat(2) to get the file type, but, in
70 * libarcive format writer, we should not use it; if it is needed, we
71 * should get about it at archive_read_disk.c.
72 * - It is possible to appear both <flags> and <ext2> elements.
73 * Xar utility generates <flags> on BSD platform and <ext2> on Linux
78 #if !(defined(HAVE_LIBXML_XMLWRITER_H) && defined(LIBXML_VERSION) &&\
79 LIBXML_VERSION >= 20703) ||\
80 !defined(HAVE_ZLIB_H) || \
81 !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
83 * xar needs several external libraries.
85 * o openssl or MD5/SHA1 hash function
91 archive_write_set_format_xar(struct archive *_a)
93 struct archive_write *a = (struct archive_write *)_a;
95 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
96 "Xar not supported on this platform");
97 return (ARCHIVE_WARN);
100 #else /* Support xar format */
102 /*#define DEBUG_PRINT_TOC 1 */
104 #define BAD_CAST_CONST (const xmlChar *)
106 #define HEADER_MAGIC 0x78617221
107 #define HEADER_SIZE 28
108 #define HEADER_VERSION 1
118 #define MAX_SUM_SIZE 20
119 #define MD5_NAME "md5"
120 #define SHA1_NAME "sha1"
132 #ifdef ARCHIVE_HAS_MD5
133 archive_md5_ctx md5ctx;
135 #ifdef ARCHIVE_HAS_SHA1
136 archive_sha1_ctx sha1ctx;
149 const unsigned char *next_in;
153 unsigned char *next_out;
159 int (*code) (struct archive *a,
160 struct la_zstream *lastrm,
161 enum la_zaction action);
162 int (*end)(struct archive *a,
163 struct la_zstream *lastrm);
169 unsigned char val[MAX_SUM_SIZE];
174 struct heap_data *next;
175 uint64_t temp_offset;
176 uint64_t length; /* archived size. */
177 uint64_t size; /* extracted size. */
178 enum enctype compression;
179 struct chksumval a_sum; /* archived checksum. */
180 struct chksumval e_sum; /* extracted checksum. */
184 struct archive_rb_node rbnode;
187 struct archive_entry *entry;
189 struct archive_rb_tree rbtree;
193 /* For hardlinked files.
194 * Use only when archive_entry_nlink() > 1 */
195 struct file *hardlink_target;
196 struct file *parent; /* parent directory entry */
198 * To manage sub directory files.
199 * We use 'chnext' a menber of struct file to chain.
206 /* For making a directory tree. */
207 struct archive_string parentdir;
208 struct archive_string basename;
209 struct archive_string symlink;
213 struct heap_data *first;
214 struct heap_data **last;
216 struct heap_data data;
217 struct archive_string script;
224 struct archive_rb_node rbnode;
234 uint64_t temp_offset;
238 struct file *cur_dirent;
239 struct archive_string cur_dirstr;
240 struct file *cur_file;
241 uint64_t bytes_remaining;
242 struct archive_string tstr;
243 struct archive_string vstr;
245 enum sumalg opt_toc_sumalg;
246 enum sumalg opt_sumalg;
247 enum enctype opt_compression;
248 int opt_compression_level;
250 struct chksumwork a_sumwrk; /* archived checksum. */
251 struct chksumwork e_sumwrk; /* extracted checksum. */
252 struct la_zstream stream;
253 struct archive_string_conv *sconv;
255 * Compressed data buffer.
257 unsigned char wbuff[1024 * 64];
258 size_t wbuff_remaining;
260 struct heap_data toc;
262 * The list of all file entries is used to manage struct file
264 * We use 'next' a menber of struct file to chain.
271 * The list of hard-linked file entries.
272 * We use 'hlnext' a menber of struct file to chain.
274 struct archive_rb_tree hardlink_rbtree;
277 static int xar_options(struct archive_write *,
278 const char *, const char *);
279 static int xar_write_header(struct archive_write *,
280 struct archive_entry *);
281 static ssize_t xar_write_data(struct archive_write *,
282 const void *, size_t);
283 static int xar_finish_entry(struct archive_write *);
284 static int xar_close(struct archive_write *);
285 static int xar_free(struct archive_write *);
287 static struct file *file_new(struct archive_write *a, struct archive_entry *);
288 static void file_free(struct file *);
289 static struct file *file_create_virtual_dir(struct archive_write *a, struct xar *,
291 static int file_add_child_tail(struct file *, struct file *);
292 static struct file *file_find_child(struct file *, const char *);
293 static int file_gen_utility_names(struct archive_write *,
295 static int get_path_component(char *, int, const char *);
296 static int file_tree(struct archive_write *, struct file **);
297 static void file_register(struct xar *, struct file *);
298 static void file_init_register(struct xar *);
299 static void file_free_register(struct xar *);
300 static int file_register_hardlink(struct archive_write *,
302 static void file_connect_hardlink_files(struct xar *);
303 static void file_init_hardlinks(struct xar *);
304 static void file_free_hardlinks(struct xar *);
306 static void checksum_init(struct chksumwork *, enum sumalg);
307 static void checksum_update(struct chksumwork *, const void *, size_t);
308 static void checksum_final(struct chksumwork *, struct chksumval *);
309 static int compression_init_encoder_gzip(struct archive *,
310 struct la_zstream *, int, int);
311 static int compression_code_gzip(struct archive *,
312 struct la_zstream *, enum la_zaction);
313 static int compression_end_gzip(struct archive *, struct la_zstream *);
314 static int compression_init_encoder_bzip2(struct archive *,
315 struct la_zstream *, int);
316 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
317 static int compression_code_bzip2(struct archive *,
318 struct la_zstream *, enum la_zaction);
319 static int compression_end_bzip2(struct archive *, struct la_zstream *);
321 static int compression_init_encoder_lzma(struct archive *,
322 struct la_zstream *, int);
323 static int compression_init_encoder_xz(struct archive *,
324 struct la_zstream *, int);
325 #if defined(HAVE_LZMA_H)
326 static int compression_code_lzma(struct archive *,
327 struct la_zstream *, enum la_zaction);
328 static int compression_end_lzma(struct archive *, struct la_zstream *);
330 static int xar_compression_init_encoder(struct archive_write *);
331 static int compression_code(struct archive *,
332 struct la_zstream *, enum la_zaction);
333 static int compression_end(struct archive *,
334 struct la_zstream *);
335 static int save_xattrs(struct archive_write *, struct file *);
336 static int getalgsize(enum sumalg);
337 static const char *getalgname(enum sumalg);
340 archive_write_set_format_xar(struct archive *_a)
342 struct archive_write *a = (struct archive_write *)_a;
345 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
346 ARCHIVE_STATE_NEW, "archive_write_set_format_xar");
348 /* If another format was already registered, unregister it. */
349 if (a->format_free != NULL)
352 xar = calloc(1, sizeof(*xar));
354 archive_set_error(&a->archive, ENOMEM,
355 "Can't allocate xar data");
356 return (ARCHIVE_FATAL);
359 file_init_register(xar);
360 file_init_hardlinks(xar);
361 archive_string_init(&(xar->tstr));
362 archive_string_init(&(xar->vstr));
365 * Create the root directory.
367 xar->root = file_create_virtual_dir(a, xar, "");
368 if (xar->root == NULL) {
370 archive_set_error(&a->archive, ENOMEM,
371 "Can't allocate xar data");
372 return (ARCHIVE_FATAL);
374 xar->root->parent = xar->root;
375 file_register(xar, xar->root);
376 xar->cur_dirent = xar->root;
377 archive_string_init(&(xar->cur_dirstr));
378 archive_string_ensure(&(xar->cur_dirstr), 1);
379 xar->cur_dirstr.s[0] = 0;
384 /* Set default checksum type. */
385 xar->opt_toc_sumalg = CKSUM_SHA1;
386 xar->opt_sumalg = CKSUM_SHA1;
387 /* Set default compression type and level. */
388 xar->opt_compression = GZIP;
389 xar->opt_compression_level = 6;
391 a->format_data = xar;
393 a->format_name = "xar";
394 a->format_options = xar_options;
395 a->format_write_header = xar_write_header;
396 a->format_write_data = xar_write_data;
397 a->format_finish_entry = xar_finish_entry;
398 a->format_close = xar_close;
399 a->format_free = xar_free;
400 a->archive.archive_format = ARCHIVE_FORMAT_XAR;
401 a->archive.archive_format_name = "xar";
407 xar_options(struct archive_write *a, const char *key, const char *value)
411 xar = (struct xar *)a->format_data;
413 if (strcmp(key, "checksum") == 0) {
415 xar->opt_sumalg = CKSUM_NONE;
416 else if (strcmp(value, "sha1") == 0)
417 xar->opt_sumalg = CKSUM_SHA1;
418 else if (strcmp(value, "md5") == 0)
419 xar->opt_sumalg = CKSUM_MD5;
421 archive_set_error(&(a->archive),
423 "Unkonwn checksum name: `%s'",
425 return (ARCHIVE_FAILED);
429 if (strcmp(key, "compression") == 0) {
430 const char *name = NULL;
433 xar->opt_compression = NONE;
434 else if (strcmp(value, "gzip") == 0)
435 xar->opt_compression = GZIP;
436 else if (strcmp(value, "bzip2") == 0)
437 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
438 xar->opt_compression = BZIP2;
442 else if (strcmp(value, "lzma") == 0)
444 xar->opt_compression = LZMA;
448 else if (strcmp(value, "xz") == 0)
450 xar->opt_compression = XZ;
455 archive_set_error(&(a->archive),
457 "Unkonwn compression name: `%s'",
459 return (ARCHIVE_FAILED);
462 archive_set_error(&(a->archive),
464 "`%s' compression not supported "
467 return (ARCHIVE_FAILED);
471 if (strcmp(key, "compression-level") == 0) {
473 !(value[0] >= '0' && value[0] <= '9') ||
475 archive_set_error(&(a->archive),
477 "Illeagal value `%s'",
479 return (ARCHIVE_FAILED);
481 xar->opt_compression_level = value[0] - '0';
484 if (strcmp(key, "toc-checksum") == 0) {
486 xar->opt_toc_sumalg = CKSUM_NONE;
487 else if (strcmp(value, "sha1") == 0)
488 xar->opt_toc_sumalg = CKSUM_SHA1;
489 else if (strcmp(value, "md5") == 0)
490 xar->opt_toc_sumalg = CKSUM_MD5;
492 archive_set_error(&(a->archive),
494 "Unkonwn checksum name: `%s'",
496 return (ARCHIVE_FAILED);
501 return (ARCHIVE_FAILED);
505 xar_write_header(struct archive_write *a, struct archive_entry *entry)
509 struct archive_entry *file_entry;
512 xar = (struct xar *)a->format_data;
513 xar->cur_file = NULL;
514 xar->bytes_remaining = 0;
516 if (xar->sconv == NULL) {
517 xar->sconv = archive_string_conversion_to_charset(
518 &a->archive, "UTF-8", 1);
519 if (xar->sconv == NULL)
520 return (ARCHIVE_FATAL);
523 file = file_new(a, entry);
525 archive_set_error(&a->archive, ENOMEM,
526 "Can't allocate data");
527 return (ARCHIVE_FATAL);
529 r2 = file_gen_utility_names(a, file);
530 if (r2 < ARCHIVE_WARN)
534 * Ignore a path which looks like the top of directory name
535 * since we have already made the root directory of an Xar archive.
537 if (archive_strlen(&(file->parentdir)) == 0 &&
538 archive_strlen(&(file->basename)) == 0) {
543 /* Add entry into tree */
544 file_entry = file->entry;
545 r = file_tree(a, &file);
548 /* There is the same file in tree and
549 * the current file is older than the file in tree.
550 * So we don't need the current file data anymore. */
551 if (file->entry != file_entry)
554 file_register(xar, file);
556 /* A virtual file, which is a directory, does not have
557 * any contents and we won't store it into a archive
558 * file other than its name. */
563 * Prepare to save the contents of the file.
565 if (xar->temp_fd == -1) {
567 xar->temp_offset = 0;
568 xar->temp_fd = __archive_mktemp(NULL);
569 if (xar->temp_fd < 0) {
570 archive_set_error(&a->archive, errno,
571 "Couldn't create temporary file");
572 return (ARCHIVE_FATAL);
574 algsize = getalgsize(xar->opt_toc_sumalg);
576 if (lseek(xar->temp_fd, algsize, SEEK_SET) < 0) {
577 archive_set_error(&(a->archive), errno,
579 return (ARCHIVE_FATAL);
581 xar->temp_offset = algsize;
585 if (archive_entry_hardlink(file->entry) == NULL) {
586 r = save_xattrs(a, file);
588 return (ARCHIVE_FATAL);
591 /* Non regular files contents are unneeded to be saved to
592 * a temporary file. */
593 if (archive_entry_filetype(file->entry) != AE_IFREG)
597 * Set the current file to cur_file to read its contents.
599 xar->cur_file = file;
601 if (archive_entry_nlink(file->entry) > 1) {
602 r = file_register_hardlink(a, file);
605 if (archive_entry_hardlink(file->entry) != NULL) {
606 archive_entry_unset_size(file->entry);
611 /* Save a offset of current file in temporary file. */
612 file->data.temp_offset = xar->temp_offset;
613 file->data.size = archive_entry_size(file->entry);
614 file->data.compression = xar->opt_compression;
615 xar->bytes_remaining = archive_entry_size(file->entry);
616 checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
617 checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
618 r = xar_compression_init_encoder(a);
627 write_to_temp(struct archive_write *a, const void *buff, size_t s)
630 const unsigned char *p;
633 xar = (struct xar *)a->format_data;
634 p = (const unsigned char *)buff;
636 ws = write(xar->temp_fd, p, s);
638 archive_set_error(&(a->archive), errno,
639 "fwrite function failed");
640 return (ARCHIVE_FATAL);
644 xar->temp_offset += ws;
650 xar_write_data(struct archive_write *a, const void *buff, size_t s)
657 xar = (struct xar *)a->format_data;
659 if (s > xar->bytes_remaining)
660 s = xar->bytes_remaining;
661 if (s == 0 || xar->cur_file == NULL)
663 if (xar->cur_file->data.compression == NONE) {
664 checksum_update(&(xar->e_sumwrk), buff, s);
665 checksum_update(&(xar->a_sumwrk), buff, s);
668 xar->stream.next_in = (const unsigned char *)buff;
669 xar->stream.avail_in = s;
670 if (xar->bytes_remaining > s)
673 run = ARCHIVE_Z_FINISH;
674 /* Compress file data. */
675 r = compression_code(&(a->archive), &(xar->stream), run);
676 if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
677 return (ARCHIVE_FATAL);
678 rsize = s - xar->stream.avail_in;
679 checksum_update(&(xar->e_sumwrk), buff, rsize);
680 size = sizeof(xar->wbuff) - xar->stream.avail_out;
681 checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
683 #if !defined(_WIN32) || defined(__CYGWIN__)
684 if (xar->bytes_remaining ==
685 (uint64_t)archive_entry_size(xar->cur_file->entry)) {
687 * Get the path of a shell script if so.
689 const unsigned char *b = (const unsigned char *)buff;
691 archive_string_empty(&(xar->cur_file->script));
692 if (rsize > 2 && b[0] == '#' && b[1] == '!') {
699 if ((rsize - off) > PATH_MAX)
700 end = off + PATH_MAX;
704 /* Find the end of a script path. */
705 for (i = off; i < end && b[i] != '\0' &&
706 b[i] != '\n' && b[i] != '\r' &&
707 b[i] != ' ' && b[i] != '\t'; i++)
709 archive_strncpy(&(xar->cur_file->script), b + off,
715 if (xar->cur_file->data.compression == NONE) {
716 if (write_to_temp(a, buff, size) != ARCHIVE_OK)
717 return (ARCHIVE_FATAL);
719 if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
720 return (ARCHIVE_FATAL);
722 xar->bytes_remaining -= rsize;
723 xar->cur_file->data.length += size;
729 xar_finish_entry(struct archive_write *a)
736 xar = (struct xar *)a->format_data;
737 if (xar->cur_file == NULL)
740 while (xar->bytes_remaining > 0) {
741 s = xar->bytes_remaining;
742 if (s > a->null_length)
744 w = xar_write_data(a, a->nulls, s);
746 xar->bytes_remaining -= w;
750 file = xar->cur_file;
751 checksum_final(&(xar->e_sumwrk), &(file->data.e_sum));
752 checksum_final(&(xar->a_sumwrk), &(file->data.a_sum));
753 xar->cur_file = NULL;
759 xmlwrite_string_attr(struct archive_write *a, xmlTextWriterPtr writer,
760 const char *key, const char *value,
761 const char *attrkey, const char *attrvalue)
765 r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(key));
767 archive_set_error(&a->archive,
769 "xmlTextWriterStartElement() failed: %d", r);
770 return (ARCHIVE_FATAL);
772 if (attrkey != NULL && attrvalue != NULL) {
773 r = xmlTextWriterWriteAttribute(writer,
774 BAD_CAST_CONST(attrkey), BAD_CAST_CONST(attrvalue));
776 archive_set_error(&a->archive,
778 "xmlTextWriterWriteAttribute() failed: %d", r);
779 return (ARCHIVE_FATAL);
783 r = xmlTextWriterWriteString(writer, BAD_CAST_CONST(value));
785 archive_set_error(&a->archive,
787 "xmlTextWriterWriteString() failed: %d", r);
788 return (ARCHIVE_FATAL);
791 r = xmlTextWriterEndElement(writer);
793 archive_set_error(&a->archive,
795 "xmlTextWriterEndElement() failed: %d", r);
796 return (ARCHIVE_FATAL);
802 xmlwrite_string(struct archive_write *a, xmlTextWriterPtr writer,
803 const char *key, const char *value)
810 r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(key));
812 archive_set_error(&a->archive,
814 "xmlTextWriterStartElement() failed: %d", r);
815 return (ARCHIVE_FATAL);
818 r = xmlTextWriterWriteString(writer, BAD_CAST_CONST(value));
820 archive_set_error(&a->archive,
822 "xmlTextWriterWriteString() failed: %d", r);
823 return (ARCHIVE_FATAL);
826 r = xmlTextWriterEndElement(writer);
828 archive_set_error(&a->archive,
830 "xmlTextWriterEndElement() failed: %d", r);
831 return (ARCHIVE_FATAL);
837 xmlwrite_fstring(struct archive_write *a, xmlTextWriterPtr writer,
838 const char *key, const char *fmt, ...)
843 xar = (struct xar *)a->format_data;
845 archive_string_empty(&xar->vstr);
846 archive_string_vsprintf(&xar->vstr, fmt, ap);
848 return (xmlwrite_string(a, writer, key, xar->vstr.s));
852 xmlwrite_time(struct archive_write *a, xmlTextWriterPtr writer,
853 const char *key, time_t t, int z)
858 #if defined(HAVE_GMTIME_R)
860 #elif defined(HAVE__GMTIME64_S)
861 _gmtime64_s(&tm, &t);
863 memcpy(&tm, gmtime(&t), sizeof(tm));
865 memset(×tr, 0, sizeof(timestr));
866 /* Do not use %F and %T for portability. */
867 strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &tm);
869 strcat(timestr, "Z");
870 return (xmlwrite_string(a, writer, key, timestr));
874 xmlwrite_mode(struct archive_write *a, xmlTextWriterPtr writer,
875 const char *key, mode_t mode)
880 ms[1] = '0' + ((mode >> 6) & 07);
881 ms[2] = '0' + ((mode >> 3) & 07);
882 ms[3] = '0' + (mode & 07);
885 return (xmlwrite_string(a, writer, key, ms));
889 xmlwrite_sum(struct archive_write *a, xmlTextWriterPtr writer,
890 const char *key, struct chksumval *sum)
894 char buff[MAX_SUM_SIZE*2 + 1];
900 algname = getalgname(sum->alg);
901 algsize = getalgsize(sum->alg);
902 if (algname != NULL) {
903 const char *hex = "0123456789abcdef";
906 for (i = 0; i < algsize; i++) {
907 *p++ = hex[(*s >> 4)];
908 *p++ = hex[(*s & 0x0f)];
912 r = xmlwrite_string_attr(a, writer,
916 return (ARCHIVE_FATAL);
923 xmlwrite_heap(struct archive_write *a, xmlTextWriterPtr writer,
924 struct heap_data *heap)
929 r = xmlwrite_fstring(a, writer, "length", "%ju", heap->length);
931 return (ARCHIVE_FATAL);
932 r = xmlwrite_fstring(a, writer, "offset", "%ju", heap->temp_offset);
934 return (ARCHIVE_FATAL);
935 r = xmlwrite_fstring(a, writer, "size", "%ju", heap->size);
937 return (ARCHIVE_FATAL);
938 switch (heap->compression) {
940 encname = "application/x-gzip"; break;
942 encname = "application/x-bzip2"; break;
944 encname = "application/x-lzma"; break;
946 encname = "application/x-xz"; break;
948 encname = "application/octet-stream"; break;
950 r = xmlwrite_string_attr(a, writer, "encoding", NULL,
953 return (ARCHIVE_FATAL);
954 r = xmlwrite_sum(a, writer, "archived-checksum", &(heap->a_sum));
956 return (ARCHIVE_FATAL);
957 r = xmlwrite_sum(a, writer, "extracted-checksum", &(heap->e_sum));
959 return (ARCHIVE_FATAL);
964 * xar utility records fflags as following xml elements:
974 * If xar is running on BSD platform, records <flags>..</flags>;
975 * if xar is running on linux platform, records <ext2>..</ext2>;
976 * otherwise does not record.
978 * Our implements records both <flags> and <ext2> if it's necessary.
981 make_fflags_entry(struct archive_write *a, xmlTextWriterPtr writer,
982 const char *element, const char *fflags_text)
984 static const struct flagentry {
989 { "sappnd", "SystemAppend"},
990 { "sappend", "SystemAppend"},
991 { "arch", "SystemArchived"},
992 { "archived", "SystemArchived"},
993 { "schg", "SystemImmutable"},
994 { "schange", "SystemImmutable"},
995 { "simmutable", "SystemImmutable"},
996 { "nosunlnk", "SystemNoUnlink"},
997 { "nosunlink", "SystemNoUnlink"},
998 { "snapshot", "SystemSnapshot"},
999 { "uappnd", "UserAppend"},
1000 { "uappend", "UserAppend"},
1001 { "uchg", "UserImmutable"},
1002 { "uchange", "UserImmutable"},
1003 { "uimmutable", "UserImmutable"},
1004 { "nodump", "UserNoDump"},
1005 { "noopaque", "UserOpaque"},
1006 { "nouunlnk", "UserNoUnlink"},
1007 { "nouunlink", "UserNoUnlink"},
1011 { "sappnd", "AppendOnly"},
1012 { "sappend", "AppendOnly"},
1013 { "schg", "Immutable"},
1014 { "schange", "Immutable"},
1015 { "simmutable", "Immutable"},
1016 { "nodump", "NoDump"},
1017 { "nouunlnk", "Undelete"},
1018 { "nouunlink", "Undelete"},
1019 { "btree", "BTree"},
1020 { "comperr", "CompError"},
1021 { "compress", "Compress"},
1022 { "noatime", "NoAtime"},
1023 { "compdirty", "CompDirty"},
1024 { "comprblk", "CompBlock"},
1025 { "dirsync", "DirSync"},
1026 { "hashidx", "HashIndexed"},
1027 { "imagic", "iMagic"},
1028 { "journal", "Journaled"},
1029 { "securedeletion", "SecureDeletion"},
1030 { "sync", "Synchronous"},
1031 { "notail", "NoTail"},
1032 { "topdir", "TopDir"},
1033 { "reserved", "Reserved"},
1036 const struct flagentry *fe, *flagentry;
1037 #define FLAGENTRY_MAXSIZE ((sizeof(flagbsd)+sizeof(flagext2))/sizeof(flagbsd))
1038 const struct flagentry *avail[FLAGENTRY_MAXSIZE];
1042 if (strcmp(element, "ext2") == 0)
1043 flagentry = flagext2;
1045 flagentry = flagbsd;
1051 cp = strchr(p, ',');
1055 for (fe = flagentry; fe->name != NULL; fe++) {
1056 if (fe->name[cp - p] != '\0'
1057 || p[0] != fe->name[0])
1059 if (strncmp(p, fe->name, cp - p) == 0) {
1068 } while (p != NULL);
1071 r = xmlTextWriterStartElement(writer, BAD_CAST_CONST(element));
1073 archive_set_error(&a->archive,
1075 "xmlTextWriterStartElement() failed: %d", r);
1076 return (ARCHIVE_FATAL);
1078 for (i = 0; i < n; i++) {
1079 r = xmlwrite_string(a, writer,
1080 avail[i]->xarname, NULL);
1081 if (r != ARCHIVE_OK)
1085 r = xmlTextWriterEndElement(writer);
1087 archive_set_error(&a->archive,
1089 "xmlTextWriterEndElement() failed: %d", r);
1090 return (ARCHIVE_FATAL);
1093 return (ARCHIVE_OK);
1097 make_file_entry(struct archive_write *a, xmlTextWriterPtr writer,
1101 const char *filetype, *filelink, *fflags;
1102 struct archive_string linkto;
1103 struct heap_data *heap;
1109 xar = (struct xar *)a->format_data;
1113 * Make a file name entry, "<name>".
1115 l = ll = archive_strlen(&(file->basename));
1118 archive_set_error(&a->archive, ENOMEM,
1119 "Can't allocate memory");
1120 return (ARCHIVE_FATAL);
1122 r = UTF8Toisolat1(tmp, &l, BAD_CAST(file->basename.s), &ll);
1125 r = xmlTextWriterStartElement(writer, BAD_CAST("name"));
1127 archive_set_error(&a->archive,
1129 "xmlTextWriterStartElement() failed: %d", r);
1130 return (ARCHIVE_FATAL);
1132 r = xmlTextWriterWriteAttribute(writer,
1133 BAD_CAST("enctype"), BAD_CAST("base64"));
1135 archive_set_error(&a->archive,
1137 "xmlTextWriterWriteAttribute() failed: %d", r);
1138 return (ARCHIVE_FATAL);
1140 r = xmlTextWriterWriteBase64(writer, file->basename.s,
1141 0, archive_strlen(&(file->basename)));
1143 archive_set_error(&a->archive,
1145 "xmlTextWriterWriteBase64() failed: %d", r);
1146 return (ARCHIVE_FATAL);
1148 r = xmlTextWriterEndElement(writer);
1150 archive_set_error(&a->archive,
1152 "xmlTextWriterEndElement() failed: %d", r);
1153 return (ARCHIVE_FATAL);
1156 r = xmlwrite_string(a, writer, "name", file->basename.s);
1158 return (ARCHIVE_FATAL);
1162 * Make a file type entry, "<type>".
1165 archive_string_init(&linkto);
1166 switch (archive_entry_filetype(file->entry)) {
1168 filetype = "directory"; break;
1170 filetype = "symlink"; break;
1172 filetype = "character special"; break;
1174 filetype = "block special"; break;
1176 filetype = "socket"; break;
1178 filetype = "fifo"; break;
1181 if (file->hardlink_target != NULL) {
1182 filetype = "hardlink";
1184 if (file->hardlink_target == file)
1185 archive_strcpy(&linkto, "original");
1187 archive_string_sprintf(&linkto, "%d",
1188 file->hardlink_target->id);
1193 r = xmlwrite_string_attr(a, writer, "type", filetype,
1194 filelink, linkto.s);
1195 archive_string_free(&linkto);
1197 return (ARCHIVE_FATAL);
1200 * On a virtual directory, we record "name" and "type" only.
1203 return (ARCHIVE_OK);
1205 switch (archive_entry_filetype(file->entry)) {
1208 * xar utility has checked a file type, which
1209 * a symblic-link file has referenced.
1211 * <link type="directory">../ref/</link>
1212 * The symlink target file is "../ref/" and its
1213 * file type is a directory.
1215 * <link type="file">../f</link>
1216 * The symlink target file is "../f" and its
1217 * file type is a regular file.
1219 * But our implemention cannot do it, and then we
1220 * always record that a attribute "type" is "borken",
1222 * <link type="broken">foo/bar</link>
1223 * It means "foo/bar" is not reachable.
1225 r = xmlwrite_string_attr(a, writer, "link",
1229 return (ARCHIVE_FATAL);
1233 r = xmlTextWriterStartElement(writer, BAD_CAST("device"));
1235 archive_set_error(&a->archive,
1237 "xmlTextWriterStartElement() failed: %d", r);
1238 return (ARCHIVE_FATAL);
1240 r = xmlwrite_fstring(a, writer, "major",
1241 "%d", archive_entry_rdevmajor(file->entry));
1243 return (ARCHIVE_FATAL);
1244 r = xmlwrite_fstring(a, writer, "minor",
1245 "%d", archive_entry_rdevminor(file->entry));
1247 return (ARCHIVE_FATAL);
1248 r = xmlTextWriterEndElement(writer);
1250 archive_set_error(&a->archive,
1252 "xmlTextWriterEndElement() failed: %d", r);
1253 return (ARCHIVE_FATAL);
1261 * Make a inode entry, "<inode>".
1263 r = xmlwrite_fstring(a, writer, "inode",
1264 "%jd", archive_entry_ino64(file->entry));
1266 return (ARCHIVE_FATAL);
1267 if (archive_entry_dev(file->entry) != 0) {
1268 r = xmlwrite_fstring(a, writer, "deviceno",
1269 "%d", archive_entry_dev(file->entry));
1271 return (ARCHIVE_FATAL);
1275 * Make a file mode entry, "<mode>".
1277 r = xmlwrite_mode(a, writer, "mode",
1278 archive_entry_mode(file->entry));
1280 return (ARCHIVE_FATAL);
1283 * Make a user entry, "<uid>" and "<user>.
1285 r = xmlwrite_fstring(a, writer, "uid",
1286 "%d", archive_entry_uid(file->entry));
1288 return (ARCHIVE_FATAL);
1289 r = archive_entry_uname_l(file->entry, &p, &len, xar->sconv);
1291 if (errno == ENOMEM) {
1292 archive_set_error(&a->archive, ENOMEM,
1293 "Can't allocate memory for Uname");
1294 return (ARCHIVE_FATAL);
1296 archive_set_error(&a->archive,
1297 ARCHIVE_ERRNO_FILE_FORMAT,
1298 "Can't translate uname '%s' to UTF-8",
1299 archive_entry_uname(file->entry));
1303 r = xmlwrite_string(a, writer, "user", p);
1305 return (ARCHIVE_FATAL);
1309 * Make a group entry, "<gid>" and "<group>.
1311 r = xmlwrite_fstring(a, writer, "gid",
1312 "%d", archive_entry_gid(file->entry));
1314 return (ARCHIVE_FATAL);
1315 r = archive_entry_gname_l(file->entry, &p, &len, xar->sconv);
1317 if (errno == ENOMEM) {
1318 archive_set_error(&a->archive, ENOMEM,
1319 "Can't allocate memory for Gname");
1320 return (ARCHIVE_FATAL);
1322 archive_set_error(&a->archive,
1323 ARCHIVE_ERRNO_FILE_FORMAT,
1324 "Can't translate gname '%s' to UTF-8",
1325 archive_entry_gname(file->entry));
1329 r = xmlwrite_string(a, writer, "group", p);
1331 return (ARCHIVE_FATAL);
1335 * Make a ctime entry, "<ctime>".
1337 if (archive_entry_ctime_is_set(file->entry)) {
1338 r = xmlwrite_time(a, writer, "ctime",
1339 archive_entry_ctime(file->entry), 1);
1341 return (ARCHIVE_FATAL);
1345 * Make a mtime entry, "<mtime>".
1347 if (archive_entry_mtime_is_set(file->entry)) {
1348 r = xmlwrite_time(a, writer, "mtime",
1349 archive_entry_mtime(file->entry), 1);
1351 return (ARCHIVE_FATAL);
1355 * Make a atime entry, "<atime>".
1357 if (archive_entry_atime_is_set(file->entry)) {
1358 r = xmlwrite_time(a, writer, "atime",
1359 archive_entry_atime(file->entry), 1);
1361 return (ARCHIVE_FATAL);
1365 * Make fflags entries, "<flags>" and "<ext2>".
1367 fflags = archive_entry_fflags_text(file->entry);
1368 if (fflags != NULL) {
1369 r = make_fflags_entry(a, writer, "flags", fflags);
1372 r = make_fflags_entry(a, writer, "ext2", fflags);
1378 * Make extended attribute entries, "<ea>".
1380 archive_entry_xattr_reset(file->entry);
1381 for (heap = file->xattr.first; heap != NULL; heap = heap->next) {
1386 archive_entry_xattr_next(file->entry,
1387 &name, &value, &size);
1388 r = xmlTextWriterStartElement(writer, BAD_CAST("ea"));
1390 archive_set_error(&a->archive,
1392 "xmlTextWriterStartElement() failed: %d", r);
1393 return (ARCHIVE_FATAL);
1395 r = xmlTextWriterWriteFormatAttribute(writer,
1396 BAD_CAST("id"), "%d", heap->id);
1398 archive_set_error(&a->archive,
1400 "xmlTextWriterWriteAttribute() failed: %d", r);
1401 return (ARCHIVE_FATAL);
1403 r = xmlwrite_heap(a, writer, heap);
1405 return (ARCHIVE_FATAL);
1406 r = xmlwrite_string(a, writer, "name", name);
1408 return (ARCHIVE_FATAL);
1410 r = xmlTextWriterEndElement(writer);
1412 archive_set_error(&a->archive,
1414 "xmlTextWriterEndElement() failed: %d", r);
1415 return (ARCHIVE_FATAL);
1420 * Make a file data entry, "<data>".
1422 if (file->data.length > 0) {
1423 r = xmlTextWriterStartElement(writer, BAD_CAST("data"));
1425 archive_set_error(&a->archive,
1427 "xmlTextWriterStartElement() failed: %d", r);
1428 return (ARCHIVE_FATAL);
1431 r = xmlwrite_heap(a, writer, &(file->data));
1433 return (ARCHIVE_FATAL);
1435 r = xmlTextWriterEndElement(writer);
1437 archive_set_error(&a->archive,
1439 "xmlTextWriterEndElement() failed: %d", r);
1440 return (ARCHIVE_FATAL);
1444 if (archive_strlen(&file->script) > 0) {
1445 r = xmlTextWriterStartElement(writer, BAD_CAST("content"));
1447 archive_set_error(&a->archive,
1449 "xmlTextWriterStartElement() failed: %d", r);
1450 return (ARCHIVE_FATAL);
1453 r = xmlwrite_string(a, writer,
1454 "interpreter", file->script.s);
1456 return (ARCHIVE_FATAL);
1458 r = xmlwrite_string(a, writer, "type", "script");
1460 return (ARCHIVE_FATAL);
1462 r = xmlTextWriterEndElement(writer);
1464 archive_set_error(&a->archive,
1466 "xmlTextWriterEndElement() failed: %d", r);
1467 return (ARCHIVE_FATAL);
1478 make_toc(struct archive_write *a)
1483 xmlTextWriterPtr writer;
1487 xar = (struct xar *)a->format_data;
1489 ret = ARCHIVE_FATAL;
1492 * Initialize xml writer.
1495 bp = xmlBufferCreate();
1497 archive_set_error(&a->archive, ENOMEM,
1498 "xmlBufferCreate() "
1499 "couldn't create xml buffer");
1502 writer = xmlNewTextWriterMemory(bp, 0);
1503 if (writer == NULL) {
1504 archive_set_error(&a->archive,
1506 "xmlNewTextWriterMemory() "
1507 "couldn't create xml writer");
1510 r = xmlTextWriterStartDocument(writer, "1.0", "UTF-8", NULL);
1512 archive_set_error(&a->archive,
1514 "xmlTextWriterStartDocument() failed: %d", r);
1517 r = xmlTextWriterSetIndent(writer, 4);
1519 archive_set_error(&a->archive,
1521 "xmlTextWriterSetIndent() failed: %d", r);
1526 * Start recoding TOC
1528 r = xmlTextWriterStartElement(writer, BAD_CAST("xar"));
1530 archive_set_error(&a->archive,
1532 "xmlTextWriterStartElement() failed: %d", r);
1535 r = xmlTextWriterStartElement(writer, BAD_CAST("toc"));
1537 archive_set_error(&a->archive,
1539 "xmlTextWriterStartDocument() failed: %d", r);
1544 * Record the creation time of the archive file.
1546 r = xmlwrite_time(a, writer, "creation-time", time(NULL), 0);
1551 * Record the checksum value of TOC
1553 algsize = getalgsize(xar->opt_toc_sumalg);
1556 * Record TOC checksum
1558 r = xmlTextWriterStartElement(writer, BAD_CAST("checksum"));
1560 archive_set_error(&a->archive,
1562 "xmlTextWriterStartElement() failed: %d", r);
1565 r = xmlTextWriterWriteAttribute(writer, BAD_CAST("style"),
1566 BAD_CAST_CONST(getalgname(xar->opt_toc_sumalg)));
1568 archive_set_error(&a->archive,
1570 "xmlTextWriterWriteAttribute() failed: %d", r);
1575 * Record the offset of the value of checksum of TOC
1577 r = xmlwrite_string(a, writer, "offset", "0");
1582 * Record the size of the value of checksum of TOC
1584 r = xmlwrite_fstring(a, writer, "size", "%d", algsize);
1588 r = xmlTextWriterEndElement(writer);
1590 archive_set_error(&a->archive,
1592 "xmlTextWriterEndElement() failed: %d", r);
1599 if (np != np->parent) {
1600 r = make_file_entry(a, writer, np);
1601 if (r != ARCHIVE_OK)
1605 if (np->dir && np->children.first != NULL) {
1606 /* Enter to sub directories. */
1607 np = np->children.first;
1608 r = xmlTextWriterStartElement(writer,
1611 archive_set_error(&a->archive,
1613 "xmlTextWriterStartElement() "
1617 r = xmlTextWriterWriteFormatAttribute(
1618 writer, BAD_CAST("id"), "%d", np->id);
1620 archive_set_error(&a->archive,
1622 "xmlTextWriterWriteAttribute() "
1628 while (np != np->parent) {
1629 r = xmlTextWriterEndElement(writer);
1631 archive_set_error(&a->archive,
1633 "xmlTextWriterEndElement() "
1637 if (np->chnext == NULL) {
1638 /* Return to the parent directory. */
1642 r = xmlTextWriterStartElement(writer,
1645 archive_set_error(&a->archive,
1647 "xmlTextWriterStartElement() "
1651 r = xmlTextWriterWriteFormatAttribute(
1652 writer, BAD_CAST("id"), "%d", np->id);
1654 archive_set_error(&a->archive,
1656 "xmlTextWriterWriteAttribute() "
1663 } while (np != np->parent);
1665 r = xmlTextWriterEndDocument(writer);
1667 archive_set_error(&a->archive,
1669 "xmlTextWriterEndDocument() failed: %d", r);
1673 fprintf(stderr, "\n---TOC-- %d bytes --\n%s\n",
1674 strlen((const char *)bp->content), bp->content);
1678 * Compress the TOC and calculate the sum of the TOC.
1680 xar->toc.temp_offset = xar->temp_offset;
1681 xar->toc.size = bp->use;
1682 checksum_init(&(xar->a_sumwrk), xar->opt_toc_sumalg);
1684 r = compression_init_encoder_gzip(&(a->archive),
1685 &(xar->stream), 6, 1);
1686 if (r != ARCHIVE_OK)
1688 xar->stream.next_in = bp->content;
1689 xar->stream.avail_in = bp->use;
1690 xar->stream.total_in = 0;
1691 xar->stream.next_out = xar->wbuff;
1692 xar->stream.avail_out = sizeof(xar->wbuff);
1693 xar->stream.total_out = 0;
1697 r = compression_code(&(a->archive),
1698 &(xar->stream), ARCHIVE_Z_FINISH);
1699 if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
1701 size = sizeof(xar->wbuff) - xar->stream.avail_out;
1702 checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
1703 if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
1705 if (r == ARCHIVE_EOF)
1707 xar->stream.next_out = xar->wbuff;
1708 xar->stream.avail_out = sizeof(xar->wbuff);
1710 r = compression_end(&(a->archive), &(xar->stream));
1711 if (r != ARCHIVE_OK)
1713 xar->toc.length = xar->stream.total_out;
1714 xar->toc.compression = GZIP;
1715 checksum_final(&(xar->a_sumwrk), &(xar->toc.a_sum));
1720 xmlFreeTextWriter(writer);
1728 flush_wbuff(struct archive_write *a)
1734 xar = (struct xar *)a->format_data;
1735 s = sizeof(xar->wbuff) - xar->wbuff_remaining;
1736 r = __archive_write_output(a, xar->wbuff, s);
1737 if (r != ARCHIVE_OK)
1739 xar->wbuff_remaining = sizeof(xar->wbuff);
1744 copy_out(struct archive_write *a, uint64_t offset, uint64_t length)
1749 xar = (struct xar *)a->format_data;
1750 if (lseek(xar->temp_fd, offset, SEEK_SET) < 0) {
1751 archive_set_error(&(a->archive), errno, "lseek failed");
1752 return (ARCHIVE_FATAL);
1759 if (length > xar->wbuff_remaining)
1760 rsize = xar->wbuff_remaining;
1762 rsize = (size_t)length;
1763 wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
1764 rs = read(xar->temp_fd, wb, rsize);
1766 archive_set_error(&(a->archive), errno,
1767 "Can't read temporary file(%jd)",
1769 return (ARCHIVE_FATAL);
1772 archive_set_error(&(a->archive), 0,
1773 "Truncated xar archive");
1774 return (ARCHIVE_FATAL);
1776 xar->wbuff_remaining -= rs;
1778 if (xar->wbuff_remaining == 0) {
1780 if (r != ARCHIVE_OK)
1784 return (ARCHIVE_OK);
1788 xar_close(struct archive_write *a)
1795 xar = (struct xar *)a->format_data;
1798 if (xar->root->children.first == NULL)
1799 return (ARCHIVE_OK);
1801 /* Save the length of all file extended attributes and contents. */
1802 length = xar->temp_offset;
1804 /* Connect hardlinked files */
1805 file_connect_hardlink_files(xar);
1809 if (r != ARCHIVE_OK)
1812 * Make the xar header on wbuff(write buffer).
1815 xar->wbuff_remaining = sizeof(xar->wbuff);
1816 archive_be32enc(&wb[0], HEADER_MAGIC);
1817 archive_be16enc(&wb[4], HEADER_SIZE);
1818 archive_be16enc(&wb[6], HEADER_VERSION);
1819 archive_be64enc(&wb[8], xar->toc.length);
1820 archive_be64enc(&wb[16], xar->toc.size);
1821 archive_be32enc(&wb[24], xar->toc.a_sum.alg);
1822 xar->wbuff_remaining -= HEADER_SIZE;
1827 r = copy_out(a, xar->toc.temp_offset, xar->toc.length);
1828 if (r != ARCHIVE_OK)
1831 /* Write the checksum value of the TOC. */
1832 if (xar->toc.a_sum.len) {
1833 if (xar->wbuff_remaining < xar->toc.a_sum.len) {
1835 if (r != ARCHIVE_OK)
1838 wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
1839 memcpy(wb, xar->toc.a_sum.val, xar->toc.a_sum.len);
1840 xar->wbuff_remaining -= xar->toc.a_sum.len;
1844 * Write all file extended attributes and contents.
1846 r = copy_out(a, xar->toc.a_sum.len, length);
1847 if (r != ARCHIVE_OK)
1854 xar_free(struct archive_write *a)
1858 xar = (struct xar *)a->format_data;
1859 archive_string_free(&(xar->cur_dirstr));
1860 archive_string_free(&(xar->tstr));
1861 archive_string_free(&(xar->vstr));
1862 file_free_hardlinks(xar);
1863 file_free_register(xar);
1864 compression_end(&(a->archive), &(xar->stream));
1867 return (ARCHIVE_OK);
1871 file_cmp_node(const struct archive_rb_node *n1,
1872 const struct archive_rb_node *n2)
1874 const struct file *f1 = (const struct file *)n1;
1875 const struct file *f2 = (const struct file *)n2;
1877 return (strcmp(f1->basename.s, f2->basename.s));
1881 file_cmp_key(const struct archive_rb_node *n, const void *key)
1883 const struct file *f = (const struct file *)n;
1885 return (strcmp(f->basename.s, (const char *)key));
1888 static struct file *
1889 file_new(struct archive_write *a, struct archive_entry *entry)
1892 static const struct archive_rb_tree_ops rb_ops = {
1893 file_cmp_node, file_cmp_key
1896 file = calloc(1, sizeof(*file));
1901 file->entry = archive_entry_clone(entry);
1903 file->entry = archive_entry_new2(&a->archive);
1904 if (file->entry == NULL) {
1908 __archive_rb_tree_init(&(file->rbtree), &rb_ops);
1909 file->children.first = NULL;
1910 file->children.last = &(file->children.first);
1911 file->xattr.first = NULL;
1912 file->xattr.last = &(file->xattr.first);
1913 archive_string_init(&(file->parentdir));
1914 archive_string_init(&(file->basename));
1915 archive_string_init(&(file->symlink));
1916 archive_string_init(&(file->script));
1917 if (entry != NULL && archive_entry_filetype(entry) == AE_IFDIR)
1924 file_free(struct file *file)
1926 struct heap_data *heap, *next_heap;
1928 heap = file->xattr.first;
1929 while (heap != NULL) {
1930 next_heap = heap->next;
1934 archive_string_free(&(file->parentdir));
1935 archive_string_free(&(file->basename));
1936 archive_string_free(&(file->symlink));
1937 archive_string_free(&(file->script));
1941 static struct file *
1942 file_create_virtual_dir(struct archive_write *a, struct xar *xar,
1943 const char *pathname)
1947 (void)xar; /* UNUSED */
1949 file = file_new(a, NULL);
1952 archive_entry_set_pathname(file->entry, pathname);
1953 archive_entry_set_mode(file->entry, 0555 | AE_IFDIR);
1962 file_add_child_tail(struct file *parent, struct file *child)
1964 if (!__archive_rb_tree_insert_node(
1965 &(parent->rbtree), (struct archive_rb_node *)child))
1967 child->chnext = NULL;
1968 *parent->children.last = child;
1969 parent->children.last = &(child->chnext);
1970 child->parent = parent;
1975 * Find a entry from `parent'
1977 static struct file *
1978 file_find_child(struct file *parent, const char *child_name)
1982 np = (struct file *)__archive_rb_tree_find_node(
1983 &(parent->rbtree), child_name);
1987 #if defined(_WIN32) || defined(__CYGWIN__)
1989 cleanup_backslash(char *utf8, size_t len)
1992 /* Convert a path-separator from '\' to '/' */
1993 while (*utf8 != '\0' && len) {
2001 #define cleanup_backslash(p, len) /* nop */
2005 * Generate a parent directory name and a base name from a pathname.
2008 file_gen_utility_names(struct archive_write *a, struct file *file)
2012 char *p, *dirname, *slash;
2016 xar = (struct xar *)a->format_data;
2017 archive_string_empty(&(file->parentdir));
2018 archive_string_empty(&(file->basename));
2019 archive_string_empty(&(file->symlink));
2021 if (file->parent == file)/* virtual root */
2022 return (ARCHIVE_OK);
2024 if (archive_entry_pathname_l(file->entry, &pp, &len, xar->sconv)
2026 if (errno == ENOMEM) {
2027 archive_set_error(&a->archive, ENOMEM,
2028 "Can't allocate memory for Pathname");
2029 return (ARCHIVE_FATAL);
2031 archive_set_error(&a->archive,
2032 ARCHIVE_ERRNO_FILE_FORMAT,
2033 "Can't translate pathname '%s' to UTF-8",
2034 archive_entry_pathname(file->entry));
2037 archive_strncpy(&(file->parentdir), pp, len);
2038 len = file->parentdir.length;
2039 p = dirname = file->parentdir.s;
2041 * Convert a path-separator from '\' to '/'
2043 cleanup_backslash(p, len);
2046 * Remove leading '/', '../' and './' elements
2052 } else if (p[0] != '.')
2054 else if (p[1] == '.' && p[2] == '/') {
2057 } else if (p[1] == '/' || (p[1] == '.' && p[2] == '\0')) {
2060 } else if (p[1] == '\0') {
2067 memmove(dirname, p, len+1);
2071 * Remove "/","/." and "/.." elements from tail.
2076 if (len > 0 && p[len-1] == '/') {
2080 if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
2084 if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
2095 /* Convert '//' --> '/' */
2097 else if (p[1] == '.' && p[2] == '/')
2098 /* Convert '/./' --> '/' */
2100 else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
2101 /* Convert 'dir/dir1/../dir2/'
2105 while (rp >= dirname) {
2114 strcpy(dirname, p+4);
2125 if (archive_entry_filetype(file->entry) == AE_IFLNK) {
2127 /* Convert symlink name too. */
2128 if (archive_entry_symlink_l(file->entry, &pp, &len2,
2130 if (errno == ENOMEM) {
2131 archive_set_error(&a->archive, ENOMEM,
2132 "Can't allocate memory for Linkname");
2133 return (ARCHIVE_FATAL);
2135 archive_set_error(&a->archive,
2136 ARCHIVE_ERRNO_FILE_FORMAT,
2137 "Can't translate symlink '%s' to UTF-8",
2138 archive_entry_symlink(file->entry));
2141 archive_strncpy(&(file->symlink), pp, len2);
2142 cleanup_backslash(file->symlink.s, file->symlink.length);
2145 * - Count up directory elements.
2146 * - Find out the position which points the last position of
2147 * path separator('/').
2150 for (; *p != '\0'; p++)
2153 if (slash == NULL) {
2154 /* The pathname doesn't have a parent directory. */
2155 file->parentdir.length = len;
2156 archive_string_copy(&(file->basename), &(file->parentdir));
2157 archive_string_empty(&(file->parentdir));
2158 file->parentdir.s = '\0';
2162 /* Make a basename from dirname and slash */
2164 file->parentdir.length = slash - dirname;
2165 archive_strcpy(&(file->basename), slash + 1);
2170 get_path_component(char *name, int n, const char *fn)
2175 p = strchr(fn, '/');
2177 if ((l = strlen(fn)) == 0)
2183 memcpy(name, fn, l);
2190 * Add a new entry into the tree.
2193 file_tree(struct archive_write *a, struct file **filepp)
2195 #if defined(_WIN32) && !defined(__CYGWIN__)
2196 char name[_MAX_FNAME];/* Included null terminator size. */
2197 #elif defined(NAME_MAX) && NAME_MAX >= 255
2198 char name[NAME_MAX+1];
2202 struct xar *xar = (struct xar *)a->format_data;
2203 struct file *dent, *file, *np;
2204 struct archive_entry *ent;
2210 if (file->parentdir.length > 0)
2211 fn = p = file->parentdir.s;
2216 * If the path of the parent directory of `file' entry is
2217 * the same as the path of `cur_dirent', add isoent to
2220 if (archive_strlen(&(xar->cur_dirstr))
2221 == archive_strlen(&(file->parentdir)) &&
2222 strcmp(xar->cur_dirstr.s, fn) == 0) {
2223 if (!file_add_child_tail(xar->cur_dirent, file)) {
2224 np = (struct file *)__archive_rb_tree_find_node(
2225 &(xar->cur_dirent->rbtree),
2229 return (ARCHIVE_OK);
2233 l = get_path_component(name, sizeof(name), fn);
2239 archive_set_error(&a->archive,
2241 "A name buffer is too small");
2244 return (ARCHIVE_FATAL);
2247 np = file_find_child(dent, name);
2248 if (np == NULL || fn[0] == '\0')
2251 /* Find next subdirectory. */
2253 /* NOT Directory! */
2254 archive_set_error(&a->archive,
2256 "`%s' is not directory, we cannot insert `%s' ",
2257 archive_entry_pathname(np->entry),
2258 archive_entry_pathname(file->entry));
2261 return (ARCHIVE_FAILED);
2270 * Create virtual parent directories.
2272 while (fn[0] != '\0') {
2274 struct archive_string as;
2276 archive_string_init(&as);
2277 archive_strncat(&as, p, fn - p + l);
2278 if (as.s[as.length-1] == '/') {
2279 as.s[as.length-1] = '\0';
2282 vp = file_create_virtual_dir(a, xar, as.s);
2284 archive_string_free(&as);
2285 archive_set_error(&a->archive, ENOMEM,
2286 "Can't allocate memory");
2289 return (ARCHIVE_FATAL);
2291 archive_string_free(&as);
2292 if (file_gen_utility_names(a, vp) <= ARCHIVE_FAILED)
2293 return (ARCHIVE_FATAL);
2294 file_add_child_tail(dent, vp);
2295 file_register(xar, vp);
2301 l = get_path_component(name, sizeof(name), fn);
2303 archive_string_free(&as);
2304 archive_set_error(&a->archive,
2306 "A name buffer is too small");
2309 return (ARCHIVE_FATAL);
2314 /* Found out the parent directory where isoent can be
2316 xar->cur_dirent = dent;
2317 archive_string_empty(&(xar->cur_dirstr));
2318 archive_string_ensure(&(xar->cur_dirstr),
2319 archive_strlen(&(dent->parentdir)) +
2320 archive_strlen(&(dent->basename)) + 2);
2321 if (archive_strlen(&(dent->parentdir)) +
2322 archive_strlen(&(dent->basename)) == 0)
2323 xar->cur_dirstr.s[0] = 0;
2325 if (archive_strlen(&(dent->parentdir)) > 0) {
2326 archive_string_copy(&(xar->cur_dirstr),
2327 &(dent->parentdir));
2328 archive_strappend_char(&(xar->cur_dirstr), '/');
2330 archive_string_concat(&(xar->cur_dirstr),
2334 if (!file_add_child_tail(dent, file)) {
2335 np = (struct file *)__archive_rb_tree_find_node(
2336 &(dent->rbtree), file->basename.s);
2339 return (ARCHIVE_OK);
2344 * We have already has the entry the filename of which is
2347 if (archive_entry_filetype(np->entry) !=
2348 archive_entry_filetype(file->entry)) {
2349 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2350 "Found duplicate entries `%s' and its file type is "
2352 archive_entry_pathname(np->entry));
2355 return (ARCHIVE_FAILED);
2360 np->entry = file->entry;
2366 return (ARCHIVE_OK);
2370 file_register(struct xar *xar, struct file *file)
2372 file->id = xar->file_idx++;
2374 *xar->file_list.last = file;
2375 xar->file_list.last = &(file->next);
2379 file_init_register(struct xar *xar)
2381 xar->file_list.first = NULL;
2382 xar->file_list.last = &(xar->file_list.first);
2386 file_free_register(struct xar *xar)
2388 struct file *file, *file_next;
2390 file = xar->file_list.first;
2391 while (file != NULL) {
2392 file_next = file->next;
2399 * Register entry to get a hardlink target.
2402 file_register_hardlink(struct archive_write *a, struct file *file)
2404 struct xar *xar = (struct xar *)a->format_data;
2405 struct hardlink *hl;
2406 const char *pathname;
2408 archive_entry_set_nlink(file->entry, 1);
2409 pathname = archive_entry_hardlink(file->entry);
2410 if (pathname == NULL) {
2411 /* This `file` is a hardlink target. */
2412 hl = malloc(sizeof(*hl));
2414 archive_set_error(&a->archive, ENOMEM,
2415 "Can't allocate memory");
2416 return (ARCHIVE_FATAL);
2419 /* A hardlink target must be the first position. */
2420 file->hlnext = NULL;
2421 hl->file_list.first = file;
2422 hl->file_list.last = &(file->hlnext);
2423 __archive_rb_tree_insert_node(&(xar->hardlink_rbtree),
2424 (struct archive_rb_node *)hl);
2426 hl = (struct hardlink *)__archive_rb_tree_find_node(
2427 &(xar->hardlink_rbtree), pathname);
2429 /* Insert `file` entry into the tail. */
2430 file->hlnext = NULL;
2431 *hl->file_list.last = file;
2432 hl->file_list.last = &(file->hlnext);
2435 archive_entry_unset_size(file->entry);
2438 return (ARCHIVE_OK);
2442 * Hardlinked files have to have the same location of extent.
2443 * We have to find out hardlink target entries for entries which
2444 * have a hardlink target name.
2447 file_connect_hardlink_files(struct xar *xar)
2449 struct archive_rb_node *n;
2450 struct hardlink *hl;
2451 struct file *target, *nf;
2453 ARCHIVE_RB_TREE_FOREACH(n, &(xar->hardlink_rbtree)) {
2454 hl = (struct hardlink *)n;
2456 /* The first entry must be a hardlink target. */
2457 target = hl->file_list.first;
2458 archive_entry_set_nlink(target->entry, hl->nlink);
2460 /* It means this file is a hardlink
2462 target->hardlink_target = target;
2463 for (nf = target->hlnext;
2464 nf != NULL; nf = nf->hlnext) {
2465 nf->hardlink_target = target;
2466 archive_entry_set_nlink(nf->entry, hl->nlink);
2472 file_hd_cmp_node(const struct archive_rb_node *n1,
2473 const struct archive_rb_node *n2)
2475 const struct hardlink *h1 = (const struct hardlink *)n1;
2476 const struct hardlink *h2 = (const struct hardlink *)n2;
2478 return (strcmp(archive_entry_pathname(h1->file_list.first->entry),
2479 archive_entry_pathname(h2->file_list.first->entry)));
2483 file_hd_cmp_key(const struct archive_rb_node *n, const void *key)
2485 const struct hardlink *h = (const struct hardlink *)n;
2487 return (strcmp(archive_entry_pathname(h->file_list.first->entry),
2488 (const char *)key));
2493 file_init_hardlinks(struct xar *xar)
2495 static const struct archive_rb_tree_ops rb_ops = {
2496 file_hd_cmp_node, file_hd_cmp_key,
2499 __archive_rb_tree_init(&(xar->hardlink_rbtree), &rb_ops);
2503 file_free_hardlinks(struct xar *xar)
2505 struct archive_rb_node *n, *next;
2507 for (n = ARCHIVE_RB_TREE_MIN(&(xar->hardlink_rbtree)); n;) {
2508 next = __archive_rb_tree_iterate(&(xar->hardlink_rbtree),
2509 n, ARCHIVE_RB_DIR_RIGHT);
2516 checksum_init(struct chksumwork *sumwrk, enum sumalg sum_alg)
2518 sumwrk->alg = sum_alg;
2523 archive_sha1_init(&(sumwrk->sha1ctx));
2526 archive_md5_init(&(sumwrk->md5ctx));
2532 checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
2535 switch (sumwrk->alg) {
2539 archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
2542 archive_md5_update(&(sumwrk->md5ctx), buff, size);
2548 checksum_final(struct chksumwork *sumwrk, struct chksumval *sumval)
2551 switch (sumwrk->alg) {
2556 archive_sha1_final(&(sumwrk->sha1ctx), sumval->val);
2557 sumval->len = SHA1_SIZE;
2560 archive_md5_final(&(sumwrk->md5ctx), sumval->val);
2561 sumval->len = MD5_SIZE;
2564 sumval->alg = sumwrk->alg;
2567 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
2569 compression_unsupported_encoder(struct archive *a,
2570 struct la_zstream *lastrm, const char *name)
2573 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2574 "%s compression not supported on this platform", name);
2576 lastrm->real_stream = NULL;
2577 return (ARCHIVE_FAILED);
2582 compression_init_encoder_gzip(struct archive *a,
2583 struct la_zstream *lastrm, int level, int withheader)
2588 compression_end(a, lastrm);
2589 strm = calloc(1, sizeof(*strm));
2591 archive_set_error(a, ENOMEM,
2592 "Can't allocate memory for gzip stream");
2593 return (ARCHIVE_FATAL);
2595 /* zlib.h is not const-correct, so we need this one bit
2596 * of ugly hackery to convert a const * pointer to
2597 * a non-const pointer. */
2598 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2599 strm->avail_in = lastrm->avail_in;
2600 strm->total_in = lastrm->total_in;
2601 strm->next_out = lastrm->next_out;
2602 strm->avail_out = lastrm->avail_out;
2603 strm->total_out = lastrm->total_out;
2604 if (deflateInit2(strm, level, Z_DEFLATED,
2605 (withheader)?15:-15,
2606 8, Z_DEFAULT_STRATEGY) != Z_OK) {
2608 lastrm->real_stream = NULL;
2609 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2610 "Internal error initializing compression library");
2611 return (ARCHIVE_FATAL);
2613 lastrm->real_stream = strm;
2615 lastrm->code = compression_code_gzip;
2616 lastrm->end = compression_end_gzip;
2617 return (ARCHIVE_OK);
2621 compression_code_gzip(struct archive *a,
2622 struct la_zstream *lastrm, enum la_zaction action)
2627 strm = (z_stream *)lastrm->real_stream;
2628 /* zlib.h is not const-correct, so we need this one bit
2629 * of ugly hackery to convert a const * pointer to
2630 * a non-const pointer. */
2631 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
2632 strm->avail_in = lastrm->avail_in;
2633 strm->total_in = lastrm->total_in;
2634 strm->next_out = lastrm->next_out;
2635 strm->avail_out = lastrm->avail_out;
2636 strm->total_out = lastrm->total_out;
2638 (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
2639 lastrm->next_in = strm->next_in;
2640 lastrm->avail_in = strm->avail_in;
2641 lastrm->total_in = strm->total_in;
2642 lastrm->next_out = strm->next_out;
2643 lastrm->avail_out = strm->avail_out;
2644 lastrm->total_out = strm->total_out;
2647 return (ARCHIVE_OK);
2649 return (ARCHIVE_EOF);
2651 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2652 "GZip compression failed:"
2653 " deflate() call returned status %d", r);
2654 return (ARCHIVE_FATAL);
2659 compression_end_gzip(struct archive *a, struct la_zstream *lastrm)
2664 strm = (z_stream *)lastrm->real_stream;
2665 r = deflateEnd(strm);
2667 lastrm->real_stream = NULL;
2670 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2671 "Failed to clean up compressor");
2672 return (ARCHIVE_FATAL);
2674 return (ARCHIVE_OK);
2677 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
2679 compression_init_encoder_bzip2(struct archive *a,
2680 struct la_zstream *lastrm, int level)
2685 compression_end(a, lastrm);
2686 strm = calloc(1, sizeof(*strm));
2688 archive_set_error(a, ENOMEM,
2689 "Can't allocate memory for bzip2 stream");
2690 return (ARCHIVE_FATAL);
2692 /* bzlib.h is not const-correct, so we need this one bit
2693 * of ugly hackery to convert a const * pointer to
2694 * a non-const pointer. */
2695 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2696 strm->avail_in = lastrm->avail_in;
2697 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2698 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2699 strm->next_out = (char *)lastrm->next_out;
2700 strm->avail_out = lastrm->avail_out;
2701 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2702 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2703 if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
2705 lastrm->real_stream = NULL;
2706 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2707 "Internal error initializing compression library");
2708 return (ARCHIVE_FATAL);
2710 lastrm->real_stream = strm;
2712 lastrm->code = compression_code_bzip2;
2713 lastrm->end = compression_end_bzip2;
2714 return (ARCHIVE_OK);
2718 compression_code_bzip2(struct archive *a,
2719 struct la_zstream *lastrm, enum la_zaction action)
2724 strm = (bz_stream *)lastrm->real_stream;
2725 /* bzlib.h is not const-correct, so we need this one bit
2726 * of ugly hackery to convert a const * pointer to
2727 * a non-const pointer. */
2728 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
2729 strm->avail_in = lastrm->avail_in;
2730 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
2731 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
2732 strm->next_out = (char *)lastrm->next_out;
2733 strm->avail_out = lastrm->avail_out;
2734 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
2735 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
2736 r = BZ2_bzCompress(strm,
2737 (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
2738 lastrm->next_in = (const unsigned char *)strm->next_in;
2739 lastrm->avail_in = strm->avail_in;
2741 (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
2742 + (uint64_t)(uint32_t)strm->total_in_lo32;
2743 lastrm->next_out = (unsigned char *)strm->next_out;
2744 lastrm->avail_out = strm->avail_out;
2746 (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
2747 + (uint64_t)(uint32_t)strm->total_out_lo32;
2749 case BZ_RUN_OK: /* Non-finishing */
2750 case BZ_FINISH_OK: /* Finishing: There's more work to do */
2751 return (ARCHIVE_OK);
2752 case BZ_STREAM_END: /* Finishing: all done */
2753 /* Only occurs in finishing case */
2754 return (ARCHIVE_EOF);
2756 /* Any other return value indicates an error */
2757 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2758 "Bzip2 compression failed:"
2759 " BZ2_bzCompress() call returned status %d", r);
2760 return (ARCHIVE_FATAL);
2765 compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
2770 strm = (bz_stream *)lastrm->real_stream;
2771 r = BZ2_bzCompressEnd(strm);
2773 lastrm->real_stream = NULL;
2776 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2777 "Failed to clean up compressor");
2778 return (ARCHIVE_FATAL);
2780 return (ARCHIVE_OK);
2785 compression_init_encoder_bzip2(struct archive *a,
2786 struct la_zstream *lastrm, int level)
2789 (void) level; /* UNUSED */
2791 compression_end(a, lastrm);
2792 return (compression_unsupported_encoder(a, lastrm, "bzip2"));
2796 #if defined(HAVE_LZMA_H)
2798 compression_init_encoder_lzma(struct archive *a,
2799 struct la_zstream *lastrm, int level)
2801 static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
2803 lzma_options_lzma lzma_opt;
2807 compression_end(a, lastrm);
2808 if (lzma_lzma_preset(&lzma_opt, level)) {
2809 lastrm->real_stream = NULL;
2810 archive_set_error(a, ENOMEM,
2811 "Internal error initializing compression library");
2812 return (ARCHIVE_FATAL);
2814 strm = calloc(1, sizeof(*strm));
2816 archive_set_error(a, ENOMEM,
2817 "Can't allocate memory for lzma stream");
2818 return (ARCHIVE_FATAL);
2820 *strm = lzma_init_data;
2821 r = lzma_alone_encoder(strm, &lzma_opt);
2824 lastrm->real_stream = strm;
2826 lastrm->code = compression_code_lzma;
2827 lastrm->end = compression_end_lzma;
2830 case LZMA_MEM_ERROR:
2832 lastrm->real_stream = NULL;
2833 archive_set_error(a, ENOMEM,
2834 "Internal error initializing compression library: "
2835 "Cannot allocate memory");
2840 lastrm->real_stream = NULL;
2841 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2842 "Internal error initializing compression library: "
2843 "It's a bug in liblzma");
2851 compression_init_encoder_xz(struct archive *a,
2852 struct la_zstream *lastrm, int level)
2854 static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
2856 lzma_filter *lzmafilters;
2857 lzma_options_lzma lzma_opt;
2861 compression_end(a, lastrm);
2862 strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
2864 archive_set_error(a, ENOMEM,
2865 "Can't allocate memory for xz stream");
2866 return (ARCHIVE_FATAL);
2868 lzmafilters = (lzma_filter *)(strm+1);
2871 if (lzma_lzma_preset(&lzma_opt, level)) {
2872 lastrm->real_stream = NULL;
2873 archive_set_error(a, ENOMEM,
2874 "Internal error initializing compression library");
2875 return (ARCHIVE_FATAL);
2877 lzmafilters[0].id = LZMA_FILTER_LZMA2;
2878 lzmafilters[0].options = &lzma_opt;
2879 lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
2881 *strm = lzma_init_data;
2882 r = lzma_stream_encoder(strm, lzmafilters, LZMA_CHECK_CRC64);
2885 lastrm->real_stream = strm;
2887 lastrm->code = compression_code_lzma;
2888 lastrm->end = compression_end_lzma;
2891 case LZMA_MEM_ERROR:
2893 lastrm->real_stream = NULL;
2894 archive_set_error(a, ENOMEM,
2895 "Internal error initializing compression library: "
2896 "Cannot allocate memory");
2901 lastrm->real_stream = NULL;
2902 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2903 "Internal error initializing compression library: "
2904 "It's a bug in liblzma");
2912 compression_code_lzma(struct archive *a,
2913 struct la_zstream *lastrm, enum la_zaction action)
2918 strm = (lzma_stream *)lastrm->real_stream;
2919 strm->next_in = lastrm->next_in;
2920 strm->avail_in = lastrm->avail_in;
2921 strm->total_in = lastrm->total_in;
2922 strm->next_out = lastrm->next_out;
2923 strm->avail_out = lastrm->avail_out;
2924 strm->total_out = lastrm->total_out;
2926 (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
2927 lastrm->next_in = strm->next_in;
2928 lastrm->avail_in = strm->avail_in;
2929 lastrm->total_in = strm->total_in;
2930 lastrm->next_out = strm->next_out;
2931 lastrm->avail_out = strm->avail_out;
2932 lastrm->total_out = strm->total_out;
2935 /* Non-finishing case */
2936 return (ARCHIVE_OK);
2937 case LZMA_STREAM_END:
2938 /* This return can only occur in finishing case. */
2939 return (ARCHIVE_EOF);
2940 case LZMA_MEMLIMIT_ERROR:
2941 archive_set_error(a, ENOMEM,
2942 "lzma compression error:"
2943 " %ju MiB would have been needed",
2944 (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
2946 return (ARCHIVE_FATAL);
2948 /* Any other return value indicates an error */
2949 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2950 "lzma compression failed:"
2951 " lzma_code() call returned status %d", r);
2952 return (ARCHIVE_FATAL);
2957 compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
2961 (void)a; /* UNUSED */
2962 strm = (lzma_stream *)lastrm->real_stream;
2966 lastrm->real_stream = NULL;
2967 return (ARCHIVE_OK);
2971 compression_init_encoder_lzma(struct archive *a,
2972 struct la_zstream *lastrm, int level)
2975 (void) level; /* UNUSED */
2977 compression_end(a, lastrm);
2978 return (compression_unsupported_encoder(a, lastrm, "lzma"));
2981 compression_init_encoder_xz(struct archive *a,
2982 struct la_zstream *lastrm, int level)
2985 (void) level; /* UNUSED */
2987 compression_end(a, lastrm);
2988 return (compression_unsupported_encoder(a, lastrm, "xz"));
2993 xar_compression_init_encoder(struct archive_write *a)
2998 xar = (struct xar *)a->format_data;
2999 switch (xar->opt_compression) {
3001 r = compression_init_encoder_gzip(
3002 &(a->archive), &(xar->stream),
3003 xar->opt_compression_level, 1);
3006 r = compression_init_encoder_bzip2(
3007 &(a->archive), &(xar->stream),
3008 xar->opt_compression_level);
3011 r = compression_init_encoder_lzma(
3012 &(a->archive), &(xar->stream),
3013 xar->opt_compression_level);
3016 r = compression_init_encoder_xz(
3017 &(a->archive), &(xar->stream),
3018 xar->opt_compression_level);
3024 if (r == ARCHIVE_OK) {
3025 xar->stream.total_in = 0;
3026 xar->stream.next_out = xar->wbuff;
3027 xar->stream.avail_out = sizeof(xar->wbuff);
3028 xar->stream.total_out = 0;
3035 compression_code(struct archive *a, struct la_zstream *lastrm,
3036 enum la_zaction action)
3039 return (lastrm->code(a, lastrm, action));
3040 return (ARCHIVE_OK);
3044 compression_end(struct archive *a, struct la_zstream *lastrm)
3047 return (lastrm->end(a, lastrm));
3048 return (ARCHIVE_OK);
3053 save_xattrs(struct archive_write *a, struct file *file)
3058 struct heap_data *heap;
3062 xar = (struct xar *)a->format_data;
3063 count = archive_entry_xattr_reset(file->entry);
3065 return (ARCHIVE_OK);
3067 archive_entry_xattr_next(file->entry,
3068 &name, &value, &size);
3069 checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
3070 checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
3072 heap = calloc(1, sizeof(*heap));
3074 archive_set_error(&a->archive, ENOMEM,
3075 "Can't allocate memory for xattr");
3076 return (ARCHIVE_FATAL);
3078 heap->id = file->ea_idx++;
3079 heap->temp_offset = xar->temp_offset;
3080 heap->size = size;/* save a extracted size */
3081 heap->compression = xar->opt_compression;
3082 /* Get a extracted sumcheck value. */
3083 checksum_update(&(xar->e_sumwrk), value, size);
3084 checksum_final(&(xar->e_sumwrk), &(heap->e_sum));
3087 * Not compression to xattr is simple way.
3089 if (heap->compression == NONE) {
3090 checksum_update(&(xar->a_sumwrk), value, size);
3091 checksum_final(&(xar->a_sumwrk), &(heap->a_sum));
3092 if (write_to_temp(a, value, size)
3094 return (ARCHIVE_FATAL);
3095 heap->length = size;
3096 /* Add heap to the tail of file->xattr. */
3098 *file->xattr.last = heap;
3099 file->xattr.last = &(heap->next);
3105 * Init compression library.
3107 r = xar_compression_init_encoder(a);
3108 if (r != ARCHIVE_OK) {
3110 return (ARCHIVE_FATAL);
3113 xar->stream.next_in = (const unsigned char *)value;
3114 xar->stream.avail_in = size;
3116 r = compression_code(&(a->archive),
3117 &(xar->stream), ARCHIVE_Z_FINISH);
3118 if (r != ARCHIVE_OK && r != ARCHIVE_EOF) {
3120 return (ARCHIVE_FATAL);
3122 size = sizeof(xar->wbuff) - xar->stream.avail_out;
3123 checksum_update(&(xar->a_sumwrk),
3125 if (write_to_temp(a, xar->wbuff, size)
3127 return (ARCHIVE_FATAL);
3128 if (r == ARCHIVE_OK) {
3129 xar->stream.next_out = xar->wbuff;
3130 xar->stream.avail_out = sizeof(xar->wbuff);
3132 checksum_final(&(xar->a_sumwrk),
3134 heap->length = xar->stream.total_out;
3135 /* Add heap to the tail of file->xattr. */
3137 *file->xattr.last = heap;
3138 file->xattr.last = &(heap->next);
3142 /* Clean up compression library. */
3143 r = compression_end(&(a->archive), &(xar->stream));
3144 if (r != ARCHIVE_OK)
3145 return (ARCHIVE_FATAL);
3147 return (ARCHIVE_OK);
3151 getalgsize(enum sumalg sumalg)
3165 getalgname(enum sumalg sumalg)
3178 #endif /* Support xar format */