001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.compress.archivers.tar; 020 021import static java.nio.charset.StandardCharsets.UTF_8; 022 023import java.io.File; 024import java.io.IOException; 025import java.io.OutputStream; 026import java.io.StringWriter; 027import java.math.BigDecimal; 028import java.math.RoundingMode; 029import java.nio.ByteBuffer; 030import java.nio.charset.StandardCharsets; 031import java.nio.file.LinkOption; 032import java.nio.file.Path; 033import java.nio.file.attribute.FileTime; 034import java.time.Instant; 035import java.util.HashMap; 036import java.util.Map; 037 038import org.apache.commons.compress.archivers.ArchiveOutputStream; 039import org.apache.commons.compress.archivers.zip.ZipEncoding; 040import org.apache.commons.compress.archivers.zip.ZipEncodingHelper; 041import org.apache.commons.compress.utils.FixedLengthBlockOutputStream; 042import org.apache.commons.io.Charsets; 043import org.apache.commons.io.file.attribute.FileTimes; 044import org.apache.commons.io.output.CountingOutputStream; 045import org.apache.commons.lang3.ArrayFill; 046 047/** 048 * The TarOutputStream writes a Unix tar archive as an OutputStream. Methods are provided to put entries, and then write their contents by writing to this 049 * stream using write(). 050 * 051 * <p> 052 * tar archives consist of a sequence of records of 512 bytes each that are grouped into blocks. Prior to Apache Commons Compress 1.14 it has been possible to 053 * configure a record size different from 512 bytes and arbitrary block sizes. Starting with Compress 1.15 512 is the only valid option for the record size and 054 * the block size must be a multiple of 512. Also the default block size changed from 10240 bytes prior to Compress 1.15 to 512 bytes with Compress 1.15. 055 * </p> 056 * 057 * @NotThreadSafe 058 */ 059public class TarArchiveOutputStream extends ArchiveOutputStream<TarArchiveEntry> { 060 061 /** 062 * Fail if a long file name is required in the archive. 063 */ 064 public static final int LONGFILE_ERROR = 0; 065 066 /** 067 * Long paths will be truncated in the archive. 068 */ 069 public static final int LONGFILE_TRUNCATE = 1; 070 071 /** 072 * GNU tar extensions are used to store long file names in the archive. 073 */ 074 public static final int LONGFILE_GNU = 2; 075 076 /** 077 * POSIX/PAX extensions are used to store long file names in the archive. 078 */ 079 public static final int LONGFILE_POSIX = 3; 080 081 /** 082 * Fail if a big number (for example size > 8GiB) is required in the archive. 083 */ 084 public static final int BIGNUMBER_ERROR = 0; 085 086 /** 087 * star/GNU tar/BSD tar extensions are used to store big number in the archive. 088 */ 089 public static final int BIGNUMBER_STAR = 1; 090 091 /** 092 * POSIX/PAX extensions are used to store big numbers in the archive. 093 */ 094 public static final int BIGNUMBER_POSIX = 2; 095 private static final int RECORD_SIZE = 512; 096 097 private static final ZipEncoding ASCII = ZipEncodingHelper.getZipEncoding(StandardCharsets.US_ASCII); 098 099 private static final int BLOCK_SIZE_UNSPECIFIED = -511; 100 private long currSize; 101 private String currName; 102 private long currBytes; 103 private final byte[] recordBuf; 104 private int longFileMode = LONGFILE_ERROR; 105 private int bigNumberMode = BIGNUMBER_ERROR; 106 107 private long recordsWritten; 108 109 private final int recordsPerBlock; 110 111 /** 112 * Indicates if putArchiveEntry has been called without closeArchiveEntry 113 */ 114 private boolean haveUnclosedEntry; 115 116 private final CountingOutputStream countingOut; 117 118 private final ZipEncoding zipEncoding; 119 120 /** 121 * The provided encoding (for unit tests). 122 */ 123 final String charsetName; 124 125 private boolean addPaxHeadersForNonAsciiNames; 126 127 /** 128 * Constructs a new instance. 129 * 130 * <p> 131 * Uses a block size of 512 bytes. 132 * </p> 133 * 134 * @param os the output stream to use 135 */ 136 public TarArchiveOutputStream(final OutputStream os) { 137 this(os, BLOCK_SIZE_UNSPECIFIED); 138 } 139 140 /** 141 * Constructs a new instance. 142 * 143 * @param os the output stream to use 144 * @param blockSize the block size to use. Must be a multiple of 512 bytes. 145 */ 146 public TarArchiveOutputStream(final OutputStream os, final int blockSize) { 147 this(os, blockSize, null); 148 } 149 150 /** 151 * Constructs a new instance. 152 * 153 * @param os the output stream to use 154 * @param blockSize the block size to use 155 * @param recordSize the record size to use. Must be 512 bytes. 156 * @deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used 157 */ 158 @Deprecated 159 public TarArchiveOutputStream(final OutputStream os, final int blockSize, final int recordSize) { 160 this(os, blockSize, recordSize, null); 161 } 162 163 /** 164 * Constructs a new instance. 165 * 166 * @param os the output stream to use 167 * @param blockSize the block size to use . Must be a multiple of 512 bytes. 168 * @param recordSize the record size to use. Must be 512 bytes. 169 * @param encoding name of the encoding to use for file names 170 * @since 1.4 171 * @deprecated recordSize must always be 512 bytes. An IllegalArgumentException will be thrown if any other value is used. 172 */ 173 @Deprecated 174 public TarArchiveOutputStream(final OutputStream os, final int blockSize, final int recordSize, final String encoding) { 175 this(os, blockSize, encoding); 176 if (recordSize != RECORD_SIZE) { 177 throw new IllegalArgumentException("Tar record size must always be 512 bytes. Attempt to set size of " + recordSize); 178 } 179 180 } 181 182 /** 183 * Constructs a new instance. 184 * 185 * @param os the output stream to use 186 * @param blockSize the block size to use. Must be a multiple of 512 bytes. 187 * @param charset name of the encoding to use for file names 188 * @since 1.4 189 */ 190 public TarArchiveOutputStream(final OutputStream os, final int blockSize, final String charset) { 191 super(os); 192 final int realBlockSize; 193 if (BLOCK_SIZE_UNSPECIFIED == blockSize) { 194 realBlockSize = RECORD_SIZE; 195 } else { 196 realBlockSize = blockSize; 197 } 198 199 if (realBlockSize <= 0 || realBlockSize % RECORD_SIZE != 0) { 200 throw new IllegalArgumentException("Block size must be a multiple of 512 bytes. Attempt to use set size of " + blockSize); 201 } 202 this.out = new FixedLengthBlockOutputStream(countingOut = new CountingOutputStream(os), RECORD_SIZE); 203 this.charsetName = Charsets.toCharset(charset).name(); 204 this.zipEncoding = ZipEncodingHelper.getZipEncoding(charset); 205 206 this.recordBuf = new byte[RECORD_SIZE]; 207 this.recordsPerBlock = realBlockSize / RECORD_SIZE; 208 } 209 210 /** 211 * Constructs a new instance. 212 * 213 * <p> 214 * Uses a block size of 512 bytes. 215 * </p> 216 * 217 * @param os the output stream to use 218 * @param charset name of the encoding to use for file names 219 * @since 1.4 220 */ 221 public TarArchiveOutputStream(final OutputStream os, final String charset) { 222 this(os, BLOCK_SIZE_UNSPECIFIED, charset); 223 } 224 225 private void addFileTimePaxHeader(final Map<String, String> paxHeaders, final String header, final FileTime value) { 226 if (value != null) { 227 final Instant instant = value.toInstant(); 228 final long seconds = instant.getEpochSecond(); 229 final int nanos = instant.getNano(); 230 if (nanos == 0) { 231 paxHeaders.put(header, String.valueOf(seconds)); 232 } else { 233 addInstantPaxHeader(paxHeaders, header, seconds, nanos); 234 } 235 } 236 } 237 238 private void addFileTimePaxHeaderForBigNumber(final Map<String, String> paxHeaders, final String header, final FileTime value, final long maxValue) { 239 if (value != null) { 240 final Instant instant = value.toInstant(); 241 final long seconds = instant.getEpochSecond(); 242 final int nanos = instant.getNano(); 243 if (nanos == 0) { 244 addPaxHeaderForBigNumber(paxHeaders, header, seconds, maxValue); 245 } else { 246 addInstantPaxHeader(paxHeaders, header, seconds, nanos); 247 } 248 } 249 } 250 251 private void addInstantPaxHeader(final Map<String, String> paxHeaders, final String header, final long seconds, final int nanos) { 252 final BigDecimal bdSeconds = BigDecimal.valueOf(seconds); 253 final BigDecimal bdNanos = BigDecimal.valueOf(nanos).movePointLeft(9).setScale(7, RoundingMode.DOWN); 254 final BigDecimal timestamp = bdSeconds.add(bdNanos); 255 paxHeaders.put(header, timestamp.toPlainString()); 256 } 257 258 private void addPaxHeaderForBigNumber(final Map<String, String> paxHeaders, final String header, final long value, final long maxValue) { 259 if (value < 0 || value > maxValue) { 260 paxHeaders.put(header, String.valueOf(value)); 261 } 262 } 263 264 private void addPaxHeadersForBigNumbers(final Map<String, String> paxHeaders, final TarArchiveEntry entry) { 265 addPaxHeaderForBigNumber(paxHeaders, "size", entry.getSize(), TarConstants.MAXSIZE); 266 addPaxHeaderForBigNumber(paxHeaders, "gid", entry.getLongGroupId(), TarConstants.MAXID); 267 addFileTimePaxHeaderForBigNumber(paxHeaders, "mtime", entry.getLastModifiedTime(), TarConstants.MAXSIZE); 268 addFileTimePaxHeader(paxHeaders, "atime", entry.getLastAccessTime()); 269 if (entry.getStatusChangeTime() != null) { 270 addFileTimePaxHeader(paxHeaders, "ctime", entry.getStatusChangeTime()); 271 } else { 272 // ctime is usually set from creation time on platforms where the real ctime is not available 273 addFileTimePaxHeader(paxHeaders, "ctime", entry.getCreationTime()); 274 } 275 addPaxHeaderForBigNumber(paxHeaders, "uid", entry.getLongUserId(), TarConstants.MAXID); 276 // libarchive extensions 277 addFileTimePaxHeader(paxHeaders, "LIBARCHIVE.creationtime", entry.getCreationTime()); 278 // star extensions by Jorg Schilling 279 addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devmajor", entry.getDevMajor(), TarConstants.MAXID); 280 addPaxHeaderForBigNumber(paxHeaders, "SCHILY.devminor", entry.getDevMinor(), TarConstants.MAXID); 281 // there is no PAX header for file mode 282 failForBigNumber("mode", entry.getMode(), TarConstants.MAXID); 283 } 284 285 /** 286 * Closes the underlying OutputStream. 287 * 288 * @throws IOException on error 289 */ 290 @Override 291 public void close() throws IOException { 292 try { 293 if (!isFinished()) { 294 finish(); 295 } 296 } finally { 297 super.close(); 298 } 299 } 300 301 /** 302 * Closes an entry. This method MUST be called for all file entries that contain data. The reason is that we must buffer data written to the stream in order 303 * to satisfy the buffer's record based writes. Thus, there may be data fragments still being assembled that must be written to the output stream before 304 * this entry is closed and the next entry written. 305 * 306 * @throws IOException on error 307 */ 308 @Override 309 public void closeArchiveEntry() throws IOException { 310 checkFinished(); 311 if (!haveUnclosedEntry) { 312 throw new IOException("No current entry to close"); 313 } 314 ((FixedLengthBlockOutputStream) out).flushBlock(); 315 if (currBytes < currSize) { 316 throw new IOException( 317 "Entry '" + currName + "' closed at '" + currBytes + "' before the '" + currSize + "' bytes specified in the header were written"); 318 } 319 recordsWritten += currSize / RECORD_SIZE; 320 321 if (0 != currSize % RECORD_SIZE) { 322 recordsWritten++; 323 } 324 haveUnclosedEntry = false; 325 } 326 327 @Override 328 public TarArchiveEntry createArchiveEntry(final File inputFile, final String entryName) throws IOException { 329 checkFinished(); 330 return new TarArchiveEntry(inputFile, entryName); 331 } 332 333 @Override 334 public TarArchiveEntry createArchiveEntry(final Path inputPath, final String entryName, final LinkOption... options) throws IOException { 335 checkFinished(); 336 return new TarArchiveEntry(inputPath, entryName, options); 337 } 338 339 private byte[] encodeExtendedPaxHeadersContents(final Map<String, String> headers) { 340 final StringWriter w = new StringWriter(); 341 headers.forEach((k, v) -> { 342 int len = k.length() + v.length() + 3 /* blank, equals and newline */ 343 + 2 /* guess 9 < actual length < 100 */; 344 String line = len + " " + k + "=" + v + "\n"; 345 int actualLength = line.getBytes(UTF_8).length; 346 while (len != actualLength) { 347 // Adjust for cases where length < 10 or > 100 348 // or where UTF-8 encoding isn't a single octet 349 // per character. 350 // Must be in loop as size may go from 99 to 100 in 351 // first pass, so we'd need a second. 352 len = actualLength; 353 line = len + " " + k + "=" + v + "\n"; 354 actualLength = line.getBytes(UTF_8).length; 355 } 356 w.write(line); 357 }); 358 return w.toString().getBytes(UTF_8); 359 } 360 361 private void failForBigNumber(final String field, final long value, final long maxValue) { 362 failForBigNumber(field, value, maxValue, ""); 363 } 364 365 private void failForBigNumber(final String field, final long value, final long maxValue, final String additionalMsg) { 366 if (value < 0 || value > maxValue) { 367 throw new IllegalArgumentException(field + " '" + value // NOSONAR 368 + "' is too big ( > " + maxValue + " )." + additionalMsg); 369 } 370 } 371 372 private void failForBigNumbers(final TarArchiveEntry entry) { 373 failForBigNumber("entry size", entry.getSize(), TarConstants.MAXSIZE); 374 failForBigNumberWithPosixMessage("group id", entry.getLongGroupId(), TarConstants.MAXID); 375 failForBigNumber("last modification time", FileTimes.toUnixTime(entry.getLastModifiedTime()), TarConstants.MAXSIZE); 376 failForBigNumber("user id", entry.getLongUserId(), TarConstants.MAXID); 377 failForBigNumber("mode", entry.getMode(), TarConstants.MAXID); 378 failForBigNumber("major device number", entry.getDevMajor(), TarConstants.MAXID); 379 failForBigNumber("minor device number", entry.getDevMinor(), TarConstants.MAXID); 380 } 381 382 private void failForBigNumberWithPosixMessage(final String field, final long value, final long maxValue) { 383 failForBigNumber(field, value, maxValue, " Use STAR or POSIX extensions to overcome this limit"); 384 } 385 386 /** 387 * Finishes the TAR archive without closing the underlying OutputStream. 388 * 389 * An archive consists of a series of file entries terminated by an end-of-archive entry, which consists of two 512 blocks of zero bytes. POSIX.1 requires 390 * two EOF records, like some other implementations. 391 * 392 * @throws IOException on error 393 */ 394 @Override 395 public void finish() throws IOException { 396 checkFinished(); 397 if (haveUnclosedEntry) { 398 throw new IOException("This archive contains unclosed entries."); 399 } 400 writeEOFRecord(); 401 writeEOFRecord(); 402 padAsNeeded(); 403 out.flush(); 404 super.finish(); 405 } 406 407 @Override 408 public long getBytesWritten() { 409 return countingOut.getByteCount(); 410 } 411 412 @Deprecated 413 @Override 414 public int getCount() { 415 return (int) getBytesWritten(); 416 } 417 418 /** 419 * Gets the record size being used by this stream's TarBuffer. 420 * 421 * @return The TarBuffer record size. 422 * @deprecated TODO Add a comment. 423 */ 424 @Deprecated 425 public int getRecordSize() { 426 return RECORD_SIZE; 427 } 428 429 /** 430 * Handles long file or link names according to the longFileMode setting. 431 * 432 * <p> 433 * I.e. if the given name is too long to be written to a plain tar header then 434 * <ul> 435 * <li>it creates a pax header who's name is given by the paxHeaderName parameter if longFileMode is POSIX</li> 436 * <li>it creates a GNU longlink entry who's type is given by the linkType parameter if longFileMode is GNU</li> 437 * <li>it throws an exception if longFileMode is ERROR</li> 438 * <li>it truncates the name if longFileMode is TRUNCATE</li> 439 * </ul> 440 * </p> 441 * 442 * @param entry entry the name belongs to 443 * @param name the name to write 444 * @param paxHeaders current map of pax headers 445 * @param paxHeaderName name of the pax header to write 446 * @param linkType type of the GNU entry to write 447 * @param fieldName the name of the field 448 * @throws IllegalArgumentException if the {@link TarArchiveOutputStream#longFileMode} equals {@link TarArchiveOutputStream#LONGFILE_ERROR} and the file 449 * name is too long 450 * @return whether a pax header has been written. 451 */ 452 private boolean handleLongName(final TarArchiveEntry entry, final String name, final Map<String, String> paxHeaders, final String paxHeaderName, 453 final byte linkType, final String fieldName) throws IOException { 454 final ByteBuffer encodedName = zipEncoding.encode(name); 455 final int len = encodedName.limit() - encodedName.position(); 456 if (len >= TarConstants.NAMELEN) { 457 458 if (longFileMode == LONGFILE_POSIX) { 459 paxHeaders.put(paxHeaderName, name); 460 return true; 461 } 462 if (longFileMode == LONGFILE_GNU) { 463 // create a TarEntry for the LongLink, the contents 464 // of which are the link's name 465 final TarArchiveEntry longLinkEntry = new TarArchiveEntry(TarConstants.GNU_LONGLINK, linkType); 466 467 longLinkEntry.setSize(len + 1L); // +1 for NUL 468 transferModTime(entry, longLinkEntry); 469 putArchiveEntry(longLinkEntry); 470 write(encodedName.array(), encodedName.arrayOffset(), len); 471 write(0); // NUL terminator 472 closeArchiveEntry(); 473 } else if (longFileMode != LONGFILE_TRUNCATE) { 474 throw new IllegalArgumentException(fieldName + " '" + name // NOSONAR 475 + "' is too long ( > " + TarConstants.NAMELEN + " bytes)"); 476 } 477 } 478 return false; 479 } 480 481 private void padAsNeeded() throws IOException { 482 final int start = Math.toIntExact(recordsWritten % recordsPerBlock); 483 if (start != 0) { 484 for (int i = start; i < recordsPerBlock; i++) { 485 writeEOFRecord(); 486 } 487 } 488 } 489 490 /** 491 * Puts an entry on the output stream. This writes the entry's header record and positions the output stream for writing the contents of the entry. Once 492 * this method is called, the stream is ready for calls to write() to write the entry's contents. Once the contents are written, closeArchiveEntry() 493 * <B>MUST</B> be called to ensure that all buffered data is completely written to the output stream. 494 * 495 * @param archiveEntry The TarEntry to be written to the archive. 496 * @throws IOException on error 497 * @throws ClassCastException if archiveEntry is not an instance of TarArchiveEntry 498 * @throws IllegalArgumentException if the {@link TarArchiveOutputStream#longFileMode} equals {@link TarArchiveOutputStream#LONGFILE_ERROR} and the file 499 * name is too long 500 * @throws IllegalArgumentException if the {@link TarArchiveOutputStream#bigNumberMode} equals {@link TarArchiveOutputStream#BIGNUMBER_ERROR} and one of the 501 * numeric values exceeds the limits of a traditional tar header. 502 */ 503 @Override 504 public void putArchiveEntry(final TarArchiveEntry archiveEntry) throws IOException { 505 checkFinished(); 506 if (archiveEntry.isGlobalPaxHeader()) { 507 final byte[] data = encodeExtendedPaxHeadersContents(archiveEntry.getExtraPaxHeaders()); 508 archiveEntry.setSize(data.length); 509 archiveEntry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR); 510 writeRecord(recordBuf); 511 currSize = archiveEntry.getSize(); 512 currBytes = 0; 513 this.haveUnclosedEntry = true; 514 write(data); 515 closeArchiveEntry(); 516 } else { 517 final Map<String, String> paxHeaders = new HashMap<>(); 518 final String entryName = archiveEntry.getName(); 519 final boolean paxHeaderContainsPath = handleLongName(archiveEntry, entryName, paxHeaders, "path", TarConstants.LF_GNUTYPE_LONGNAME, "file name"); 520 final String linkName = archiveEntry.getLinkName(); 521 final boolean paxHeaderContainsLinkPath = linkName != null && !linkName.isEmpty() 522 && handleLongName(archiveEntry, linkName, paxHeaders, "linkpath", TarConstants.LF_GNUTYPE_LONGLINK, "link name"); 523 524 if (bigNumberMode == BIGNUMBER_POSIX) { 525 addPaxHeadersForBigNumbers(paxHeaders, archiveEntry); 526 } else if (bigNumberMode != BIGNUMBER_STAR) { 527 failForBigNumbers(archiveEntry); 528 } 529 530 if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsPath && !ASCII.canEncode(entryName)) { 531 paxHeaders.put("path", entryName); 532 } 533 534 if (addPaxHeadersForNonAsciiNames && !paxHeaderContainsLinkPath && (archiveEntry.isLink() || archiveEntry.isSymbolicLink()) 535 && !ASCII.canEncode(linkName)) { 536 paxHeaders.put("linkpath", linkName); 537 } 538 paxHeaders.putAll(archiveEntry.getExtraPaxHeaders()); 539 540 if (!paxHeaders.isEmpty()) { 541 writePaxHeaders(archiveEntry, entryName, paxHeaders); 542 } 543 544 archiveEntry.writeEntryHeader(recordBuf, zipEncoding, bigNumberMode == BIGNUMBER_STAR); 545 writeRecord(recordBuf); 546 547 currBytes = 0; 548 549 if (archiveEntry.isDirectory()) { 550 currSize = 0; 551 } else { 552 currSize = archiveEntry.getSize(); 553 } 554 currName = entryName; 555 haveUnclosedEntry = true; 556 } 557 } 558 559 /** 560 * Sets whether to add a PAX extension header for non-ASCII file names. 561 * 562 * @param b whether to add a PAX extension header for non-ASCII file names. 563 * @since 1.4 564 */ 565 public void setAddPaxHeadersForNonAsciiNames(final boolean b) { 566 addPaxHeadersForNonAsciiNames = b; 567 } 568 569 /** 570 * Sets the big number mode. This can be BIGNUMBER_ERROR(0), BIGNUMBER_STAR(1) or BIGNUMBER_POSIX(2). This specifies the treatment of big files (sizes > 571 * TarConstants.MAXSIZE) and other numeric values too big to fit into a traditional tar header. Default is BIGNUMBER_ERROR. 572 * 573 * @param bigNumberMode the mode to use 574 * @since 1.4 575 */ 576 public void setBigNumberMode(final int bigNumberMode) { 577 this.bigNumberMode = bigNumberMode; 578 } 579 580 /** 581 * Sets the long file mode. This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1), LONGFILE_GNU(2) or LONGFILE_POSIX(3). This specifies the treatment of long 582 * file names (names >= TarConstants.NAMELEN). Default is LONGFILE_ERROR. 583 * 584 * @param longFileMode the mode to use 585 */ 586 public void setLongFileMode(final int longFileMode) { 587 this.longFileMode = longFileMode; 588 } 589 590 /** 591 * Tests whether the character could lead to problems when used inside a TarArchiveEntry name for a PAX header. 592 * 593 * @return true if the character could lead to problems when used inside a TarArchiveEntry name for a PAX header. 594 */ 595 private boolean shouldBeReplaced(final char c) { 596 return c == 0 // would be read as Trailing null 597 || c == '/' // when used as last character TAE will consider the PAX header a directory 598 || c == '\\'; // same as '/' as slashes get "normalized" on Windows 599 } 600 601 private String stripTo7Bits(final String name) { 602 final int length = name.length(); 603 final StringBuilder result = new StringBuilder(length); 604 for (int i = 0; i < length; i++) { 605 final char stripped = (char) (name.charAt(i) & 0x7F); 606 if (shouldBeReplaced(stripped)) { 607 result.append("_"); 608 } else { 609 result.append(stripped); 610 } 611 } 612 return result.toString(); 613 } 614 615 private void transferModTime(final TarArchiveEntry from, final TarArchiveEntry to) { 616 long fromModTimeSeconds = FileTimes.toUnixTime(from.getLastModifiedTime()); 617 if (fromModTimeSeconds < 0 || fromModTimeSeconds > TarConstants.MAXSIZE) { 618 fromModTimeSeconds = 0; 619 } 620 to.setLastModifiedTime(FileTimes.fromUnixTime(fromModTimeSeconds)); 621 } 622 623 /** 624 * Writes bytes to the current tar archive entry. This method is aware of the current entry and will throw an exception if you attempt to write bytes past 625 * the length specified for the current entry. 626 * 627 * @param wBuf The buffer to write to the archive. 628 * @param wOffset The offset in the buffer from which to get bytes. 629 * @param numToWrite The number of bytes to write. 630 * @throws IOException on error 631 */ 632 @Override 633 public void write(final byte[] wBuf, final int wOffset, final int numToWrite) throws IOException { 634 if (!haveUnclosedEntry) { 635 throw new IllegalStateException("No current tar entry"); 636 } 637 if (currBytes + numToWrite > currSize) { 638 throw new IOException( 639 "Request to write '" + numToWrite + "' bytes exceeds size in header of '" + currSize + "' bytes for entry '" + currName + "'"); 640 } 641 out.write(wBuf, wOffset, numToWrite); 642 currBytes += numToWrite; 643 } 644 645 /** 646 * Writes an EOF (end of archive) record to the tar archive. An EOF record consists of a record of all zeros. 647 */ 648 private void writeEOFRecord() throws IOException { 649 writeRecord(ArrayFill.fill(recordBuf, (byte) 0)); 650 } 651 652 /** 653 * Writes a PAX extended header with the given map as contents. 654 * 655 * @since 1.4 656 */ 657 void writePaxHeaders(final TarArchiveEntry entry, final String entryName, final Map<String, String> headers) throws IOException { 658 String name = "./PaxHeaders.X/" + stripTo7Bits(entryName); 659 if (name.length() >= TarConstants.NAMELEN) { 660 name = name.substring(0, TarConstants.NAMELEN - 1); 661 } 662 final TarArchiveEntry pex = new TarArchiveEntry(name, TarConstants.LF_PAX_EXTENDED_HEADER_LC); 663 transferModTime(entry, pex); 664 665 final byte[] data = encodeExtendedPaxHeadersContents(headers); 666 pex.setSize(data.length); 667 putArchiveEntry(pex); 668 write(data); 669 closeArchiveEntry(); 670 } 671 672 /** 673 * Writes an archive record to the archive. 674 * 675 * @param record The record data to write to the archive. 676 * @throws IOException on error 677 */ 678 private void writeRecord(final byte[] record) throws IOException { 679 if (record.length != RECORD_SIZE) { 680 throw new IOException("Record to write has length '" + record.length + "' which is not the record size of '" + RECORD_SIZE + "'"); 681 } 682 683 out.write(record); 684 recordsWritten++; 685 } 686}