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
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
//! A library for reading and writing TAR archives
//!
//! This library provides utilities necessary to manage TAR archives [1]
//! abstracted over a reader or writer. Great strides are taken to ensure that
//! an archive is never required to be fully resident in memory, all objects
//! provide largely a streaming interface to read bytes from.
//!
//! [1]: http://en.wikipedia.org/wiki/Tar_%28computing%29

#![doc(html_root_url = "http://alexcrichton.com/tar-rs")]
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]

extern crate libc;
extern crate winapi;
extern crate filetime;

use std::borrow::Cow;
use std::cell::{RefCell, Cell};
use std::cmp;
use std::error;
use std::fmt;
use std::fs;
use std::io::prelude::*;
use std::io::{self, Error, ErrorKind, SeekFrom};
use std::iter::repeat;
use std::mem;
use std::path::{Path, PathBuf, Component};
use std::str;

use filetime::FileTime;

#[cfg(unix)] use std::os::unix::prelude::*;
#[cfg(unix)] use std::ffi::{OsStr, OsString};
#[cfg(windows)] use std::os::windows::prelude::*;

macro_rules! try_iter{ ($me:expr, $e:expr) => (
    match $e {
        Ok(e) => e,
        Err(e) => { $me.done = true; return Some(Err(e)) }
    }
) }

/// A top-level representation of an archive file.
///
/// This archive can have a file added to it and it can be iterated over.
pub struct Archive<R> {
    obj: RefCell<R>,
    pos: Cell<u64>,
}

/// An iterator over the files of an archive.
///
/// Requires that `R` implement `Seek`.
pub struct Files<'a, R:'a> {
    archive: &'a Archive<R>,
    done: bool,
    offset: u64,
}

/// An iterator over the files of an archive.
///
/// Does not require that `R` implements `Seek`, but each file must be processed
/// before the next.
pub struct FilesMut<'a, R:'a> {
    archive: &'a Archive<R>,
    next: u64,
    done: bool,
}

/// A read-only view into a file of an archive.
///
/// This structure is a window into a portion of a borrowed archive which can
/// be inspected. It acts as a file handle by implementing the Reader and Seek
/// traits. A file cannot be rewritten once inserted into an archive.
pub struct File<'a, R: 'a> {
    header: Header,
    archive: &'a Archive<R>,
    pos: u64,
    size: u64,

    // Used in read() to make sure we're positioned at the next byte. For a
    // `Files` iterator these are meaningful while for a `FilesMut` iterator
    // these are both unused/noops.
    seek: fn(&File<R>) -> io::Result<()>,
    tar_offset: u64,
}

/// Representation of the header of a file in an archive
#[repr(C)]
#[allow(missing_docs)]
pub struct Header {
    pub name: [u8; 100],
    pub mode: [u8; 8],
    pub owner_id: [u8; 8],
    pub group_id: [u8; 8],
    pub size: [u8; 12],
    pub mtime: [u8; 12],
    pub cksum: [u8; 8],
    pub link: [u8; 1],
    pub linkname: [u8; 100],

    // UStar format
    pub ustar: [u8; 6],
    pub ustar_version: [u8; 2],
    pub owner_name: [u8; 32],
    pub group_name: [u8; 32],
    pub dev_major: [u8; 8],
    pub dev_minor: [u8; 8],
    pub prefix: [u8; 155],
    _rest: [u8; 12],
}

#[doc(hidden)]
#[derive(Debug)]
pub struct TarError {
    desc: String,
    io: io::Error,
}

impl<O> Archive<O> {
    /// Create a new archive with the underlying object as the reader/writer.
    ///
    /// Different methods are available on an archive depending on the traits
    /// that the underlying object implements.
    pub fn new(obj: O) -> Archive<O> {
        Archive { obj: RefCell::new(obj), pos: Cell::new(0) }
    }

    /// Unwrap this archive, returning the underlying object.
    pub fn into_inner(self) -> O {
        self.obj.into_inner()
    }
}

impl<R: Seek + Read> Archive<R> {
    /// Construct an iterator over the files of this archive.
    ///
    /// This function can return an error if any underlying I/O operation fails
    /// while attempting to construct the iterator.
    ///
    /// Additionally, the iterator yields `io::Result<File>` instead of `File` to
    /// handle invalid tar archives as well as any intermittent I/O error that
    /// occurs.
    pub fn files(&self) -> io::Result<Files<R>> {
        try!(self.seek(0));
        Ok(Files { archive: self, done: false, offset: 0 })
    }

    fn seek(&self, pos: u64) -> io::Result<()> {
        if self.pos.get() == pos { return Ok(()) }
        try!(self.obj.borrow_mut().seek(SeekFrom::Start(pos)));
        self.pos.set(pos);
        Ok(())
    }
}

impl<R: Read> Archive<R> {
    /// Construct an iterator over the files in this archive.
    ///
    /// While similar to the `files` iterator, this iterator does not require
    /// that `R` implement `Seek` and restricts the iterator to processing only
    /// one file at a time in a streaming fashion.
    ///
    /// Note that care must be taken to consider each file within an archive in
    /// sequence. If files are processed out of sequence (from what the iterator
    /// returns), then the contents read for each file may be corrupted.
    pub fn files_mut(&mut self) -> io::Result<FilesMut<R>> {
        if self.pos.get() != 0 {
            return Err(Error::new(ErrorKind::Other, "cannot call files_mut \
                                                     unless archive is at \
                                                     position 0"))
        }
        Ok(FilesMut { archive: self, done: false, next: 0 })
    }

    /// Unpacks the contents tarball into the specified `dst`.
    ///
    /// This function will iterate over the entire contents of this tarball,
    /// extracting each file in turn to the location specified by the entry's
    /// path name.
    ///
    /// This operation is relatively sensitive in that it will not write files
    /// outside of the path specified by `into`. Files in the archive which have
    /// a '..' in their path are skipped during the unpacking process.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::fs::File;
    /// use tar::Archive;
    ///
    /// let mut ar = Archive::new(File::open("foo.tar").unwrap());
    /// ar.unpack("foo").unwrap();
    /// ```
    pub fn unpack<P: AsRef<Path>>(&mut self, dst: P) -> io::Result<()> {
        self.unpack2(dst.as_ref())
    }

