cpio/
newc.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//! Read/write `newc` (SVR4) format archives.

use std::io::{self, Read, Seek, SeekFrom, Write};

const HEADER_LEN: usize = 110; // 6 byte magic number + 104 bytes of metadata

const MAGIC_NUMBER_NEWASCII: &[u8] = b"070701";
const MAGIC_NUMBER_NEWCRC: &[u8] = b"070702";

const TRAILER_NAME: &str = "TRAILER!!!";

/// Whether this header is of the "new ascii" form (without checksum) or the "crc" form which
/// is structurally identical but includes a checksum, depending on the magic number present.
#[derive(Clone)]
enum EntryType {
    Crc,
    Newc,
}

/// Metadata about one entry from an archive.
#[derive(Clone)]
pub struct Entry {
    entry_type: EntryType,
    name: String,
    ino: u32,
    mode: u32,
    uid: u32,
    gid: u32,
    nlink: u32,
    mtime: u32,
    file_size: u32,
    dev_major: u32,
    dev_minor: u32,
    rdev_major: u32,
    rdev_minor: u32,
    checksum: u32,
}

/// Reads one entry header/data from an archive.
pub struct Reader<R: Read> {
    inner: R,
    entry: Entry,
    bytes_read: u32,
}

/// Builds metadata for one entry to be written into an archive.
#[derive(Clone)]
pub struct Builder {
    name: String,
    ino: u32,
    mode: u32,
    uid: u32,
    gid: u32,
    nlink: u32,
    mtime: u32,
    dev_major: u32,
    dev_minor: u32,
    rdev_major: u32,
    rdev_minor: u32,
}

/// Writes one entry header/data into an archive.
pub struct Writer<W: Write> {
    inner: W,
    written: u32,
    file_size: u32,
    header_size: usize,
    header: Vec<u8>,
}

fn pad(len: usize) -> Option<Vec<u8>> {
    // pad out to a multiple of 4 bytes
    let overhang = len % 4;
    if overhang != 0 {
        let repeat = 4 - overhang;
        Some(vec![0u8; repeat])
    } else {
        None
    }
}

pub enum ModeFileType {
    Symlink,
    Fifo,
    Char,
    Block,
    NetworkSpecial,
    Socket,
    Directory,
    Regular,
}

impl ModeFileType {
    const MASK: u32 = 0o170000;
}

impl From<ModeFileType> for u32 {
    fn from(m: ModeFileType) -> u32 {
        match m {
            ModeFileType::Fifo => 0o010000,
            ModeFileType::Char => 0o020000,
            ModeFileType::Directory => 0o040000,
            ModeFileType::Block => 0o060000,
            ModeFileType::Regular => 0o100000,
            ModeFileType::NetworkSpecial => 0o110000,
            ModeFileType::Symlink => 0o120000,
            ModeFileType::Socket => 0o140000,
        }
    }
}

fn read_hex_u32<R: Read>(reader: &mut R) -> io::Result<u32> {
    let mut bytes = [0u8; 8];
    reader.read_exact(&mut bytes)?;
    ::std::str::from_utf8(&bytes)
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Invalid utf-8 header field"))
        .and_then(|string| {
            u32::from_str_radix(string, 16).map_err(|_| {
                io::Error::new(io::ErrorKind::InvalidData, "Invalid hex u32 header field")
            })
        })
}

impl Entry {
    /// Returns the name of the file.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the inode number of the file. Sometimes this is just an index.
    pub fn ino(&self) -> u32 {
        self.ino
    }

    /// Returns the file's "mode" - the same as an inode "mode" field - containing permission bits
    /// and a bit of metadata about the type of file represented.
    pub fn mode(&self) -> u32 {
        self.mode
    }

    /// Returns the UID for this file's owner.
    pub fn uid(&self) -> u32 {
        self.uid
    }

    /// Returns the GID for this file's group.
    pub fn gid(&self) -> u32 {
        self.gid
    }

    /// Returns the number of links associated with this file.
    pub fn nlink(&self) -> u32 {
        self.nlink
    }

