1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm 26228753Smm#include "archive_platform.h" 27228763Smm__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/archive_write_open_fd.c 368707 2020-12-16 22:25:20Z mm $"); 28228753Smm 29228753Smm#ifdef HAVE_SYS_STAT_H 30228753Smm#include <sys/stat.h> 31228753Smm#endif 32228753Smm#ifdef HAVE_ERRNO_H 33228753Smm#include <errno.h> 34228753Smm#endif 35228753Smm#ifdef HAVE_FCNTL_H 36228753Smm#include <fcntl.h> 37228753Smm#endif 38228753Smm#ifdef HAVE_IO_H 39228753Smm#include <io.h> 40228753Smm#endif 41228753Smm#ifdef HAVE_STDLIB_H 42228753Smm#include <stdlib.h> 43228753Smm#endif 44228753Smm#ifdef HAVE_STRING_H 45228753Smm#include <string.h> 46228753Smm#endif 47228753Smm#ifdef HAVE_UNISTD_H 48228753Smm#include <unistd.h> 49228753Smm#endif 50228753Smm 51228753Smm#include "archive.h" 52228753Smm 53228753Smmstruct write_fd_data { 54228753Smm int fd; 55228753Smm}; 56228753Smm 57368707Smmstatic int file_free(struct archive *, void *); 58228753Smmstatic int file_open(struct archive *, void *); 59228753Smmstatic ssize_t file_write(struct archive *, void *, const void *buff, size_t); 60228753Smm 61228753Smmint 62228753Smmarchive_write_open_fd(struct archive *a, int fd) 63228753Smm{ 64228753Smm struct write_fd_data *mine; 65228753Smm 66228753Smm mine = (struct write_fd_data *)malloc(sizeof(*mine)); 67228753Smm if (mine == NULL) { 68228753Smm archive_set_error(a, ENOMEM, "No memory"); 69228753Smm return (ARCHIVE_FATAL); 70228753Smm } 71228753Smm mine->fd = fd; 72228753Smm#if defined(__CYGWIN__) || defined(_WIN32) 73228753Smm setmode(mine->fd, O_BINARY); 74228753Smm#endif 75368707Smm return (archive_write_open2(a, mine, 76368707Smm file_open, file_write, NULL, file_free)); 77228753Smm} 78228753Smm 79228753Smmstatic int 80228753Smmfile_open(struct archive *a, void *client_data) 81228753Smm{ 82228753Smm struct write_fd_data *mine; 83228753Smm struct stat st; 84228753Smm 85228753Smm mine = (struct write_fd_data *)client_data; 86228753Smm 87228753Smm if (fstat(mine->fd, &st) != 0) { 88228753Smm archive_set_error(a, errno, "Couldn't stat fd %d", mine->fd); 89228753Smm return (ARCHIVE_FATAL); 90228753Smm } 91228753Smm 92228753Smm /* 93228753Smm * If this is a regular file, don't add it to itself. 94228753Smm */ 95228753Smm if (S_ISREG(st.st_mode)) 96228753Smm archive_write_set_skip_file(a, st.st_dev, st.st_ino); 97228753Smm 98228753Smm /* 99228753Smm * If client hasn't explicitly set the last block handling, 100228753Smm * then set it here. 101228753Smm */ 102228753Smm if (archive_write_get_bytes_in_last_block(a) < 0) { 103228753Smm /* If the output is a block or character device, fifo, 104228753Smm * or stdout, pad the last block, otherwise leave it 105228753Smm * unpadded. */ 106228753Smm if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || 107228753Smm S_ISFIFO(st.st_mode) || (mine->fd == 1)) 108228753Smm /* Last block will be fully padded. */ 109228753Smm archive_write_set_bytes_in_last_block(a, 0); 110228753Smm else 111228753Smm archive_write_set_bytes_in_last_block(a, 1); 112228753Smm } 113228753Smm 114228753Smm return (ARCHIVE_OK); 115228753Smm} 116228753Smm 117228753Smmstatic ssize_t 118228753Smmfile_write(struct archive *a, void *client_data, const void *buff, size_t length) 119228753Smm{ 120228753Smm struct write_fd_data *mine; 121228753Smm ssize_t bytesWritten; 122228753Smm 123228753Smm mine = (struct write_fd_data *)client_data; 124228753Smm for (;;) { 125228753Smm bytesWritten = write(mine->fd, buff, length); 126228753Smm if (bytesWritten <= 0) { 127228753Smm if (errno == EINTR) 128228753Smm continue; 129228753Smm archive_set_error(a, errno, "Write error"); 130228753Smm return (-1); 131228753Smm } 132228753Smm return (bytesWritten); 133228753Smm } 134228753Smm} 135228753Smm 136228753Smmstatic int 137368707Smmfile_free(struct archive *a, void *client_data) 138228753Smm{ 139228753Smm struct write_fd_data *mine = (struct write_fd_data *)client_data; 140228753Smm 141228753Smm (void)a; /* UNUSED */ 142368707Smm if (mine == NULL) 143368707Smm return (ARCHIVE_OK); 144228753Smm free(mine); 145228753Smm return (ARCHIVE_OK); 146228753Smm} 147