2 * Copyright (c) 2003-2004 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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
39 struct write_file_data {
44 static int file_close(struct archive *, void *);
45 static int file_open(struct archive *, void *);
46 static ssize_t file_write(struct archive *, void *, void *buff, size_t);
49 archive_write_open_file(struct archive *a, const char *filename)
51 return (archive_write_open_filename(a, filename));
55 archive_write_open_filename(struct archive *a, const char *filename)
57 struct write_file_data *mine;
59 if (filename == NULL || filename[0] == '\0') {
60 mine = malloc(sizeof(*mine));
62 archive_set_error(a, ENOMEM, "No memory");
63 return (ARCHIVE_FATAL);
65 mine->filename[0] = '\0'; /* Record that we're using stdout. */
67 mine = malloc(sizeof(*mine) + strlen(filename));
69 archive_set_error(a, ENOMEM, "No memory");
70 return (ARCHIVE_FATAL);
72 strcpy(mine->filename, filename);
75 return (archive_write_open(a, mine,
76 file_open, file_write, file_close));
80 file_open(struct archive *a, void *client_data)
83 struct write_file_data *mine;
88 flags = O_WRONLY | O_CREAT | O_TRUNC;
90 if (mine->filename[0] != '\0') {
91 mine->fd = open(mine->filename, flags, 0666);
94 * If client hasn't explicitly set the last block
95 * handling, then set it here: If the output is a
96 * block or character device, pad the last block,
97 * otherwise leave it unpadded.
99 if (mine->fd >= 0 && archive_write_get_bytes_in_last_block(a) < 0) {
100 if (fstat(mine->fd, &st) == 0) {
102 if (S_ISCHR(st.st_mode) ||
103 S_ISBLK(st.st_mode) ||
104 S_ISFIFO(st.st_mode))
105 /* Pad last block. */
106 archive_write_set_bytes_in_last_block(a, 0);
108 /* Don't pad last block. */
109 archive_write_set_bytes_in_last_block(a, 1);
114 if (archive_write_get_bytes_in_last_block(a) < 0) /* Still default? */
115 /* Last block will be fully padded. */
116 archive_write_set_bytes_in_last_block(a, 0);
120 archive_set_error(a, errno, "Failed to open '%s'",
122 return (ARCHIVE_FATAL);
125 if (pst == NULL && fstat(mine->fd, &st) == 0)
128 archive_set_error(a, errno, "Couldn't stat '%s'",
130 return (ARCHIVE_FATAL);
134 * If the output file is a regular file, don't add it to
135 * itself. If it's a device file, it's okay to add the device
136 * entry to the output archive.
138 if (S_ISREG(pst->st_mode))
139 archive_write_set_skip_file(a, pst->st_dev, pst->st_ino);
145 file_write(struct archive *a, void *client_data, void *buff, size_t length)
147 struct write_file_data *mine;
148 ssize_t bytesWritten;
151 bytesWritten = write(mine->fd, buff, length);
152 if (bytesWritten <= 0) {
153 archive_set_error(a, errno, "Write error");
156 return (bytesWritten);
160 file_close(struct archive *a, void *client_data)
162 struct write_file_data *mine = client_data;
164 (void)a; /* UNUSED */
165 if (mine->filename[0] != '\0')