Import libarchive and bsdtar version 2.0.25
[dragonfly/port-amd64.git] / contrib / libarchive-2.0 / libarchive / archive_write_set_format_cpio.c
blobb88eb3e379c10a8b6f0c615a0fcee32e010a5450
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_format_cpio.c,v 1.10 2007/03/03 07:37:36 kientzle Exp $");
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 #include <stdio.h>
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #endif
43 #include "archive.h"
44 #include "archive_entry.h"
45 #include "archive_private.h"
46 #include "archive_write_private.h"
48 static ssize_t archive_write_cpio_data(struct archive_write *,
49 const void *buff, size_t s);
50 static int archive_write_cpio_finish(struct archive_write *);
51 static int archive_write_cpio_destroy(struct archive_write *);
52 static int archive_write_cpio_finish_entry(struct archive_write *);
53 static int archive_write_cpio_header(struct archive_write *,
54 struct archive_entry *);
55 static int format_octal(int64_t, void *, int);
56 static int64_t format_octal_recursive(int64_t, char *, int);
58 struct cpio {
59 uint64_t entry_bytes_remaining;
62 struct cpio_header {
63 char c_magic[6];
64 char c_dev[6];
65 char c_ino[6];
66 char c_mode[6];
67 char c_uid[6];
68 char c_gid[6];
69 char c_nlink[6];
70 char c_rdev[6];
71 char c_mtime[11];
72 char c_namesize[6];
73 char c_filesize[11];
77 * Set output format to 'cpio' format.
79 int
80 archive_write_set_format_cpio(struct archive *_a)
82 struct archive_write *a = (struct archive_write *)_a;
83 struct cpio *cpio;
85 /* If someone else was already registered, unregister them. */
86 if (a->format_destroy != NULL)
87 (a->format_destroy)(a);
89 cpio = (struct cpio *)malloc(sizeof(*cpio));
90 if (cpio == NULL) {
91 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
92 return (ARCHIVE_FATAL);
94 memset(cpio, 0, sizeof(*cpio));
95 a->format_data = cpio;
97 a->pad_uncompressed = 1;
98 a->format_write_header = archive_write_cpio_header;
99 a->format_write_data = archive_write_cpio_data;
100 a->format_finish_entry = archive_write_cpio_finish_entry;
101 a->format_finish = archive_write_cpio_finish;
102 a->format_destroy = archive_write_cpio_destroy;
103 a->archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
104 a->archive_format_name = "POSIX cpio";
105 return (ARCHIVE_OK);
108 static int
109 archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
111 struct cpio *cpio;
112 const char *p, *path;
113 int pathlength, ret;
114 const struct stat *st;
115 struct cpio_header h;
117 cpio = (struct cpio *)a->format_data;
118 ret = 0;
120 path = archive_entry_pathname(entry);
121 pathlength = strlen(path) + 1; /* Include trailing null. */
122 st = archive_entry_stat(entry);
124 memset(&h, 0, sizeof(h));
125 format_octal(070707, &h.c_magic, sizeof(h.c_magic));
126 format_octal(st->st_dev, &h.c_dev, sizeof(h.c_dev));
128 * TODO: Generate artificial inode numbers rather than just
129 * re-using the ones off the disk. That way, the 18-bit c_ino
130 * field only limits the number of files in the archive.
132 if (st->st_ino > 0777777) {
133 archive_set_error(&a->archive, ERANGE, "large inode number truncated");
134 ret = ARCHIVE_WARN;
137 format_octal(st->st_ino & 0777777, &h.c_ino, sizeof(h.c_ino));
138 format_octal(st->st_mode, &h.c_mode, sizeof(h.c_mode));
139 format_octal(st->st_uid, &h.c_uid, sizeof(h.c_uid));
140 format_octal(st->st_gid, &h.c_gid, sizeof(h.c_gid));
141 format_octal(st->st_nlink, &h.c_nlink, sizeof(h.c_nlink));
142 if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode))
143 format_octal(st->st_rdev, &h.c_rdev, sizeof(h.c_rdev));
144 else
145 format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
146 format_octal(st->st_mtime, &h.c_mtime, sizeof(h.c_mtime));
147 format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
149 /* Symlinks get the link written as the body of the entry. */
150 p = archive_entry_symlink(entry);
151 if (p != NULL && *p != '\0')
152 format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
153 else
154 format_octal(st->st_size, &h.c_filesize, sizeof(h.c_filesize));
156 ret = (a->compression_write)(a, &h, sizeof(h));
157 if (ret != ARCHIVE_OK)
158 return (ARCHIVE_FATAL);
160 ret = (a->compression_write)(a, path, pathlength);
161 if (ret != ARCHIVE_OK)
162 return (ARCHIVE_FATAL);
164 cpio->entry_bytes_remaining = st->st_size;
166 /* Write the symlink now. */
167 if (p != NULL && *p != '\0')
168 ret = (a->compression_write)(a, p, strlen(p));
170 return (ret);
173 static ssize_t
174 archive_write_cpio_data(struct archive_write *a, const void *buff, size_t s)
176 struct cpio *cpio;
177 int ret;
179 cpio = (struct cpio *)a->format_data;
180 if (s > cpio->entry_bytes_remaining)
181 s = cpio->entry_bytes_remaining;
183 ret = (a->compression_write)(a, buff, s);
184 cpio->entry_bytes_remaining -= s;
185 if (ret >= 0)
186 return (s);
187 else
188 return (ret);
192 * Format a number into the specified field.
194 static int
195 format_octal(int64_t v, void *p, int digits)
197 int64_t max;
198 int ret;
200 max = (((int64_t)1) << (digits * 3)) - 1;
201 if (v >= 0 && v <= max) {
202 format_octal_recursive(v, (char *)p, digits);
203 ret = 0;
204 } else {
205 format_octal_recursive(max, (char *)p, digits);
206 ret = -1;
208 return (ret);
211 static int64_t
212 format_octal_recursive(int64_t v, char *p, int s)
214 if (s == 0)
215 return (v);
216 v = format_octal_recursive(v, p+1, s-1);
217 *p = '0' + (v & 7);
218 return (v >>= 3);
221 static int
222 archive_write_cpio_finish(struct archive_write *a)
224 struct cpio *cpio;
225 struct stat st;
226 int er;
227 struct archive_entry *trailer;
229 cpio = (struct cpio *)a->format_data;
230 trailer = archive_entry_new();
231 memset(&st, 0, sizeof(st));
232 st.st_nlink = 1;
233 archive_entry_copy_stat(trailer, &st);
234 archive_entry_set_pathname(trailer, "TRAILER!!!");
235 er = archive_write_cpio_header(a, trailer);
236 archive_entry_free(trailer);
237 return (er);
240 static int
241 archive_write_cpio_destroy(struct archive_write *a)
243 struct cpio *cpio;
245 cpio = (struct cpio *)a->format_data;
246 free(cpio);
247 a->format_data = NULL;
248 return (ARCHIVE_OK);
251 static int
252 archive_write_cpio_finish_entry(struct archive_write *a)
254 struct cpio *cpio;
255 int to_write, ret;
257 cpio = (struct cpio *)a->format_data;
258 ret = ARCHIVE_OK;
259 while (cpio->entry_bytes_remaining > 0) {
260 to_write = cpio->entry_bytes_remaining < a->null_length ?
261 cpio->entry_bytes_remaining : a->null_length;
262 ret = (a->compression_write)(a, a->nulls, to_write);
263 if (ret != ARCHIVE_OK)
264 return (ret);
265 cpio->entry_bytes_remaining -= to_write;
267 return (ret);