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.

UnpackedObject.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2010, 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.BufferedInputStream;
  47. import java.io.ByteArrayInputStream;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.FileNotFoundException;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.util.zip.Inflater;
  54. import java.util.zip.InflaterInputStream;
  55. import java.util.zip.ZipException;
  56. import org.eclipse.jgit.JGitText;
  57. import org.eclipse.jgit.errors.CorruptObjectException;
  58. import org.eclipse.jgit.errors.LargeObjectException;
  59. import org.eclipse.jgit.errors.MissingObjectException;
  60. import org.eclipse.jgit.lib.AnyObjectId;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.InflaterCache;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.ObjectLoader;
  65. import org.eclipse.jgit.lib.ObjectStream;
  66. import org.eclipse.jgit.util.IO;
  67. import org.eclipse.jgit.util.MutableInteger;
  68. import org.eclipse.jgit.util.RawParseUtils;
  69. /**
  70. * Loose object loader. This class loads an object not stored in a pack.
  71. */
  72. public class UnpackedObject {
  73. private static final int BUFFER_SIZE = 8192;
  74. /**
  75. * Parse an object from the unpacked object format.
  76. *
  77. * @param raw
  78. * complete contents of the compressed object.
  79. * @param id
  80. * expected ObjectId of the object, used only for error reporting
  81. * in exceptions.
  82. * @return loader to read the inflated contents.
  83. * @throws IOException
  84. * the object cannot be parsed.
  85. */
  86. public static ObjectLoader parse(byte[] raw, AnyObjectId id)
  87. throws IOException {
  88. WindowCursor wc = new WindowCursor(null);
  89. try {
  90. return open(new ByteArrayInputStream(raw), null, id, wc);
  91. } finally {
  92. wc.release();
  93. }
  94. }
  95. static ObjectLoader open(InputStream in, File path, AnyObjectId id,
  96. WindowCursor wc) throws IOException {
  97. try {
  98. in = buffer(in);
  99. in.mark(20);
  100. final byte[] hdr = new byte[64];
  101. IO.readFully(in, hdr, 0, 2);
  102. if (isStandardFormat(hdr)) {
  103. in.reset();
  104. in = inflate(in, wc);
  105. int avail = readSome(in, hdr, 0, 64);
  106. if (avail < 5)
  107. throw new CorruptObjectException(id,
  108. JGitText.get().corruptObjectNoHeader);
  109. final MutableInteger p = new MutableInteger();
  110. int type = Constants.decodeTypeString(id, hdr, (byte) ' ', p);
  111. long size = RawParseUtils.parseLongBase10(hdr, p.value, p);
  112. if (size < 0)
  113. throw new CorruptObjectException(id,
  114. JGitText.get().corruptObjectNegativeSize);
  115. if (hdr[p.value++] != 0)
  116. throw new CorruptObjectException(id,
  117. JGitText.get().corruptObjectGarbageAfterSize);
  118. if (path == null && Integer.MAX_VALUE < size)
  119. throw new LargeObjectException(id.copy());
  120. if (size < wc.getStreamFileThreshold() || path == null) {
  121. byte[] data = new byte[(int) size];
  122. int n = avail - p.value;
  123. if (n > 0)
  124. System.arraycopy(hdr, p.value, data, 0, n);
  125. IO.readFully(in, data, n, data.length - n);
  126. return new ObjectLoader.SmallObject(type, data);
  127. }
  128. return new LargeObject(type, size, path, id, wc.db);
  129. } else {
  130. readSome(in, hdr, 2, 18);
  131. int c = hdr[0] & 0xff;
  132. int type = (c >> 4) & 7;
  133. long size = c & 15;
  134. int shift = 4;
  135. int p = 1;
  136. while ((c & 0x80) != 0) {
  137. c = hdr[p++] & 0xff;
  138. size += (c & 0x7f) << shift;
  139. shift += 7;
  140. }
  141. switch (type) {
  142. case Constants.OBJ_COMMIT:
  143. case Constants.OBJ_TREE:
  144. case Constants.OBJ_BLOB:
  145. case Constants.OBJ_TAG:
  146. // Acceptable types for a loose object.
  147. break;
  148. default:
  149. throw new CorruptObjectException(id,
  150. JGitText.get().corruptObjectInvalidType);
  151. }
  152. if (path == null && Integer.MAX_VALUE < size)
  153. throw new LargeObjectException(id.copy());
  154. if (size < wc.getStreamFileThreshold() || path == null) {
  155. in.reset();
  156. IO.skipFully(in, p);
  157. in = inflate(in, wc);
  158. byte[] data = new byte[(int) size];
  159. IO.readFully(in, data, 0, data.length);
  160. return new ObjectLoader.SmallObject(type, data);
  161. }
  162. return new LargeObject(type, size, path, id, wc.db);
  163. }
  164. } catch (ZipException badStream) {
  165. throw new CorruptObjectException(id,
  166. JGitText.get().corruptObjectBadStream);
  167. }
  168. }
  169. private static boolean isStandardFormat(final byte[] hdr) {
  170. // Try to determine if this is a standard format loose object or
  171. // a pack style loose object. The standard format is completely
  172. // compressed with zlib so the first byte must be 0x78 (15-bit
  173. // window size, deflated) and the first 16 bit word must be
  174. // evenly divisible by 31. Otherwise its a pack style object.
  175. //
  176. final int fb = hdr[0] & 0xff;
  177. return fb == 0x78 && (((fb << 8) | hdr[1] & 0xff) % 31) == 0;
  178. }
  179. private static InputStream inflate(InputStream in, final ObjectId id) {
  180. final Inflater inf = InflaterCache.get();
  181. return new InflaterInputStream(in, inf) {
  182. @Override
  183. public int read(byte[] b, int off, int cnt) throws IOException {
  184. try {
  185. return super.read(b, off, cnt);
  186. } catch (ZipException badStream) {
  187. throw new CorruptObjectException(id,
  188. JGitText.get().corruptObjectBadStream);
  189. }
  190. }
  191. @Override
  192. public void close() throws IOException {
  193. super.close();
  194. InflaterCache.release(inf);
  195. }
  196. };
  197. }
  198. private static InputStream inflate(InputStream in, WindowCursor wc) {
  199. return new InflaterInputStream(in, wc.inflater(), BUFFER_SIZE);
  200. }
  201. private static BufferedInputStream buffer(InputStream in) {
  202. return new BufferedInputStream(in, BUFFER_SIZE);
  203. }
  204. private static int readSome(InputStream in, final byte[] hdr, int off,
  205. int cnt) throws IOException {
  206. int avail = 0;
  207. while (0 < cnt) {
  208. int n = in.read(hdr, off, cnt);
  209. if (n < 0)
  210. break;
  211. avail += n;
  212. cnt -= n;
  213. }
  214. return avail;
  215. }
  216. private static final class LargeObject extends ObjectLoader {
  217. private final int type;
  218. private final long size;
  219. private final File path;
  220. private final ObjectId id;
  221. private final FileObjectDatabase source;
  222. private LargeObject(int type, long size, File path, AnyObjectId id,
  223. FileObjectDatabase db) {
  224. this.type = type;
  225. this.size = size;
  226. this.path = path;
  227. this.id = id.copy();
  228. this.source = db;
  229. }
  230. @Override
  231. public int getType() {
  232. return type;
  233. }
  234. @Override
  235. public long getSize() {
  236. return size;
  237. }
  238. @Override
  239. public boolean isLarge() {
  240. return true;
  241. }
  242. @Override
  243. public byte[] getCachedBytes() throws LargeObjectException {
  244. throw new LargeObjectException(id);
  245. }
  246. @Override
  247. public ObjectStream openStream() throws MissingObjectException,
  248. IOException {
  249. InputStream in;
  250. try {
  251. in = buffer(new FileInputStream(path));
  252. } catch (FileNotFoundException gone) {
  253. // If the loose file no longer exists, it may have been
  254. // moved into a pack file in the mean time. Try again
  255. // to locate the object.
  256. //
  257. return source.open(id, type).openStream();
  258. }
  259. boolean ok = false;
  260. try {
  261. final byte[] hdr = new byte[64];
  262. in.mark(20);
  263. IO.readFully(in, hdr, 0, 2);
  264. if (isStandardFormat(hdr)) {
  265. in.reset();
  266. in = buffer(inflate(in, id));
  267. while (0 < in.read())
  268. continue;
  269. } else {
  270. readSome(in, hdr, 2, 18);
  271. int c = hdr[0] & 0xff;
  272. int p = 1;
  273. while ((c & 0x80) != 0)
  274. c = hdr[p++] & 0xff;
  275. in.reset();
  276. IO.skipFully(in, p);
  277. in = buffer(inflate(in, id));
  278. }
  279. ok = true;
  280. return new ObjectStream.Filter(type, size, in);
  281. } finally {
  282. if (!ok)
  283. in.close();
  284. }
  285. }
  286. }
  287. }