1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26#include "archive_platform.h"
27
28#ifdef HAVE_SYS_TYPES_H
29#include <sys/types.h>
30#endif
31
32#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
35
36#include "archive.h"
37#include "archive_private.h"
38#include "archive_write_set_format_private.h"
39
40/* A table that maps format codes to functions. */
41static const
42struct { int code; int (*setter)(struct archive *); } codes[] =
43{
44	{ ARCHIVE_FORMAT_7ZIP,		archive_write_set_format_7zip },
45	{ ARCHIVE_FORMAT_CPIO,		archive_write_set_format_cpio },
46	{ ARCHIVE_FORMAT_CPIO_BIN_LE,	archive_write_set_format_cpio_bin },
47	{ ARCHIVE_FORMAT_CPIO_PWB,	archive_write_set_format_cpio_pwb },
48	{ ARCHIVE_FORMAT_CPIO_POSIX,	archive_write_set_format_cpio_odc },
49	{ ARCHIVE_FORMAT_CPIO_SVR4_NOCRC,	archive_write_set_format_cpio_newc },
50	{ ARCHIVE_FORMAT_ISO9660,	archive_write_set_format_iso9660 },
51	{ ARCHIVE_FORMAT_MTREE,		archive_write_set_format_mtree },
52	{ ARCHIVE_FORMAT_RAW,		archive_write_set_format_raw },
53	{ ARCHIVE_FORMAT_SHAR,		archive_write_set_format_shar },
54	{ ARCHIVE_FORMAT_SHAR_BASE,	archive_write_set_format_shar },
55	{ ARCHIVE_FORMAT_SHAR_DUMP,	archive_write_set_format_shar_dump },
56	{ ARCHIVE_FORMAT_TAR,	archive_write_set_format_pax_restricted },
57	{ ARCHIVE_FORMAT_TAR_GNUTAR,	archive_write_set_format_gnutar },
58	{ ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE, archive_write_set_format_pax },
59	{ ARCHIVE_FORMAT_TAR_PAX_RESTRICTED,
60				archive_write_set_format_pax_restricted },
61	{ ARCHIVE_FORMAT_TAR_USTAR,	archive_write_set_format_ustar },
62	{ ARCHIVE_FORMAT_WARC,		archive_write_set_format_warc },
63	{ ARCHIVE_FORMAT_XAR,		archive_write_set_format_xar },
64	{ ARCHIVE_FORMAT_ZIP,		archive_write_set_format_zip },
65	{ 0,		NULL }
66};
67
68int
69archive_write_set_format(struct archive *a, int code)
70{
71	int i;
72
73	for (i = 0; codes[i].code != 0; i++) {
74		if (code == codes[i].code)
75			return ((codes[i].setter)(a));
76	}
77
78	archive_set_error(a, EINVAL, "No such format");
79	return (ARCHIVE_FATAL);
80}
81
82void
83__archive_write_entry_filetype_unsupported(struct archive *a,
84    struct archive_entry *entry, const char *format)
85{
86	const char *name = NULL;
87
88	switch (archive_entry_filetype(entry)) {
89	/*
90	 * All formats should be able to archive regular files (AE_IFREG)
91	 */
92	case AE_IFDIR:
93		name = "directories";
94		break;
95	case AE_IFLNK:
96		name = "symbolic links";
97		break;
98	case AE_IFCHR:
99		name = "character devices";
100		break;
101	case AE_IFBLK:
102		name = "block devices";
103		break;
104	case AE_IFIFO:
105		name = "named pipes";
106		break;
107	case AE_IFSOCK:
108		name = "sockets";
109		break;
110	default:
111		break;
112	}
113
114	if (name != NULL) {
115		archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
116		    "%s: %s format cannot archive %s",
117		    archive_entry_pathname(entry), format, name);
118	} else {
119		archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
120		    "%s: %s format cannot archive files with mode 0%lo",
121		    archive_entry_pathname(entry), format,
122		    (unsigned long)archive_entry_mode(entry));
123	}
124}
125