    fn unpack2(&mut self, dst: &Path) -> io::Result<()> {
        'outer: for file in try!(self.files_mut()) {
            let mut file = try!(file.map_err(|e| {
                TarError::new("failed to iterate over archive", e)
            }));

            // Notes regarding bsdtar 2.8.3 / libarchive 2.8.3:
            // * Leading '/'s are trimmed. For example, `///test` is treated as
            //   `test`.
            // * If the filename contains '..', then the file is skipped when
            //   extracting the tarball.
            // * '//' within a filename is effectively skipped. An error is
            //   logged, but otherwise the effect is as if any two or more
            //   adjacent '/'s within the filename were consolidated into one
            //   '/'.
            //
            // Most of this is handled by the `path` module of the standard
            // library, but we specially handle a few cases here as well.

            let mut file_dst = dst.to_path_buf();
            {
                let path = try!(file.header().path().map_err(|e| {
                    TarError::new("invalid path in entry header", e)
                }));
                for part in path.components() {
                    match part {
                        // Leading '/' characters, root paths, and '.'
                        // components are just ignored and treated as "empty
                        // components"
                        Component::Prefix(..) |
                        Component::RootDir |
                        Component::CurDir => continue,

                        // If any part of the filename is '..', then skip over
                        // unpacking the file to prevent directory traversal
                        // security issues.  See, e.g.: CVE-2001-1267,
                        // CVE-2002-0399, CVE-2005-1918, CVE-2007-4131
                        Component::ParentDir => continue 'outer,

                        Component::Normal(part) => file_dst.push(part),
                    }
                }
            }

            // Skip cases where only slashes or '.' parts were seen, because
            // this is effectively an empty filename.
            if *dst == *file_dst {
                continue
            }

            if file.header().link[0] == b'5' {
                try!(fs::create_dir_all(&file_dst).map_err(|e| {
                    TarError::new(&format!("failed to create `{}`",
                                           file_dst.display()), e)
                }));
            } else {
                let dir = file_dst.parent().unwrap();
                try!(fs::create_dir_all(&dir).map_err(|e| {
                    TarError::new(&format!("failed to create `{}`",
                                           dir.display()), e)
                }));
                try!(file.unpack(&file_dst));
            }
        }
        Ok(())
    }

    fn skip(&self, mut amt: u64) -> io::Result<()> {
        let mut buf = [0u8; 4096 * 8];
        let mut me = self;
        while amt > 0 {
            let n = cmp::min(amt, buf.len() as u64);
            let n = try!(Read::read(&mut me, &mut buf[..n as usize]));
            if n == 0 {
                let errstr = "unexpected EOF during skip";
                return Err(Error::new(ErrorKind::Other, errstr));
            }
            amt -= n as u64;
        }
        Ok(())
    }

    // Assumes that the underlying reader is positioned at the start of a valid
    // header to parse.
    fn next_file(&self, offset: &mut u64, seek: fn(&File<R>) -> io::Result<()>)
                 -> io::Result<Option<File<R>>> {
        // If we have 2 or more sections of 0s, then we're done!
        let mut chunk = [0; 512];
        let mut me = self;
        try!(read_all(&mut me, &mut chunk));
        *offset += 512;
        // A block of 0s is never valid as a header (because of the checksum),
        // so if it's all zero it must be the first of the two end blocks
        if chunk.iter().all(|i| *i == 0) {
            try!(read_all(&mut me, &mut chunk));
            *offset += 512;
            return if chunk.iter().all(|i| *i == 0) {
                Ok(None)
            } else {
                Err(bad_archive())
            }
        }

        let sum = chunk[..148].iter().map(|i| *i as u32).fold(0, |a, b| a + b) +
                  chunk[156..].iter().map(|i| *i as u32).fold(0, |a, b| a + b) +
                  32 * 8;

        let header: Header = unsafe { mem::transmute(chunk) };
        let ret = File {
            archive: self,
            pos: 0,
            size: try!(header.size()),
            header: header,
            tar_offset: *offset,
            seek: seek,
        };

        // Make sure the checksum is ok
        let cksum = try!(ret.header.cksum());
        if sum != cksum { return Err(bad_archive()) }

        // Figure out where the next file is
        let size = (ret.size + 511) & !(512 - 1);
        *offset += size;

        return Ok(Some(ret));
    }
}

impl<W: Write> Archive<W> {
    /// Adds a new entry to this archive.
    ///
    /// This function will append the header specified, followed by contents of
    /// the stream specified by `data`. To produce a valid archive the `size`
    /// field of `header` must be the same as the length of the stream that's
    /// being written. Additionally the checksum for the header should have been
    /// set via the `set_cksum` method.
    ///
    /// Note that this will not attempt to seek the archive to a valid position,
    /// so if the archive is in the middle of a read or some other similar
    /// operation then this may corrupt the archive.
    ///
    /// Also note that after all files have been written to an archive the
    /// `finish` function needs to be called to finish writing the archive.
    ///
    /// # Errors
    ///
    /// This function will return an error for any intermittent I/O error which
    /// occurs when either reading or writing.
    ///
    /// # Examples
    ///
    /// ```
    /// use tar::{Archive, Header};
    ///
    /// let mut header = Header::new();
    /// header.set_path("foo");
    /// header.set_size(4);
    /// header.set_cksum();
    ///
    /// let mut data: &[u8] = &[1, 2, 3, 4];
    ///
    /// let mut ar = Archive::new(Vec::new());
    /// ar.append(&header, &mut data).unwrap();
    /// let archive = ar.into_inner();
    /// ```
    pub fn append(&self, header: &Header, mut data: &mut Read) -> io::Result<()> {
        let mut obj = self.obj.borrow_mut();
        try!(obj.write_all(header.as_bytes()));
        let len = try!(io::copy(&mut data, &mut *obj));

        // Pad with zeros if necessary.
        let buf = [0; 512];
        let remaining = 512 - (len % 512);
        if remaining < 512 {
            try!(obj.write_all(&buf[..remaining as usize]));
        }

        Ok(())
    }