    /// Returns the modification time of this file.
    pub fn mtime(&self) -> u32 {
        self.mtime
    }

    /// Returns the size of this file, in bytes.
    pub fn file_size(&self) -> u32 {
        self.file_size
    }

    /// Returns the major component of the device ID, describing the device on which this file
    /// resides.
    ///
    /// Device IDs are comprised of a major and minor component. The major component identifies
    /// the class of device, while the minor component identifies a specific device of that class.
    pub fn dev_major(&self) -> u32 {
        self.dev_major
    }

    /// Returns the minor component of the device ID, describing the device on which this file
    /// resides.
    ///
    /// Device IDs are comprised of a major and minor component. The major component identifies
    /// the class of device, while the minor component identifies a specific device of that class.
    pub fn dev_minor(&self) -> u32 {
        self.dev_minor
    }

    /// Returns the major component of the rdev ID, describes the device that this file
    /// (inode) represents.
    ///
    /// Device IDs are comprised of a major and minor component. The major component identifies
    /// the class of device, while the minor component identifies a specific device of that class.
    pub fn rdev_major(&self) -> u32 {
        self.rdev_major
    }

    /// Returns the minor component of the rdev ID, field describes the device that this file
    /// (inode) represents.
    ///
    /// Device IDs are comprised of a major and minor component. The major component identifies
    /// the class of device, while the minor component identifies a specific device of that class.
    pub fn rdev_minor(&self) -> u32 {
        self.rdev_minor
    }

    /// Returns true if this is a trailer entry.
    pub fn is_trailer(&self) -> bool {
        self.name == TRAILER_NAME
    }

    /// Return the checksum of this entry.
    ///
    /// The checksum is calculated by summing the bytes in the file and taking the least
    /// significant 32 bits. Not all CPIO archives use checksums.
    pub fn checksum(&self) -> Option<u32> {
        match self.entry_type {
            EntryType::Crc => Some(self.checksum),
            EntryType::Newc => None,
        }
    }
}

