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.

ObjectDirectoryInserter.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2009, Google Inc.
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.internal.storage.file;
  46. import java.io.EOFException;
  47. import java.io.File;
  48. import java.io.FileNotFoundException;
  49. import java.io.FileOutputStream;
  50. import java.io.FilterOutputStream;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.io.OutputStream;
  54. import java.nio.channels.Channels;
  55. import java.text.MessageFormat;
  56. import java.util.zip.Deflater;
  57. import java.util.zip.DeflaterOutputStream;
  58. import org.eclipse.jgit.errors.ObjectWritingException;
  59. import org.eclipse.jgit.internal.JGitText;
  60. import org.eclipse.jgit.lib.Config;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.ObjectInserter;
  64. import org.eclipse.jgit.lib.ObjectReader;
  65. import org.eclipse.jgit.transport.PackParser;
  66. import org.eclipse.jgit.util.FileUtils;
  67. import org.eclipse.jgit.util.IO;
  68. import org.eclipse.jgit.util.sha1.SHA1;
  69. /** Creates loose objects in a {@link ObjectDirectory}. */
  70. class ObjectDirectoryInserter extends ObjectInserter {
  71. private final FileObjectDatabase db;
  72. private final WriteConfig config;
  73. private Deflater deflate;
  74. ObjectDirectoryInserter(FileObjectDatabase dest, Config cfg) {
  75. db = dest;
  76. config = cfg.get(WriteConfig.KEY);
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public ObjectId insert(int type, byte[] data, int off, int len)
  81. throws IOException {
  82. return insert(type, data, off, len, false);
  83. }
  84. /**
  85. * Insert a loose object into the database. If createDuplicate is true,
  86. * write the loose object even if we already have it in the loose or packed
  87. * ODB.
  88. *
  89. * @param type
  90. * @param data
  91. * @param off
  92. * @param len
  93. * @param createDuplicate
  94. * @return ObjectId
  95. * @throws IOException
  96. */
  97. private ObjectId insert(
  98. int type, byte[] data, int off, int len, boolean createDuplicate)
  99. throws IOException {
  100. ObjectId id = idFor(type, data, off, len);
  101. if (!createDuplicate && db.has(id)) {
  102. return id;
  103. } else {
  104. File tmp = toTemp(type, data, off, len);
  105. return insertOneObject(tmp, id, createDuplicate);
  106. }
  107. }
  108. /** {@inheritDoc} */
  109. @Override
  110. public ObjectId insert(int type, long len, InputStream is)
  111. throws IOException {
  112. return insert(type, len, is, false);
  113. }
  114. /**
  115. * Insert a loose object into the database. If createDuplicate is true,
  116. * write the loose object even if we already have it in the loose or packed
  117. * ODB.
  118. *
  119. * @param type
  120. * @param len
  121. * @param is
  122. * @param createDuplicate
  123. * @return ObjectId
  124. * @throws IOException
  125. */
  126. ObjectId insert(int type, long len, InputStream is, boolean createDuplicate)
  127. throws IOException {
  128. if (len <= buffer().length) {
  129. byte[] buf = buffer();
  130. int actLen = IO.readFully(is, buf, 0);
  131. return insert(type, buf, 0, actLen, createDuplicate);
  132. } else {
  133. SHA1 md = digest();
  134. File tmp = toTemp(md, type, len, is);
  135. ObjectId id = md.toObjectId();
  136. return insertOneObject(tmp, id, createDuplicate);
  137. }
  138. }
  139. private ObjectId insertOneObject(
  140. File tmp, ObjectId id, boolean createDuplicate)
  141. throws IOException, ObjectWritingException {
  142. switch (db.insertUnpackedObject(tmp, id, createDuplicate)) {
  143. case INSERTED:
  144. case EXISTS_PACKED:
  145. case EXISTS_LOOSE:
  146. return id;
  147. case FAILURE:
  148. default:
  149. break;
  150. }
  151. final File dst = db.fileFor(id);
  152. throw new ObjectWritingException(MessageFormat
  153. .format(JGitText.get().unableToCreateNewObject, dst));
  154. }
  155. /** {@inheritDoc} */
  156. @Override
  157. public PackParser newPackParser(InputStream in) throws IOException {
  158. return new ObjectDirectoryPackParser(db, in);
  159. }
  160. /** {@inheritDoc} */
  161. @Override
  162. public ObjectReader newReader() {
  163. return new WindowCursor(db, this);
  164. }
  165. /** {@inheritDoc} */
  166. @Override
  167. public void flush() throws IOException {
  168. // Do nothing. Loose objects are immediately visible.
  169. }
  170. /** {@inheritDoc} */
  171. @Override
  172. public void close() {
  173. if (deflate != null) {
  174. try {
  175. deflate.end();
  176. } finally {
  177. deflate = null;
  178. }
  179. }
  180. }
  181. @SuppressWarnings("resource" /* java 7 */)
  182. private File toTemp(final SHA1 md, final int type, long len,
  183. final InputStream is) throws IOException, FileNotFoundException,
  184. Error {
  185. boolean delete = true;
  186. File tmp = newTempFile();
  187. try {
  188. FileOutputStream fOut = new FileOutputStream(tmp);
  189. try {
  190. OutputStream out = fOut;
  191. if (config.getFSyncObjectFiles())
  192. out = Channels.newOutputStream(fOut.getChannel());
  193. DeflaterOutputStream cOut = compress(out);
  194. SHA1OutputStream dOut = new SHA1OutputStream(cOut, md);
  195. writeHeader(dOut, type, len);
  196. final byte[] buf = buffer();
  197. while (len > 0) {
  198. int n = is.read(buf, 0, (int) Math.min(len, buf.length));
  199. if (n <= 0)
  200. throw shortInput(len);
  201. dOut.write(buf, 0, n);
  202. len -= n;
  203. }
  204. dOut.flush();
  205. cOut.finish();
  206. } finally {
  207. if (config.getFSyncObjectFiles())
  208. fOut.getChannel().force(true);
  209. fOut.close();
  210. }
  211. delete = false;
  212. return tmp;
  213. } finally {
  214. if (delete)
  215. FileUtils.delete(tmp, FileUtils.RETRY);
  216. }
  217. }
  218. @SuppressWarnings("resource" /* java 7 */)
  219. private File toTemp(final int type, final byte[] buf, final int pos,
  220. final int len) throws IOException, FileNotFoundException {
  221. boolean delete = true;
  222. File tmp = newTempFile();
  223. try {
  224. FileOutputStream fOut = new FileOutputStream(tmp);
  225. try {
  226. OutputStream out = fOut;
  227. if (config.getFSyncObjectFiles())
  228. out = Channels.newOutputStream(fOut.getChannel());
  229. DeflaterOutputStream cOut = compress(out);
  230. writeHeader(cOut, type, len);
  231. cOut.write(buf, pos, len);
  232. cOut.finish();
  233. } finally {
  234. if (config.getFSyncObjectFiles())
  235. fOut.getChannel().force(true);
  236. fOut.close();
  237. }
  238. delete = false;
  239. return tmp;
  240. } finally {
  241. if (delete)
  242. FileUtils.delete(tmp, FileUtils.RETRY);
  243. }
  244. }
  245. void writeHeader(OutputStream out, int type, long len)
  246. throws IOException {
  247. out.write(Constants.encodedTypeString(type));
  248. out.write((byte) ' ');
  249. out.write(Constants.encodeASCII(len));
  250. out.write((byte) 0);
  251. }
  252. File newTempFile() throws IOException {
  253. return File.createTempFile("noz", null, db.getDirectory()); //$NON-NLS-1$
  254. }
  255. DeflaterOutputStream compress(OutputStream out) {
  256. if (deflate == null)
  257. deflate = new Deflater(config.getCompression());
  258. else
  259. deflate.reset();
  260. return new DeflaterOutputStream(out, deflate, 8192);
  261. }
  262. private static EOFException shortInput(long missing) {
  263. return new EOFException(MessageFormat.format(
  264. JGitText.get().inputDidntMatchLength, Long.valueOf(missing)));
  265. }
  266. private static class SHA1OutputStream extends FilterOutputStream {
  267. private final SHA1 md;
  268. SHA1OutputStream(OutputStream out, SHA1 md) {
  269. super(out);
  270. this.md = md;
  271. }
  272. @Override
  273. public void write(int b) throws IOException {
  274. md.update((byte) b);
  275. out.write(b);
  276. }
  277. @Override
  278. public void write(byte[] in, int p, int n) throws IOException {
  279. md.update(in, p, n);
  280. out.write(in, p, n);
  281. }
  282. }
  283. }