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 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
#[cfg(unix)]
use std::os::unix::prelude::*;
#[cfg(windows)]
use std::os::windows::prelude::*;
use std::borrow::Cow;
use std::fmt;
use std::fs;
use std::io;
use std::iter;
use std::iter::repeat;
use std::mem;
use std::path::{Component, Path, PathBuf};
use std::str;
use crate::other;
use crate::EntryType;
/// Representation of the header of an entry in an archive
#[repr(C)]
#[allow(missing_docs)]
pub struct Header {
bytes: [u8; 512],
}
/// Declares the information that should be included when filling a Header
/// from filesystem metadata.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum HeaderMode {
/// All supported metadata, including mod/access times and ownership will
/// be included.
Complete,
/// Only metadata that is directly relevant to the identity of a file will
/// be included. In particular, ownership and mod/access times are excluded.
Deterministic,
}
/// Representation of the header of an entry in an archive
#[repr(C)]
#[allow(missing_docs)]
pub struct OldHeader {
pub name: [u8; 100],
pub mode: [u8; 8],
pub uid: [u8; 8],
pub gid: [u8; 8],
pub size: [u8; 12],
pub mtime: [u8; 12],
pub cksum: [u8; 8],
pub linkflag: [u8; 1],
pub linkname: [u8; 100],
pub pad: [u8; 255],
}
/// Representation of the header of an entry in an archive
#[repr(C)]
#[allow(missing_docs)]
pub struct UstarHeader {
pub name: [u8; 100],
pub mode: [u8; 8],
pub uid: [u8; 8],
pub gid: [u8; 8],
pub size: [u8; 12],
pub mtime: [u8; 12],
pub cksum: [u8; 8],
pub typeflag: [u8; 1],
pub linkname: [u8; 100],
// UStar format
pub magic: [u8; 6],
pub version: [u8; 2],
pub uname: [u8; 32],
pub gname: [u8; 32],
pub dev_major: [u8; 8],
pub dev_minor: [u8; 8],
pub prefix: [u8; 155],
pub pad: [u8; 12],
}
/// Representation of the header of an entry in an archive
#[repr(C)]
#[allow(missing_docs)]
pub struct GnuHeader {
pub name: [u8; 100],
pub mode: [u8; 8],
pub uid: [u8; 8],
pub gid: [u8; 8],
pub size: [u8; 12],
pub mtime: [u8; 12],
pub cksum: [u8; 8],
pub typeflag: [u8; 1],
pub linkname: [u8; 100],
// GNU format
pub magic: [u8; 6],
pub version: [u8; 2],
pub uname: [u8; 32],
pub gname: [u8; 32],
pub dev_major: [u8; 8],
pub dev_minor: [u8; 8],
pub atime: [u8; 12],
pub ctime: [u8; 12],
pub offset: [u8; 12],
pub longnames: [u8; 4],
pub unused: [u8; 1],
pub sparse: [GnuSparseHeader; 4],
pub isextended: [u8; 1],
pub realsize: [u8; 12],
pub pad: [u8; 17],
}
/// Description of the header of a spare entry.
///
/// Specifies the offset/number of bytes of a chunk of data in octal.
#[repr(C)]
#[allow(missing_docs)]
pub struct GnuSparseHeader {
pub offset: [u8; 12],
pub numbytes: [u8; 12],
}
/// Representation of the entry found to represent extended GNU sparse files.
///
/// When a `GnuHeader` has the `isextended` flag set to `1` then the contents of
/// the next entry will be one of these headers.
#[repr(C)]
#[allow(missing_docs)]
pub struct GnuExtSparseHeader {
pub sparse: [GnuSparseHeader; 21],
pub isextended: [u8; 1],
pub padding: [u8; 7],
}
impl Header {
/// Creates a new blank GNU header.
///
/// The GNU style header is the default for this library and allows various
/// extensions such as long path names, long link names, and setting the
/// atime/ctime metadata attributes of files.
pub fn new_gnu() -> Header {
let mut header = Header { bytes: [0; 512] };
unsafe {
let gnu = cast_mut::<_, GnuHeader>(&mut header);
gnu.magic = *b"ustar ";
gnu.version = *b" \0";
}
header.set_mtime(0);
header
}
/// Creates a new blank UStar header.
///
/// The UStar style header is an extension of the original archive header
/// which enables some extra metadata along with storing a longer (but not
/// too long) path name.
///
/// UStar is also the basis used for pax archives.
pub fn new_ustar() -> Header {
let mut header = Header { bytes: [0; 512] };
unsafe {
let gnu = cast_mut::<_, UstarHeader>(&mut header);
gnu.magic = *b"ustar\0";
gnu.version = *b"00";
}
header.set_mtime(0);
header
}
/// Creates a new blank old header.
///
/// This header format is the original archive header format which all other
/// versions are compatible with (e.g. they are a superset). This header
/// format limits the path name limit and isn't able to contain extra
/// metadata like atime/ctime.
pub fn new_old() -> Header {
let mut header = Header { bytes: [0; 512] };
header.set_mtime(0);
header
}
fn is_ustar(&self) -> bool {
let ustar = unsafe { cast::<_, UstarHeader>(self) };
ustar.magic[..] == b"ustar\0"[..] && ustar.version[..] == b"00"[..]
}
fn is_gnu(&self) -> bool {
let ustar = unsafe { cast::<_, UstarHeader>(self) };
ustar.magic[..] == b"ustar "[..] && ustar.version[..] == b" \0"[..]
}
/// View this archive header as a raw "old" archive header.
///
/// This view will always succeed as all archive header formats will fill
/// out at least the fields specified in the old header format.
pub fn as_old(&self) -> &OldHeader {
unsafe { cast(self) }
}
/// Same as `as_old`, but the mutable version.
pub fn as_old_mut(&mut self) -> &mut OldHeader {
unsafe { cast_mut(self) }
}
/// View this archive header as a raw UStar archive header.
///
/// The UStar format is an extension to the tar archive format which enables
/// longer pathnames and a few extra attributes such as the group and user
/// name.
///
/// This cast may not succeed as this function will test whether the
/// magic/version fields of the UStar format have the appropriate values,
/// returning `None` if they aren't correct.
pub fn as_ustar(&self) -> Option<&UstarHeader> {
if self.is_ustar() {
Some(unsafe { cast(self) })
} else {
None
}
}
/// Same as `as_ustar_mut`, but the mutable version.
pub fn as_ustar_mut(&mut self) -> Option<&mut UstarHeader> {
if self.is_ustar() {
Some(unsafe { cast_mut(self) })
} else {
None
}
}
/// View this archive header as a raw GNU archive header.
///
/// The GNU format is an extension to the tar archive format which enables
/// longer pathnames and a few extra attributes such as the group and user
/// name.
///
/// This cast may not succeed as this function will test whether the
/// magic/version fields of the GNU format have the appropriate values,
/// returning `None` if they aren't correct.
pub fn as_gnu(&self) -> Option<&GnuHeader> {
if self.is_gnu() {
Some(unsafe { cast(self) })
} else {
None
}
}
/// Same as `as_gnu`, but the mutable version.
pub fn as_gnu_mut(&mut self) -> Option<&mut GnuHeader> {
if self.is_gnu() {
Some(unsafe { cast_mut(self) })
} else {
None
}
}
/// Treats the given byte slice as a header.
///
/// Panics if the length of the passed slice is not equal to 512.
pub fn from_byte_slice(bytes: &[u8]) -> &Header {
assert_eq!(bytes.len(), mem::size_of::<Header>());
assert_eq!(mem::align_of_val(bytes), mem::align_of::<Header>());
unsafe { &*(bytes.as_ptr() as *const Header) }
}
/// Returns a view into this header as a byte array.
pub fn as_bytes(&self) -> &[u8; 512] {
&self.bytes
}
/// Returns a view into this header as a byte array.
pub fn as_mut_bytes(&mut self) -> &mut [u8; 512] {
&mut self.bytes
}
/// 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. By default, this will use `HeaderMode::Complete` to include all
/// metadata.
pub fn set_metadata(&mut self, meta: &fs::Metadata) {
self.fill_from(meta, HeaderMode::Complete);
}
/// Sets only the metadata relevant to the given HeaderMode in this header
/// from the metadata argument provided.
pub fn set_metadata_in_mode(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
self.fill_from(meta, mode);
}
/// Returns the size of entry's data this header represents.
///
/// This is different from `Header::size` for sparse files, which have
/// some longer `size()` but shorter `entry_size()`. The `entry_size()`
/// listed here should be the number of bytes in the archive this header
/// describes.
///
/// May return an error if the field is corrupted.
pub fn entry_size(&self) -> io::Result<u64> {
num_field_wrapper_from(&self.as_old().size).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting size for {}", err, self.path_lossy()),
)
})
}
/// Returns the file size this header represents.
///
/// May return an error if the field is corrupted.
pub fn size(&self) -> io::Result<u64> {
if self.entry_type().is_gnu_sparse() {
self.as_gnu()
.ok_or_else(|| other("sparse header was not a gnu header"))
.and_then(|h| h.real_size())
} else {
self.entry_size()
}
}
/// Encodes the `size` argument into the size field of this header.
pub fn set_size(&mut self, size: u64) {
num_field_wrapper_into(&mut self.as_old_mut().size, size);
}
/// Returns the raw path name 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>> {
bytes2path(self.path_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 let Some(ustar) = self.as_ustar() {
ustar.path_bytes()
} else {
let name = truncate(&self.as_old().name);
Cow::Borrowed(name)
}
}
/// Gets the path in a "lossy" way, used for error reporting ONLY.
fn path_lossy(&self) -> String {
String::from_utf8_lossy(&self.path_bytes()).to_string()
}
/// 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. Will
/// strip out any "." path component, which signifies the current directory.
///
/// Note: This function does not support names over 100 bytes, or paths
/// over 255 bytes, even for formats that support longer names. Instead,
/// use `Builder` methods to insert a long-name extension at the same time
/// as the file content.
pub fn set_path<P: AsRef<Path>>(&mut self, p: P) -> io::Result<()> {
self._set_path(p.as_ref())
}
fn _set_path(&mut self, path: &Path) -> io::Result<()> {
if let Some(ustar) = self.as_ustar_mut() {
return ustar.set_path(path);
}
copy_path_into(&mut self.as_old_mut().name, path, false).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when setting path for {}", err, self.path_lossy()),
)
})
}
/// Returns the link name stored in this header, if any is found.
///
/// This method may fail if the pathname is not valid Unicode and this is
/// called on a Windows platform. `Ok(None)` being returned, however,
/// indicates that the link name was not present.
///
/// Note that this function will convert any `\` characters to directory
/// separators.
pub fn link_name(&self) -> io::Result<Option<Cow<Path>>> {
match self.link_name_bytes() {
Some(bytes) => bytes2path(bytes).map(Some),
None => Ok(None),
}
}
/// Returns the link name stored in this header as a byte array, if any.
///
/// This function is guaranteed to succeed, but you may wish to call the
/// `link_name` method to convert to a `Path`.
///
/// Note that this function will convert any `\` characters to directory
/// separators.
pub fn link_name_bytes(&self) -> Option<Cow<[u8]>> {
let old = self.as_old();
if old.linkname[0] != 0 {
Some(Cow::Borrowed(truncate(&old.linkname)))
} else {
None
}
}
/// Sets the link name for this header.
///
/// This function will set the linkname listed in this header, encoding it
/// in the appropriate format. May fail if the link name is too long or if
/// the path specified is not Unicode and this is a Windows platform. Will
/// strip out any "." path component, which signifies the current directory.
///
/// To use GNU long link names, prefer instead [`crate::Builder::append_link`].
pub fn set_link_name<P: AsRef<Path>>(&mut self, p: P) -> io::Result<()> {
self._set_link_name(p.as_ref())
}
fn _set_link_name(&mut self, path: &Path) -> io::Result<()> {
copy_path_into(&mut self.as_old_mut().linkname, path, true).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when setting link name for {}", err, self.path_lossy()),
)
})
}
/// Sets the link name for this header without any transformation.
///
/// This function is like [`Self::set_link_name`] but accepts an arbitrary byte array.
/// Hence it will not perform any canonicalization, such as replacing duplicate `//` with `/`.
pub fn set_link_name_literal<P: AsRef<[u8]>>(&mut self, p: P) -> io::Result<()> {
self._set_link_name_literal(p.as_ref())
}
fn _set_link_name_literal(&mut self, bytes: &[u8]) -> io::Result<()> {
copy_into(&mut self.as_old_mut().linkname, 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.as_old().mode)
.map(|u| u as u32)
.map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting mode for {}", err, self.path_lossy()),
)
})
}
/// Encodes the `mode` provided into this header.
pub fn set_mode(&mut self, mode: u32) {
octal_into(&mut self.as_old_mut().mode, mode);
}
/// 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<u64> {
num_field_wrapper_from(&self.as_old().uid)
.map(|u| u as u64)
.map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting uid for {}", err, self.path_lossy()),
)
})
}
/// Encodes the `uid` provided into this header.
pub fn set_uid(&mut self, uid: u64) {
num_field_wrapper_into(&mut self.as_old_mut().uid, uid);
}
/// Returns the value of the group's user ID field
pub fn gid(&self) -> io::Result<u64> {
num_field_wrapper_from(&self.as_old().gid)
.map(|u| u as u64)
.map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting gid for {}", err, self.path_lossy()),
)
})
}
/// Encodes the `gid` provided into this header.
pub fn set_gid(&mut self, gid: u64) {
num_field_wrapper_into(&mut self.as_old_mut().gid, gid);
}
/// Returns the last modification time in Unix time format
pub fn mtime(&self) -> io::Result<u64> {
num_field_wrapper_from(&self.as_old().mtime).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting mtime for {}", err, self.path_lossy()),
)
})
}
/// 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) {
num_field_wrapper_into(&mut self.as_old_mut().mtime, mtime);
}
/// Return the user name of the owner of this file.
///
/// A return value of `Ok(Some(..))` indicates that the user name was
/// present and was valid utf-8, `Ok(None)` indicates that the user name is
/// not present in this archive format, and `Err` indicates that the user
/// name was present but was not valid utf-8.
pub fn username(&self) -> Result<Option<&str>, str::Utf8Error> {
match self.username_bytes() {
Some(bytes) => str::from_utf8(bytes).map(Some),
None => Ok(None),
}
}
/// Returns the user name of the owner of this file, if present.
///
/// A return value of `None` indicates that the user name is not present in
/// this header format.
pub fn username_bytes(&self) -> Option<&[u8]> {
if let Some(ustar) = self.as_ustar() {
Some(ustar.username_bytes())
} else if let Some(gnu) = self.as_gnu() {
Some(gnu.username_bytes())
} else {
None
}
}
/// Sets the username inside this header.
///
/// This function will return an error if this header format cannot encode a
/// user name or the name is too long.
pub fn set_username(&mut self, name: &str) -> io::Result<()> {
if let Some(ustar) = self.as_ustar_mut() {
return ustar.set_username(name);
}
if let Some(gnu) = self.as_gnu_mut() {
gnu.set_username(name)
} else {
Err(other("not a ustar or gnu archive, cannot set username"))
}
}
/// Return the group name of the owner of this file.
///
/// A return value of `Ok(Some(..))` indicates that the group name was
/// present and was valid utf-8, `Ok(None)` indicates that the group name is
/// not present in this archive format, and `Err` indicates that the group
/// name was present but was not valid utf-8.
pub fn groupname(&self) -> Result<Option<&str>, str::Utf8Error> {
match self.groupname_bytes() {
Some(bytes) => str::from_utf8(bytes).map(Some),
None => Ok(None),
}
}
/// Returns the group name of the owner of this file, if present.
///
/// A return value of `None` indicates that the group name is not present in
/// this header format.
pub fn groupname_bytes(&self) -> Option<&[u8]> {
if let Some(ustar) = self.as_ustar() {
Some(ustar.groupname_bytes())
} else if let Some(gnu) = self.as_gnu() {
Some(gnu.groupname_bytes())
} else {
None
}
}
/// Sets the group name inside this header.
///
/// This function will return an error if this header format cannot encode a
/// group name or the name is too long.
pub fn set_groupname(&mut self, name: &str) -> io::Result<()> {
if let Some(ustar) = self.as_ustar_mut() {
return ustar.set_groupname(name);
}
if let Some(gnu) = self.as_gnu_mut() {
gnu.set_groupname(name)
} else {
Err(other("not a ustar or gnu archive, cannot set groupname"))
}
}
/// Returns the device major number, if present.
///
/// This field may not be present in all archives, and it may not be
/// correctly formed in all archives. `Ok(Some(..))` means it was present
/// and correctly decoded, `Ok(None)` indicates that this header format does
/// not include the device major number, and `Err` indicates that it was
/// present and failed to decode.
pub fn device_major(&self) -> io::Result<Option<u32>> {
if let Some(ustar) = self.as_ustar() {
ustar.device_major().map(Some)
} else if let Some(gnu) = self.as_gnu() {
gnu.device_major().map(Some)
} else {
Ok(None)
}
}
/// Encodes the value `major` into the dev_major field of this header.
///
/// This function will return an error if this header format cannot encode a
/// major device number.
pub fn set_device_major(&mut self, major: u32) -> io::Result<()> {
if let Some(ustar) = self.as_ustar_mut() {
ustar.set_device_major(major);
Ok(())
} else if let Some(gnu) = self.as_gnu_mut() {
gnu.set_device_major(major);
Ok(())
} else {
Err(other("not a ustar or gnu archive, cannot set dev_major"))
}
}
/// Returns the device minor number, if present.
///
/// This field may not be present in all archives, and it may not be
/// correctly formed in all archives. `Ok(Some(..))` means it was present
/// and correctly decoded, `Ok(None)` indicates that this header format does
/// not include the device minor number, and `Err` indicates that it was
/// present and failed to decode.
pub fn device_minor(&self) -> io::Result<Option<u32>> {
if let Some(ustar) = self.as_ustar() {
ustar.device_minor().map(Some)
} else if let Some(gnu) = self.as_gnu() {
gnu.device_minor().map(Some)
} else {
Ok(None)
}
}
/// Encodes the value `minor` into the dev_minor field of this header.
///
/// This function will return an error if this header format cannot encode a
/// minor device number.
pub fn set_device_minor(&mut self, minor: u32) -> io::Result<()> {
if let Some(ustar) = self.as_ustar_mut() {
ustar.set_device_minor(minor);
Ok(())
} else if let Some(gnu) = self.as_gnu_mut() {
gnu.set_device_minor(minor);
Ok(())
} else {
Err(other("not a ustar or gnu archive, cannot set dev_minor"))
}
}
/// Returns the type of file described by this header.
pub fn entry_type(&self) -> EntryType {
EntryType::new(self.as_old().linkflag[0])
}
/// Sets the type of file that will be described by this header.
pub fn set_entry_type(&mut self, ty: EntryType) {
self.as_old_mut().linkflag = [ty.as_byte()];
}
/// 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.as_old().cksum)
.map(|u| u as u32)
.map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting cksum for {}", err, self.path_lossy()),
)
})
}
/// Sets the checksum field of this header based on the current fields in
/// this header.
pub fn set_cksum(&mut self) {
let cksum = self.calculate_cksum();
octal_into(&mut self.as_old_mut().cksum, cksum);
}
fn calculate_cksum(&self) -> u32 {
let old = self.as_old();
let start = old as *const _ as usize;
let cksum_start = old.cksum.as_ptr() as *const _ as usize;
let offset = cksum_start - start;
let len = old.cksum.len();
self.bytes[0..offset]
.iter()
.chain(iter::repeat(&b' ').take(len))
.chain(&self.bytes[offset + len..])
.fold(0, |a, b| a + (*b as u32))
}
fn fill_from(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
self.fill_platform_from(meta, mode);
// Set size of directories to zero
self.set_size(if meta.is_dir() || meta.file_type().is_symlink() {
0
} else {
meta.len()
});
if let Some(ustar) = self.as_ustar_mut() {
ustar.set_device_major(0);
ustar.set_device_minor(0);
}
if let Some(gnu) = self.as_gnu_mut() {
gnu.set_device_major(0);
gnu.set_device_minor(0);
}
}
#[cfg(target_arch = "wasm32")]
#[allow(unused_variables)]
fn fill_platform_from(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
unimplemented!();
}
#[cfg(unix)]
fn fill_platform_from(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
match mode {
HeaderMode::Complete => {
self.set_mtime(meta.mtime() as u64);
self.set_uid(meta.uid() as u64);
self.set_gid(meta.gid() as u64);
self.set_mode(meta.mode() as u32);
}
HeaderMode::Deterministic => {
// We could in theory set the mtime to zero here, but not all
// tools seem to behave well when ingesting files with a 0
// timestamp. For example rust-lang/cargo#9512 shows that lldb
// doesn't ingest files with a zero timestamp correctly.
//
// We just need things to be deterministic here so just pick
// something that isn't zero. This time, chosen after careful
// deliberation, corresponds to Jul 23, 2006 -- the date of the
// first commit for what would become Rust.
self.set_mtime(1153704088);
self.set_uid(0);
self.set_gid(0);
// Use a default umask value, but propagate the (user) execute bit.
let fs_mode = if meta.is_dir() || (0o100 & meta.mode() == 0o100) {
0o755
} else {
0o644
};
self.set_mode(fs_mode);
}
}
// Note that if we are a GNU header we *could* set atime/ctime, except
// the `tar` utility doesn't do that by default and it causes problems
// with 7-zip [1].
//
// It's always possible to fill them out manually, so we just don't fill
// it out automatically here.
//
// [1]: https://github.com/alexcrichton/tar-rs/issues/70
// TODO: need to bind more file types
self.set_entry_type(entry_type(meta.mode()));
fn entry_type(mode: u32) -> EntryType {
match mode as libc::mode_t & libc::S_IFMT {
libc::S_IFREG => EntryType::file(),
libc::S_IFLNK => EntryType::symlink(),
libc::S_IFCHR => EntryType::character_special(),
libc::S_IFBLK => EntryType::block_special(),
libc::S_IFDIR => EntryType::dir(),
libc::S_IFIFO => EntryType::fifo(),
_ => EntryType::new(b' '),
}
}
}
#[cfg(windows)]
fn fill_platform_from(&mut self, meta: &fs::Metadata, mode: HeaderMode) {
// There's no concept of a file mode on Windows, so do a best approximation here.
match mode {
HeaderMode::Complete => {
self.set_uid(0);
self.set_gid(0);
// 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);
let fs_mode = {
const FILE_ATTRIBUTE_READONLY: u32 = 0x00000001;
let readonly = meta.file_attributes() & FILE_ATTRIBUTE_READONLY;
match (meta.is_dir(), readonly != 0) {
(true, false) => 0o755,
(true, true) => 0o555,
(false, false) => 0o644,
(false, true) => 0o444,
}
};
self.set_mode(fs_mode);
}
HeaderMode::Deterministic => {
self.set_uid(0);
self.set_gid(0);
self.set_mtime(123456789); // see above in unix
let fs_mode = if meta.is_dir() { 0o755 } else { 0o644 };
self.set_mode(fs_mode);
}
}
let ft = meta.file_type();
self.set_entry_type(if ft.is_dir() {
EntryType::dir()
} else if ft.is_file() {
EntryType::file()
} else if ft.is_symlink() {
EntryType::symlink()
} else {
EntryType::new(b' ')
});
}
fn debug_fields(&self, b: &mut fmt::DebugStruct) {
if let Ok(entry_size) = self.entry_size() {
b.field("entry_size", &entry_size);
}
if let Ok(size) = self.size() {
b.field("size", &size);
}
if let Ok(path) = self.path() {
b.field("path", &path);
}
if let Ok(link_name) = self.link_name() {
b.field("link_name", &link_name);
}
if let Ok(mode) = self.mode() {
b.field("mode", &DebugAsOctal(mode));
}
if let Ok(uid) = self.uid() {
b.field("uid", &uid);
}
if let Ok(gid) = self.gid() {
b.field("gid", &gid);
}
if let Ok(mtime) = self.mtime() {
b.field("mtime", &mtime);
}
if let Ok(username) = self.username() {
b.field("username", &username);
}
if let Ok(groupname) = self.groupname() {
b.field("groupname", &groupname);
}
if let Ok(device_major) = self.device_major() {
b.field("device_major", &device_major);
}
if let Ok(device_minor) = self.device_minor() {
b.field("device_minor", &device_minor);
}
if let Ok(cksum) = self.cksum() {
b.field("cksum", &cksum);
b.field("cksum_valid", &(cksum == self.calculate_cksum()));
}
}
}
struct DebugAsOctal<T>(T);
impl<T: fmt::Octal> fmt::Debug for DebugAsOctal<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Octal::fmt(&self.0, f)
}
}
unsafe fn cast<T, U>(a: &T) -> &U {
assert_eq!(mem::size_of_val(a), mem::size_of::<U>());
assert_eq!(mem::align_of_val(a), mem::align_of::<U>());
&*(a as *const T as *const U)
}
unsafe fn cast_mut<T, U>(a: &mut T) -> &mut U {
assert_eq!(mem::size_of_val(a), mem::size_of::<U>());
assert_eq!(mem::align_of_val(a), mem::align_of::<U>());
&mut *(a as *mut T as *mut U)
}
impl Clone for Header {
fn clone(&self) -> Header {
Header { bytes: self.bytes }
}
}
impl fmt::Debug for Header {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(me) = self.as_ustar() {
me.fmt(f)
} else if let Some(me) = self.as_gnu() {
me.fmt(f)
} else {
self.as_old().fmt(f)
}
}
}
impl OldHeader {
/// Views this as a normal `Header`
pub fn as_header(&self) -> &Header {
unsafe { cast(self) }
}
/// Views this as a normal `Header`
pub fn as_header_mut(&mut self) -> &mut Header {
unsafe { cast_mut(self) }
}
}
impl fmt::Debug for OldHeader {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut f = f.debug_struct("OldHeader");
self.as_header().debug_fields(&mut f);
f.finish()
}
}
impl UstarHeader {
/// See `Header::path_bytes`
pub fn path_bytes(&self) -> Cow<[u8]> {
if self.prefix[0] == 0 && !self.name.contains(&b'\\') {
Cow::Borrowed(truncate(&self.name))
} else {
let mut bytes = Vec::new();
let prefix = truncate(&self.prefix);
if !prefix.is_empty() {
bytes.extend_from_slice(prefix);
bytes.push(b'/');
}
bytes.extend_from_slice(truncate(&self.name));
Cow::Owned(bytes)
}
}
/// Gets the path in a "lossy" way, used for error reporting ONLY.
fn path_lossy(&self) -> String {
String::from_utf8_lossy(&self.path_bytes()).to_string()
}
/// See `Header::set_path`
pub fn set_path<P: AsRef<Path>>(&mut self, p: P) -> io::Result<()> {
self._set_path(p.as_ref())
}
fn _set_path(&mut self, path: &Path) -> io::Result<()> {
// This can probably be optimized quite a bit more, but for now just do
// something that's relatively easy and readable.
//
// First up, if the path fits within `self.name` then we just shove it
// in there. If not then we try to split it between some existing path
// components where it can fit in name/prefix. To do that we peel off
// enough until the path fits in `prefix`, then we try to put both
// halves into their destination.
let bytes = path2bytes(path)?;
let (maxnamelen, maxprefixlen) = (self.name.len(), self.prefix.len());
if bytes.len() <= maxnamelen {
copy_path_into(&mut self.name, path, false).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when setting path for {}", err, self.path_lossy()),
)
})?;
} else {
let mut prefix = path;
let mut prefixlen;
loop {
match prefix.parent() {
Some(parent) => prefix = parent,
None => {
return Err(other(&format!(
"path cannot be split to be inserted into archive: {}",
path.display()
)));
}
}
prefixlen = path2bytes(prefix)?.len();
if prefixlen <= maxprefixlen {
break;
}
}
copy_path_into(&mut self.prefix, prefix, false).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when setting path for {}", err, self.path_lossy()),
)
})?;
let path = bytes2path(Cow::Borrowed(&bytes[prefixlen + 1..]))?;
copy_path_into(&mut self.name, &path, false).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when setting path for {}", err, self.path_lossy()),
)
})?;
}
Ok(())
}
/// See `Header::username_bytes`
pub fn username_bytes(&self) -> &[u8] {
truncate(&self.uname)
}
/// See `Header::set_username`
pub fn set_username(&mut self, name: &str) -> io::Result<()> {
copy_into(&mut self.uname, name.as_bytes()).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when setting username for {}", err, self.path_lossy()),
)
})
}
/// See `Header::groupname_bytes`
pub fn groupname_bytes(&self) -> &[u8] {
truncate(&self.gname)
}
/// See `Header::set_groupname`
pub fn set_groupname(&mut self, name: &str) -> io::Result<()> {
copy_into(&mut self.gname, name.as_bytes()).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when setting groupname for {}", err, self.path_lossy()),
)
})
}
/// See `Header::device_major`
pub fn device_major(&self) -> io::Result<u32> {
octal_from(&self.dev_major)
.map(|u| u as u32)
.map_err(|err| {
io::Error::new(
err.kind(),
format!(
"{} when getting device_major for {}",
err,
self.path_lossy()
),
)
})
}
/// See `Header::set_device_major`
pub fn set_device_major(&mut self, major: u32) {
octal_into(&mut self.dev_major, major);
}
/// See `Header::device_minor`
pub fn device_minor(&self) -> io::Result<u32> {
octal_from(&self.dev_minor)
.map(|u| u as u32)
.map_err(|err| {
io::Error::new(
err.kind(),
format!(
"{} when getting device_minor for {}",
err,
self.path_lossy()
),
)
})
}
/// See `Header::set_device_minor`
pub fn set_device_minor(&mut self, minor: u32) {
octal_into(&mut self.dev_minor, minor);
}
/// Views this as a normal `Header`
pub fn as_header(&self) -> &Header {
unsafe { cast(self) }
}
/// Views this as a normal `Header`
pub fn as_header_mut(&mut self) -> &mut Header {
unsafe { cast_mut(self) }
}
}
impl fmt::Debug for UstarHeader {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut f = f.debug_struct("UstarHeader");
self.as_header().debug_fields(&mut f);
f.finish()
}
}
impl GnuHeader {
/// See `Header::username_bytes`
pub fn username_bytes(&self) -> &[u8] {
truncate(&self.uname)
}
/// Gets the fullname (group:user) in a "lossy" way, used for error reporting ONLY.
fn fullname_lossy(&self) -> String {
format!(
"{}:{}",
String::from_utf8_lossy(self.groupname_bytes()),
String::from_utf8_lossy(self.username_bytes()),
)
}
/// See `Header::set_username`
pub fn set_username(&mut self, name: &str) -> io::Result<()> {
copy_into(&mut self.uname, name.as_bytes()).map_err(|err| {
io::Error::new(
err.kind(),
format!(
"{} when setting username for {}",
err,
self.fullname_lossy()
),
)
})
}
/// See `Header::groupname_bytes`
pub fn groupname_bytes(&self) -> &[u8] {
truncate(&self.gname)
}
/// See `Header::set_groupname`
pub fn set_groupname(&mut self, name: &str) -> io::Result<()> {
copy_into(&mut self.gname, name.as_bytes()).map_err(|err| {
io::Error::new(
err.kind(),
format!(
"{} when setting groupname for {}",
err,
self.fullname_lossy()
),
)
})
}
/// See `Header::device_major`
pub fn device_major(&self) -> io::Result<u32> {
octal_from(&self.dev_major)
.map(|u| u as u32)
.map_err(|err| {
io::Error::new(
err.kind(),
format!(
"{} when getting device_major for {}",
err,
self.fullname_lossy()
),
)
})
}
/// See `Header::set_device_major`
pub fn set_device_major(&mut self, major: u32) {
octal_into(&mut self.dev_major, major);
}
/// See `Header::device_minor`
pub fn device_minor(&self) -> io::Result<u32> {
octal_from(&self.dev_minor)
.map(|u| u as u32)
.map_err(|err| {
io::Error::new(
err.kind(),
format!(
"{} when getting device_minor for {}",
err,
self.fullname_lossy()
),
)
})
}
/// See `Header::set_device_minor`
pub fn set_device_minor(&mut self, minor: u32) {
octal_into(&mut self.dev_minor, minor);
}
/// Returns the last modification time in Unix time format
pub fn atime(&self) -> io::Result<u64> {
num_field_wrapper_from(&self.atime).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting atime for {}", err, self.fullname_lossy()),
)
})
}
/// Encodes the `atime` provided into this header.
///
/// Note that this time is typically a number of seconds passed since
/// January 1, 1970.
pub fn set_atime(&mut self, atime: u64) {
num_field_wrapper_into(&mut self.atime, atime);
}
/// Returns the last modification time in Unix time format
pub fn ctime(&self) -> io::Result<u64> {
num_field_wrapper_from(&self.ctime).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting ctime for {}", err, self.fullname_lossy()),
)
})
}
/// Encodes the `ctime` provided into this header.
///
/// Note that this time is typically a number of seconds passed since
/// January 1, 1970.
pub fn set_ctime(&mut self, ctime: u64) {
num_field_wrapper_into(&mut self.ctime, ctime);
}
/// Returns the "real size" of the file this header represents.
///
/// This is applicable for sparse files where the returned size here is the
/// size of the entire file after the sparse regions have been filled in.
pub fn real_size(&self) -> io::Result<u64> {
octal_from(&self.realsize).map_err(|err| {
io::Error::new(
err.kind(),
format!(
"{} when getting real_size for {}",
err,
self.fullname_lossy()
),
)
})
}
/// Indicates whether this header will be followed by additional
/// sparse-header records.
///
/// Note that this is handled internally by this library, and is likely only
/// interesting if a `raw` iterator is being used.
pub fn is_extended(&self) -> bool {
self.isextended[0] == 1
}
/// Views this as a normal `Header`
pub fn as_header(&self) -> &Header {
unsafe { cast(self) }
}
/// Views this as a normal `Header`
pub fn as_header_mut(&mut self) -> &mut Header {
unsafe { cast_mut(self) }
}
}
impl fmt::Debug for GnuHeader {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut f = f.debug_struct("GnuHeader");
self.as_header().debug_fields(&mut f);
if let Ok(atime) = self.atime() {
f.field("atime", &atime);
}
if let Ok(ctime) = self.ctime() {
f.field("ctime", &ctime);
}
f.field("is_extended", &self.is_extended())
.field("sparse", &DebugSparseHeaders(&self.sparse))
.finish()
}
}
struct DebugSparseHeaders<'a>(&'a [GnuSparseHeader]);
impl<'a> fmt::Debug for DebugSparseHeaders<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut f = f.debug_list();
for header in self.0 {
if !header.is_empty() {
f.entry(header);
}
}
f.finish()
}
}
impl GnuSparseHeader {
/// Returns true if block is empty
pub fn is_empty(&self) -> bool {
self.offset[0] == 0 || self.numbytes[0] == 0
}
/// Offset of the block from the start of the file
///
/// Returns `Err` for a malformed `offset` field.
pub fn offset(&self) -> io::Result<u64> {
octal_from(&self.offset).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting offset from sparse header", err),
)
})
}
/// Length of the block
///
/// Returns `Err` for a malformed `numbytes` field.
pub fn length(&self) -> io::Result<u64> {
octal_from(&self.numbytes).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting length from sparse header", err),
)
})
}
}
impl fmt::Debug for GnuSparseHeader {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut f = f.debug_struct("GnuSparseHeader");
if let Ok(offset) = self.offset() {
f.field("offset", &offset);
}
if let Ok(length) = self.length() {
f.field("length", &length);
}
f.finish()
}
}
impl GnuExtSparseHeader {
/// Crates a new zero'd out sparse header entry.
pub fn new() -> GnuExtSparseHeader {
unsafe { mem::zeroed() }
}
/// Returns a view into this header as a byte array.
pub fn as_bytes(&self) -> &[u8; 512] {
debug_assert_eq!(mem::size_of_val(self), 512);
unsafe { mem::transmute(self) }
}
/// Returns a view into this header as a byte array.
pub fn as_mut_bytes(&mut self) -> &mut [u8; 512] {
debug_assert_eq!(mem::size_of_val(self), 512);
unsafe { mem::transmute(self) }
}
/// Returns a slice of the underlying sparse headers.
///
/// Some headers may represent empty chunks of both the offset and numbytes
/// fields are 0.
pub fn sparse(&self) -> &[GnuSparseHeader; 21] {
&self.sparse
}
/// Indicates if another sparse header should be following this one.
pub fn is_extended(&self) -> bool {
self.isextended[0] == 1
}
}
impl Default for GnuExtSparseHeader {
fn default() -> Self {
Self::new()
}
}
fn octal_from(slice: &[u8]) -> io::Result<u64> {
let trun = truncate(slice);
let num = match str::from_utf8(trun) {
Ok(n) => n,
Err(_) => {
return Err(other(&format!(
"numeric field did not have utf-8 text: {}",
String::from_utf8_lossy(trun)
)));
}
};
match u64::from_str_radix(num.trim(), 8) {
Ok(n) => Ok(n),
Err(_) => Err(other(&format!("numeric field was not a number: {}", num))),
}
}
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;
}
}
// Wrapper to figure out if we should fill the header field using tar's numeric
// extension (binary) or not (octal).
fn num_field_wrapper_into(dst: &mut [u8], src: u64) {
if src >= 8589934592 || (src >= 2097152 && dst.len() == 8) {
numeric_extended_into(dst, src);
} else {
octal_into(dst, src);
}
}
// Wrapper to figure out if we should read the header field in binary (numeric
// extension) or octal (standard encoding).
fn num_field_wrapper_from(src: &[u8]) -> io::Result<u64> {
if src[0] & 0x80 != 0 {
Ok(numeric_extended_from(src))
} else {
octal_from(src)
}
}
// When writing numeric fields with is the extended form, the high bit of the
// first byte is set to 1 and the remainder of the field is treated as binary
// instead of octal ascii.
// This handles writing u64 to 8 (uid, gid) or 12 (size, *time) bytes array.
fn numeric_extended_into(dst: &mut [u8], src: u64) {
let len: usize = dst.len();
for (slot, val) in dst.iter_mut().zip(
repeat(0)
.take(len - 8) // to zero init extra bytes
.chain((0..8).rev().map(|x| ((src >> (8 * x)) & 0xff) as u8)),
) {
*slot = val;
}
dst[0] |= 0x80;
}
fn numeric_extended_from(src: &[u8]) -> u64 {
let mut dst: u64 = 0;
let mut b_to_skip = 1;
if src.len() == 8 {
// read first byte without extension flag bit
dst = (src[0] ^ 0x80) as u64;
} else {
// only read last 8 bytes
b_to_skip = src.len() - 8;
}
for byte in src.iter().skip(b_to_skip) {
dst <<= 8;
dst |= *byte as u64;
}
dst
}
fn truncate(slice: &[u8]) -> &[u8] {
match slice.iter().position(|i| *i == 0) {
Some(i) => &slice[..i],
None => slice,
}
}
/// Copies `bytes` into the `slot` provided, returning an error if the `bytes`
/// array is too long or if it contains any nul bytes.
fn copy_into(slot: &mut [u8], bytes: &[u8]) -> io::Result<()> {
if bytes.len() > slot.len() {
Err(other("provided value is too long"))
} else if bytes.iter().any(|b| *b == 0) {
Err(other("provided value contains a nul byte"))
} else {
for (slot, val) in slot.iter_mut().zip(bytes.iter().chain(Some(&0))) {
*slot = *val;
}
Ok(())
}
}
/// Copies `path` into the `slot` provided
///
/// Returns an error if:
///
/// * the path is too long to fit
/// * a nul byte was found
/// * an invalid path component is encountered (e.g. a root path or parent dir)
/// * the path itself is empty
fn copy_path_into(mut slot: &mut [u8], path: &Path, is_link_name: bool) -> io::Result<()> {
let mut emitted = false;
let mut needs_slash = false;
for component in path.components() {
let bytes = path2bytes(Path::new(component.as_os_str()))?;
match (component, is_link_name) {
(Component::Prefix(..), false) | (Component::RootDir, false) => {
return Err(other("paths in archives must be relative"));
}
(Component::ParentDir, false) => {
return Err(other("paths in archives must not have `..`"));
}
// Allow "./" as the path
(Component::CurDir, false) if path.components().count() == 1 => {}
(Component::CurDir, false) => continue,
(Component::Normal(_), _) | (_, true) => {}
};
if needs_slash {
copy(&mut slot, b"/")?;
}
if bytes.contains(&b'/') {
if let Component::Normal(..) = component {
return Err(other("path component in archive cannot contain `/`"));
}
}
copy(&mut slot, &*bytes)?;
if &*bytes != b"/" {
needs_slash = true;
}
emitted = true;
}
if !emitted {
return Err(other("paths in archives must have at least one component"));
}
if ends_with_slash(path) {
copy(&mut slot, &[b'/'])?;
}
return Ok(());
fn copy(slot: &mut &mut [u8], bytes: &[u8]) -> io::Result<()> {
copy_into(*slot, bytes)?;
let tmp = mem::replace(slot, &mut []);
*slot = &mut tmp[bytes.len()..];
Ok(())
}
}
#[cfg(target_arch = "wasm32")]
fn ends_with_slash(p: &Path) -> bool {
p.to_string_lossy().ends_with('/')
}
#[cfg(windows)]
fn ends_with_slash(p: &Path) -> bool {
let last = p.as_os_str().encode_wide().last();
last == Some(b'/' as u16) || last == Some(b'\\' as u16)
}
#[cfg(unix)]
fn ends_with_slash(p: &Path) -> bool {
p.as_os_str().as_bytes().ends_with(&[b'/'])
}
#[cfg(any(windows, target_arch = "wasm32"))]
pub fn path2bytes(p: &Path) -> io::Result<Cow<[u8]>> {
p.as_os_str()
.to_str()
.map(|s| s.as_bytes())
.ok_or_else(|| other(&format!("path {} was not valid Unicode", p.display())))
.map(|bytes| {
if bytes.contains(&b'\\') {
// Normalize to Unix-style path separators
let mut bytes = bytes.to_owned();
for b in &mut bytes {
if *b == b'\\' {
*b = b'/';
}
}
Cow::Owned(bytes)
} else {
Cow::Borrowed(bytes)
}
})
}
#[cfg(unix)]
/// On unix this will never fail
pub fn path2bytes(p: &Path) -> io::Result<Cow<[u8]>> {
Ok(p.as_os_str().as_bytes()).map(Cow::Borrowed)
}
#[cfg(windows)]
/// On windows we cannot accept non-Unicode bytes because it
/// is impossible to convert it to UTF-16.
pub fn bytes2path(bytes: Cow<[u8]>) -> io::Result<Cow<Path>> {
return match bytes {
Cow::Borrowed(bytes) => {
let s = str::from_utf8(bytes).map_err(|_| not_unicode(bytes))?;
Ok(Cow::Borrowed(Path::new(s)))
}
Cow::Owned(bytes) => {
let s = String::from_utf8(bytes).map_err(|uerr| not_unicode(&uerr.into_bytes()))?;
Ok(Cow::Owned(PathBuf::from(s)))
}
};
fn not_unicode(v: &[u8]) -> io::Error {
other(&format!(
"only Unicode paths are supported on Windows: {}",
String::from_utf8_lossy(v)
))
}
}
#[cfg(unix)]
/// On unix this operation can never fail.
pub fn bytes2path(bytes: Cow<[u8]>) -> io::Result<Cow<Path>> {
use std::ffi::{OsStr, OsString};
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))),
})
}
#[cfg(target_arch = "wasm32")]
pub fn bytes2path(bytes: Cow<[u8]>) -> io::Result<Cow<Path>> {
Ok(match bytes {
Cow::Borrowed(bytes) => {
Cow::Borrowed({ Path::new(str::from_utf8(bytes).map_err(invalid_utf8)?) })
}
Cow::Owned(bytes) => {
Cow::Owned({ PathBuf::from(String::from_utf8(bytes).map_err(invalid_utf8)?) })
}
})
}
#[cfg(target_arch = "wasm32")]
fn invalid_utf8<T>(_: T) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, "Invalid utf-8")
}