impl<R: Read> Reader<R> {
    /// Parses metadata for the next entry in an archive, and returns a reader
    /// that will yield the entry data.
    pub fn new(mut inner: R) -> io::Result<Reader<R>> {
        // char    c_magic[6];
        let mut magic = [0u8; 6];
        inner.read_exact(&mut magic)?;
        let entry_type = match magic.as_slice() {
            MAGIC_NUMBER_NEWASCII => EntryType::Newc,
            MAGIC_NUMBER_NEWCRC => EntryType::Crc,
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "Invalid magic number",
                ))
            }
        };

        // char    c_ino[8];
        let ino = read_hex_u32(&mut inner)?;
        // char    c_mode[8];
        let mode = read_hex_u32(&mut inner)?;
        // char    c_uid[8];
        let uid = read_hex_u32(&mut inner)?;
        // char    c_gid[8];
        let gid = read_hex_u32(&mut inner)?;
        // char    c_nlink[8];
        let nlink = read_hex_u32(&mut inner)?;
        // char    c_mtime[8];
        let mtime = read_hex_u32(&mut inner)?;
        // char    c_filesize[8];
        let file_size = read_hex_u32(&mut inner)?;
        // char    c_devmajor[8];
        let dev_major = read_hex_u32(&mut inner)?;
        // char    c_devminor[8];
        let dev_minor = read_hex_u32(&mut inner)?;
        // char    c_rdevmajor[8];
        let rdev_major = read_hex_u32(&mut inner)?;
        // char    c_rdevminor[8];
        let rdev_minor = read_hex_u32(&mut inner)?;
        // char    c_namesize[8];
        let name_len = read_hex_u32(&mut inner)? as usize;
        // char    c_checksum[8];
        let checksum = read_hex_u32(&mut inner)?;

        // NUL-terminated name with length `name_len` (including NUL byte).
        let mut name_bytes = vec![0u8; name_len];
        inner.read_exact(&mut name_bytes)?;
        if name_bytes.last() != Some(&0) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Entry name was not NUL-terminated",
            ));
        }
        name_bytes.pop();
        // dracut-cpio sometimes pads the name to the next filesystem block.
        // See https://github.com/dracutdevs/dracut/commit/a9c67046
        while name_bytes.last() == Some(&0) {
            name_bytes.pop();
        }
        let name = String::from_utf8(name_bytes).map_err(|_| {
            io::Error::new(io::ErrorKind::InvalidData, "Entry name was not valid UTF-8")
        })?;

        // Pad out to a multiple of 4 bytes.
        if let Some(mut padding) = pad(HEADER_LEN + name_len) {
            inner.read_exact(&mut padding)?;
        }

        let entry = Entry {
            entry_type,
            name,
            ino,
            mode,
            uid,
            gid,
            nlink,
            mtime,
            file_size,
            dev_major,
            dev_minor,
            rdev_major,
            rdev_minor,
            checksum,
        };
        Ok(Reader {
            inner,
            entry,
            bytes_read: 0,
        })
    }

    /// Returns the metadata for this entry.
    pub fn entry(&self) -> &Entry {
        &self.entry
    }

    /// Finishes reading this entry and returns the underlying reader in a
    /// position ready to read the next entry (if any).
    pub fn finish(mut self) -> io::Result<R> {
        let remaining = self.entry.file_size - self.bytes_read;
        if remaining > 0 {
            io::copy(
                &mut self.inner.by_ref().take(remaining as u64),
                &mut io::sink(),
            )?;
        }
        if let Some(mut padding) = pad(self.entry.file_size as usize) {
            self.inner.read_exact(&mut padding)?;
        }
        Ok(self.inner)
    }

    /// Write the contents of the entry out to the writer using `io::copy`, taking advantage of any
    /// platform-specific behavior to effeciently copy data that `io::copy` can use. If any of the
    /// file data has already been read through the `Read` interface, this will copy the
    /// _remaining_ data in the entry.
    pub fn to_writer<W: Write>(mut self, mut writer: W) -> io::Result<R> {
        let remaining = self.entry.file_size - self.bytes_read;
        if remaining > 0 {
            io::copy(&mut self.inner.by_ref().take(remaining as u64), &mut writer)?;
        }
        if let Some(mut padding) = pad(self.entry.file_size as usize) {
            self.inner.read_exact(&mut padding)?;
        }
        Ok(self.inner)
    }
}

impl<R: Read + Seek> Reader<R> {
    /// Returns the offset within inner, which can be useful for efficient
    /// io::copy()/copy_file_range() of file data.
    pub fn offset(&mut self) -> io::Result<u64> {
        self.inner.stream_position()
    }

    /// Skip past all remaining file data in this entry, returning the
    /// underlying reader in a position ready to read the next entry (if any).
    pub fn skip(mut self) -> io::Result<R> {
        let mut remaining: i64 = (self.entry.file_size - self.bytes_read).into();
        match pad(self.entry.file_size as usize) {
            Some(p) => remaining += p.len() as i64,
            None {} => {}
        };
        if remaining > 0 {
            self.inner.seek(SeekFrom::Current(remaining))?;
        }
        Ok(self.inner)
    }
}

impl<R: Read> Read for Reader<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let remaining = self.entry.file_size - self.bytes_read;
        let limit = buf.len().min(remaining as usize);
        if limit > 0 {
            let num_bytes = self.inner.read(&mut buf[..limit])?;
            self.bytes_read += num_bytes as u32;
            Ok(num_bytes)
        } else {
            Ok(0)
        }
    }
}

