xref: /freebsd/contrib/libarchive/libarchive/archive_write_open_file.c (revision 0d4ad64077bcddcff5a170ee97273db95b9cab55)
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 
49 struct write_FILE_data {
50 	FILE		*f;
51 };
52 
53 static int	file_free(struct archive *, void *);
54 static int	file_open(struct archive *, void *);
55 static ssize_t	file_write(struct archive *, void *, const void *buff, size_t);
56 
57 int
58 archive_write_open_FILE(struct archive *a, FILE *f)
59 {
60 	struct write_FILE_data *mine;
61 
62 	mine = (struct write_FILE_data *)malloc(sizeof(*mine));
63 	if (mine == NULL) {
64 		archive_set_error(a, ENOMEM, "No memory");
65 		return (ARCHIVE_FATAL);
66 	}
67 	mine->f = f;
68 	return (archive_write_open2(a, mine, file_open, file_write,
69 	    NULL, file_free));
70 }
71 
72 static int
73 file_open(struct archive *a, void *client_data)
74 {
75 	(void)a; /* UNUSED */
76 	(void)client_data; /* UNUSED */
77 
78 	return (ARCHIVE_OK);
79 }
80 
81 static ssize_t
82 file_write(struct archive *a, void *client_data, const void *buff, size_t length)
83 {
84 	struct write_FILE_data	*mine;
85 	size_t	bytesWritten;
86 
87 	mine = client_data;
88 	for (;;) {
89 		bytesWritten = fwrite(buff, 1, length, mine->f);
90 		if (bytesWritten <= 0) {
91 			if (errno == EINTR)
92 				continue;
93 			archive_set_error(a, errno, "Write error");
94 			return (-1);
95 		}
96 		return (bytesWritten);
97 	}
98 }
99 
100 static int
101 file_free(struct archive *a, void *client_data)
102 {
103 	struct write_FILE_data	*mine = client_data;
104 
105 	(void)a; /* UNUSED */
106 	if (mine == NULL)
107 		return (ARCHIVE_OK);
108 	free(mine);
109 	return (ARCHIVE_OK);
110 }
111