archive_write_open_file.c revision 229592
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" 27229592Smm__FBSDID("$FreeBSD: stable/9/contrib/libarchive/libarchive/archive_write_open_file.c 229592 2012-01-05 12:06:54Z 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_STDLIB_H 39228753Smm#include <stdlib.h> 40228753Smm#endif 41228753Smm#ifdef HAVE_STRING_H 42228753Smm#include <string.h> 43228753Smm#endif 44228753Smm#ifdef HAVE_UNISTD_H 45228753Smm#include <unistd.h> 46228753Smm#endif 47228753Smm 48228753Smm#include "archive.h" 49228753Smm 50228753Smmstruct write_FILE_data { 51228753Smm FILE *f; 52228753Smm}; 53228753Smm 54228753Smmstatic int file_close(struct archive *, void *); 55228753Smmstatic int file_open(struct archive *, void *); 56228753Smmstatic ssize_t file_write(struct archive *, void *, const void *buff, size_t); 57228753Smm 58228753Smmint 59228753Smmarchive_write_open_FILE(struct archive *a, FILE *f) 60228753Smm{ 61228753Smm struct write_FILE_data *mine; 62228753Smm 63228753Smm mine = (struct write_FILE_data *)malloc(sizeof(*mine)); 64228753Smm if (mine == NULL) { 65228753Smm archive_set_error(a, ENOMEM, "No memory"); 66228753Smm return (ARCHIVE_FATAL); 67228753Smm } 68228753Smm mine->f = f; 69228753Smm return (archive_write_open(a, mine, 70228753Smm file_open, file_write, file_close)); 71228753Smm} 72228753Smm 73228753Smmstatic int 74228753Smmfile_open(struct archive *a, void *client_data) 75228753Smm{ 76228753Smm (void)a; /* UNUSED */ 77228753Smm (void)client_data; /* UNUSED */ 78228753Smm 79228753Smm return (ARCHIVE_OK); 80228753Smm} 81228753Smm 82228753Smmstatic ssize_t 83228753Smmfile_write(struct archive *a, void *client_data, const void *buff, size_t length) 84228753Smm{ 85228753Smm struct write_FILE_data *mine; 86228753Smm size_t bytesWritten; 87228753Smm 88228753Smm mine = client_data; 89228753Smm for (;;) { 90228753Smm bytesWritten = fwrite(buff, 1, length, mine->f); 91228753Smm if (bytesWritten <= 0) { 92228753Smm if (errno == EINTR) 93228753Smm continue; 94228753Smm archive_set_error(a, errno, "Write error"); 95228753Smm return (-1); 96228753Smm } 97228753Smm return (bytesWritten); 98228753Smm } 99228753Smm} 100228753Smm 101228753Smmstatic int 102228753Smmfile_close(struct archive *a, void *client_data) 103228753Smm{ 104228753Smm struct write_FILE_data *mine = client_data; 105228753Smm 106228753Smm (void)a; /* UNUSED */ 107228753Smm free(mine); 108228753Smm return (ARCHIVE_OK); 109228753Smm} 110