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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.IOException;
  51. import java.io.InputStream;
  52. import java.io.OutputStream;
  53. import java.nio.channels.Channels;
  54. import java.security.DigestOutputStream;
  55. import java.security.MessageDigest;
  56. import java.util.zip.Deflater;
  57. import java.util.zip.DeflaterOutputStream;
  58. import org.eclipse.jgit.errors.ObjectWritingException;
  59. import org.eclipse.jgit.lib.Config;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.ObjectId;
  62. import org.eclipse.jgit.lib.ObjectInserter;
  63. import org.eclipse.jgit.transport.PackParser;
  64. import org.eclipse.jgit.util.FileUtils;
  65. import org.eclipse.jgit.util.IO;
  66. /** Creates loose objects in a {@link ObjectDirectory}. */
  67. class ObjectDirectoryInserter extends ObjectInserter {
  68. private final FileObjectDatabase db;
  69. private final WriteConfig config;
  70. private Deflater deflate;
  71. ObjectDirectoryInserter(final FileObjectDatabase dest, final Config cfg) {
  72. db = dest;
  73. config = cfg.get(WriteConfig.KEY);
  74. }
  75. @Override
  76. public ObjectId insert(int type, byte[] data, int off, int len)
  77. throws IOException {
  78. ObjectId id = idFor(type, data, off, len);
  79. if (db.has(id)) {
  80. return id;
  81. } else {
  82. File tmp = toTemp(type, data, off, len);
  83. return insertOneObject(tmp, id);
  84. }
  85. }
  86. @Override
  87. public ObjectId insert(final int type, long len, final InputStream is)
  88. throws IOException {
  89. if (len <= buffer().length) {
  90. byte[] buf = buffer();
  91. int actLen = IO.readFully(is, buf, 0);
  92. return insert(type, buf, 0, actLen);
  93. } else {
  94. MessageDigest md = digest();
  95. File tmp = toTemp(md, type, len, is);
  96. ObjectId id = ObjectId.fromRaw(md.digest());
  97. return insertOneObject(tmp, id);
  98. }
  99. }
  100. private ObjectId insertOneObject(final File tmp, final ObjectId id)
  101. throws IOException, ObjectWritingException {
  102. switch (db.insertUnpackedObject(tmp, id, false /* no duplicate */)) {
  103. case INSERTED:
  104. case EXISTS_PACKED:
  105. case EXISTS_LOOSE:
  106. return id;
  107. case FAILURE:
  108. default:
  109. break;
  110. }
  111. final File dst = db.fileFor(id);
  112. throw new ObjectWritingException("Unable to create new object: " + dst);
  113. }
  114. @Override
  115. public PackParser newPackParser(InputStream in) throws IOException {
  116. return new ObjectDirectoryPackParser(db, in);
  117. }
  118. @Override
  119. public void flush() throws IOException {
  120. // Do nothing. Objects are immediately visible.
  121. }
  122. @Override
  123. public void release() {
  124. if (deflate != null) {
  125. try {
  126. deflate.end();
  127. } finally {
  128. deflate = null;
  129. }
  130. }
  131. }
  132. @SuppressWarnings("resource" /* java 7 */)
  133. private File toTemp(final MessageDigest md, final int type, long len,
  134. final InputStream is) throws IOException, FileNotFoundException,
  135. Error {
  136. boolean delete = true;
  137. File tmp = newTempFile();
  138. try {
  139. FileOutputStream fOut = new FileOutputStream(tmp);
  140. try {
  141. OutputStream out = fOut;
  142. if (config.getFSyncObjectFiles())
  143. out = Channels.newOutputStream(fOut.getChannel());
  144. DeflaterOutputStream cOut = compress(out);
  145. DigestOutputStream dOut = new DigestOutputStream(cOut, md);
  146. writeHeader(dOut, type, len);
  147. final byte[] buf = buffer();
  148. while (len > 0) {
  149. int n = is.read(buf, 0, (int) Math.min(len, buf.length));
  150. if (n <= 0)
  151. throw shortInput(len);
  152. dOut.write(buf, 0, n);
  153. len -= n;
  154. }
  155. dOut.flush();
  156. cOut.finish();
  157. } finally {
  158. if (config.getFSyncObjectFiles())
  159. fOut.getChannel().force(true);
  160. fOut.close();
  161. }
  162. delete = false;
  163. return tmp;
  164. } finally {
  165. if (delete)
  166. FileUtils.delete(tmp);
  167. }
  168. }
  169. @SuppressWarnings("resource" /* java 7 */)
  170. private File toTemp(final int type, final byte[] buf, final int pos,
  171. final int len) throws IOException, FileNotFoundException {
  172. boolean delete = true;
  173. File tmp = newTempFile();
  174. try {
  175. FileOutputStream fOut = new FileOutputStream(tmp);
  176. try {
  177. OutputStream out = fOut;
  178. if (config.getFSyncObjectFiles())
  179. out = Channels.newOutputStream(fOut.getChannel());
  180. DeflaterOutputStream cOut = compress(out);
  181. writeHeader(cOut, type, len);
  182. cOut.write(buf, pos, len);
  183. cOut.finish();
  184. } finally {
  185. if (config.getFSyncObjectFiles())
  186. fOut.getChannel().force(true);
  187. fOut.close();
  188. }
  189. delete = false;
  190. return tmp;
  191. } finally {
  192. if (delete)
  193. FileUtils.delete(tmp);
  194. }
  195. }
  196. void writeHeader(OutputStream out, final int type, long len)
  197. throws IOException {
  198. out.write(Constants.encodedTypeString(type));
  199. out.write((byte) ' ');
  200. out.write(Constants.encodeASCII(len));
  201. out.write((byte) 0);
  202. }
  203. File newTempFile() throws IOException {
  204. return File.createTempFile("noz", null, db.getDirectory()); //$NON-NLS-1$
  205. }
  206. DeflaterOutputStream compress(final OutputStream out) {
  207. if (deflate == null)
  208. deflate = new Deflater(config.getCompression());
  209. else
  210. deflate.reset();
  211. return new DeflaterOutputStream(out, deflate, 8192);
  212. }
  213. private static EOFException shortInput(long missing) {
  214. return new EOFException("Input did not match supplied length. "
  215. + missing + " bytes are missing.");
  216. }
  217. }