    /// Adds a file on the local filesystem to this archive.
    ///
    /// This function will open the file specified by `path` and insert the file
    /// into the archive with the appropriate metadata set, returning any I/O
    /// error which occurs while writing. The path name for the file inside of
    /// this archive will be the same as `path`, and it is recommended that the
    /// path is a relative path.
    ///
    /// Note that this will not attempt to seek the archive to a valid position,
    /// so if the archive is in the middle of a read or some other similar
    /// operation then this may corrupt the archive.
    ///
    /// Also note that after all files have been written to an archive the
    /// `finish` function needs to be called to finish writing the archive.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use tar::Archive;
    ///
    /// let mut ar = Archive::new(Vec::new());
    ///
    /// ar.append_path("foo/bar.txt").unwrap();
    /// ```
    pub fn append_path<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
        self.append_path2(path.as_ref())
    }

    fn append_path2(&self, path: &Path) -> io::Result<()> {
        let stat = try!(fs::metadata(path));
        let header = try!(Header::from_path_and_metadata(path, &stat));
        if stat.is_file() {
            let mut file = try!(fs::File::open(path));
            self.append(&header, &mut file)
        } else if stat.is_dir() {
            self.append(&header, &mut io::empty())
        } else {
            Err(Error::new(ErrorKind::Other, "path has unknown file type"))
        }
    }

    /// Adds a file to this archive with the given path as the name of the file
    /// in the archive.
    ///
    /// This will use the metadata of `file` to populate a `Header`, and it will
    /// then append the file to the archive with the name `path`.
    ///
    /// Note that this will not attempt to seek the archive to a valid position,
    /// so if the archive is in the middle of a read or some other similar
    /// operation then this may corrupt the archive.
    ///
    /// Also note that after all files have been written to an archive the
    /// `finish` function needs to be called to finish writing the archive.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::fs::File;
    /// use tar::Archive;
    ///
    /// let mut ar = Archive::new(Vec::new());
    ///
    /// // Open the file at one location, but insert it into the archive with a
    /// // different name.
    /// let mut f = File::open("foo/bar/baz.txt").unwrap();
    /// ar.append_file("bar/baz.txt", &mut f).unwrap();
    /// ```
    pub fn append_file<P: AsRef<Path>>(&self, path: P, file: &mut fs::File)
                                       -> io::Result<()> {
        self.append_file2(path.as_ref(), file)
    }

    fn append_file2(&self, path: &Path, file: &mut fs::File) -> io::Result<()> {
        let stat = try!(file.metadata());
        let header = try!(Header::from_path_and_metadata(path, &stat));
        self.append(&header, file)
    }

    /// Adds a directory to this archive with the given path as the name of the
    /// directory in the archive.
    ///
    /// This will use `stat` to populate a `Header`, and it will then append the
    /// directory to the archive with the name `path`.
    ///
    /// Note that this will not attempt to seek the archive to a valid position,
    /// so if the archive is in the middle of a read or some other similar
    /// operation then this may corrupt the archive.
    ///
    /// Also note that after all files have been written to an archive the
    /// `finish` function needs to be called to finish writing the archive.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::fs;
    /// use tar::Archive;
    ///
    /// let mut ar = Archive::new(Vec::new());
    ///
    /// // Use the directory at one location, but insert it into the archive
    /// // with a different name.
    /// ar.append_dir("bardir", ".").unwrap();
    /// ```
    pub fn append_dir<P: AsRef<Path>, P2: AsRef<Path>>(
                      &self, path: P, src_path: P2) -> io::Result<()> {
        self.append_dir2(path.as_ref(), src_path.as_ref())
    }

    fn append_dir2(&self, path: &Path, src_path: &Path) -> io::Result<()> {
        let stat = try!(fs::metadata(src_path));
        let header = try!(Header::from_path_and_metadata(path, &stat));
        self.append(&header, &mut io::empty())
    }

    /// Finish writing this archive, emitting the termination sections.
    ///
    /// This function is required to be called to complete the archive, it will
    /// be invalid if this is not called.
    pub fn finish(&self) -> io::Result<()> {
        let b = [0; 1024];
        self.obj.borrow_mut().write_all(&b)
    }
}

impl<'a, R: Seek + Read> Iterator for Files<'a, R> {
    type Item = io::Result<File<'a, R>>;

    fn next(&mut self) -> Option<io::Result<File<'a, R>>> {
        // If we hit a previous error, or we reached the end, we're done here
        if self.done { return None }

        // Seek to the start of the next header in the archive
        try_iter!(self, self.archive.seek(self.offset));

        fn doseek<R: Seek + Read>(file: &File<R>) -> io::Result<()> {
            file.archive.seek(file.tar_offset + file.pos)
        }

        // Parse the next file header
        match try_iter!(self, self.archive.next_file(&mut self.offset, doseek)) {
            None => { self.done = true; None }
            Some(f) => Some(Ok(f)),
        }
    }
}


impl<'a, R: Read> Iterator for FilesMut<'a, R> {
    type Item = io::Result<File<'a, R>>;

    fn next(&mut self) -> Option<io::Result<File<'a, R>>> {
        // If we hit a previous error, or we reached the end, we're done here
        if self.done { return None }

        // Seek to the start of the next header in the archive
        let delta = self.next - self.archive.pos.get();
        try_iter!(self, self.archive.skip(delta));

        // no-op because this reader can't seek
        fn doseek<R>(_: &File<R>) -> io::Result<()> { Ok(()) }

        // Parse the next file header
        match try_iter!(self, self.archive.next_file(&mut self.next, doseek)) {
            None => { self.done = true; None }
            Some(f) => Some(Ok(f)),
        }
    }
}

impl Clone for Header {
    fn clone(&self) -> Header {
        Header { ..*self }
    }
}

impl Header {
    /// Creates a new blank ustar header ready to be filled in
    pub fn new() -> Header {
        let mut header: Header = unsafe { mem::zeroed() };
        // Flag this header as a UStar archive
        header.ustar = *b"ustar\0";
        header.ustar_version = *b"00";
        return header
    }

    fn from_path_and_metadata(path: &Path, stat: &fs::Metadata)
                              -> io::Result<Header> {
        let mut header = Header::new();
        // TODO: add trailing path::MAIN_SEPARATOR onto directories for
        // compatibility. Requires either the std::path to allow it or OsStr
        // to permit character checks
        // https://github.com/rust-lang/rust/issues/29008
        try!(header.set_path(path));
        header.set_metadata(&stat);
        header.set_cksum();
        Ok(header)
    }

    fn is_ustar(&self) -> bool {
        &self.ustar[..5] == b"ustar"
    }

    /// Returns a view into this header as a byte array.
    pub fn as_bytes(&self) -> &[u8; 512] {
        debug_assert_eq!(512, mem::size_of_val(self));
        unsafe { &*(self as *const _ as *const [u8; 512]) }
    }

    /// Blanket sets the metadata in this header from the metadata argument
    /// provided.
    ///
    /// This is useful for initializing a `Header` from the OS's metadata from a
    /// file.
    pub fn set_metadata(&mut self, meta: &fs::Metadata) {
        // Platform-specific fill
        self.fill_from(meta);
        // Platform-agnostic fill
        // Set size of directories to zero
        self.set_size(if meta.is_dir() { 0 } else { meta.len() });
        self.set_device_major(0);
        self.set_device_minor(0);
    }

    /// Returns the file size this header represents.
    ///
    /// May return an error if the field is corrupted.
    pub fn size(&self) -> io::Result<u64> {
        octal_from(&self.size)
    }

    /// Encodes the `size` argument into the size field of this header.
    pub fn set_size(&mut self, size: u64) {
        octal_into(&mut self.size, size)
    }

