1 /*
2 * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.com>
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43 package org.eclipse.jgit.lfs;
44
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.OutputStream;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.StandardCopyOption;
51
52 import org.eclipse.jgit.attributes.FilterCommand;
53 import org.eclipse.jgit.attributes.FilterCommandFactory;
54 import org.eclipse.jgit.attributes.FilterCommandRegistry;
55 import org.eclipse.jgit.lfs.errors.CorruptMediaFile;
56 import org.eclipse.jgit.lfs.internal.AtomicObjectOutputStream;
57 import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
58 import org.eclipse.jgit.lfs.lib.Constants;
59 import org.eclipse.jgit.lib.Repository;
60 import org.eclipse.jgit.util.FileUtils;
61
62 /**
63 * Built-in LFS clean filter
64 *
65 * When new content is about to be added to the git repository and this filter
66 * is configured for that content, then this filter will replace the original
67 * content with content of a so-called LFS pointer file. The pointer file
68 * content will then be added to the git repository. Additionally this filter
69 * writes the original content in a so-called 'media file' to '.git/lfs/objects/
70 * <first-two-characters-of-contentid>/<rest-of-contentid>'
71 *
72 * @see <a href="https://github.com/github/git-lfs/blob/master/docs/spec.md">Git
73 * LFS Specification</a>
74 * @since 4.6
75 */
76 public class CleanFilter extends FilterCommand {
77 /**
78 * The factory is responsible for creating instances of
79 * {@link org.eclipse.jgit.lfs.CleanFilter}
80 */
81 public final static FilterCommandFactory FACTORY = new FilterCommandFactory() {
82
83 @Override
84 public FilterCommand create(Repository db, InputStream in,
85 OutputStream out) throws IOException {
86 return new CleanFilter(db, in, out);
87 }
88 };
89
90 /**
91 * Registers this filter by calling
92 * {@link FilterCommandRegistry#register(String, FilterCommandFactory)}
93 */
94 static void register() {
95 FilterCommandRegistry
96 .register(org.eclipse.jgit.lib.Constants.BUILTIN_FILTER_PREFIX
97 + Constants.ATTR_FILTER_DRIVER_PREFIX
98 + org.eclipse.jgit.lib.Constants.ATTR_FILTER_TYPE_CLEAN,
99 FACTORY);
100 }
101
102 // Used to compute the hash for the original content
103 private AtomicObjectOutputStream aOut;
104
105 private Lfs lfsUtil;
106
107 // the size of the original content
108 private long size;
109
110 // a temporary file into which the original content is written. When no
111 // errors occur this file will be renamed to the mediafile
112 private Path tmpFile;
113
114 /**
115 * Constructor for CleanFilter.
116 *
117 * @param db
118 * the repository
119 * @param in
120 * an {@link java.io.InputStream} providing the original content
121 * @param out
122 * the {@link java.io.OutputStream} into which the content of the
123 * pointer file should be written. That's the content which will
124 * be added to the git repository
125 * @throws java.io.IOException
126 * when the creation of the temporary file fails or when no
127 * {@link java.io.OutputStream} for this file can be created
128 */
129 public CleanFilter(Repository db, InputStream in, OutputStream out)
130 throws IOException {
131 super(in, out);
132 lfsUtil = new Lfs(db);
133 Files.createDirectories(lfsUtil.getLfsTmpDir());
134 tmpFile = lfsUtil.createTmpFile();
135 this.aOut = new AtomicObjectOutputStream(tmpFile.toAbsolutePath());
136 }
137
138 /** {@inheritDoc} */
139 @Override
140 public int run() throws IOException {
141 try {
142 byte[] buf = new byte[8192];
143 int length = in.read(buf);
144 if (length != -1) {
145 aOut.write(buf, 0, length);
146 size += length;
147 return length;
148 } else {
149 aOut.close();
150 AnyLongObjectId loid = aOut.getId();
151 aOut = null;
152 Path mediaFile = lfsUtil.getMediaFile(loid);
153 if (Files.isRegularFile(mediaFile)) {
154 long fsSize = Files.size(mediaFile);
155 if (fsSize != size) {
156 throw new CorruptMediaFile(mediaFile, size, fsSize);
157 } else {
158 FileUtils.delete(tmpFile.toFile());
159 }
160 } else {
161 Path parent = mediaFile.getParent();
162 if (parent != null) {
163 FileUtils.mkdirs(parent.toFile(), true);
164 }
165 FileUtils.rename(tmpFile.toFile(), mediaFile.toFile(),
166 StandardCopyOption.ATOMIC_MOVE);
167 }
168 LfsPointer lfsPointer = new LfsPointer(loid, size);
169 lfsPointer.encode(out);
170 in.close();
171 out.close();
172 return -1;
173 }
174 } catch (IOException e) {
175 if (aOut != null) {
176 aOut.abort();
177 }
178 in.close();
179 out.close();
180 throw e;
181 }
182 }
183 }