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_STAT_H
29 #include <sys/stat.h>
30 #endif
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_FCNTL_H
35 #include <fcntl.h>
36 #endif
37 #ifdef HAVE_IO_H
38 #include <io.h>
39 #endif
40 #ifdef HAVE_STDLIB_H
41 #include <stdlib.h>
42 #endif
43 #ifdef HAVE_STRING_H
44 #include <string.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49
50 #include "archive.h"
51
52 struct write_fd_data {
53 int fd;
54 };
55
56 static int file_free(struct archive *, void *);
57 static int file_open(struct archive *, void *);
58 static ssize_t file_write(struct archive *, void *, const void *buff, size_t);
59
60 int
archive_write_open_fd(struct archive * a,int fd)61 archive_write_open_fd(struct archive *a, int fd)
62 {
63 struct write_fd_data *mine;
64
65 mine = malloc(sizeof(*mine));
66 if (mine == NULL) {
67 archive_set_error(a, ENOMEM, "No memory");
68 return (ARCHIVE_FATAL);
69 }
70 mine->fd = fd;
71 #if defined(__CYGWIN__) || defined(_WIN32)
72 setmode(mine->fd, O_BINARY);
73 #endif
74 return (archive_write_open2(a, mine,
75 file_open, file_write, NULL, file_free));
76 }
77
78 static int
file_open(struct archive * a,void * client_data)79 file_open(struct archive *a, void *client_data)
80 {
81 struct write_fd_data *mine;
82 struct stat st;
83
84 mine = (struct write_fd_data *)client_data;
85
86 if (fstat(mine->fd, &st) != 0) {
87 archive_set_error(a, errno, "Couldn't stat fd %d", mine->fd);
88 return (ARCHIVE_FATAL);
89 }
90
91 /*
92 * If this is a regular file, don't add it to itself.
93 */
94 if (S_ISREG(st.st_mode))
95 archive_write_set_skip_file(a, st.st_dev, st.st_ino);
96
97 /*
98 * If client hasn't explicitly set the last block handling,
99 * then set it here.
100 */
101 if (archive_write_get_bytes_in_last_block(a) < 0) {
102 /* If the output is a block or character device, fifo,
103 * or stdout, pad the last block, otherwise leave it
104 * unpadded. */
105 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) ||
106 S_ISFIFO(st.st_mode) || (mine->fd == 1))
107 /* Last block will be fully padded. */
108 archive_write_set_bytes_in_last_block(a, 0);
109 else
110 archive_write_set_bytes_in_last_block(a, 1);
111 }
112
113 return (ARCHIVE_OK);
114 }
115
116 static ssize_t
file_write(struct archive * a,void * client_data,const void * buff,size_t length)117 file_write(struct archive *a, void *client_data, const void *buff, size_t length)
118 {
119 struct write_fd_data *mine;
120 ssize_t bytesWritten;
121
122 mine = (struct write_fd_data *)client_data;
123 for (;;) {
124 bytesWritten = write(mine->fd, buff, length);
125 if (bytesWritten <= 0) {
126 if (errno == EINTR)
127 continue;
128 archive_set_error(a, errno, "Write error");
129 return (-1);
130 }
131 return (bytesWritten);
132 }
133 }
134
135 static int
file_free(struct archive * a,void * client_data)136 file_free(struct archive *a, void *client_data)
137 {
138 struct write_fd_data *mine = (struct write_fd_data *)client_data;
139
140 (void)a; /* UNUSED */
141 if (mine == NULL)
142 return (ARCHIVE_OK);
143 free(mine);
144 return (ARCHIVE_OK);
145 }
146