    /// Returns the pathname stored in this header.
    ///
    /// This method may fail if the pathname is not valid unicode and this is
    /// called on a Windows platform.
    ///
    /// Note that this function will convert any `\` characters to directory
    /// separators.
    pub fn path(&self) -> io::Result<Cow<Path>> {
        return bytes2path(self.path_bytes());

        #[cfg(windows)]
        fn bytes2path(bytes: Cow<[u8]>) -> io::Result<Cow<Path>> {
            match bytes {
                Cow::Borrowed(bytes) => {
                    let s = try!(str::from_utf8(bytes).map_err(|_| {
                        not_unicode()
                    }));
                    Ok(Cow::Borrowed(Path::new(s)))
                }
                Cow::Owned(bytes) => {
                    let s = try!(String::from_utf8(bytes).map_err(|_| {
                        not_unicode()
                    }));
                    Ok(Cow::Owned(PathBuf::from(s)))
                }
            }
        }
        #[cfg(unix)]
        fn bytes2path(bytes: Cow<[u8]>) -> io::Result<Cow<Path>> {
            Ok(match bytes {
                Cow::Borrowed(bytes) => Cow::Borrowed({
                    Path::new(OsStr::from_bytes(bytes))
                }),
                Cow::Owned(bytes) => Cow::Owned({
                    PathBuf::from(OsString::from_vec(bytes))
                })
            })
        }
    }

    /// Returns the pathname stored in this header as a byte array.
    ///
    /// This function is guaranteed to succeed, but you may wish to call the
    /// `path` method to convert to a `Path`.
    ///
    /// Note that this function will convert any `\` characters to directory
    /// separators.
    pub fn path_bytes(&self) -> Cow<[u8]> {
        if (!self.is_ustar() || self.prefix[0] == 0) &&
           !self.name.contains(&b'\\') {
            Cow::Borrowed(truncate(&self.name))
        } else {
            fn noslash(b: &u8) -> u8 {
                if *b == b'\\' {b'/'} else {*b}
            }
            let mut bytes = Vec::new();
            let prefix = truncate(&self.prefix);
            if prefix.len() > 0 {
                bytes.extend(prefix.iter().map(noslash));
                bytes.push(b'/');
            }
            bytes.extend(truncate(&self.name).iter().map(noslash));
            Cow::Owned(bytes)
        }
    }

    /// Sets the path name for this header.
    ///
    /// This function will set the pathname listed in this header, encoding it
    /// in the appropriate format. May fail if the path is too long or if the
    /// path specified is not unicode and this is a Windows platform.
    pub fn set_path<P: AsRef<Path>>(&mut self, p: P) -> io::Result<()> {
        self.set_path2(p.as_ref())
    }

    fn set_path2(&mut self, path: &Path) -> io::Result<()> {
        let bytes = match bytes(path) {
            Some(b) => b,
            None => return Err(Error::new(ErrorKind::Other, "path was not \
                                                             valid unicode")),
        };
        if bytes.iter().any(|b| *b == 0) {
            return Err(Error::new(ErrorKind::Other, "path contained a nul byte"))
        }

        let (namelen, prefixlen) = (self.name.len(), self.prefix.len());
        if bytes.len() < namelen {
            try!(copy_into(&mut self.name, bytes, true));
        } else {
            let prefix = &bytes[..cmp::min(bytes.len(), prefixlen)];
            let pos = match prefix.iter().rposition(|&b| b == b'/' || b == b'\\') {
                Some(i) => i,
                None => return Err(Error::new(ErrorKind::Other,
                                              "path cannot be split to be \
                                               inserted into archive")),
            };
            try!(copy_into(&mut self.name, &bytes[pos + 1..], true));
            try!(copy_into(&mut self.prefix, &bytes[..pos], true));
        }
        return Ok(());

        #[cfg(windows)]
        fn bytes(p: &Path) -> Option<&[u8]> {
            p.as_os_str().to_str().map(|s| s.as_bytes())
        }
        #[cfg(unix)]
        fn bytes(p: &Path) -> Option<&[u8]> {
            Some(p.as_os_str().as_bytes())
        }
    }

    /// Returns the mode bits for this file
    ///
    /// May return an error if the field is corrupted.
    pub fn mode(&self) -> io::Result<u32> {
        octal_from(&self.mode).map(|u| u as u32)
    }

    /// Encodes the `mode` provided into this header.
    pub fn set_mode(&mut self, mode: u32) {
        octal_into(&mut self.mode, mode & 0o3777);
    }

    /// Returns the value of the owner's user ID field
    ///
    /// May return an error if the field is corrupted.
    pub fn uid(&self) -> io::Result<u32> {
        octal_from(&self.owner_id).map(|u| u as u32)
    }

    /// Encodes the `uid` provided into this header.
    pub fn set_uid(&mut self, uid: u32) {
        octal_into(&mut self.owner_id, uid);
    }

    /// Returns the value of the group's user ID field
    pub fn gid(&self) -> io::Result<u32> {
        octal_from(&self.group_id).map(|u| u as u32)
    }

    /// Encodes the `gid` provided into this header.
    pub fn set_gid(&mut self, gid: u32) {
        octal_into(&mut self.group_id, gid);
    }

    /// Returns the last modification time in Unix time format
    pub fn mtime(&self) -> io::Result<u64> {
        octal_from(&self.mtime)
    }

    /// Encodes the `mtime` provided into this header.
    ///
    /// Note that this time is typically a number of seconds passed since
    /// January 1, 1970.
    pub fn set_mtime(&mut self, mtime: u64) {
        octal_into(&mut self.mtime, mtime);
    }

    /// Return the username of the owner of this file, if present and if valid
    /// utf8
    pub fn username(&self) -> Option<&str> {
        self.username_bytes().and_then(|s| str::from_utf8(s).ok())
    }

    /// Returns the username of the owner of this file, if present
    pub fn username_bytes(&self) -> Option<&[u8]> {
        if self.is_ustar() {
            Some(truncate(&self.owner_name))
        } else {
            None
        }
    }

    /// Sets the username inside this header.
    ///
    /// May return an error if the name provided is too long.
    pub fn set_username(&mut self, name: &str) -> io::Result<()> {
        copy_into(&mut self.owner_name, name.as_bytes(), false)
    }

    /// Return the group name of the owner of this file, if present and if valid
    /// utf8
    pub fn groupname(&self) -> Option<&str> {
        self.groupname_bytes().and_then(|s| str::from_utf8(s).ok())
    }

    /// Returns the group name of the owner of this file, if present
    pub fn groupname_bytes(&self) -> Option<&[u8]> {
        if self.is_ustar() {
            Some(truncate(&self.group_name))
        } else {
            None
        }
    }

