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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /** Creates loose objects in a {@link ObjectDirectory}. */
  66. class ObjectDirectoryInserter extends ObjectInserter {
  67. private final FileObjectDatabase db;
  68. private final WriteConfig config;
  69. private Deflater deflate;
  70. ObjectDirectoryInserter(final FileObjectDatabase dest, final Config cfg) {
  71. db = dest;
  72. config = cfg.get(WriteConfig.KEY);
  73. }
  74. @Override
  75. public ObjectId insert(final int type, long len, final InputStream is)
  76. throws IOException {
  77. final MessageDigest md = digest();
  78. final File tmp = toTemp(md, type, len, is);
  79. final ObjectId id = ObjectId.fromRaw(md.digest());
  80. switch (db.insertUnpackedObject(tmp, id, false /* no duplicate */)) {
  81. case INSERTED:
  82. case EXISTS_PACKED:
  83. case EXISTS_LOOSE:
  84. return id;
  85. case FAILURE:
  86. default:
  87. break;
  88. }
  89. final File dst = db.fileFor(id);
  90. throw new ObjectWritingException("Unable to create new object: " + dst);
  91. }
  92. @Override
  93. public PackParser newPackParser(InputStream in) throws IOException {
  94. return new ObjectDirectoryPackParser(db, in);
  95. }
  96. @Override
  97. public void flush() throws IOException {
  98. // Do nothing. Objects are immediately visible.
  99. }
  100. @Override
  101. public void release() {
  102. if (deflate != null) {
  103. try {
  104. deflate.end();
  105. } finally {
  106. deflate = null;
  107. }
  108. }
  109. }
  110. private File toTemp(final MessageDigest md, final int type, long len,
  111. final InputStream is) throws IOException, FileNotFoundException,
  112. Error {
  113. boolean delete = true;
  114. File tmp = newTempFile();
  115. try {
  116. FileOutputStream fOut = new FileOutputStream(tmp);
  117. try {
  118. OutputStream out = fOut;
  119. if (config.getFSyncObjectFiles())
  120. out = Channels.newOutputStream(fOut.getChannel());
  121. DeflaterOutputStream cOut = compress(out);
  122. DigestOutputStream dOut = new DigestOutputStream(cOut, md);
  123. writeHeader(dOut, type, len);
  124. final byte[] buf = buffer();
  125. while (len > 0) {
  126. int n = is.read(buf, 0, (int) Math.min(len, buf.length));
  127. if (n <= 0)
  128. throw shortInput(len);
  129. dOut.write(buf, 0, n);
  130. len -= n;
  131. }
  132. dOut.flush();
  133. cOut.finish();
  134. } finally {
  135. if (config.getFSyncObjectFiles())
  136. fOut.getChannel().force(true);
  137. fOut.close();
  138. }
  139. delete = false;
  140. return tmp;
  141. } finally {
  142. if (delete)
  143. FileUtils.delete(tmp);
  144. }
  145. }
  146. void writeHeader(OutputStream out, final int type, long len)
  147. throws IOException {
  148. out.write(Constants.encodedTypeString(type));
  149. out.write((byte) ' ');
  150. out.write(Constants.encodeASCII(len));
  151. out.write((byte) 0);
  152. }
  153. File newTempFile() throws IOException {
  154. return File.createTempFile("noz", null, db.getDirectory());
  155. }
  156. DeflaterOutputStream compress(final OutputStream out) {
  157. if (deflate == null)
  158. deflate = new Deflater(config.getCompression());
  159. else
  160. deflate.reset();
  161. return new DeflaterOutputStream(out, deflate);
  162. }
  163. private static EOFException shortInput(long missing) {
  164. return new EOFException("Input did not match supplied length. "
  165. + missing + " bytes are missing.");
  166. }
  167. }