40
40
#include "archive.h"
41
41
#include "archive_entry.h"
42
#include "archive_entry_locale.h"
42
43
#include "archive_private.h"
43
44
#include "archive_write_private.h"
45
46
static ssize_t archive_write_cpio_data(struct archive_write *,
46
47
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 *);
48
static int archive_write_cpio_close(struct archive_write *);
49
static int archive_write_cpio_free(struct archive_write *);
49
50
static int archive_write_cpio_finish_entry(struct archive_write *);
50
51
static int archive_write_cpio_header(struct archive_write *,
51
52
struct archive_entry *);
53
static int archive_write_cpio_options(struct archive_write *,
54
const char *, const char *);
52
55
static int format_octal(int64_t, void *, int);
53
56
static int64_t format_octal_recursive(int64_t, char *, int);
57
static int write_header(struct archive_write *, struct archive_entry *);
56
60
uint64_t entry_bytes_remaining;
85
102
struct archive_write *a = (struct archive_write *)_a;
86
103
struct cpio *cpio;
105
archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
106
ARCHIVE_STATE_NEW, "archive_write_set_format_cpio");
88
108
/* If someone else was already registered, unregister them. */
89
if (a->format_destroy != NULL)
90
(a->format_destroy)(a);
109
if (a->format_free != NULL)
92
cpio = (struct cpio *)malloc(sizeof(*cpio));
112
cpio = (struct cpio *)calloc(1, sizeof(*cpio));
93
113
if (cpio == NULL) {
94
114
archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
95
115
return (ARCHIVE_FATAL);
97
memset(cpio, 0, sizeof(*cpio));
98
117
a->format_data = cpio;
100
a->pad_uncompressed = 1;
101
118
a->format_name = "cpio";
119
a->format_options = archive_write_cpio_options;
102
120
a->format_write_header = archive_write_cpio_header;
103
121
a->format_write_data = archive_write_cpio_data;
104
122
a->format_finish_entry = archive_write_cpio_finish_entry;
105
a->format_finish = archive_write_cpio_finish;
106
a->format_destroy = archive_write_cpio_destroy;
123
a->format_close = archive_write_cpio_close;
124
a->format_free = archive_write_cpio_free;
107
125
a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
108
126
a->archive.archive_format_name = "POSIX cpio";
109
127
return (ARCHIVE_OK);
131
archive_write_cpio_options(struct archive_write *a, const char *key,
134
struct cpio *cpio = (struct cpio *)a->format_data;
135
int ret = ARCHIVE_FAILED;
137
if (strcmp(key, "hdrcharset") == 0) {
138
if (val == NULL || val[0] == 0)
139
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
140
"%s: hdrcharset option needs a character-set name",
143
cpio->opt_sconv = archive_string_conversion_to_charset(
144
&a->archive, val, 0);
145
if (cpio->opt_sconv != NULL)
151
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
152
"%s: unknown keyword ``%s''", a->format_name, key);
113
158
* Ino values are as long as 64 bits on some systems; cpio format
114
159
* only allows 18 bits and relies on the ino values to identify hardlinked
174
219
return (ino_new);
223
static struct archive_string_conv *
224
get_sconv(struct archive_write *a)
227
struct archive_string_conv *sconv;
229
cpio = (struct cpio *)a->format_data;
230
sconv = cpio->opt_sconv;
232
if (!cpio->init_default_conversion) {
233
cpio->sconv_default =
234
archive_string_default_conversion_for_write(
236
cpio->init_default_conversion = 1;
238
sconv = cpio->sconv_default;
178
244
archive_write_cpio_header(struct archive_write *a, struct archive_entry *entry)
249
if (archive_entry_filetype(entry) == 0) {
250
archive_set_error(&a->archive, -1, "Filetype required");
251
return (ARCHIVE_FAILED);
254
if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
255
&& errno == ENOMEM) {
256
archive_set_error(&a->archive, ENOMEM,
257
"Can't allocate memory for Pathname");
258
return (ARCHIVE_FATAL);
260
if (len == 0 || path == NULL || path[0] == '\0') {
261
archive_set_error(&a->archive, -1, "Pathname required");
262
return (ARCHIVE_FAILED);
265
if (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0) {
266
archive_set_error(&a->archive, -1, "Size required");
267
return (ARCHIVE_FAILED);
269
return write_header(a, entry);
273
write_header(struct archive_write *a, struct archive_entry *entry)
180
275
struct cpio *cpio;
181
276
const char *p, *path;
182
int pathlength, ret, ret2;
277
int pathlength, ret, ret_final;
184
struct cpio_header h;
280
struct archive_string_conv *sconv;
186
283
cpio = (struct cpio *)a->format_data;
189
path = archive_entry_pathname(entry);
190
pathlength = (int)strlen(path) + 1; /* Include trailing null. */
192
memset(&h, 0, sizeof(h));
193
format_octal(070707, &h.c_magic, sizeof(h.c_magic));
194
format_octal(archive_entry_dev(entry), &h.c_dev, sizeof(h.c_dev));
284
ret_final = ARCHIVE_OK;
285
sconv = get_sconv(a);
287
ret = archive_entry_pathname_l(entry, &path, &len, sconv);
289
if (errno == ENOMEM) {
290
archive_set_error(&a->archive, ENOMEM,
291
"Can't allocate memory for Pathname");
292
return (ARCHIVE_FATAL);
294
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
295
"Can't translate pathname '%s' to %s",
296
archive_entry_pathname(entry),
297
archive_string_conversion_charset_name(sconv));
298
ret_final = ARCHIVE_WARN;
300
/* Include trailing null. */
301
pathlength = (int)len + 1;
303
memset(h, 0, sizeof(h));
304
format_octal(070707, h + c_magic_offset, c_magic_size);
305
format_octal(archive_entry_dev(entry), h + c_dev_offset, c_dev_size);
196
307
ino = synthesize_ino_value(cpio, entry);
203
314
"Too many files for this cpio format");
204
315
return (ARCHIVE_FATAL);
206
format_octal(ino & 0777777, &h.c_ino, sizeof(h.c_ino));
317
format_octal(ino & 0777777, h + c_ino_offset, c_ino_size);
208
format_octal(archive_entry_mode(entry), &h.c_mode, sizeof(h.c_mode));
209
format_octal(archive_entry_uid(entry), &h.c_uid, sizeof(h.c_uid));
210
format_octal(archive_entry_gid(entry), &h.c_gid, sizeof(h.c_gid));
211
format_octal(archive_entry_nlink(entry), &h.c_nlink, sizeof(h.c_nlink));
319
/* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
320
format_octal(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
321
format_octal(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
322
format_octal(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
323
format_octal(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
212
324
if (archive_entry_filetype(entry) == AE_IFBLK
213
325
|| archive_entry_filetype(entry) == AE_IFCHR)
214
format_octal(archive_entry_dev(entry), &h.c_rdev, sizeof(h.c_rdev));
326
format_octal(archive_entry_dev(entry), h + c_rdev_offset, c_rdev_size);
216
format_octal(0, &h.c_rdev, sizeof(h.c_rdev));
217
format_octal(archive_entry_mtime(entry), &h.c_mtime, sizeof(h.c_mtime));
218
format_octal(pathlength, &h.c_namesize, sizeof(h.c_namesize));
328
format_octal(0, h + c_rdev_offset, c_rdev_size);
329
format_octal(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
330
format_octal(pathlength, h + c_namesize_offset, c_namesize_size);
220
332
/* Non-regular files don't store bodies. */
221
333
if (archive_entry_filetype(entry) != AE_IFREG)
222
334
archive_entry_set_size(entry, 0);
224
336
/* Symlinks get the link written as the body of the entry. */
225
p = archive_entry_symlink(entry);
226
if (p != NULL && *p != '\0')
227
format_octal(strlen(p), &h.c_filesize, sizeof(h.c_filesize));
337
ret = archive_entry_symlink_l(entry, &p, &len, sconv);
339
if (errno == ENOMEM) {
340
archive_set_error(&a->archive, ENOMEM,
341
"Can't allocate memory for Linkname");
342
return (ARCHIVE_FATAL);
344
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
345
"Can't translate linkname '%s' to %s",
346
archive_entry_symlink(entry),
347
archive_string_conversion_charset_name(sconv));
348
ret_final = ARCHIVE_WARN;
350
if (len > 0 && p != NULL && *p != '\0')
351
ret = format_octal(strlen(p), h + c_filesize_offset,
229
format_octal(archive_entry_size(entry),
230
&h.c_filesize, sizeof(h.c_filesize));
354
ret = format_octal(archive_entry_size(entry),
355
h + c_filesize_offset, c_filesize_size);
357
archive_set_error(&a->archive, ERANGE,
358
"File is too large for cpio format.");
359
return (ARCHIVE_FAILED);
232
ret = (a->compressor.write)(a, &h, sizeof(h));
362
ret = __archive_write_output(a, h, sizeof(h));
233
363
if (ret != ARCHIVE_OK)
234
364
return (ARCHIVE_FATAL);
236
ret = (a->compressor.write)(a, path, pathlength);
366
ret = __archive_write_output(a, path, pathlength);
237
367
if (ret != ARCHIVE_OK)
238
368
return (ARCHIVE_FATAL);
240
370
cpio->entry_bytes_remaining = archive_entry_size(entry);
242
372
/* Write the symlink now. */
243
if (p != NULL && *p != '\0')
244
ret = (a->compressor.write)(a, p, strlen(p));
246
if (ret == ARCHIVE_OK)
373
if (p != NULL && *p != '\0') {
374
ret = __archive_write_output(a, p, strlen(p));
375
if (ret != ARCHIVE_OK)
376
return (ARCHIVE_FATAL);
300
archive_write_cpio_finish(struct archive_write *a)
430
archive_write_cpio_close(struct archive_write *a)
303
433
struct archive_entry *trailer;
305
trailer = archive_entry_new();
435
trailer = archive_entry_new2(NULL);
306
436
/* nlink = 1 here for GNU cpio compat. */
307
437
archive_entry_set_nlink(trailer, 1);
438
archive_entry_set_size(trailer, 0);
308
439
archive_entry_set_pathname(trailer, "TRAILER!!!");
309
er = archive_write_cpio_header(a, trailer);
440
er = write_header(a, trailer);
310
441
archive_entry_free(trailer);
315
archive_write_cpio_destroy(struct archive_write *a)
446
archive_write_cpio_free(struct archive_write *a)
317
448
struct cpio *cpio;