Import of libarchive and bsdtar 1.3.1
[dragonfly.git] / contrib / libarchive-1.3.1 / libarchive / archive_write_open_filename.c
1 /*-
2  * Copyright (c) 2003-2004 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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #include "archive.h"
38
39 struct write_file_data {
40         int             fd;
41         char            filename[1];
42 };
43
44 static int      file_close(struct archive *, void *);
45 static int      file_open(struct archive *, void *);
46 static ssize_t  file_write(struct archive *, void *, void *buff, size_t);
47
48 int
49 archive_write_open_file(struct archive *a, const char *filename)
50 {
51         return (archive_write_open_filename(a, filename));
52 }
53
54 int
55 archive_write_open_filename(struct archive *a, const char *filename)
56 {
57         struct write_file_data *mine;
58
59         if (filename == NULL || filename[0] == '\0') {
60                 mine = malloc(sizeof(*mine));
61                 if (mine == NULL) {
62                         archive_set_error(a, ENOMEM, "No memory");
63                         return (ARCHIVE_FATAL);
64                 }
65                 mine->filename[0] = '\0'; /* Record that we're using stdout. */
66         } else {
67                 mine = malloc(sizeof(*mine) + strlen(filename));
68                 if (mine == NULL) {
69                         archive_set_error(a, ENOMEM, "No memory");
70                         return (ARCHIVE_FATAL);
71                 }
72                 strcpy(mine->filename, filename);
73         }
74         mine->fd = -1;
75         return (archive_write_open(a, mine,
76                     file_open, file_write, file_close));
77 }
78
79 static int
80 file_open(struct archive *a, void *client_data)
81 {
82         int flags;
83         struct write_file_data *mine;
84         struct stat st, *pst;
85
86         pst = NULL;
87         mine = client_data;
88         flags = O_WRONLY | O_CREAT | O_TRUNC;
89
90         if (mine->filename[0] != '\0') {
91                 mine->fd = open(mine->filename, flags, 0666);
92
93                 /*
94                  * If client hasn't explicitly set the last block
95                  * handling, then set it here: If the output is a
96                  * block or character device, pad the last block,
97                  * otherwise leave it unpadded.
98                  */
99                 if (mine->fd >= 0 && archive_write_get_bytes_in_last_block(a) < 0) {
100                         if (fstat(mine->fd, &st) == 0) {
101                                 pst = &st;
102                                 if (S_ISCHR(st.st_mode) ||
103                                     S_ISBLK(st.st_mode) ||
104                                     S_ISFIFO(st.st_mode))
105                                         /* Pad last block. */
106                                         archive_write_set_bytes_in_last_block(a, 0);
107                                 else
108                                         /* Don't pad last block. */
109                                         archive_write_set_bytes_in_last_block(a, 1);
110                         }
111                 }
112         } else {
113                 mine->fd = 1;
114                 if (archive_write_get_bytes_in_last_block(a) < 0) /* Still default? */
115                         /* Last block will be fully padded. */
116                         archive_write_set_bytes_in_last_block(a, 0);
117         }
118
119         if (mine->fd < 0) {
120                 archive_set_error(a, errno, "Failed to open '%s'",
121                     mine->filename);
122                 return (ARCHIVE_FATAL);
123         }
124
125         if (pst == NULL && fstat(mine->fd, &st) == 0)
126                 pst = &st;
127         if (pst == NULL) {
128                 archive_set_error(a, errno, "Couldn't stat '%s'",
129                     mine->filename);
130                 return (ARCHIVE_FATAL);
131         }
132
133         /*
134          * If the output file is a regular file, don't add it to
135          * itself.  If it's a device file, it's okay to add the device
136          * entry to the output archive.
137          */
138         if (S_ISREG(pst->st_mode))
139                 archive_write_set_skip_file(a, pst->st_dev, pst->st_ino);
140
141         return (ARCHIVE_OK);
142 }
143
144 static ssize_t
145 file_write(struct archive *a, void *client_data, void *buff, size_t length)
146 {
147         struct write_file_data  *mine;
148         ssize_t bytesWritten;
149
150         mine = client_data;
151         bytesWritten = write(mine->fd, buff, length);
152         if (bytesWritten <= 0) {
153                 archive_set_error(a, errno, "Write error");
154                 return (-1);
155         }
156         return (bytesWritten);
157 }
158
159 static int
160 file_close(struct archive *a, void *client_data)
161 {
162         struct write_file_data  *mine = client_data;
163
164         (void)a; /* UNUSED */
165         if (mine->filename[0] != '\0')
166                 close(mine->fd);
167         free(mine);
168         return (ARCHIVE_OK);
169 }