2 * Copyright (c) 2003-2007 Tim Kientzle
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$");
29 #ifdef HAVE_SYS_STAT_H
49 #include "archive_private.h"
50 #include "archive_string.h"
59 struct write_file_data {
61 struct archive_mstring filename;
64 static int file_close(struct archive *, void *);
65 static int file_free(struct archive *, void *);
66 static int file_open(struct archive *, void *);
67 static ssize_t file_write(struct archive *, void *, const void *buff, size_t);
68 static int open_filename(struct archive *, int, const void *);
71 archive_write_open_file(struct archive *a, const char *filename)
73 return (archive_write_open_filename(a, filename));
77 archive_write_open_filename(struct archive *a, const char *filename)
80 if (filename == NULL || filename[0] == '\0')
81 return (archive_write_open_fd(a, 1));
83 return (open_filename(a, 1, filename));
87 archive_write_open_filename_w(struct archive *a, const wchar_t *filename)
90 if (filename == NULL || filename[0] == L'\0')
91 return (archive_write_open_fd(a, 1));
93 return (open_filename(a, 0, filename));
97 open_filename(struct archive *a, int mbs_fn, const void *filename)
99 struct write_file_data *mine;
102 mine = (struct write_file_data *)calloc(1, sizeof(*mine));
104 archive_set_error(a, ENOMEM, "No memory");
105 return (ARCHIVE_FATAL);
108 r = archive_mstring_copy_mbs(&mine->filename, filename);
110 r = archive_mstring_copy_wcs(&mine->filename, filename);
112 if (errno == ENOMEM) {
113 archive_set_error(a, ENOMEM, "No memory");
114 return (ARCHIVE_FATAL);
117 archive_set_error(a, ARCHIVE_ERRNO_MISC,
118 "Can't convert '%s' to WCS",
119 (const char *)filename);
121 archive_set_error(a, ARCHIVE_ERRNO_MISC,
122 "Can't convert '%S' to MBS",
123 (const wchar_t *)filename);
124 return (ARCHIVE_FAILED);
127 return (archive_write_open2(a, mine,
128 file_open, file_write, file_close, file_free));
132 file_open(struct archive *a, void *client_data)
135 struct write_file_data *mine;
137 #if defined(_WIN32) && !defined(__CYGWIN__)
143 mine = (struct write_file_data *)client_data;
144 flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_CLOEXEC;
149 mbs = NULL; wcs = NULL;
150 #if defined(_WIN32) && !defined(__CYGWIN__)
151 if (archive_mstring_get_wcs(a, &mine->filename, &wcs) != 0) {
153 archive_set_error(a, errno, "No memory");
155 archive_mstring_get_mbs(a, &mine->filename, &mbs);
156 archive_set_error(a, errno,
157 "Can't convert '%s' to WCS", mbs);
159 return (ARCHIVE_FATAL);
161 fullpath = __la_win_permissive_name_w(wcs);
162 if (fullpath != NULL) {
163 mine->fd = _wopen(fullpath, flags, 0666);
166 mine->fd = _wopen(wcs, flags, 0666);
168 if (archive_mstring_get_mbs(a, &mine->filename, &mbs) != 0) {
170 archive_set_error(a, errno, "No memory");
172 archive_mstring_get_wcs(a, &mine->filename, &wcs);
173 archive_set_error(a, errno,
174 "Can't convert '%S' to MBS", wcs);
176 return (ARCHIVE_FATAL);
178 mine->fd = open(mbs, flags, 0666);
179 __archive_ensure_cloexec_flag(mine->fd);
183 archive_set_error(a, errno, "Failed to open '%s'", mbs);
185 archive_set_error(a, errno, "Failed to open '%S'", wcs);
186 return (ARCHIVE_FATAL);
189 if (fstat(mine->fd, &st) != 0) {
191 archive_set_error(a, errno, "Couldn't stat '%s'", mbs);
193 archive_set_error(a, errno, "Couldn't stat '%S'", wcs);
194 return (ARCHIVE_FATAL);
198 * Set up default last block handling.
200 if (archive_write_get_bytes_in_last_block(a) < 0) {
201 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) ||
202 S_ISFIFO(st.st_mode))
203 /* Pad last block when writing to device or FIFO. */
204 archive_write_set_bytes_in_last_block(a, 0);
206 /* Don't pad last block otherwise. */
207 archive_write_set_bytes_in_last_block(a, 1);
211 * If the output file is a regular file, don't add it to
212 * itself. If it's a device file, it's okay to add the device
213 * entry to the output archive.
215 if (S_ISREG(st.st_mode))
216 archive_write_set_skip_file(a, st.st_dev, st.st_ino);
222 file_write(struct archive *a, void *client_data, const void *buff,
225 struct write_file_data *mine;
226 ssize_t bytesWritten;
228 mine = (struct write_file_data *)client_data;
230 bytesWritten = write(mine->fd, buff, length);
231 if (bytesWritten <= 0) {
234 archive_set_error(a, errno, "Write error");
237 return (bytesWritten);
242 file_close(struct archive *a, void *client_data)
244 struct write_file_data *mine = (struct write_file_data *)client_data;
246 (void)a; /* UNUSED */
249 return (ARCHIVE_FATAL);
258 file_free(struct archive *a, void *client_data)
260 struct write_file_data *mine = (struct write_file_data *)client_data;
262 (void)a; /* UNUSED */
267 archive_mstring_clean(&mine->filename);