2 * Copyright (c) 2003-2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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 $");
37 #include "archive_entry.h"
38 #include "archive_private.h"
40 static int archive_write_cpio_data(struct archive *, const void *buff,
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);
50 uint64_t entry_bytes_remaining;
68 * Set output format to 'cpio' format.
71 archive_write_set_format_cpio(struct archive *a)
75 /* If someone else was already registered, unregister them. */
76 if (a->format_finish != NULL)
77 (a->format_finish)(a);
79 cpio = malloc(sizeof(*cpio));
81 archive_set_error(a, ENOMEM, "Can't allocate cpio data");
82 return (ARCHIVE_FATAL);
84 memset(cpio, 0, sizeof(*cpio));
85 a->format_data = cpio;
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";
98 archive_write_cpio_header(struct archive *a, struct archive_entry *entry)
101 const char *p, *path;
103 const struct stat *st;
104 struct cpio_header h;
106 cpio = a->format_data;
109 path = archive_entry_pathname(entry);
110 pathlength = strlen(path) + 1; /* Include trailing null. */
111 st = archive_entry_stat(entry);
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));
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.
121 if (st->st_ino > 0777777) {
122 archive_set_error(a, ERANGE, "large inode number truncated");
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));
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));
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));
143 format_octal(st->st_size, &h.c_filesize, sizeof(h.c_filesize));
145 ret = (a->compression_write)(a, &h, sizeof(h));
146 if (ret != ARCHIVE_OK)
147 return (ARCHIVE_FATAL);
149 ret = (a->compression_write)(a, path, pathlength);
150 if (ret != ARCHIVE_OK)
151 return (ARCHIVE_FATAL);
153 cpio->entry_bytes_remaining = st->st_size;
155 /* Write the symlink now. */
156 if (p != NULL && *p != '\0')
157 ret = (a->compression_write)(a, p, strlen(p));
163 archive_write_cpio_data(struct archive *a, const void *buff, size_t s)
168 cpio = a->format_data;
169 if (s > cpio->entry_bytes_remaining)
170 s = cpio->entry_bytes_remaining;
172 ret = (a->compression_write)(a, buff, s);
173 cpio->entry_bytes_remaining -= s;
178 * Format a number into the specified field.
181 format_octal(int64_t v, void *p, int digits)
186 max = (((int64_t)1) << (digits * 3)) - 1;
187 if (v >= 0 && v <= max) {
188 format_octal_recursive(v, p, digits);
191 format_octal_recursive(max, p, digits);
198 format_octal_recursive(int64_t v, char *p, int s)
202 v = format_octal_recursive(v, p+1, s-1);
208 archive_write_cpio_finish(struct archive *a)
213 struct archive_entry *trailer;
215 cpio = a->format_data;
216 trailer = archive_entry_new();
217 memset(&st, 0, sizeof(st));
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);
225 a->format_data = NULL;
230 archive_write_cpio_finish_entry(struct archive *a)
235 cpio = a->format_data;
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)
243 cpio->entry_bytes_remaining -= to_write;