    /// Sets the group name inside this header.
    ///
    /// May return an error if the name provided is too long.
    pub fn set_groupname(&mut self, name: &str) -> io::Result<()> {
        copy_into(&mut self.group_name, name.as_bytes(), false)
    }

    /// Returns the device major number, if present.
    ///
    /// This field is only present in UStar archives. A value of `None` means
    /// that this archive is not a UStar archive, while a value of `Some`
    /// represents the attempt to decode the field in the header.
    pub fn device_major(&self) -> Option<io::Result<u32>> {
        if self.is_ustar() {
            Some(octal_from(&self.dev_major).map(|u| u as u32))
        } else {
            None
        }
    }

    /// Encodes the value `major` into the dev_major field of this header.
    pub fn set_device_major(&mut self, major: u32) {
        octal_into(&mut self.dev_major, major);
    }

    /// Returns the device minor number, if present.
    ///
    /// This field is only present in UStar archives. A value of `None` means
    /// that this archive is not a UStar archive, while a value of `Some`
    /// represents the attempt to decode the field in the header.
    pub fn device_minor(&self) -> Option<io::Result<u32>> {
        if self.is_ustar() {
            Some(octal_from(&self.dev_minor).map(|u| u as u32))
        } else {
            None
        }
    }

    /// Encodes the value `minor` into the dev_major field of this header.
    pub fn set_device_minor(&mut self, minor: u32) {
        octal_into(&mut self.dev_minor, minor);
    }

    /// Returns the checksum field of this header.
    ///
    /// May return an error if the field is corrupted.
    pub fn cksum(&self) -> io::Result<u32> {
        octal_from(&self.cksum).map(|u| u as u32)
    }

    /// Sets the checksum field of this header based on the current fields in
    /// this header.
    pub fn set_cksum(&mut self) {
        let cksum = {
            let bytes = self.as_bytes();
            bytes[..148].iter().map(|i| *i as u32).fold(0, |a, b| a + b) +
                bytes[156..].iter().map(|i| *i as u32).fold(0, |a, b| a + b) +
                32 * (self.cksum.len() as u32)
        };
        octal_into(&mut self.cksum, cksum);
    }

    #[cfg(unix)]
    fn fill_from(&mut self, meta: &fs::Metadata) {
        self.set_mode((meta.mode() & 0o3777) as u32);
        self.set_mtime(meta.mtime() as u64);
        self.set_uid(meta.uid() as u32);
        self.set_gid(meta.gid() as u32);

        // TODO: need to bind more file types
        self.link[0] = match meta.mode() & libc::S_IFMT {
            libc::S_IFREG => b'0',
            libc::S_IFLNK => b'2',
            libc::S_IFCHR => b'3',
            libc::S_IFBLK => b'4',
            libc::S_IFDIR => b'5',
            libc::S_IFIFO => b'6',
            _ => b' ',
        };
    }

    #[cfg(windows)]
    fn fill_from(&mut self, meta: &fs::Metadata) {
        let readonly = meta.file_attributes() & winapi::FILE_ATTRIBUTE_READONLY;

        // There's no concept of a mode on windows, so do a best approximation
        // here.
        let mode = match (meta.is_dir(), readonly != 0) {
            (true, false) => 0o755,
            (true, true) => 0o555,
            (false, false) => 0o644,
            (false, true) => 0o444,
        };
        self.set_mode(mode);
        self.set_uid(0);
        self.set_gid(0);

        let ft = meta.file_type();
        self.link[0] = if ft.is_dir() {
            b'5'
        } else if ft.is_file() {
            b'0'
        } else if ft.is_symlink() {
            b'2'
        } else {
            b' '
        };

        // The dates listed in tarballs are always seconds relative to
        // January 1, 1970. On Windows, however, the timestamps are returned as
        // dates relative to January 1, 1601 (in 100ns intervals), so we need to
        // add in some offset for those dates.
        let mtime = (meta.last_write_time() / (1_000_000_000 / 100)) - 11644473600;
        self.set_mtime(mtime);
    }
}

impl<'a, R: Read> File<'a, R> {
    /// Returns access to the header of this file in the archive.
    ///
    /// This provides access to the the metadata for this file in the archive.
    pub fn header(&self) -> &Header { &self.header }

    /// Writes this file to the specified location.
    ///
    /// This function will write the entire contents of this file into the
    /// location specified by `dst`. Metadata will also be propagated to the
    /// path `dst`.
    ///
    /// This function will create a file at the path `dst`, and it is required
    /// that the intermediate directories are created. Any existing file at the
    /// location `dst` will be overwritten.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::fs::File;
    /// use tar::Archive;
    ///
    /// let ar = Archive::new(File::open("foo.tar").unwrap());
    ///
    /// for (i, file) in ar.files().unwrap().enumerate() {
    ///     let mut file = file.unwrap();
    ///     file.unpack(format!("file-{}", i)).unwrap();
    /// }
    /// ```
    pub fn unpack<P: AsRef<Path>>(&mut self, dst: P) -> io::Result<()> {
        self.unpack2(dst.as_ref())
    }

    fn unpack2(&mut self, dst: &Path) -> io::Result<()> {
        try!(fs::File::create(dst).and_then(|mut f| {
            if try!(io::copy(self, &mut f)) != self.size {
                return Err(bad_archive());
            }
            Ok(())
        }).map_err(|e| {
            let header = self.header().path_bytes();
            TarError::new(&format!("failed to unpack `{}` into `{}`",
                                   String::from_utf8_lossy(&header),
                                   dst.display()), e)
        }));

        if let Ok(mtime) = self.header().mtime() {
            let mtime = FileTime::from_seconds_since_1970(mtime, 0);
            try!(filetime::set_file_times(dst, mtime, mtime).map_err(|e| {
                TarError::new(&format!("failed to set mtime for `{}`",
                                       dst.display()), e)
            }));
        }
        if let Ok(mode) = self.header().mode() {
            try!(set_perms(dst, mode).map_err(|e| {
                TarError::new(&format!("failed to set permissions to {:o} \
                                        for `{}`", mode, dst.display()), e)
            }));
        }
        return Ok(());

        #[cfg(unix)]
        fn set_perms(dst: &Path, mode: u32) -> io::Result<()> {
            use std::os::unix::raw;
            let perm = fs::Permissions::from_mode(mode as raw::mode_t);
            fs::set_permissions(dst, perm)
        }
        #[cfg(windows)]
        fn set_perms(dst: &Path, mode: u32) -> io::Result<()> {
            let mut perm = try!(fs::metadata(dst)).permissions();
            perm.set_readonly(mode & 0o200 != 0o200);
            fs::set_permissions(dst, perm)
        }
    }
}

