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.

CleanFilter.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.com> 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.lfs;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.nio.file.Files;
  15. import java.nio.file.Path;
  16. import java.nio.file.StandardCopyOption;
  17. import org.eclipse.jgit.attributes.FilterCommand;
  18. import org.eclipse.jgit.attributes.FilterCommandFactory;
  19. import org.eclipse.jgit.attributes.FilterCommandRegistry;
  20. import org.eclipse.jgit.lfs.errors.CorruptMediaFile;
  21. import org.eclipse.jgit.lfs.internal.AtomicObjectOutputStream;
  22. import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
  23. import org.eclipse.jgit.lfs.lib.Constants;
  24. import org.eclipse.jgit.lib.Repository;
  25. import org.eclipse.jgit.util.FileUtils;
  26. /**
  27. * Built-in LFS clean filter
  28. *
  29. * When new content is about to be added to the git repository and this filter
  30. * is configured for that content, then this filter will replace the original
  31. * content with content of a so-called LFS pointer file. The pointer file
  32. * content will then be added to the git repository. Additionally this filter
  33. * writes the original content in a so-called 'media file' to '.git/lfs/objects/
  34. * &lt;first-two-characters-of-contentid&gt;/&lt;rest-of-contentid&gt;'
  35. *
  36. * @see <a href="https://github.com/github/git-lfs/blob/master/docs/spec.md">Git
  37. * LFS Specification</a>
  38. * @since 4.6
  39. */
  40. public class CleanFilter extends FilterCommand {
  41. /**
  42. * The factory is responsible for creating instances of
  43. * {@link org.eclipse.jgit.lfs.CleanFilter}
  44. */
  45. public static final FilterCommandFactory FACTORY = CleanFilter::new;
  46. /**
  47. * Registers this filter by calling
  48. * {@link FilterCommandRegistry#register(String, FilterCommandFactory)}
  49. */
  50. static void register() {
  51. FilterCommandRegistry
  52. .register(org.eclipse.jgit.lib.Constants.BUILTIN_FILTER_PREFIX
  53. + Constants.ATTR_FILTER_DRIVER_PREFIX
  54. + org.eclipse.jgit.lib.Constants.ATTR_FILTER_TYPE_CLEAN,
  55. FACTORY);
  56. }
  57. // Used to compute the hash for the original content
  58. private AtomicObjectOutputStream aOut;
  59. private Lfs lfsUtil;
  60. // the size of the original content
  61. private long size;
  62. // a temporary file into which the original content is written. When no
  63. // errors occur this file will be renamed to the mediafile
  64. private Path tmpFile;
  65. /**
  66. * Constructor for CleanFilter.
  67. *
  68. * @param db
  69. * the repository
  70. * @param in
  71. * an {@link java.io.InputStream} providing the original content
  72. * @param out
  73. * the {@link java.io.OutputStream} into which the content of the
  74. * pointer file should be written. That's the content which will
  75. * be added to the git repository
  76. * @throws java.io.IOException
  77. * when the creation of the temporary file fails or when no
  78. * {@link java.io.OutputStream} for this file can be created
  79. */
  80. public CleanFilter(Repository db, InputStream in, OutputStream out)
  81. throws IOException {
  82. super(in, out);
  83. lfsUtil = new Lfs(db);
  84. Files.createDirectories(lfsUtil.getLfsTmpDir());
  85. tmpFile = lfsUtil.createTmpFile();
  86. this.aOut = new AtomicObjectOutputStream(tmpFile.toAbsolutePath());
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public int run() throws IOException {
  91. try {
  92. byte[] buf = new byte[8192];
  93. int length = in.read(buf);
  94. if (length != -1) {
  95. aOut.write(buf, 0, length);
  96. size += length;
  97. return length;
  98. }
  99. aOut.close();
  100. AnyLongObjectId loid = aOut.getId();
  101. aOut = null;
  102. Path mediaFile = lfsUtil.getMediaFile(loid);
  103. if (Files.isRegularFile(mediaFile)) {
  104. long fsSize = Files.size(mediaFile);
  105. if (fsSize != size) {
  106. throw new CorruptMediaFile(mediaFile, size, fsSize);
  107. }
  108. FileUtils.delete(tmpFile.toFile());
  109. } else {
  110. Path parent = mediaFile.getParent();
  111. if (parent != null) {
  112. FileUtils.mkdirs(parent.toFile(), true);
  113. }
  114. FileUtils.rename(tmpFile.toFile(), mediaFile.toFile(),
  115. StandardCopyOption.ATOMIC_MOVE);
  116. }
  117. LfsPointer lfsPointer = new LfsPointer(loid, size);
  118. lfsPointer.encode(out);
  119. in.close();
  120. out.close();
  121. return -1;
  122. } catch (IOException e) {
  123. if (aOut != null) {
  124. aOut.abort();
  125. }
  126. in.close();
  127. out.close();
  128. throw e;
  129. }
  130. }
  131. }