2 * Copyright (c) 2003-2007 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 * 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.14 2008/03/15 11:04:45 kientzle Exp $");
41 #include "archive_entry.h"
42 #include "archive_private.h"
43 #include "archive_write_private.h"
45 static ssize_t
archive_write_cpio_data(struct archive_write
*,
46 const void *buff
, size_t s
);
47 static int archive_write_cpio_finish(struct archive_write
*);
48 static int archive_write_cpio_destroy(struct archive_write
*);
49 static int archive_write_cpio_finish_entry(struct archive_write
*);
50 static int archive_write_cpio_header(struct archive_write
*,
51 struct archive_entry
*);
52 static int format_octal(int64_t, void *, int);
53 static int64_t format_octal_recursive(int64_t, char *, int);
56 uint64_t entry_bytes_remaining
;
74 * Set output format to 'cpio' format.
77 archive_write_set_format_cpio(struct archive
*_a
)
79 struct archive_write
*a
= (struct archive_write
*)_a
;
82 /* If someone else was already registered, unregister them. */
83 if (a
->format_destroy
!= NULL
)
84 (a
->format_destroy
)(a
);
86 cpio
= (struct cpio
*)malloc(sizeof(*cpio
));
88 archive_set_error(&a
->archive
, ENOMEM
, "Can't allocate cpio data");
89 return (ARCHIVE_FATAL
);
91 memset(cpio
, 0, sizeof(*cpio
));
92 a
->format_data
= cpio
;
94 a
->pad_uncompressed
= 1;
95 a
->format_write_header
= archive_write_cpio_header
;
96 a
->format_write_data
= archive_write_cpio_data
;
97 a
->format_finish_entry
= archive_write_cpio_finish_entry
;
98 a
->format_finish
= archive_write_cpio_finish
;
99 a
->format_destroy
= archive_write_cpio_destroy
;
100 a
->archive
.archive_format
= ARCHIVE_FORMAT_CPIO_POSIX
;
101 a
->archive
.archive_format_name
= "POSIX cpio";
106 archive_write_cpio_header(struct archive_write
*a
, struct archive_entry
*entry
)
109 const char *p
, *path
;
111 struct cpio_header h
;
113 cpio
= (struct cpio
*)a
->format_data
;
116 path
= archive_entry_pathname(entry
);
117 pathlength
= strlen(path
) + 1; /* Include trailing null. */
119 memset(&h
, 0, sizeof(h
));
120 format_octal(070707, &h
.c_magic
, sizeof(h
.c_magic
));
121 format_octal(archive_entry_dev(entry
), &h
.c_dev
, sizeof(h
.c_dev
));
123 * TODO: Generate artificial inode numbers rather than just
124 * re-using the ones off the disk. That way, the 18-bit c_ino
125 * field only limits the number of files in the archive.
127 if (archive_entry_ino(entry
) > 0777777) {
128 archive_set_error(&a
->archive
, ERANGE
, "large inode number truncated");
132 format_octal(archive_entry_ino(entry
) & 0777777, &h
.c_ino
, sizeof(h
.c_ino
));
133 format_octal(archive_entry_mode(entry
), &h
.c_mode
, sizeof(h
.c_mode
));
134 format_octal(archive_entry_uid(entry
), &h
.c_uid
, sizeof(h
.c_uid
));
135 format_octal(archive_entry_gid(entry
), &h
.c_gid
, sizeof(h
.c_gid
));
136 format_octal(archive_entry_nlink(entry
), &h
.c_nlink
, sizeof(h
.c_nlink
));
137 if (archive_entry_filetype(entry
) == AE_IFBLK
138 || archive_entry_filetype(entry
) == AE_IFCHR
)
139 format_octal(archive_entry_dev(entry
), &h
.c_rdev
, sizeof(h
.c_rdev
));
141 format_octal(0, &h
.c_rdev
, sizeof(h
.c_rdev
));
142 format_octal(archive_entry_mtime(entry
), &h
.c_mtime
, sizeof(h
.c_mtime
));
143 format_octal(pathlength
, &h
.c_namesize
, sizeof(h
.c_namesize
));
145 /* Non-regular files don't store bodies. */
146 if (archive_entry_filetype(entry
) != AE_IFREG
)
147 archive_entry_set_size(entry
, 0);
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
));
154 format_octal(archive_entry_size(entry
),
155 &h
.c_filesize
, sizeof(h
.c_filesize
));
157 ret
= (a
->compressor
.write
)(a
, &h
, sizeof(h
));
158 if (ret
!= ARCHIVE_OK
)
159 return (ARCHIVE_FATAL
);
161 ret
= (a
->compressor
.write
)(a
, path
, pathlength
);
162 if (ret
!= ARCHIVE_OK
)
163 return (ARCHIVE_FATAL
);
165 cpio
->entry_bytes_remaining
= archive_entry_size(entry
);
167 /* Write the symlink now. */
168 if (p
!= NULL
&& *p
!= '\0')
169 ret
= (a
->compressor
.write
)(a
, p
, strlen(p
));
175 archive_write_cpio_data(struct archive_write
*a
, const void *buff
, size_t s
)
180 cpio
= (struct cpio
*)a
->format_data
;
181 if (s
> cpio
->entry_bytes_remaining
)
182 s
= cpio
->entry_bytes_remaining
;
184 ret
= (a
->compressor
.write
)(a
, buff
, s
);
185 cpio
->entry_bytes_remaining
-= s
;
193 * Format a number into the specified field.
196 format_octal(int64_t v
, void *p
, int digits
)
201 max
= (((int64_t)1) << (digits
* 3)) - 1;
202 if (v
>= 0 && v
<= max
) {
203 format_octal_recursive(v
, (char *)p
, digits
);
206 format_octal_recursive(max
, (char *)p
, digits
);
213 format_octal_recursive(int64_t v
, char *p
, int s
)
217 v
= format_octal_recursive(v
, p
+1, s
-1);
223 archive_write_cpio_finish(struct archive_write
*a
)
227 struct archive_entry
*trailer
;
229 cpio
= (struct cpio
*)a
->format_data
;
230 trailer
= archive_entry_new();
231 /* nlink = 1 here for GNU cpio compat. */
232 archive_entry_set_nlink(trailer
, 1);
233 archive_entry_set_pathname(trailer
, "TRAILER!!!");
234 er
= archive_write_cpio_header(a
, trailer
);
235 archive_entry_free(trailer
);
240 archive_write_cpio_destroy(struct archive_write
*a
)
244 cpio
= (struct cpio
*)a
->format_data
;
246 a
->format_data
= NULL
;
251 archive_write_cpio_finish_entry(struct archive_write
*a
)
256 cpio
= (struct cpio
*)a
->format_data
;
258 while (cpio
->entry_bytes_remaining
> 0) {
259 to_write
= cpio
->entry_bytes_remaining
< a
->null_length
?
260 cpio
->entry_bytes_remaining
: a
->null_length
;
261 ret
= (a
->compressor
.write
)(a
, a
->nulls
, to_write
);
262 if (ret
!= ARCHIVE_OK
)
264 cpio
->entry_bytes_remaining
-= to_write
;