impl<'a, R: Read> Read for &'a Archive<R> {
    fn read(&mut self, into: &mut [u8]) -> io::Result<usize> {
        self.obj.borrow_mut().read(into).map(|i| {
            self.pos.set(self.pos.get() + i as u64);
            i
        })
    }
}

impl<'a, R: Read> Read for File<'a, R> {
    fn read(&mut self, into: &mut [u8]) -> io::Result<usize> {
        if self.size == self.pos { return Ok(0) }

        try!((self.seek)(self));
        let amt = cmp::min((self.size - self.pos) as usize, into.len());
        let amt = try!(Read::read(&mut self.archive, &mut into[..amt]));
        self.pos += amt as u64;
        Ok(amt)
    }
}

impl<'a, R: Read + Seek> Seek for File<'a, R> {
    fn seek(&mut self, how: SeekFrom) -> io::Result<u64> {
        let next = match how {
            SeekFrom::Start(pos) => pos as i64,
            SeekFrom::Current(pos) => self.pos as i64 + pos,
            SeekFrom::End(pos) => self.size as i64 + pos,
        };
        if next < 0 {
            Err(Error::new(ErrorKind::Other, "cannot seek before position 0"))
        } else if next as u64 > self.size {
            Err(Error::new(ErrorKind::Other, "cannot seek past end of file"))
        } else {
            self.pos = next as u64;
            Ok(self.pos)
        }
    }
}

fn bad_archive() -> Error {
    Error::new(ErrorKind::Other, "invalid tar archive")
}

fn octal_from(slice: &[u8]) -> io::Result<u64> {
    let num = match str::from_utf8(truncate(slice)) {
        Ok(n) => n,
        Err(_) => return Err(bad_archive()),
    };
    match u64::from_str_radix(num.trim(), 8) {
        Ok(n) => Ok(n),
        Err(_) => Err(bad_archive())
    }
}

fn octal_into<T: fmt::Octal>(dst: &mut [u8], val: T) {
    let o = format!("{:o}", val);
    let value = o.bytes().rev().chain(repeat(b'0'));
    for (slot, value) in dst.iter_mut().rev().skip(1).zip(value) {
        *slot = value;
    }
}

fn truncate<'a>(slice: &'a [u8]) -> &'a [u8] {
    match slice.iter().position(|i| *i == 0) {
        Some(i) => &slice[..i],
        None => slice,
    }
}

fn read_all<R: Read>(r: &mut R, buf: &mut [u8]) -> io::Result<()> {
    let mut read = 0;
    while read < buf.len() {
        match try!(r.read(&mut buf[read..])) {
            0 => return Err(bad_archive()),
            n => read += n,
        }
    }
    Ok(())
}

/// Copies `bytes` into the `slot` provided, returning an error if the `bytes`
/// array is too long or if it contains any nul bytes.
///
/// Also provides the option to map '\' characters to '/' characters for the
/// names of paths in archives. The `tar` utility doesn't seem to like windows
/// backslashes when unpacking on Unix.
fn copy_into(slot: &mut [u8], bytes: &[u8], map_slashes: bool) -> io::Result<()> {
    if bytes.len() > slot.len() {
        Err(Error::new(ErrorKind::Other, "provided value is too long"))
    } else if bytes.iter().any(|b| *b == 0) {
        Err(Error::new(ErrorKind::Other, "provided value contains a nul byte"))
    } else {
        for (slot, val) in slot.iter_mut().zip(bytes) {
            if map_slashes && *val == b'\\' {
                *slot = b'/';
            } else {
                *slot = *val;
            }
        }
        Ok(())
    }
}

#[cfg(windows)]
fn not_unicode() -> Error {
    Error::new(ErrorKind::Other, "only unicode paths are supported on windows")
}

impl TarError {
    fn new(desc: &str, err: Error) -> TarError {
        TarError {
            desc: desc.to_string(),
            io: err,
        }
    }
}

impl error::Error for TarError {
    fn description(&self) -> &str {
        &self.desc
    }

    fn cause(&self) -> Option<&error::Error> {
        Some(&self.io)
    }
}

impl fmt::Display for TarError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.desc.fmt(f)
    }
}

impl From<TarError> for Error {
    fn from(t: TarError) -> Error {
        Error::new(t.io.kind(), t)
    }
}

#[cfg(test)]
mod tests {
    extern crate tempdir;

    use std::io::prelude::*;
    use std::io::{self, Cursor, SeekFrom};
    use std::iter::repeat;
    use std::fs::{self, File};
    use std::path::Path;

    use filetime::FileTime;
    use self::tempdir::TempDir;
    use super::{Archive, Header};

    macro_rules! t {
        ($e:expr) => (match $e {
            Ok(v) => v,
            Err(e) => panic!("{} returned {}", stringify!($e), e),
        })
    }

    #[test]
    fn simple() {
        let ar = Archive::new(Cursor::new(&include_bytes!("tests/simple.tar")[..]));
        for file in t!(ar.files()) {
            t!(file);
        }
    }

    #[test]
    fn header_impls() {
        let ar = Archive::new(Cursor::new(&include_bytes!("tests/simple.tar")[..]));
        let hn = Header::new();
        let hnb = hn.as_bytes();
        for file in t!(ar.files()) {
            let file = t!(file);
            let h1 = file.header();
            let h1b = h1.as_bytes();
            let h2 = h1.clone();
            let h2b = h2.as_bytes();
            assert!(h1b[..] == h2b[..] && h2b[..] != hnb[..])
        }
    }

    #[test]
    fn reading_files() {
        let rdr = Cursor::new(&include_bytes!("tests/reading_files.tar")[..]);
        let ar = Archive::new(rdr);
        let mut files = t!(ar.files());
        let mut a = t!(files.next().unwrap());
        let mut b = t!(files.next().unwrap());
        assert!(files.next().is_none());

        assert_eq!(&*a.header().path_bytes(), b"a");
        assert_eq!(&*b.header().path_bytes(), b"b");
        let mut s = String::new();
        t!(a.read_to_string(&mut s));
        assert_eq!(s, "a\na\na\na\na\na\na\na\na\na\na\n");
        s.truncate(0);
        t!(b.read_to_string(&mut s));
        assert_eq!(s, "b\nb\nb\nb\nb\nb\nb\nb\nb\nb\nb\n");
        t!(a.seek(SeekFrom::Start(0)));
        s.truncate(0);
        t!(a.read_to_string(&mut s));
        assert_eq!(s, "a\na\na\na\na\na\na\na\na\na\na\n");
    }