impl Builder {
    /// Create the metadata for one CPIO entry
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            ino: 0,
            mode: 0,
            uid: 0,
            gid: 0,
            nlink: 1,
            mtime: 0,
            dev_major: 0,
            dev_minor: 0,
            rdev_major: 0,
            rdev_minor: 0,
        }
    }

    /// Set the inode number for this file. In modern times however, typically this is just a
    /// a unique index ID for the file, rather than the actual inode number.
    pub fn ino(mut self, ino: u32) -> Self {
        self.ino = ino;
        self
    }

    /// Set the file's "mode" - the same as an inode "mode" field - containing permission bits
    /// and a bit of metadata about the type of file represented.
    pub fn mode(mut self, mode: u32) -> Self {
        self.mode = mode;
        self
    }

    /// Set this file's UID.
    pub fn uid(mut self, uid: u32) -> Self {
        self.uid = uid;
        self
    }

    /// Set this file's GID.
    pub fn gid(mut self, gid: u32) -> Self {
        self.gid = gid;
        self
    }

    /// Set the number of links associated with this file.
    pub fn nlink(mut self, nlink: u32) -> Self {
        self.nlink = nlink;
        self
    }

    /// Set the modification time of this file.
    pub fn mtime(mut self, mtime: u32) -> Self {
        self.mtime = mtime;
        self
    }

    /// Set the major component of the device ID, describing the device on which this file
    /// resides.
    ///
    /// Device IDs are comprised of a major and minor component. The major component identifies
    /// the class of device, while the minor component identifies a specific device of that class.
    pub fn dev_major(mut self, dev_major: u32) -> Self {
        self.dev_major = dev_major;
        self
    }

    /// Set the minor component of the device ID, describing the device on which this file
    /// resides.
    ///
    /// Device IDs are comprised of a major and minor component. The major component identifies
    /// the class of device, while the minor component identifies a specific device of that class.
    pub fn dev_minor(mut self, dev_minor: u32) -> Self {
        self.dev_minor = dev_minor;
        self
    }

    /// Set the major component of the rdev ID, describes the device that this file
    /// (inode) represents.
    ///
    /// Device IDs are comprised of a major and minor component. The major component identifies
    /// the class of device, while the minor component identifies a specific device of that class.
    pub fn rdev_major(mut self, rdev_major: u32) -> Self {
        self.rdev_major = rdev_major;
        self
    }

    /// Set the minor component of the rdev ID, field describes the device that this file
    /// (inode) represents.
    ///
    /// Device IDs are comprised of a major and minor component. The major component identifies
    /// the class of device, while the minor component identifies a specific device of that class.
    pub fn rdev_minor(mut self, rdev_minor: u32) -> Self {
        self.rdev_minor = rdev_minor;
        self
    }

    /// Set the mode file type of the entry
    pub fn set_mode_file_type(mut self, file_type: ModeFileType) -> Self {
        self.mode &= !ModeFileType::MASK;
        self.mode |= u32::from(file_type);
        self
    }

    /// Write out an entry to the provided writer in SVR4 "new ascii" CPIO format.
    pub fn write<W: Write>(self, w: W, file_size: u32) -> Writer<W> {
        let header = self.into_header(file_size, None);

        Writer {
            inner: w,
            written: 0,
            file_size,
            header_size: header.len(),
            header,
        }
    }

    /// Write out an entry to the provided writer in SVR4 "new crc" CPIO format.
    pub fn write_crc<W: Write>(self, w: W, file_size: u32, file_checksum: u32) -> Writer<W> {
        let header = self.into_header(file_size, Some(file_checksum));

        Writer {
            inner: w,
            written: 0,
            file_size,
            header_size: header.len(),
            header,
        }
    }

    /// Build a newc header from the entry metadata.
    fn into_header(self, file_size: u32, file_checksum: Option<u32>) -> Vec<u8> {
        let mut header = Vec::with_capacity(HEADER_LEN);

        // char    c_magic[6];
        if file_checksum.is_some() {
            header.extend(MAGIC_NUMBER_NEWCRC);
        } else {
            header.extend(MAGIC_NUMBER_NEWASCII);
        }
        // char    c_ino[8];
        header.extend(format!("{:08x}", self.ino).as_bytes());
        // char    c_mode[8];
        header.extend(format!("{:08x}", self.mode).as_bytes());
        // char    c_uid[8];
        header.extend(format!("{:08x}", self.uid).as_bytes());
        // char    c_gid[8];
        header.extend(format!("{:08x}", self.gid).as_bytes());
        // char    c_nlink[8];
        header.extend(format!("{:08x}", self.nlink).as_bytes());
        // char    c_mtime[8];
        header.extend(format!("{:08x}", self.mtime).as_bytes());
        // char    c_filesize[8];
        header.extend(format!("{:08x}", file_size).as_bytes());
        // char    c_devmajor[8];
        header.extend(format!("{:08x}", self.dev_major).as_bytes());
        // char    c_devminor[8];
        header.extend(format!("{:08x}", self.dev_minor).as_bytes());
        // char    c_rdevmajor[8];
        header.extend(format!("{:08x}", self.rdev_major).as_bytes());
        // char    c_rdevminor[8];
        header.extend(format!("{:08x}", self.rdev_minor).as_bytes());
        // char    c_namesize[8];
        let name_len = self.name.len() + 1;
        header.extend(format!("{:08x}", name_len).as_bytes());
        // char    c_check[8];
        header.extend(format!("{:08x}", file_checksum.unwrap_or(0)).as_bytes());

        // append the name to the end of the header
        header.extend(self.name.as_bytes());
        header.push(0u8);

        // pad out to a multiple of 4 bytes
        if let Some(pad) = pad(HEADER_LEN + name_len) {
            header.extend(pad);
        }

        header
    }
}

