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/10/contrib/libarchive/libarchive/archive_write_open_filename.c 368708 2020-12-16 22:25:40Z 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" 49248616Smm#include "archive_private.h" 50232153Smm#include "archive_string.h" 51228753Smm 52228753Smm#ifndef O_BINARY 53228753Smm#define O_BINARY 0 54228753Smm#endif 55248616Smm#ifndef O_CLOEXEC 56248616Smm#define O_CLOEXEC 0 57248616Smm#endif 58228753Smm 59228753Smmstruct write_file_data { 60228753Smm int fd; 61238856Smm struct archive_mstring filename; 62228753Smm}; 63228753Smm 64228753Smmstatic int file_close(struct archive *, void *); 65368708Smmstatic int file_free(struct archive *, void *); 66228753Smmstatic int file_open(struct archive *, void *); 67228753Smmstatic ssize_t file_write(struct archive *, void *, const void *buff, size_t); 68238856Smmstatic int open_filename(struct archive *, int, const void *); 69228753Smm 70228753Smmint 71228753Smmarchive_write_open_file(struct archive *a, const char *filename) 72228753Smm{ 73228753Smm return (archive_write_open_filename(a, filename)); 74228753Smm} 75228753Smm 76228753Smmint 77228753Smmarchive_write_open_filename(struct archive *a, const char *filename) 78228753Smm{ 79228753Smm 80228753Smm if (filename == NULL || filename[0] == '\0') 81228753Smm return (archive_write_open_fd(a, 1)); 82228753Smm 83238856Smm return (open_filename(a, 1, filename)); 84228753Smm} 85228753Smm 86232153Smmint 87232153Smmarchive_write_open_filename_w(struct archive *a, const wchar_t *filename) 88232153Smm{ 89232153Smm 90232153Smm if (filename == NULL || filename[0] == L'\0') 91232153Smm return (archive_write_open_fd(a, 1)); 92232153Smm 93238856Smm return (open_filename(a, 0, filename)); 94238856Smm} 95238856Smm 96238856Smmstatic int 97238856Smmopen_filename(struct archive *a, int mbs_fn, const void *filename) 98238856Smm{ 99238856Smm struct write_file_data *mine; 100238856Smm int r; 101238856Smm 102238856Smm mine = (struct write_file_data *)calloc(1, sizeof(*mine)); 103232153Smm if (mine == NULL) { 104232153Smm archive_set_error(a, ENOMEM, "No memory"); 105232153Smm return (ARCHIVE_FATAL); 106232153Smm } 107238856Smm if (mbs_fn) 108238856Smm r = archive_mstring_copy_mbs(&mine->filename, filename); 109238856Smm else 110238856Smm r = archive_mstring_copy_wcs(&mine->filename, filename); 111238856Smm if (r < 0) { 112238856Smm if (errno == ENOMEM) { 113238856Smm archive_set_error(a, ENOMEM, "No memory"); 114238856Smm return (ARCHIVE_FATAL); 115238856Smm } 116238856Smm if (mbs_fn) 117238856Smm archive_set_error(a, ARCHIVE_ERRNO_MISC, 118238856Smm "Can't convert '%s' to WCS", 119238856Smm (const char *)filename); 120238856Smm else 121238856Smm archive_set_error(a, ARCHIVE_ERRNO_MISC, 122238856Smm "Can't convert '%S' to MBS", 123238856Smm (const wchar_t *)filename); 124238856Smm return (ARCHIVE_FAILED); 125238856Smm } 126232153Smm mine->fd = -1; 127368708Smm return (archive_write_open2(a, mine, 128368708Smm file_open, file_write, file_close, file_free)); 129232153Smm} 130232153Smm 131228753Smmstatic int 132228753Smmfile_open(struct archive *a, void *client_data) 133228753Smm{ 134228753Smm int flags; 135228753Smm struct write_file_data *mine; 136228753Smm struct stat st; 137238856Smm#if defined(_WIN32) && !defined(__CYGWIN__) 138238856Smm wchar_t *fullpath; 139238856Smm#endif 140238856Smm const wchar_t *wcs; 141238856Smm const char *mbs; 142228753Smm 143228753Smm mine = (struct write_file_data *)client_data; 144248616Smm flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_CLOEXEC; 145228753Smm 146228753Smm /* 147228753Smm * Open the file. 148228753Smm */ 149238856Smm mbs = NULL; wcs = NULL; 150232153Smm#if defined(_WIN32) && !defined(__CYGWIN__) 151238856Smm if (archive_mstring_get_wcs(a, &mine->filename, &wcs) != 0) { 152238856Smm if (errno == ENOMEM) 153238856Smm archive_set_error(a, errno, "No memory"); 154238856Smm else { 155238856Smm archive_mstring_get_mbs(a, &mine->filename, &mbs); 156238856Smm archive_set_error(a, errno, 157238856Smm "Can't convert '%s' to WCS", mbs); 158232153Smm } 159238856Smm return (ARCHIVE_FATAL); 160238856Smm } 161238856Smm fullpath = __la_win_permissive_name_w(wcs); 162238856Smm if (fullpath != NULL) { 163238856Smm mine->fd = _wopen(fullpath, flags, 0666); 164238856Smm free(fullpath); 165238856Smm } else 166238856Smm mine->fd = _wopen(wcs, flags, 0666); 167238856Smm#else 168238856Smm if (archive_mstring_get_mbs(a, &mine->filename, &mbs) != 0) { 169238856Smm if (errno == ENOMEM) 170238856Smm archive_set_error(a, errno, "No memory"); 171238856Smm else { 172238856Smm archive_mstring_get_wcs(a, &mine->filename, &wcs); 173238856Smm archive_set_error(a, errno, 174238856Smm "Can't convert '%S' to MBS", wcs); 175232153Smm } 176228753Smm return (ARCHIVE_FATAL); 177238856Smm } 178238856Smm mine->fd = open(mbs, flags, 0666); 179248616Smm __archive_ensure_cloexec_flag(mine->fd); 180232153Smm#endif 181238856Smm if (mine->fd < 0) { 182238856Smm if (mbs != NULL) 183238856Smm archive_set_error(a, errno, "Failed to open '%s'", mbs); 184238856Smm else 185238856Smm archive_set_error(a, errno, "Failed to open '%S'", wcs); 186238856Smm return (ARCHIVE_FATAL); 187228753Smm } 188228753Smm 189238856Smm if (fstat(mine->fd, &st) != 0) { 190238856Smm if (mbs != NULL) 191238856Smm archive_set_error(a, errno, "Couldn't stat '%s'", mbs); 192238856Smm else 193238856Smm archive_set_error(a, errno, "Couldn't stat '%S'", wcs); 194238856Smm return (ARCHIVE_FATAL); 195238856Smm } 196238856Smm 197228753Smm /* 198228753Smm * Set up default last block handling. 199228753Smm */ 200228753Smm if (archive_write_get_bytes_in_last_block(a) < 0) { 201228753Smm if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) || 202228753Smm S_ISFIFO(st.st_mode)) 203228753Smm /* Pad last block when writing to device or FIFO. */ 204228753Smm archive_write_set_bytes_in_last_block(a, 0); 205228753Smm else 206228753Smm /* Don't pad last block otherwise. */ 207228753Smm archive_write_set_bytes_in_last_block(a, 1); 208228753Smm } 209228753Smm 210228753Smm /* 211228753Smm * If the output file is a regular file, don't add it to 212228753Smm * itself. If it's a device file, it's okay to add the device 213228753Smm * entry to the output archive. 214228753Smm */ 215228753Smm if (S_ISREG(st.st_mode)) 216228753Smm archive_write_set_skip_file(a, st.st_dev, st.st_ino); 217228753Smm 218228753Smm return (ARCHIVE_OK); 219228753Smm} 220228753Smm 221228753Smmstatic ssize_t 222238856Smmfile_write(struct archive *a, void *client_data, const void *buff, 223238856Smm size_t length) 224228753Smm{ 225228753Smm struct write_file_data *mine; 226228753Smm ssize_t bytesWritten; 227228753Smm 228228753Smm mine = (struct write_file_data *)client_data; 229228753Smm for (;;) { 230228753Smm bytesWritten = write(mine->fd, buff, length); 231228753Smm if (bytesWritten <= 0) { 232228753Smm if (errno == EINTR) 233228753Smm continue; 234228753Smm archive_set_error(a, errno, "Write error"); 235228753Smm return (-1); 236228753Smm } 237228753Smm return (bytesWritten); 238228753Smm } 239228753Smm} 240228753Smm 241228753Smmstatic int 242228753Smmfile_close(struct archive *a, void *client_data) 243228753Smm{ 244228753Smm struct write_file_data *mine = (struct write_file_data *)client_data; 245228753Smm 246228753Smm (void)a; /* UNUSED */ 247302001Smm 248368708Smm if (mine == NULL) 249368708Smm return (ARCHIVE_FATAL); 250368708Smm 251302001Smm if (mine->fd >= 0) 252302001Smm close(mine->fd); 253302001Smm 254368708Smm return (ARCHIVE_OK); 255368708Smm} 256368708Smm 257368708Smmstatic int 258368708Smmfile_free(struct archive *a, void *client_data) 259368708Smm{ 260368708Smm struct write_file_data *mine = (struct write_file_data *)client_data; 261368708Smm 262368708Smm (void)a; /* UNUSED */ 263368708Smm 264368708Smm if (mine == NULL) 265368708Smm return (ARCHIVE_OK); 266368708Smm 267238856Smm archive_mstring_clean(&mine->filename); 268228753Smm free(mine); 269228753Smm return (ARCHIVE_OK); 270228753Smm} 271