    #[test]
    fn writing_files() {
        let wr = Cursor::new(Vec::new());
        let ar = Archive::new(wr);
        let td = t!(TempDir::new("tar-rs"));

        let path = td.path().join("test");
        t!(t!(File::create(&path)).write_all(b"test"));

        t!(ar.append_file("test2", &mut t!(File::open(&path))));
        t!(ar.finish());

        let rd = Cursor::new(ar.into_inner().into_inner());
        let ar = Archive::new(rd);
        let mut files = t!(ar.files());
        let mut f = t!(files.next().unwrap());
        assert!(files.next().is_none());

        assert_eq!(&*f.header().path_bytes(), b"test2");
        assert_eq!(f.header().size().unwrap(), 4);
        let mut s = String::new();
        t!(f.read_to_string(&mut s));
        assert_eq!(s, "test");
    }

    #[test]
    fn large_filename() {
        let ar = Archive::new(Cursor::new(Vec::new()));
        let td = t!(TempDir::new("tar-rs"));

        let path = td.path().join("test");
        t!(t!(File::create(&path)).write_all(b"test"));

        let filename = repeat("abcd/").take(50).collect::<String>();
        t!(ar.append_file(&filename, &mut t!(File::open(&path))));
        t!(ar.finish());

        let too_long = repeat("abcd").take(200).collect::<String>();
        assert!(ar.append_file(&too_long, &mut t!(File::open(&path))).is_err());

        let rd = Cursor::new(ar.into_inner().into_inner());
        let ar = Archive::new(rd);
        let mut files = t!(ar.files());
        let mut f = files.next().unwrap().unwrap();
        assert!(files.next().is_none());

        assert_eq!(&*f.header().path_bytes(), filename.as_bytes());
        assert_eq!(f.header().size().unwrap(), 4);
        let mut s = String::new();
        t!(f.read_to_string(&mut s));
        assert_eq!(s, "test");
    }

    #[test]
    fn reading_files_mut() {
        let rdr = Cursor::new(&include_bytes!("tests/reading_files.tar")[..]);
        let mut ar = Archive::new(rdr);
        let mut files = t!(ar.files_mut());
        let mut a = t!(files.next().unwrap());
        assert_eq!(&*a.header().path_bytes(), b"a");
        let mut s = String::new();
        t!(a.read_to_string(&mut s));
        assert_eq!(s, "a\na\na\na\na\na\na\na\na\na\na\n");
        s.truncate(0);
        t!(a.read_to_string(&mut s));
        assert_eq!(s, "");
        let mut b = t!(files.next().unwrap());

        assert_eq!(&*b.header().path_bytes(), b"b");
        s.truncate(0);
        t!(b.read_to_string(&mut s));
        assert_eq!(s, "b\nb\nb\nb\nb\nb\nb\nb\nb\nb\nb\n");
        assert!(files.next().is_none());
    }

    fn check_dirtree(td: &TempDir) {
        let dir_a = td.path().join("a");
        let dir_b = td.path().join("a/b");
        let file_c = td.path().join("a/c");
        assert!(fs::metadata(&dir_a).map(|m| m.is_dir()).unwrap_or(false));
        assert!(fs::metadata(&dir_b).map(|m| m.is_dir()).unwrap_or(false));
        assert!(fs::metadata(&file_c).map(|m| m.is_file()).unwrap_or(false));
    }

    #[test]
    fn extracting_directories() {
        let td = t!(TempDir::new("tar-rs"));
        let rdr = Cursor::new(&include_bytes!("tests/directory.tar")[..]);
        let mut ar = Archive::new(rdr);
        t!(ar.unpack(td.path()));
        check_dirtree(&td);
    }

    #[test]
    fn writing_and_extracting_directories() {
        let td = t!(TempDir::new("tar-rs"));

        let cur = Cursor::new(Vec::new());
        let ar = Archive::new(cur);
        let tmppath = td.path().join("tmpfile");
        t!(t!(File::create(&tmppath)).write_all(b"c"));
        t!(ar.append_dir("a", "."));
        t!(ar.append_dir("a/b", "."));
        t!(ar.append_file("a/c", &mut t!(File::open(&tmppath))));
        t!(ar.finish());

        let rdr = Cursor::new(ar.into_inner().into_inner());
        let mut ar = Archive::new(rdr);
        t!(ar.unpack(td.path()));
        check_dirtree(&td);
    }

    #[test]
    fn extracting_duplicate_dirs() {
        let td = t!(TempDir::new("tar-rs"));
        let rdr = Cursor::new(&include_bytes!("tests/duplicate_dirs.tar")[..]);
        let mut ar = Archive::new(rdr);
        t!(ar.unpack(td.path()));

        let some_dir = td.path().join("some_dir");
        assert!(fs::metadata(&some_dir).map(|m| m.is_dir()).unwrap_or(false));
    }

    #[test]
    fn handling_incorrect_file_size() {
        let td = t!(TempDir::new("tar-rs"));

        let cur = Cursor::new(Vec::new());
        let ar = Archive::new(cur);

        let path = td.path().join("tmpfile");
        t!(File::create(&path));
        let mut file = t!(File::open(&path));
        let mut header = Header::new();
        t!(header.set_path("somepath"));
        header.set_metadata(&t!(file.metadata()));
        header.set_size(2048); // past the end of file null blocks
        header.set_cksum();
        t!(ar.append(&header, &mut file));
        t!(ar.finish());

        // Extracting
        let rdr = Cursor::new(ar.into_inner().into_inner());
        let mut ar = Archive::new(rdr);
        assert!(ar.unpack(td.path()).is_err());

        // Iterating
        let rdr = Cursor::new(ar.into_inner().into_inner());
        let mut ar = Archive::new(rdr);
        assert!(t!(ar.files_mut()).any(|fr| fr.is_err()));
    }

