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

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