You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TarFormat.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2012 Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.archive;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import java.io.IOException;
  13. import java.io.OutputStream;
  14. import java.text.MessageFormat;
  15. import java.util.Arrays;
  16. import java.util.Collections;
  17. import java.util.List;
  18. import java.util.Map;
  19. import org.apache.commons.compress.archivers.ArchiveOutputStream;
  20. import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
  21. import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
  22. import org.apache.commons.compress.archivers.tar.TarConstants;
  23. import org.eclipse.jgit.api.ArchiveCommand;
  24. import org.eclipse.jgit.archive.internal.ArchiveText;
  25. import org.eclipse.jgit.lib.FileMode;
  26. import org.eclipse.jgit.lib.ObjectId;
  27. import org.eclipse.jgit.lib.ObjectLoader;
  28. import org.eclipse.jgit.revwalk.RevCommit;
  29. /**
  30. * Unix TAR format (ustar + some PAX extensions).
  31. */
  32. public final class TarFormat extends BaseFormat implements
  33. ArchiveCommand.Format<ArchiveOutputStream> {
  34. private static final List<String> SUFFIXES = Collections
  35. .unmodifiableList(Arrays.asList(".tar")); //$NON-NLS-1$
  36. /** {@inheritDoc} */
  37. @Override
  38. public ArchiveOutputStream createArchiveOutputStream(OutputStream s)
  39. throws IOException {
  40. return createArchiveOutputStream(s,
  41. Collections.<String, Object> emptyMap());
  42. }
  43. /** {@inheritDoc} */
  44. @Override
  45. public ArchiveOutputStream createArchiveOutputStream(OutputStream s,
  46. Map<String, Object> o) throws IOException {
  47. TarArchiveOutputStream out = new TarArchiveOutputStream(s,
  48. UTF_8.name());
  49. out.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
  50. out.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
  51. return applyFormatOptions(out, o);
  52. }
  53. /** {@inheritDoc} */
  54. @Override
  55. public void putEntry(ArchiveOutputStream out,
  56. ObjectId tree, String path, FileMode mode, ObjectLoader loader)
  57. throws IOException {
  58. if (mode == FileMode.SYMLINK) {
  59. final TarArchiveEntry entry = new TarArchiveEntry(
  60. path, TarConstants.LF_SYMLINK);
  61. entry.setLinkName(new String(loader.getCachedBytes(100), UTF_8));
  62. out.putArchiveEntry(entry);
  63. out.closeArchiveEntry();
  64. return;
  65. }
  66. // TarArchiveEntry detects directories by checking
  67. // for '/' at the end of the filename.
  68. if (path.endsWith("/") && mode != FileMode.TREE) //$NON-NLS-1$
  69. throw new IllegalArgumentException(MessageFormat.format(
  70. ArchiveText.get().pathDoesNotMatchMode, path, mode));
  71. if (!path.endsWith("/") && mode == FileMode.TREE) //$NON-NLS-1$
  72. path = path + "/"; //$NON-NLS-1$
  73. final TarArchiveEntry entry = new TarArchiveEntry(path);
  74. if (tree instanceof RevCommit) {
  75. long t = ((RevCommit) tree).getCommitTime() * 1000L;
  76. entry.setModTime(t);
  77. }
  78. if (mode == FileMode.TREE) {
  79. out.putArchiveEntry(entry);
  80. out.closeArchiveEntry();
  81. return;
  82. }
  83. if (mode == FileMode.REGULAR_FILE) {
  84. // ok
  85. } else if (mode == FileMode.EXECUTABLE_FILE) {
  86. entry.setMode(mode.getBits());
  87. } else {
  88. // Unsupported mode (e.g., GITLINK).
  89. throw new IllegalArgumentException(MessageFormat.format(
  90. ArchiveText.get().unsupportedMode, mode));
  91. }
  92. entry.setSize(loader.getSize());
  93. out.putArchiveEntry(entry);
  94. loader.copyTo(out);
  95. out.closeArchiveEntry();
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public Iterable<String> suffixes() {
  100. return SUFFIXES;
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public boolean equals(Object other) {
  105. return (other instanceof TarFormat);
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public int hashCode() {
  110. return getClass().hashCode();
  111. }
  112. }