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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.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. byte[] buf = buffer(len);
  90. if (len <= buf.length) {
  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. private File toTemp(final MessageDigest md, final int type, long len,
  133. final InputStream is) throws IOException, FileNotFoundException,
  134. Error {
  135. boolean delete = true;
  136. File tmp = newTempFile();
  137. try {
  138. FileOutputStream fOut = new FileOutputStream(tmp);
  139. try {
  140. OutputStream out = fOut;
  141. if (config.getFSyncObjectFiles())
  142. out = Channels.newOutputStream(fOut.getChannel());
  143. DeflaterOutputStream cOut = compress(out);
  144. DigestOutputStream dOut = new DigestOutputStream(cOut, md);
  145. writeHeader(dOut, type, len);
  146. final byte[] buf = buffer();
  147. while (len > 0) {
  148. int n = is.read(buf, 0, (int) Math.min(len, buf.length));
  149. if (n <= 0)
  150. throw shortInput(len);
  151. dOut.write(buf, 0, n);
  152. len -= n;
  153. }
  154. dOut.flush();
  155. cOut.finish();
  156. } finally {
  157. if (config.getFSyncObjectFiles())
  158. fOut.getChannel().force(true);
  159. fOut.close();
  160. }
  161. delete = false;
  162. return tmp;
  163. } finally {
  164. if (delete)
  165. FileUtils.delete(tmp);
  166. }
  167. }
  168. private File toTemp(final int type, final byte[] buf, final int pos,
  169. final int len) throws IOException, FileNotFoundException {
  170. boolean delete = true;
  171. File tmp = newTempFile();
  172. try {
  173. FileOutputStream fOut = new FileOutputStream(tmp);
  174. try {
  175. OutputStream out = fOut;
  176. if (config.getFSyncObjectFiles())
  177. out = Channels.newOutputStream(fOut.getChannel());
  178. DeflaterOutputStream cOut = compress(out);
  179. writeHeader(cOut, type, len);
  180. cOut.write(buf, pos, len);
  181. cOut.finish();
  182. } finally {
  183. if (config.getFSyncObjectFiles())
  184. fOut.getChannel().force(true);
  185. fOut.close();
  186. }
  187. delete = false;
  188. return tmp;
  189. } finally {
  190. if (delete)
  191. FileUtils.delete(tmp);
  192. }
  193. }
  194. void writeHeader(OutputStream out, final int type, long len)
  195. throws IOException {
  196. out.write(Constants.encodedTypeString(type));
  197. out.write((byte) ' ');
  198. out.write(Constants.encodeASCII(len));
  199. out.write((byte) 0);
  200. }
  201. File newTempFile() throws IOException {
  202. return File.createTempFile("noz", null, db.getDirectory());
  203. }
  204. DeflaterOutputStream compress(final OutputStream out) {
  205. if (deflate == null)
  206. deflate = new Deflater(config.getCompression());
  207. else
  208. deflate.reset();
  209. return new DeflaterOutputStream(out, deflate, 8192);
  210. }
  211. private static EOFException shortInput(long missing) {
  212. return new EOFException("Input did not match supplied length. "
  213. + missing + " bytes are missing.");
  214. }
  215. }