xref: /dflybsd-src/contrib/libarchive/libarchive/archive_write_open_fd.c (revision afd311f52496a4b5c3df02ea6d4bdab591886c60)
160b4ad09SPeter Avalos /*-
260b4ad09SPeter Avalos  * Copyright (c) 2003-2007 Tim Kientzle
360b4ad09SPeter Avalos  * All rights reserved.
460b4ad09SPeter Avalos  *
560b4ad09SPeter Avalos  * Redistribution and use in source and binary forms, with or without
660b4ad09SPeter Avalos  * modification, are permitted provided that the following conditions
760b4ad09SPeter Avalos  * are met:
860b4ad09SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
960b4ad09SPeter Avalos  *    notice, this list of conditions and the following disclaimer.
1060b4ad09SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1160b4ad09SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1260b4ad09SPeter Avalos  *    documentation and/or other materials provided with the distribution.
1360b4ad09SPeter Avalos  *
1460b4ad09SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1560b4ad09SPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1660b4ad09SPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1760b4ad09SPeter Avalos  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1860b4ad09SPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1960b4ad09SPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2060b4ad09SPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2160b4ad09SPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2260b4ad09SPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2360b4ad09SPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2460b4ad09SPeter Avalos  */
2560b4ad09SPeter Avalos 
2660b4ad09SPeter Avalos #include "archive_platform.h"
279c82a63eSPeter Avalos __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_open_fd.c 201093 2009-12-28 02:28:44Z kientzle $");
2860b4ad09SPeter Avalos 
2960b4ad09SPeter Avalos #ifdef HAVE_SYS_STAT_H
3060b4ad09SPeter Avalos #include <sys/stat.h>
3160b4ad09SPeter Avalos #endif
3260b4ad09SPeter Avalos #ifdef HAVE_ERRNO_H
3360b4ad09SPeter Avalos #include <errno.h>
3460b4ad09SPeter Avalos #endif
359c82a63eSPeter Avalos #ifdef HAVE_FCNTL_H
369c82a63eSPeter Avalos #include <fcntl.h>
379c82a63eSPeter Avalos #endif
389c82a63eSPeter Avalos #ifdef HAVE_IO_H
399c82a63eSPeter Avalos #include <io.h>
409c82a63eSPeter Avalos #endif
4160b4ad09SPeter Avalos #ifdef HAVE_STDLIB_H
4260b4ad09SPeter Avalos #include <stdlib.h>
4360b4ad09SPeter Avalos #endif
4460b4ad09SPeter Avalos #ifdef HAVE_STRING_H
4560b4ad09SPeter Avalos #include <string.h>
4660b4ad09SPeter Avalos #endif
4760b4ad09SPeter Avalos #ifdef HAVE_UNISTD_H
4860b4ad09SPeter Avalos #include <unistd.h>
4960b4ad09SPeter Avalos #endif
5060b4ad09SPeter Avalos 
5160b4ad09SPeter Avalos #include "archive.h"
5260b4ad09SPeter Avalos 
5360b4ad09SPeter Avalos struct write_fd_data {
5460b4ad09SPeter Avalos 	int		fd;
5560b4ad09SPeter Avalos };
5660b4ad09SPeter Avalos 
57*50f8aa9cSAntonio Huete Jimenez static int	file_free(struct archive *, void *);
5860b4ad09SPeter Avalos static int	file_open(struct archive *, void *);
5960b4ad09SPeter Avalos static ssize_t	file_write(struct archive *, void *, const void *buff, size_t);
6060b4ad09SPeter Avalos 
6160b4ad09SPeter Avalos int
archive_write_open_fd(struct archive * a,int fd)6260b4ad09SPeter Avalos archive_write_open_fd(struct archive *a, int fd)
6360b4ad09SPeter Avalos {
6460b4ad09SPeter Avalos 	struct write_fd_data *mine;
6560b4ad09SPeter Avalos 
6660b4ad09SPeter Avalos 	mine = (struct write_fd_data *)malloc(sizeof(*mine));
6760b4ad09SPeter Avalos 	if (mine == NULL) {
6860b4ad09SPeter Avalos 		archive_set_error(a, ENOMEM, "No memory");
6960b4ad09SPeter Avalos 		return (ARCHIVE_FATAL);
7060b4ad09SPeter Avalos 	}
7160b4ad09SPeter Avalos 	mine->fd = fd;
729c82a63eSPeter Avalos #if defined(__CYGWIN__) || defined(_WIN32)
739c82a63eSPeter Avalos 	setmode(mine->fd, O_BINARY);
749c82a63eSPeter Avalos #endif
75*50f8aa9cSAntonio Huete Jimenez 	return (archive_write_open2(a, mine,
76*50f8aa9cSAntonio Huete Jimenez 		    file_open, file_write, NULL, file_free));
7760b4ad09SPeter Avalos }
7860b4ad09SPeter Avalos 
7960b4ad09SPeter Avalos static int
file_open(struct archive * a,void * client_data)8060b4ad09SPeter Avalos file_open(struct archive *a, void *client_data)
8160b4ad09SPeter Avalos {
8260b4ad09SPeter Avalos 	struct write_fd_data *mine;
8360b4ad09SPeter Avalos 	struct stat st;
8460b4ad09SPeter Avalos 
8560b4ad09SPeter Avalos 	mine = (struct write_fd_data *)client_data;
8660b4ad09SPeter Avalos 
8760b4ad09SPeter Avalos 	if (fstat(mine->fd, &st) != 0) {
8860b4ad09SPeter Avalos 		archive_set_error(a, errno, "Couldn't stat fd %d", mine->fd);
8960b4ad09SPeter Avalos 		return (ARCHIVE_FATAL);
9060b4ad09SPeter Avalos 	}
9160b4ad09SPeter Avalos 
9260b4ad09SPeter Avalos 	/*
9360b4ad09SPeter Avalos 	 * If this is a regular file, don't add it to itself.
9460b4ad09SPeter Avalos 	 */
9560b4ad09SPeter Avalos 	if (S_ISREG(st.st_mode))
9660b4ad09SPeter Avalos 		archive_write_set_skip_file(a, st.st_dev, st.st_ino);
9760b4ad09SPeter Avalos 
9860b4ad09SPeter Avalos 	/*
9960b4ad09SPeter Avalos 	 * If client hasn't explicitly set the last block handling,
10060b4ad09SPeter Avalos 	 * then set it here.
10160b4ad09SPeter Avalos 	 */
10260b4ad09SPeter Avalos 	if (archive_write_get_bytes_in_last_block(a) < 0) {
10360b4ad09SPeter Avalos 		/* If the output is a block or character device, fifo,
10460b4ad09SPeter Avalos 		 * or stdout, pad the last block, otherwise leave it
10560b4ad09SPeter Avalos 		 * unpadded. */
10660b4ad09SPeter Avalos 		if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) ||
10760b4ad09SPeter Avalos 		    S_ISFIFO(st.st_mode) || (mine->fd == 1))
10860b4ad09SPeter Avalos 			/* Last block will be fully padded. */
10960b4ad09SPeter Avalos 			archive_write_set_bytes_in_last_block(a, 0);
11060b4ad09SPeter Avalos 		else
11160b4ad09SPeter Avalos 			archive_write_set_bytes_in_last_block(a, 1);
11260b4ad09SPeter Avalos 	}
11360b4ad09SPeter Avalos 
11460b4ad09SPeter Avalos 	return (ARCHIVE_OK);
11560b4ad09SPeter Avalos }
11660b4ad09SPeter Avalos 
11760b4ad09SPeter Avalos static ssize_t
file_write(struct archive * a,void * client_data,const void * buff,size_t length)11860b4ad09SPeter Avalos file_write(struct archive *a, void *client_data, const void *buff, size_t length)
11960b4ad09SPeter Avalos {
12060b4ad09SPeter Avalos 	struct write_fd_data	*mine;
12160b4ad09SPeter Avalos 	ssize_t	bytesWritten;
12260b4ad09SPeter Avalos 
12360b4ad09SPeter Avalos 	mine = (struct write_fd_data *)client_data;
124ce5fd9c5SPeter Avalos 	for (;;) {
12560b4ad09SPeter Avalos 		bytesWritten = write(mine->fd, buff, length);
12660b4ad09SPeter Avalos 		if (bytesWritten <= 0) {
127ce5fd9c5SPeter Avalos 			if (errno == EINTR)
128ce5fd9c5SPeter Avalos 				continue;
12960b4ad09SPeter Avalos 			archive_set_error(a, errno, "Write error");
13060b4ad09SPeter Avalos 			return (-1);
13160b4ad09SPeter Avalos 		}
13260b4ad09SPeter Avalos 		return (bytesWritten);
13360b4ad09SPeter Avalos 	}
134ce5fd9c5SPeter Avalos }
13560b4ad09SPeter Avalos 
13660b4ad09SPeter Avalos static int
file_free(struct archive * a,void * client_data)137*50f8aa9cSAntonio Huete Jimenez file_free(struct archive *a, void *client_data)
13860b4ad09SPeter Avalos {
13960b4ad09SPeter Avalos 	struct write_fd_data	*mine = (struct write_fd_data *)client_data;
14060b4ad09SPeter Avalos 
14160b4ad09SPeter Avalos 	(void)a; /* UNUSED */
142*50f8aa9cSAntonio Huete Jimenez 	if (mine == NULL)
143*50f8aa9cSAntonio Huete Jimenez 		return (ARCHIVE_OK);
14460b4ad09SPeter Avalos 	free(mine);
14560b4ad09SPeter Avalos 	return (ARCHIVE_OK);
14660b4ad09SPeter Avalos }
147