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.

WindowCursor.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.internal.storage.file;
  45. import java.io.IOException;
  46. import java.security.MessageDigest;
  47. import java.text.MessageFormat;
  48. import java.util.Arrays;
  49. import java.util.Collection;
  50. import java.util.Collections;
  51. import java.util.HashSet;
  52. import java.util.List;
  53. import java.util.Set;
  54. import java.util.zip.DataFormatException;
  55. import java.util.zip.Inflater;
  56. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  57. import org.eclipse.jgit.errors.MissingObjectException;
  58. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  59. import org.eclipse.jgit.internal.JGitText;
  60. import org.eclipse.jgit.internal.storage.pack.CachedPack;
  61. import org.eclipse.jgit.internal.storage.pack.ObjectReuseAsIs;
  62. import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
  63. import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
  64. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  65. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  66. import org.eclipse.jgit.lib.AnyObjectId;
  67. import org.eclipse.jgit.lib.BitmapIndex;
  68. import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
  69. import org.eclipse.jgit.lib.Constants;
  70. import org.eclipse.jgit.lib.InflaterCache;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.ObjectLoader;
  73. import org.eclipse.jgit.lib.ObjectReader;
  74. import org.eclipse.jgit.lib.ProgressMonitor;
  75. /** Active handle to a ByteWindow. */
  76. final class WindowCursor extends ObjectReader implements ObjectReuseAsIs {
  77. /** Temporary buffer large enough for at least one raw object id. */
  78. final byte[] tempId = new byte[Constants.OBJECT_ID_LENGTH];
  79. private Inflater inf;
  80. private ByteWindow window;
  81. private DeltaBaseCache baseCache;
  82. final FileObjectDatabase db;
  83. WindowCursor(FileObjectDatabase db) {
  84. this.db = db;
  85. }
  86. DeltaBaseCache getDeltaBaseCache() {
  87. if (baseCache == null)
  88. baseCache = new DeltaBaseCache();
  89. return baseCache;
  90. }
  91. @Override
  92. public ObjectReader newReader() {
  93. return new WindowCursor(db);
  94. }
  95. @Override
  96. public BitmapIndex getBitmapIndex() throws IOException {
  97. for (PackFile pack : db.getPacks()) {
  98. PackBitmapIndex index = pack.getBitmapIndex();
  99. if (index != null)
  100. return new BitmapIndexImpl(index);
  101. }
  102. return null;
  103. }
  104. public Collection<CachedPack> getCachedPacksAndUpdate(
  105. BitmapBuilder needBitmap) throws IOException {
  106. for (PackFile pack : db.getPacks()) {
  107. PackBitmapIndex index = pack.getBitmapIndex();
  108. if (needBitmap.removeAllOrNone(index))
  109. return Collections.<CachedPack> singletonList(
  110. new LocalCachedPack(Collections.singletonList(pack)));
  111. }
  112. return Collections.emptyList();
  113. }
  114. @Override
  115. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  116. throws IOException {
  117. if (id.isComplete())
  118. return Collections.singleton(id.toObjectId());
  119. HashSet<ObjectId> matches = new HashSet<ObjectId>(4);
  120. db.resolve(matches, id);
  121. return matches;
  122. }
  123. public boolean has(AnyObjectId objectId) throws IOException {
  124. return db.has(objectId);
  125. }
  126. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  127. throws MissingObjectException, IncorrectObjectTypeException,
  128. IOException {
  129. final ObjectLoader ldr = db.openObject(this, objectId);
  130. if (ldr == null) {
  131. if (typeHint == OBJ_ANY)
  132. throw new MissingObjectException(objectId.copy(), "unknown");
  133. throw new MissingObjectException(objectId.copy(), typeHint);
  134. }
  135. if (typeHint != OBJ_ANY && ldr.getType() != typeHint)
  136. throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
  137. return ldr;
  138. }
  139. @Override
  140. public Set<ObjectId> getShallowCommits() throws IOException {
  141. return db.getShallowCommits();
  142. }
  143. public long getObjectSize(AnyObjectId objectId, int typeHint)
  144. throws MissingObjectException, IncorrectObjectTypeException,
  145. IOException {
  146. long sz = db.getObjectSize(this, objectId);
  147. if (sz < 0) {
  148. if (typeHint == OBJ_ANY)
  149. throw new MissingObjectException(objectId.copy(), "unknown");
  150. throw new MissingObjectException(objectId.copy(), typeHint);
  151. }
  152. return sz;
  153. }
  154. public LocalObjectToPack newObjectToPack(AnyObjectId objectId, int type) {
  155. return new LocalObjectToPack(objectId, type);
  156. }
  157. public void selectObjectRepresentation(PackWriter packer,
  158. ProgressMonitor monitor, Iterable<ObjectToPack> objects)
  159. throws IOException, MissingObjectException {
  160. for (ObjectToPack otp : objects) {
  161. db.selectObjectRepresentation(packer, otp, this);
  162. monitor.update(1);
  163. }
  164. }
  165. public void copyObjectAsIs(PackOutputStream out, ObjectToPack otp,
  166. boolean validate) throws IOException,
  167. StoredObjectRepresentationNotAvailableException {
  168. LocalObjectToPack src = (LocalObjectToPack) otp;
  169. src.pack.copyAsIs(out, src, validate, this);
  170. }
  171. public void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  172. throws IOException {
  173. for (ObjectToPack otp : list)
  174. out.writeObject(otp);
  175. }
  176. /**
  177. * Copy bytes from the window to a caller supplied buffer.
  178. *
  179. * @param pack
  180. * the file the desired window is stored within.
  181. * @param position
  182. * position within the file to read from.
  183. * @param dstbuf
  184. * destination buffer to copy into.
  185. * @param dstoff
  186. * offset within <code>dstbuf</code> to start copying into.
  187. * @param cnt
  188. * number of bytes to copy. This value may exceed the number of
  189. * bytes remaining in the window starting at offset
  190. * <code>pos</code>.
  191. * @return number of bytes actually copied; this may be less than
  192. * <code>cnt</code> if <code>cnt</code> exceeded the number of bytes
  193. * available.
  194. * @throws IOException
  195. * this cursor does not match the provider or id and the proper
  196. * window could not be acquired through the provider's cache.
  197. */
  198. int copy(final PackFile pack, long position, final byte[] dstbuf,
  199. int dstoff, final int cnt) throws IOException {
  200. final long length = pack.length;
  201. int need = cnt;
  202. while (need > 0 && position < length) {
  203. pin(pack, position);
  204. final int r = window.copy(position, dstbuf, dstoff, need);
  205. position += r;
  206. dstoff += r;
  207. need -= r;
  208. }
  209. return cnt - need;
  210. }
  211. public void copyPackAsIs(PackOutputStream out, CachedPack pack,
  212. boolean validate) throws IOException {
  213. ((LocalCachedPack) pack).copyAsIs(out, validate, this);
  214. }
  215. void copyPackAsIs(final PackFile pack, final long length, boolean validate,
  216. final PackOutputStream out) throws IOException {
  217. MessageDigest md = null;
  218. if (validate) {
  219. md = Constants.newMessageDigest();
  220. byte[] buf = out.getCopyBuffer();
  221. pin(pack, 0);
  222. if (window.copy(0, buf, 0, 12) != 12) {
  223. pack.setInvalid();
  224. throw new IOException(JGitText.get().packfileIsTruncated);
  225. }
  226. md.update(buf, 0, 12);
  227. }
  228. long position = 12;
  229. long remaining = length - (12 + 20);
  230. while (0 < remaining) {
  231. pin(pack, position);
  232. int ptr = (int) (position - window.start);
  233. int n = (int) Math.min(window.size() - ptr, remaining);
  234. window.write(out, position, n, md);
  235. position += n;
  236. remaining -= n;
  237. }
  238. if (md != null) {
  239. byte[] buf = new byte[20];
  240. byte[] actHash = md.digest();
  241. pin(pack, position);
  242. if (window.copy(position, buf, 0, 20) != 20) {
  243. pack.setInvalid();
  244. throw new IOException(JGitText.get().packfileIsTruncated);
  245. }
  246. if (!Arrays.equals(actHash, buf)) {
  247. pack.setInvalid();
  248. throw new IOException(MessageFormat.format(
  249. JGitText.get().packfileCorruptionDetected, pack
  250. .getPackFile().getPath()));
  251. }
  252. }
  253. }
  254. /**
  255. * Inflate a region of the pack starting at {@code position}.
  256. *
  257. * @param pack
  258. * the file the desired window is stored within.
  259. * @param position
  260. * position within the file to read from.
  261. * @param dstbuf
  262. * destination buffer the inflater should output decompressed
  263. * data to. Must be large enough to store the entire stream,
  264. * unless headerOnly is true.
  265. * @param headerOnly
  266. * if true the caller wants only {@code dstbuf.length} bytes.
  267. * @return number of bytes inflated into <code>dstbuf</code>.
  268. * @throws IOException
  269. * this cursor does not match the provider or id and the proper
  270. * window could not be acquired through the provider's cache.
  271. * @throws DataFormatException
  272. * the inflater encountered an invalid chunk of data. Data
  273. * stream corruption is likely.
  274. */
  275. int inflate(final PackFile pack, long position, final byte[] dstbuf,
  276. boolean headerOnly) throws IOException, DataFormatException {
  277. prepareInflater();
  278. pin(pack, position);
  279. position += window.setInput(position, inf);
  280. for (int dstoff = 0;;) {
  281. int n = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
  282. dstoff += n;
  283. if (inf.finished() || (headerOnly && dstoff == dstbuf.length))
  284. return dstoff;
  285. if (inf.needsInput()) {
  286. pin(pack, position);
  287. position += window.setInput(position, inf);
  288. } else if (n == 0)
  289. throw new DataFormatException();
  290. }
  291. }
  292. ByteArrayWindow quickCopy(PackFile p, long pos, long cnt)
  293. throws IOException {
  294. pin(p, pos);
  295. if (window instanceof ByteArrayWindow
  296. && window.contains(p, pos + (cnt - 1)))
  297. return (ByteArrayWindow) window;
  298. return null;
  299. }
  300. Inflater inflater() {
  301. prepareInflater();
  302. return inf;
  303. }
  304. private void prepareInflater() {
  305. if (inf == null)
  306. inf = InflaterCache.get();
  307. else
  308. inf.reset();
  309. }
  310. void pin(final PackFile pack, final long position)
  311. throws IOException {
  312. final ByteWindow w = window;
  313. if (w == null || !w.contains(pack, position)) {
  314. // If memory is low, we may need what is in our window field to
  315. // be cleaned up by the GC during the get for the next window.
  316. // So we always clear it, even though we are just going to set
  317. // it again.
  318. //
  319. window = null;
  320. window = WindowCache.get(pack, position);
  321. }
  322. }
  323. int getStreamFileThreshold() {
  324. return WindowCache.getStreamFileThreshold();
  325. }
  326. /** Release the current window cursor. */
  327. public void release() {
  328. window = null;
  329. baseCache = null;
  330. try {
  331. InflaterCache.release(inf);
  332. } finally {
  333. inf = null;
  334. }
  335. }
  336. }