Home | History | Annotate | Line # | Download | only in libarchive
      1 /*-
      2  * Copyright (c) 2003-2007 Tim Kientzle
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
     15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
     18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "archive_platform.h"
     27 
     28 #ifdef HAVE_SYS_STAT_H
     29 #include <sys/stat.h>
     30 #endif
     31 #ifdef HAVE_ERRNO_H
     32 #include <errno.h>
     33 #endif
     34 #ifdef HAVE_FCNTL_H
     35 #include <fcntl.h>
     36 #endif
     37 #ifdef HAVE_STDLIB_H
     38 #include <stdlib.h>
     39 #endif
     40 #ifdef HAVE_STRING_H
     41 #include <string.h>
     42 #endif
     43 #ifdef HAVE_UNISTD_H
     44 #include <unistd.h>
     45 #endif
     46 
     47 #include "archive.h"
     48 #include "archive_private.h"
     49 #include "archive_string.h"
     50 
     51 #ifndef O_BINARY
     52 #define O_BINARY 0
     53 #endif
     54 #ifndef O_CLOEXEC
     55 #define O_CLOEXEC	0
     56 #endif
     57 
     58 struct write_file_data {
     59 	int		fd;
     60 	struct archive_mstring filename;
     61 };
     62 
     63 static int	file_close(struct archive *, void *);
     64 static int	file_free(struct archive *, void *);
     65 static int	file_open(struct archive *, void *);
     66 static ssize_t	file_write(struct archive *, void *, const void *buff, size_t);
     67 static int	open_filename(struct archive *, int, const void *);
     68 
     69 int
     70 archive_write_open_file(struct archive *a, const char *filename)
     71 {
     72 	return (archive_write_open_filename(a, filename));
     73 }
     74 
     75 int
     76 archive_write_open_filename(struct archive *a, const char *filename)
     77 {
     78 
     79 	if (filename == NULL || filename[0] == '\0')
     80 		return (archive_write_open_fd(a, 1));
     81 
     82 	return (open_filename(a, 1, filename));
     83 }
     84 
     85 int
     86 archive_write_open_filename_w(struct archive *a, const wchar_t *filename)
     87 {
     88 
     89 	if (filename == NULL || filename[0] == L'\0')
     90 		return (archive_write_open_fd(a, 1));
     91 
     92 	return (open_filename(a, 0, filename));
     93 }
     94 
     95 static int
     96 open_filename(struct archive *a, int mbs_fn, const void *filename)
     97 {
     98 	struct write_file_data *mine;
     99 	int r;
    100 
    101 	mine = calloc(1, sizeof(*mine));
    102 	if (mine == NULL) {
    103 		archive_set_error(a, ENOMEM, "No memory");
    104 		return (ARCHIVE_FATAL);
    105 	}
    106 	if (mbs_fn)
    107 		r = archive_mstring_copy_mbs(&mine->filename, filename);
    108 	else
    109 		r = archive_mstring_copy_wcs(&mine->filename, filename);
    110 	if (r < 0) {
    111 		free(mine);
    112 		if (errno == ENOMEM) {
    113 			archive_set_error(a, ENOMEM, "No memory");
    114 			return (ARCHIVE_FATAL);
    115 		}
    116 		if (mbs_fn)
    117 			archive_set_error(a, ARCHIVE_ERRNO_MISC,
    118 			    "Can't convert '%s' to WCS",
    119 			    (const char *)filename);
    120 		else
    121 			archive_set_error(a, ARCHIVE_ERRNO_MISC,
    122 			    "Can't convert '%ls' to MBS",
    123 			    (const wchar_t *)filename);
    124 		return (ARCHIVE_FAILED);
    125 	}
    126 	mine->fd = -1;
    127 	return (archive_write_open2(a, mine,
    128 		    file_open, file_write, file_close, file_free));
    129 }
    130 
    131 static int
    132 file_open(struct archive *a, void *client_data)
    133 {
    134 	int flags;
    135 	struct write_file_data *mine;
    136 	struct stat st;
    137 #if defined(_WIN32) && !defined(__CYGWIN__)
    138 	wchar_t *fullpath;
    139 #endif
    140 	const wchar_t *wcs;
    141 	const char *mbs;
    142 
    143 	mine = (struct write_file_data *)client_data;
    144 	flags = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_CLOEXEC;
    145 
    146 	/*
    147 	 * Open the file.
    148 	 */
    149 	mbs = NULL; wcs = NULL;
    150 #if defined(_WIN32) && !defined(__CYGWIN__)
    151 	if (archive_mstring_get_wcs(a, &mine->filename, &wcs) != 0) {
    152 		if (errno == ENOMEM)
    153 			archive_set_error(a, errno, "No memory");
    154 		else {
    155 			archive_mstring_get_mbs(a, &mine->filename, &mbs);
    156 			archive_set_error(a, errno,
    157 			    "Can't convert '%s' to WCS", mbs);
    158 		}
    159 		return (ARCHIVE_FATAL);
    160 	}
    161 	fullpath = __la_win_permissive_name_w(wcs);
    162 	if (fullpath != NULL) {
    163 		mine->fd = _wopen(fullpath, flags, 0666);
    164 		free(fullpath);
    165 	} else
    166 		mine->fd = _wopen(wcs, flags, 0666);
    167 #else
    168 	if (archive_mstring_get_mbs(a, &mine->filename, &mbs) != 0) {
    169 		if (errno == ENOMEM)
    170 			archive_set_error(a, errno, "No memory");
    171 		else {
    172 			archive_mstring_get_wcs(a, &mine->filename, &wcs);
    173 			archive_set_error(a, errno,
    174 			    "Can't convert '%ls' to MBS", wcs);
    175 		}
    176 		return (ARCHIVE_FATAL);
    177 	}
    178 	mine->fd = open(mbs, flags, 0666);
    179 	__archive_ensure_cloexec_flag(mine->fd);
    180 #endif
    181 	if (mine->fd < 0) {
    182 		if (mbs != NULL)
    183 			archive_set_error(a, errno, "Failed to open '%s'", mbs);
    184 		else
    185 			archive_set_error(a, errno, "Failed to open '%ls'", wcs);
    186 		return (ARCHIVE_FATAL);
    187 	}
    188 
    189 	if (fstat(mine->fd, &st) != 0) {
    190 		if (mbs != NULL)
    191 			archive_set_error(a, errno, "Couldn't stat '%s'", mbs);
    192 		else
    193 			archive_set_error(a, errno, "Couldn't stat '%ls'", wcs);
    194 		close(mine->fd);
    195 		mine->fd = -1;
    196 		return (ARCHIVE_FATAL);
    197 	}
    198 
    199 	/*
    200 	 * Set up default last block handling.
    201 	 */
    202 	if (archive_write_get_bytes_in_last_block(a) < 0) {
    203 		if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) ||
    204 		    S_ISFIFO(st.st_mode))
    205 			/* Pad last block when writing to device or FIFO. */
    206 			archive_write_set_bytes_in_last_block(a, 0);
    207 		else
    208 			/* Don't pad last block otherwise. */
    209 			archive_write_set_bytes_in_last_block(a, 1);
    210 	}
    211 
    212 	/*
    213 	 * If the output file is a regular file, don't add it to
    214 	 * itself.  If it's a device file, it's okay to add the device
    215 	 * entry to the output archive.
    216 	 */
    217 	if (S_ISREG(st.st_mode))
    218 		archive_write_set_skip_file(a, st.st_dev, st.st_ino);
    219 
    220 	return (ARCHIVE_OK);
    221 }
    222 
    223 static ssize_t
    224 file_write(struct archive *a, void *client_data, const void *buff,
    225     size_t length)
    226 {
    227 	struct write_file_data	*mine;
    228 	ssize_t	bytesWritten;
    229 
    230 	mine = (struct write_file_data *)client_data;
    231 	for (;;) {
    232 		bytesWritten = write(mine->fd, buff, length);
    233 		if (bytesWritten < 0) {
    234 			if (errno == EINTR)
    235 				continue;
    236 			archive_set_error(a, errno, "Write error");
    237 			return (-1);
    238 		}
    239 		return (bytesWritten);
    240 	}
    241 }
    242 
    243 static int
    244 file_close(struct archive *a, void *client_data)
    245 {
    246 	struct write_file_data	*mine = (struct write_file_data *)client_data;
    247 
    248 	(void)a; /* UNUSED */
    249 
    250 	if (mine == NULL)
    251 		return (ARCHIVE_FATAL);
    252 
    253 	if (mine->fd >= 0)
    254 		close(mine->fd);
    255 
    256 	return (ARCHIVE_OK);
    257 }
    258 
    259 static int
    260 file_free(struct archive *a, void *client_data)
    261 {
    262 	struct write_file_data	*mine = (struct write_file_data *)client_data;
    263 
    264 	(void)a; /* UNUSED */
    265 
    266 	if (mine == NULL)
    267 		return (ARCHIVE_OK);
    268 
    269 	archive_mstring_clean(&mine->filename);
    270 	free(mine);
    271 	return (ARCHIVE_OK);
    272 }
    273