Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-2.1 / libarchive / archive_write_open_filename.c
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 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_open_filename.c,v 1.19 2007/01/09 08:05:56 kientzle Exp $");
28
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef HAVE_ERRNO_H
33 #include <errno.h>
34 #endif
35 #ifdef HAVE_FCNTL_H
36 #include <fcntl.h>
37 #endif
38 #ifdef HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41 #ifdef HAVE_STRING_H
42 #include <string.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 #include "archive.h"
49
50 struct write_file_data {
51         int             fd;
52         char            filename[1];
53 };
54
55 static int      file_close(struct archive *, void *);
56 static int      file_open(struct archive *, void *);
57 static ssize_t  file_write(struct archive *, void *, const void *buff, size_t);
58
59 int
60 archive_write_open_file(struct archive *a, const char *filename)
61 {
62         return (archive_write_open_filename(a, filename));
63 }
64
65 int
66 archive_write_open_filename(struct archive *a, const char *filename)
67 {
68         struct write_file_data *mine;
69
70         if (filename == NULL || filename[0] == '\0') {
71                 mine = (struct write_file_data *)malloc(sizeof(*mine));
72                 if (mine == NULL) {
73                         archive_set_error(a, ENOMEM, "No memory");
74                         return (ARCHIVE_FATAL);
75                 }
76                 mine->filename[0] = '\0'; /* Record that we're using stdout. */
77         } else {
78                 mine = (struct write_file_data *)malloc(sizeof(*mine) + strlen(filename));
79                 if (mine == NULL) {
80                         archive_set_error(a, ENOMEM, "No memory");
81                         return (ARCHIVE_FATAL);
82                 }
83                 strcpy(mine->filename, filename);
84         }
85         mine->fd = -1;
86         return (archive_write_open(a, mine,
87                     file_open, file_write, file_close));
88 }
89
90 static int
91 file_open(struct archive *a, void *client_data)
92 {
93         int flags;
94         struct write_file_data *mine;
95         struct stat st;
96
97         mine = (struct write_file_data *)client_data;
98         flags = O_WRONLY | O_CREAT | O_TRUNC;
99
100         /*
101          * Open the file.
102          */
103         if (mine->filename[0] != '\0') {
104                 mine->fd = open(mine->filename, flags, 0666);
105                 if (mine->fd < 0) {
106                         archive_set_error(a, errno, "Failed to open '%s'",
107                             mine->filename);
108                         return (ARCHIVE_FATAL);
109                 }
110         } else {
111                 /*
112                  * NULL filename is stdout.
113                  */
114                 mine->fd = 1;
115                 /* By default, pad archive when writing to stdout. */
116                 if (archive_write_get_bytes_in_last_block(a) < 0)
117                         archive_write_set_bytes_in_last_block(a, 0);
118         }
119
120         if (fstat(mine->fd, &st) != 0) {
121                archive_set_error(a, errno, "Couldn't stat '%s'",
122                    mine->filename);
123                return (ARCHIVE_FATAL);
124         }
125
126         /*
127          * Set up default last block handling.
128          */
129         if (archive_write_get_bytes_in_last_block(a) < 0) {
130                 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode) ||
131                     S_ISFIFO(st.st_mode))
132                         /* Pad last block when writing to device or FIFO. */
133                         archive_write_set_bytes_in_last_block(a, 0);
134                 else
135                         /* Don't pad last block otherwise. */
136                         archive_write_set_bytes_in_last_block(a, 1);
137         }
138
139         /*
140          * If the output file is a regular file, don't add it to
141          * itself.  If it's a device file, it's okay to add the device
142          * entry to the output archive.
143          */
144         if (S_ISREG(st.st_mode))
145                 archive_write_set_skip_file(a, st.st_dev, st.st_ino);
146
147         return (ARCHIVE_OK);
148 }
149
150 static ssize_t
151 file_write(struct archive *a, void *client_data, const void *buff, size_t length)
152 {
153         struct write_file_data  *mine;
154         ssize_t bytesWritten;
155
156         mine = (struct write_file_data *)client_data;
157         bytesWritten = write(mine->fd, buff, length);
158         if (bytesWritten <= 0) {
159                 archive_set_error(a, errno, "Write error");
160                 return (-1);
161         }
162         return (bytesWritten);
163 }
164
165 static int
166 file_close(struct archive *a, void *client_data)
167 {
168         struct write_file_data  *mine = (struct write_file_data *)client_data;
169
170         (void)a; /* UNUSED */
171         if (mine->filename[0] != '\0')
172                 close(mine->fd);
173         free(mine);
174         return (ARCHIVE_OK);
175 }