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__FBSDID("$FreeBSD$");
28
29#ifdef HAVE_SYS_STAT_H
30#include <sys/stat.h>
31#endif
32#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
35#ifdef HAVE_FCNTL_H
36#include <fcntl.h>
37#endif
38#ifdef HAVE_STDLIB_H
39#include <stdlib.h>
40#endif
41#ifdef HAVE_STRING_H
42#include <string.h>
43#endif
44#ifdef HAVE_UNISTD_H
45#include <unistd.h>
46#endif
47
48#include "archive.h"
49#include "archive_private.h"
50#include "archive_string.h"
51
52#ifndef O_BINARY
53#define O_BINARY 0
54#endif
55#ifndef O_CLOEXEC
56#define O_CLOEXEC	0
57#endif
58
59struct write_file_data {
60	int		fd;
61	struct archive_mstring filename;
62};
63
64static int	file_close(struct archive *, void *);
65static int	file_open(struct archive *, void *);
66static ssize_t	file_write(struct archive *, void *, const void *buff, size_t);
67static int	open_filename(struct archive *, int, const void *);
68
69int
70archive_write_open_file(struct archive *a, const char *filename)
71{
72	return (archive_write_open_filename(a, filename));
73}
74
75int
76archive_write_open_filename(struct archive *a, const char *filename)
77{
78
79	if (filename == NULL || filename[0] == '\0')
80		return (archive_write_open_fd(a, 1));
81
82	return (open_filename(a, 1, filename));
83}
84
85int
86archive_write_open_filename_w(struct archive *a, const wchar_t *filename)
87{
88
89	if (filename == NULL || filename[0] == L'\0')
90		return (archive_write_open_fd(a, 1));
91
92	return (open_filename(a, 0, filename));
93}
94
95static int
96open_filename(struct archive *a, int mbs_fn, const void *filename)
97{
98	struct write_file_data *mine;
99	int r;
100
101	mine = (struct write_file_data *)calloc(1, sizeof(*mine));
102	if (mine == NULL) {
103		archive_set_error(a, ENOMEM, "No memory");
104		return (ARCHIVE_FATAL);
105	}
106	if (mbs_fn)
107		r = archive_mstring_copy_mbs(&mine->filename, filename);
108	else
109		r = archive_mstring_copy_wcs(&mine->filename, filename);
110	if (r < 0) {
111		if (errno == ENOMEM) {
112			archive_set_error(a, ENOMEM, "No memory");
113			return (ARCHIVE_FATAL);
114		}
115		if (mbs_fn)
116			archive_set_error(a, ARCHIVE_ERRNO_MISC,
117			    "Can't convert '%s' to WCS",
118			    (const char *)filename);
119		else
120			archive_set_error(a, ARCHIVE_ERRNO_MISC,
121			    "Can't convert '%S' to MBS",
122			    (const wchar_t *)filename);
123		return (ARCHIVE_FAILED);
124	}
125	mine->fd = -1;
126	return (archive_write_open(a, mine,
127		file_open, file_write, file_close));
128}
129
130static int
131file_open(struct archive *a, void *client_data)
132{
133	int flags;
134	struct write_file_data *mine;
135	struct stat st;
136#if defined(_WIN32) && !defined(__CYGWIN__)
137	wchar_t *fullpath;
138#endif
139	const wchar_t *wcs;
140	const char *mbs;
141
142	mine = (struct write_file_data *)client_data;
143	flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_CLOEXEC;
144
145	/*
146	 * Open the file.
147	 */
148	mbs = NULL; wcs = NULL;
149#if defined(_WIN32) && !defined(__CYGWIN__)
150	if (archive_mstring_get_wcs(a, &mine->filename, &wcs) != 0) {
151		if (errno == ENOMEM)
152			archive_set_error(a, errno, "No memory");
153		else {
154			archive_mstring_get_mbs(a, &mine->filename, &mbs);
155			archive_set_error(a, errno,
156			    "Can't convert '%s' to WCS", mbs);
157		}
158		return (ARCHIVE_FATAL);
159	}
160	fullpath = __la_win_permissive_name_w(wcs);
161	if (fullpath != NULL) {
162		mine->fd = _wopen(fullpath, flags, 0666);
163		free(fullpath);
164	} else
165		mine->fd = _wopen(wcs, flags, 0666);
166#else
167	if (archive_mstring_get_mbs(a, &mine->filename, &mbs) != 0) {
168		if (errno == ENOMEM)
169			archive_set_error(a, errno, "No memory");
170		else {
171			archive_mstring_get_wcs(a, &mine->filename, &wcs);
172			archive_set_error(a, errno,
173			    "Can't convert '%S' to MBS", wcs);
174		}
175		return (ARCHIVE_FATAL);
176	}
177	mine->fd = open(mbs, flags, 0666);
178	__archive_ensure_cloexec_flag(mine->fd);
179#endif
180	if (mine->fd < 0) {
181		if (mbs != NULL)
182			archive_set_error(a, errno, "Failed to open '%s'", mbs);
183		else
184			archive_set_error(a, errno, "Failed to open '%S'", wcs);
185		return (ARCHIVE_FATAL);
186	}
187
188	if (fstat(mine->fd, &st) != 0) {
189		if (mbs != NULL)
190			archive_set_error(a, errno, "Couldn't stat '%s'", mbs);
191		else
192			archive_set_error(a, errno, "Couldn't stat '%S'", wcs);
193		return (ARCHIVE_FATAL);
194	}
195
196	/*
197	 * Set up default last block handling.
198	 */
199	if (archive_write_get_bytes_in_last_block(a) < 0) {
200		if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) ||
201		    S_ISFIFO(st.st_mode))
202			/* Pad last block when writing to device or FIFO. */
203			archive_write_set_bytes_in_last_block(a, 0);
204		else
205			/* Don't pad last block otherwise. */
206			archive_write_set_bytes_in_last_block(a, 1);
207	}
208
209	/*
210	 * If the output file is a regular file, don't add it to
211	 * itself.  If it's a device file, it's okay to add the device
212	 * entry to the output archive.
213	 */
214	if (S_ISREG(st.st_mode))
215		archive_write_set_skip_file(a, st.st_dev, st.st_ino);
216
217	return (ARCHIVE_OK);
218}
219
220static ssize_t
221file_write(struct archive *a, void *client_data, const void *buff,
222    size_t length)
223{
224	struct write_file_data	*mine;
225	ssize_t	bytesWritten;
226
227	mine = (struct write_file_data *)client_data;
228	for (;;) {
229		bytesWritten = write(mine->fd, buff, length);
230		if (bytesWritten <= 0) {
231			if (errno == EINTR)
232				continue;
233			archive_set_error(a, errno, "Write error");
234			return (-1);
235		}
236		return (bytesWritten);
237	}
238}
239
240static int
241file_close(struct archive *a, void *client_data)
242{
243	struct write_file_data	*mine = (struct write_file_data *)client_data;
244
245	(void)a; /* UNUSED */
246	close(mine->fd);
247	archive_mstring_clean(&mine->filename);
248	free(mine);
249	return (ARCHIVE_OK);
250}
251