impl<W: Write> Writer<W> {
    pub fn finish(mut self) -> io::Result<W> {
        self.do_finish()?;
        Ok(self.inner)
    }

    fn try_write_header(&mut self) -> io::Result<()> {
        if !self.header.is_empty() {
            self.inner.write_all(&self.header)?;
            self.header.truncate(0);
        }
        Ok(())
    }

    fn do_finish(&mut self) -> io::Result<()> {
        self.try_write_header()?;

        if self.written == self.file_size {
            if let Some(pad) = pad(self.header_size + self.file_size as usize) {
                self.inner.write_all(&pad)?;
                self.inner.flush()?;
            }
        }

        Ok(())
    }
}

impl<W: Write> Write for Writer<W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        if self.written + buf.len() as u32 <= self.file_size {
            self.try_write_header()?;

            let n = self.inner.write(buf)?;
            self.written += n as u32;
            Ok(n)
        } else {
            Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "trying to write more than the specified file size",
            ))
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

/// Writes a trailer entry into an archive.
pub fn trailer<W: Write>(w: W) -> io::Result<W> {
    let b = Builder::new(TRAILER_NAME).nlink(1);
    let writer = b.write(w, 0);
    writer.finish()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{copy, Cursor};

    #[test]
    fn test_single_file() {
        // Set up our input file
        let data: &[u8] = b"Hello, World";
        let length = data.len() as u32;
        let mut input = Cursor::new(data);

        // Set up our output file
        let output = vec![];

        // Set up the descriptor of our input file
        let b = Builder::new("./hello_world");
        // and get a writer for that input file
        let mut writer = b.write(output, length);

        // Copy the input file into our CPIO archive
        copy(&mut input, &mut writer).unwrap();
        let output = writer.finish().unwrap();

        // Finish up by writing the trailer for the archive
        let output = trailer(output).unwrap();

        // Now read the archive back in and make sure we get the same data.
        let mut reader = Reader::new(output.as_slice()).unwrap();
        assert_eq!(reader.entry.name(), "./hello_world");
        assert_eq!(reader.entry.file_size(), length);
        let mut contents = vec![];
        copy(&mut reader, &mut contents).unwrap();
        assert_eq!(contents, data);
        let reader = Reader::new(reader.finish().unwrap()).unwrap();
        assert!(reader.entry().is_trailer());
    }

    #[test]
    fn test_multi_file() {
        // Set up our input files
        let data1: &[u8] = b"Hello, World";
        let length1 = data1.len() as u32;
        let mut input1 = Cursor::new(data1);

        let data2: &[u8] = b"Hello, World 2";
        let length2 = data2.len() as u32;
        let mut input2 = Cursor::new(data2);

        // Set up our output file
        let output = vec![];

        // Set up the descriptor of our input file
        let b = Builder::new("./hello_world")
            .ino(1)
            .uid(1000)
            .gid(1000)
            .mode(0o100644);
        // and get a writer for that input file
        let mut writer = b.write(output, length1);

        // Copy the input file into our CPIO archive
        copy(&mut input1, &mut writer).unwrap();
        let output = writer.finish().unwrap();

        // Set up the descriptor of our second input file
        let b = Builder::new("./hello_world2")
            .ino(2)
            .uid(1000)
            .gid(1000)
            .mode(0o100644);
        // and get a writer for that input file
        let mut writer = b.write(output, length2);

        // Copy the second input file into our CPIO archive
        copy(&mut input2, &mut writer).unwrap();
        let output = writer.finish().unwrap();

        // Finish up by writing the trailer for the archive
        let output = trailer(output).unwrap();

        // Now read the archive back in and make sure we get the same data.
        let mut reader = Reader::new(output.as_slice()).unwrap();
        assert_eq!(reader.entry().name(), "./hello_world");
        assert_eq!(reader.entry().file_size(), length1);
        assert_eq!(reader.entry().ino(), 1);
        assert_eq!(reader.entry().uid(), 1000);
        assert_eq!(reader.entry().gid(), 1000);
        assert_eq!(reader.entry().mode(), 0o100644);
        let mut contents = vec![];
        copy(&mut reader, &mut contents).unwrap();
        assert_eq!(contents, data1);

        let mut reader = Reader::new(reader.finish().unwrap()).unwrap();
        assert_eq!(reader.entry().name(), "./hello_world2");
        assert_eq!(reader.entry().file_size(), length2);
        assert_eq!(reader.entry().ino(), 2);
        let mut contents = vec![];
        copy(&mut reader, &mut contents).unwrap();
        assert_eq!(contents, data2);

        let reader = Reader::new(reader.finish().unwrap()).unwrap();
        assert!(reader.entry().is_trailer());
    }

    #[test]
    fn test_multi_file_to_writer() {
        // Set up our input files
        let data1: &[u8] = b"Hello, World";
        let length1 = data1.len() as u32;
        let mut input1 = Cursor::new(data1);

        let data2: &[u8] = b"Hello, World 2";
        let length2 = data2.len() as u32;
        let mut input2 = Cursor::new(data2);

        // Set up our output file
        let output = vec![];

        // Set up the descriptor of our input file
        let b = Builder::new("./hello_world")
            .ino(1)
            .uid(1000)
            .gid(1000)
            .mode(0o100644);
        // and get a writer for that input file
        let mut writer = b.write(output, length1);

        // Copy the input file into our CPIO archive
        copy(&mut input1, &mut writer).unwrap();
        let output = writer.finish().unwrap();

        // Set up the descriptor of our second input file
        let b = Builder::new("./hello_world2")
            .ino(2)
            .uid(1000)
            .gid(1000)
            .mode(0o100644);
        // and get a writer for that input file
        let mut writer = b.write(output, length2);

        // Copy the second input file into our CPIO archive
        copy(&mut input2, &mut writer).unwrap();
        let output = writer.finish().unwrap();

        // Finish up by writing the trailer for the archive
        let output = trailer(output).unwrap();

        // Now read the archive back in and make sure we get the same data.
        let reader = Reader::new(output.as_slice()).unwrap();
        assert_eq!(reader.entry().name(), "./hello_world");
        assert_eq!(reader.entry().file_size(), length1);
        assert_eq!(reader.entry().ino(), 1);
        assert_eq!(reader.entry().uid(), 1000);
        assert_eq!(reader.entry().gid(), 1000);
        assert_eq!(reader.entry().mode(), 0o100644);
        let mut contents = vec![];
        let handle = reader.to_writer(&mut contents).unwrap();
        assert_eq!(contents, data1);

        let reader = Reader::new(handle).unwrap();
        assert_eq!(reader.entry().name(), "./hello_world2");
        assert_eq!(reader.entry().file_size(), length2);
        assert_eq!(reader.entry().ino(), 2);
        let mut contents = vec![];
        let handle = reader.to_writer(&mut contents).unwrap();
        assert_eq!(contents, data2);

        let reader = Reader::new(handle).unwrap();
        assert!(reader.entry().is_trailer());
    }
}