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