Sync with FreeBSD. This adds read-only support for zip and ISO9660.
[dragonfly.git] / contrib / libarchive / archive_write_set_format_cpio.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: src/lib/libarchive/archive_write_set_format_cpio.c,v 1.5 2004/11/05 05:26:30 kientzle Exp $");
29
30 #include <sys/stat.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "archive.h"
37 #include "archive_entry.h"
38 #include "archive_private.h"
39
40 static int      archive_write_cpio_data(struct archive *, const void *buff,
41                     size_t s);
42 static int      archive_write_cpio_finish(struct archive *);
43 static int      archive_write_cpio_finish_entry(struct archive *);
44 static int      archive_write_cpio_header(struct archive *,
45                     struct archive_entry *);
46 static int      format_octal(int64_t, void *, int);
47 static int64_t  format_octal_recursive(int64_t, char *, int);
48
49 struct cpio {
50         uint64_t          entry_bytes_remaining;
51 };
52
53 struct cpio_header {
54         char    c_magic[6];
55         char    c_dev[6];
56         char    c_ino[6];
57         char    c_mode[6];
58         char    c_uid[6];
59         char    c_gid[6];
60         char    c_nlink[6];
61         char    c_rdev[6];
62         char    c_mtime[11];
63         char    c_namesize[6];
64         char    c_filesize[11];
65 };
66
67 /*
68  * Set output format to 'cpio' format.
69  */
70 int
71 archive_write_set_format_cpio(struct archive *a)
72 {
73         struct cpio *cpio;
74
75         /* If someone else was already registered, unregister them. */
76         if (a->format_finish != NULL)
77                 (a->format_finish)(a);
78
79         cpio = malloc(sizeof(*cpio));
80         if (cpio == NULL) {
81                 archive_set_error(a, ENOMEM, "Can't allocate cpio data");
82                 return (ARCHIVE_FATAL);
83         }
84         memset(cpio, 0, sizeof(*cpio));
85         a->format_data = cpio;
86
87         a->pad_uncompressed = 1;
88         a->format_write_header = archive_write_cpio_header;
89         a->format_write_data = archive_write_cpio_data;
90         a->format_finish_entry = archive_write_cpio_finish_entry;
91         a->format_finish = archive_write_cpio_finish;
92         a->archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
93         a->archive_format_name = "POSIX cpio";
94         return (ARCHIVE_OK);
95 }
96
97 static int
98 archive_write_cpio_header(struct archive *a, struct archive_entry *entry)
99 {
100         struct cpio *cpio;
101         const char *p, *path;
102         int pathlength, ret;
103         const struct stat       *st;
104         struct cpio_header       h;
105
106         cpio = a->format_data;
107         ret = 0;
108
109         path = archive_entry_pathname(entry);
110         pathlength = strlen(path) + 1; /* Include trailing null. */
111         st = archive_entry_stat(entry);
112
113         memset(&h, 0, sizeof(h));
114         format_octal(070707, &h.c_magic, sizeof(h.c_magic));
115         format_octal(st->st_dev, &h.c_dev, sizeof(h.c_dev));
116         /*
117          * TODO: Generate artificial inode numbers rather than just
118          * re-using the ones off the disk.  That way, the 18-bit c_ino
119          * field only limits the number of files in the archive.
120          */
121         if (st->st_ino > 0777777) {
122                 archive_set_error(a, ERANGE, "large inode number truncated");
123                 ret = ARCHIVE_WARN;
124         }
125
126         format_octal(st->st_ino & 0777777, &h.c_ino, sizeof(h.c_ino));
127         format_octal(st->st_mode, &h.c_mode, sizeof(h.c_mode));
128         format_octal(st->st_uid, &h.c_uid, sizeof(h.c_uid));
129         format_octal(st->st_gid, &h.c_gid, sizeof(h.c_gid));
130         format_octal(st->st_nlink, &h.c_nlink, sizeof(h.c_nlink));
131         if(S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode))
132             format_octal(st->st_rdev, &h.c_rdev, sizeof(h.c_rdev));
133         else
134             format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
135         format_octal(st->st_mtime, &h.c_mtime, sizeof(h.c_mtime));
136         format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
137
138         /* Symlinks get the link written as the body of the entry. */
139         p = archive_entry_symlink(entry);
140         if (p != NULL  &&  *p != '\0')
141                 format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
142         else
143                 format_octal(st->st_size, &h.c_filesize, sizeof(h.c_filesize));
144
145         ret = (a->compression_write)(a, &h, sizeof(h));
146         if (ret != ARCHIVE_OK)
147                 return (ARCHIVE_FATAL);
148
149         ret = (a->compression_write)(a, path, pathlength);
150         if (ret != ARCHIVE_OK)
151                 return (ARCHIVE_FATAL);
152
153         cpio->entry_bytes_remaining = st->st_size;
154
155         /* Write the symlink now. */
156         if (p != NULL  &&  *p != '\0')
157                 ret = (a->compression_write)(a, p, strlen(p));
158
159         return (ret);
160 }
161
162 static int
163 archive_write_cpio_data(struct archive *a, const void *buff, size_t s)
164 {
165         struct cpio *cpio;
166         int ret;
167
168         cpio = a->format_data;
169         if (s > cpio->entry_bytes_remaining)
170                 s = cpio->entry_bytes_remaining;
171
172         ret = (a->compression_write)(a, buff, s);
173         cpio->entry_bytes_remaining -= s;
174         return (ret);
175 }
176
177 /*
178  * Format a number into the specified field.
179  */
180 static int
181 format_octal(int64_t v, void *p, int digits)
182 {
183         int64_t max;
184         int     ret;
185
186         max = (((int64_t)1) << (digits * 3)) - 1;
187         if (v >= 0  &&  v <= max) {
188             format_octal_recursive(v, p, digits);
189             ret = 0;
190         } else {
191             format_octal_recursive(max, p, digits);
192             ret = -1;
193         }
194         return (ret);
195 }
196
197 static int64_t
198 format_octal_recursive(int64_t v, char *p, int s)
199 {
200         if (s == 0)
201                 return (v);
202         v = format_octal_recursive(v, p+1, s-1);
203         *p = '0' + (v & 7);
204         return (v >>= 3);
205 }
206
207 static int
208 archive_write_cpio_finish(struct archive *a)
209 {
210         struct cpio *cpio;
211         struct stat st;
212         int er;
213         struct archive_entry *trailer;
214
215         cpio = a->format_data;
216         trailer = archive_entry_new();
217         memset(&st, 0, sizeof(st));
218         st.st_nlink = 1;
219         archive_entry_copy_stat(trailer, &st);
220         archive_entry_set_pathname(trailer, "TRAILER!!!");
221         er = archive_write_cpio_header(a, trailer);
222         archive_entry_free(trailer);
223
224         free(cpio);
225         a->format_data = NULL;
226         return (er);
227 }
228
229 static int
230 archive_write_cpio_finish_entry(struct archive *a)
231 {
232         struct cpio *cpio;
233         int to_write, ret;
234
235         cpio = a->format_data;
236         ret = ARCHIVE_OK;
237         while (cpio->entry_bytes_remaining > 0) {
238                 to_write = cpio->entry_bytes_remaining < a->null_length ?
239                     cpio->entry_bytes_remaining : a->null_length;
240                 ret = (a->compression_write)(a, a->nulls, to_write);
241                 if (ret != ARCHIVE_OK)
242                         return (ret);
243                 cpio->entry_bytes_remaining -= to_write;
244         }
245         return (ret);
246 }