1*543adbedSBen Gras /*-
2*543adbedSBen Gras * Copyright (c) 2003-2007 Tim Kientzle
3*543adbedSBen Gras * All rights reserved.
4*543adbedSBen Gras *
5*543adbedSBen Gras * Redistribution and use in source and binary forms, with or without
6*543adbedSBen Gras * modification, are permitted provided that the following conditions
7*543adbedSBen Gras * are met:
8*543adbedSBen Gras * 1. Redistributions of source code must retain the above copyright
9*543adbedSBen Gras * notice, this list of conditions and the following disclaimer.
10*543adbedSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
11*543adbedSBen Gras * notice, this list of conditions and the following disclaimer in the
12*543adbedSBen Gras * documentation and/or other materials provided with the distribution.
13*543adbedSBen Gras *
14*543adbedSBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15*543adbedSBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*543adbedSBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*543adbedSBen Gras * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18*543adbedSBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*543adbedSBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*543adbedSBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*543adbedSBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*543adbedSBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*543adbedSBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*543adbedSBen Gras */
25*543adbedSBen Gras
26*543adbedSBen Gras #include "archive_platform.h"
27*543adbedSBen Gras __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_open_fd.c 201093 2009-12-28 02:28:44Z kientzle $");
28*543adbedSBen Gras
29*543adbedSBen Gras #ifdef HAVE_SYS_STAT_H
30*543adbedSBen Gras #include <sys/stat.h>
31*543adbedSBen Gras #endif
32*543adbedSBen Gras #ifdef HAVE_ERRNO_H
33*543adbedSBen Gras #include <errno.h>
34*543adbedSBen Gras #endif
35*543adbedSBen Gras #ifdef HAVE_FCNTL_H
36*543adbedSBen Gras #include <fcntl.h>
37*543adbedSBen Gras #endif
38*543adbedSBen Gras #ifdef HAVE_IO_H
39*543adbedSBen Gras #include <io.h>
40*543adbedSBen Gras #endif
41*543adbedSBen Gras #ifdef HAVE_STDLIB_H
42*543adbedSBen Gras #include <stdlib.h>
43*543adbedSBen Gras #endif
44*543adbedSBen Gras #ifdef HAVE_STRING_H
45*543adbedSBen Gras #include <string.h>
46*543adbedSBen Gras #endif
47*543adbedSBen Gras #ifdef HAVE_UNISTD_H
48*543adbedSBen Gras #include <unistd.h>
49*543adbedSBen Gras #endif
50*543adbedSBen Gras
51*543adbedSBen Gras #include "archive.h"
52*543adbedSBen Gras
53*543adbedSBen Gras struct write_fd_data {
54*543adbedSBen Gras off_t offset;
55*543adbedSBen Gras int fd;
56*543adbedSBen Gras };
57*543adbedSBen Gras
58*543adbedSBen Gras static int file_close(struct archive *, void *);
59*543adbedSBen Gras static int file_open(struct archive *, void *);
60*543adbedSBen Gras static ssize_t file_write(struct archive *, void *, const void *buff, size_t);
61*543adbedSBen Gras
62*543adbedSBen Gras int
archive_write_open_fd(struct archive * a,int fd)63*543adbedSBen Gras archive_write_open_fd(struct archive *a, int fd)
64*543adbedSBen Gras {
65*543adbedSBen Gras struct write_fd_data *mine;
66*543adbedSBen Gras
67*543adbedSBen Gras mine = (struct write_fd_data *)malloc(sizeof(*mine));
68*543adbedSBen Gras if (mine == NULL) {
69*543adbedSBen Gras archive_set_error(a, ENOMEM, "No memory");
70*543adbedSBen Gras return (ARCHIVE_FATAL);
71*543adbedSBen Gras }
72*543adbedSBen Gras mine->fd = fd;
73*543adbedSBen Gras #if defined(__CYGWIN__) || defined(_WIN32)
74*543adbedSBen Gras setmode(mine->fd, O_BINARY);
75*543adbedSBen Gras #endif
76*543adbedSBen Gras return (archive_write_open(a, mine,
77*543adbedSBen Gras file_open, file_write, file_close));
78*543adbedSBen Gras }
79*543adbedSBen Gras
80*543adbedSBen Gras static int
file_open(struct archive * a,void * client_data)81*543adbedSBen Gras file_open(struct archive *a, void *client_data)
82*543adbedSBen Gras {
83*543adbedSBen Gras struct write_fd_data *mine;
84*543adbedSBen Gras struct stat st;
85*543adbedSBen Gras
86*543adbedSBen Gras mine = (struct write_fd_data *)client_data;
87*543adbedSBen Gras
88*543adbedSBen Gras if (fstat(mine->fd, &st) != 0) {
89*543adbedSBen Gras archive_set_error(a, errno, "Couldn't stat fd %d", mine->fd);
90*543adbedSBen Gras return (ARCHIVE_FATAL);
91*543adbedSBen Gras }
92*543adbedSBen Gras
93*543adbedSBen Gras /*
94*543adbedSBen Gras * If this is a regular file, don't add it to itself.
95*543adbedSBen Gras */
96*543adbedSBen Gras if (S_ISREG(st.st_mode))
97*543adbedSBen Gras archive_write_set_skip_file(a, st.st_dev, st.st_ino);
98*543adbedSBen Gras
99*543adbedSBen Gras /*
100*543adbedSBen Gras * If client hasn't explicitly set the last block handling,
101*543adbedSBen Gras * then set it here.
102*543adbedSBen Gras */
103*543adbedSBen Gras if (archive_write_get_bytes_in_last_block(a) < 0) {
104*543adbedSBen Gras /* If the output is a block or character device, fifo,
105*543adbedSBen Gras * or stdout, pad the last block, otherwise leave it
106*543adbedSBen Gras * unpadded. */
107*543adbedSBen Gras if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) ||
108*543adbedSBen Gras S_ISFIFO(st.st_mode) || (mine->fd == 1))
109*543adbedSBen Gras /* Last block will be fully padded. */
110*543adbedSBen Gras archive_write_set_bytes_in_last_block(a, 0);
111*543adbedSBen Gras else
112*543adbedSBen Gras archive_write_set_bytes_in_last_block(a, 1);
113*543adbedSBen Gras }
114*543adbedSBen Gras
115*543adbedSBen Gras return (ARCHIVE_OK);
116*543adbedSBen Gras }
117*543adbedSBen Gras
118*543adbedSBen Gras static ssize_t
file_write(struct archive * a,void * client_data,const void * buff,size_t length)119*543adbedSBen Gras file_write(struct archive *a, void *client_data, const void *buff, size_t length)
120*543adbedSBen Gras {
121*543adbedSBen Gras struct write_fd_data *mine;
122*543adbedSBen Gras ssize_t bytesWritten;
123*543adbedSBen Gras
124*543adbedSBen Gras mine = (struct write_fd_data *)client_data;
125*543adbedSBen Gras bytesWritten = write(mine->fd, buff, length);
126*543adbedSBen Gras if (bytesWritten <= 0) {
127*543adbedSBen Gras archive_set_error(a, errno, "Write error");
128*543adbedSBen Gras return (-1);
129*543adbedSBen Gras }
130*543adbedSBen Gras return (bytesWritten);
131*543adbedSBen Gras }
132*543adbedSBen Gras
133*543adbedSBen Gras static int
file_close(struct archive * a,void * client_data)134*543adbedSBen Gras file_close(struct archive *a, void *client_data)
135*543adbedSBen Gras {
136*543adbedSBen Gras struct write_fd_data *mine = (struct write_fd_data *)client_data;
137*543adbedSBen Gras
138*543adbedSBen Gras (void)a; /* UNUSED */
139*543adbedSBen Gras free(mine);
140*543adbedSBen Gras return (ARCHIVE_OK);
141*543adbedSBen Gras }
142