    #[test]
    fn extracting_malicious_tarball() {
        use std::fs;
        use std::fs::OpenOptions;
        use std::io::{Seek, Write};

        let td = t!(TempDir::new("tar-rs"));

        let mut evil_tar = Cursor::new(Vec::new());

        {
            let a = Archive::new(&mut evil_tar);
            let mut evil_txt_f = t!(OpenOptions::new().read(true).write(true)
                                                .create(true)
                                                .open(td.path().join("evil.txt")));
            t!(writeln!(evil_txt_f, "This is an evil file."));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("/tmp/abs_evil.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("//tmp/abs_evil2.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("///tmp/abs_evil3.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("/./tmp/abs_evil4.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("//./tmp/abs_evil5.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("///./tmp/abs_evil6.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("/../tmp/rel_evil.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("../rel_evil2.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("./../rel_evil3.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("some/../../rel_evil4.txt", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file("././//./", &mut evil_txt_f));
            t!(evil_txt_f.seek(SeekFrom::Start(0)));
            t!(a.append_file(".", &mut evil_txt_f));
            t!(a.finish());
        }

        t!(evil_tar.seek(SeekFrom::Start(0)));
        let mut ar = Archive::new(&mut evil_tar);
        t!(ar.unpack(td.path()));

        assert!(fs::metadata("/tmp/abs_evil.txt").is_err());
        assert!(fs::metadata("/tmp/abs_evil.txt2").is_err());
        assert!(fs::metadata("/tmp/abs_evil.txt3").is_err());
        assert!(fs::metadata("/tmp/abs_evil.txt4").is_err());
        assert!(fs::metadata("/tmp/abs_evil.txt5").is_err());
        assert!(fs::metadata("/tmp/abs_evil.txt6").is_err());
        assert!(fs::metadata("/tmp/rel_evil.txt").is_err());
        assert!(fs::metadata("/tmp/rel_evil.txt").is_err());
        assert!(fs::metadata(td.path().join("../tmp/rel_evil.txt")).is_err());
        assert!(fs::metadata(td.path().join("../rel_evil2.txt")).is_err());
        assert!(fs::metadata(td.path().join("../rel_evil3.txt")).is_err());
        assert!(fs::metadata(td.path().join("../rel_evil4.txt")).is_err());

        // The `some` subdirectory should not be created because the only
        // filename that references this has '..'.
        assert!(fs::metadata(td.path().join("some")).is_err());

        // The `tmp` subdirectory should be created and within this
        // subdirectory, there should be files named `abs_evil.txt` through
        // `abs_evil6.txt`.
        assert!(fs::metadata(td.path().join("tmp")).map(|m| m.is_dir())
                   .unwrap_or(false));
        assert!(fs::metadata(td.path().join("tmp/abs_evil.txt"))
                   .map(|m| m.is_file()).unwrap_or(false));
        assert!(fs::metadata(td.path().join("tmp/abs_evil2.txt"))
                   .map(|m| m.is_file()).unwrap_or(false));
        assert!(fs::metadata(td.path().join("tmp/abs_evil3.txt"))
                   .map(|m| m.is_file()).unwrap_or(false));
        assert!(fs::metadata(td.path().join("tmp/abs_evil4.txt"))
                   .map(|m| m.is_file()).unwrap_or(false));
        assert!(fs::metadata(td.path().join("tmp/abs_evil5.txt"))
                   .map(|m| m.is_file()).unwrap_or(false));
        assert!(fs::metadata(td.path().join("tmp/abs_evil6.txt"))
                   .map(|m| m.is_file()).unwrap_or(false));
    }

    #[test]
    fn octal_spaces() {
        let rdr = Cursor::new(&include_bytes!("tests/spaces.tar")[..]);
        let ar = Archive::new(rdr);

        let file = ar.files().unwrap().next().unwrap().unwrap();
        assert_eq!(file.header().mode().unwrap() & 0o777, 0o777);
        assert_eq!(file.header().uid().unwrap(), 0);
        assert_eq!(file.header().gid().unwrap(), 0);
        assert_eq!(file.header().size().unwrap(), 2);
        assert_eq!(file.header().mtime().unwrap(), 0o12440016664);
        assert_eq!(file.header().cksum().unwrap(), 0o4253);
    }

    #[test]
    fn extracting_malformed_tar_null_blocks() {
        let td = t!(TempDir::new("tar-rs"));

        let cur = Cursor::new(Vec::new());
        let ar = Archive::new(cur);

        let path1 = td.path().join("tmpfile1");
        let path2 = td.path().join("tmpfile2");
        t!(File::create(&path1));
        t!(File::create(&path2));
        t!(ar.append_path(&path1));
        let mut wrtr = ar.into_inner();
        t!(wrtr.write_all(&[0; 512]));
        let ar = Archive::new(wrtr);
        t!(ar.append_path(&path2));
        t!(ar.finish());

        let rdr = Cursor::new(ar.into_inner().into_inner());
        let mut ar = Archive::new(rdr);
        assert!(ar.unpack(td.path()).is_err());
    }

    #[test]
    fn empty_filename()
    {
        let td = t!(TempDir::new("tar-rs"));
        let rdr = Cursor::new(&include_bytes!("tests/empty_filename.tar")[..]);
        let mut ar = Archive::new(rdr);
        assert!(ar.unpack(td.path()).is_err());
    }

    #[test]
    fn file_times() {
        let td = t!(TempDir::new("tar-rs"));
        let rdr = Cursor::new(&include_bytes!("tests/file_times.tar")[..]);
        let mut ar = Archive::new(rdr);
        t!(ar.unpack(td.path()));

        let meta = fs::metadata(td.path().join("a")).unwrap();
        let mtime = FileTime::from_last_modification_time(&meta);
        let atime = FileTime::from_last_access_time(&meta);
        assert_eq!(mtime.seconds_relative_to_1970(), 1000000000);
        assert_eq!(mtime.nanoseconds(), 0);
        assert_eq!(atime.seconds_relative_to_1970(), 1000000000);
        assert_eq!(atime.nanoseconds(), 0);
    }

    #[test]
    fn backslash_same_as_slash() {
        // Insert a file into an archive with a backslash
        let td = t!(TempDir::new("tar-rs"));
        let ar = Archive::new(Vec::<u8>::new());
        t!(ar.append_dir("foo\\bar", td.path()));
        ar.finish().unwrap();
        let ar = Archive::new(Cursor::new(ar.into_inner()));
        let f = t!(t!(ar.files()).next().unwrap());
        assert_eq!(&*f.header().path().unwrap(), Path::new("foo/bar"));

        // Unpack an archive with a backslash in the name
        let ar = Archive::new(Vec::<u8>::new());
        let mut header = Header::new();
        header.set_metadata(&t!(fs::metadata(td.path())));
        header.set_size(0);
        for (a, b) in header.name.iter_mut().zip(b"foo\\bar\x00") {
            *a = *b;
        }
        header.set_cksum();
        t!(ar.append(&header, &mut io::empty()));
        ar.finish().unwrap();
        let mut ar = Archive::new(Cursor::new(ar.into_inner()));
        {
            let f = t!(t!(ar.files()).next().unwrap());
            assert_eq!(&*f.header().path().unwrap(), Path::new("foo/bar"));
        }
        t!(ar.files()); // seek to 0
        t!(ar.unpack(td.path()));
        assert!(fs::metadata(td.path().join("foo/bar")).is_ok());
    }

    #[cfg(unix)]
    #[test]
    fn nul_bytes_in_path() {
        use std::os::unix::prelude::*;
        use std::ffi::OsStr;

        let nul_path = OsStr::from_bytes(b"foo\0");
        let td = t!(TempDir::new("tar-rs"));
        let ar = Archive::new(Vec::<u8>::new());
        let err = ar.append_dir(nul_path, td.path()).unwrap_err();
        assert!(err.to_string().contains("contained a nul byte"));
    }
}