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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.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.lib.AbbreviatedObjectId;
  61. import org.eclipse.jgit.lib.AnyObjectId;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.InflaterCache;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectLoader;
  66. import org.eclipse.jgit.lib.ObjectReader;
  67. import org.eclipse.jgit.lib.ProgressMonitor;
  68. import org.eclipse.jgit.storage.pack.CachedPack;
  69. import org.eclipse.jgit.storage.pack.ObjectReuseAsIs;
  70. import org.eclipse.jgit.storage.pack.ObjectToPack;
  71. import org.eclipse.jgit.storage.pack.PackOutputStream;
  72. import org.eclipse.jgit.storage.pack.PackWriter;
  73. /** Active handle to a ByteWindow. */
  74. final class WindowCursor extends ObjectReader implements ObjectReuseAsIs {
  75. /** Temporary buffer large enough for at least one raw object id. */
  76. final byte[] tempId = new byte[Constants.OBJECT_ID_LENGTH];
  77. private Inflater inf;
  78. private ByteWindow window;
  79. private DeltaBaseCache baseCache;
  80. final FileObjectDatabase db;
  81. WindowCursor(FileObjectDatabase db) {
  82. this.db = db;
  83. }
  84. DeltaBaseCache getDeltaBaseCache() {
  85. if (baseCache == null)
  86. baseCache = new DeltaBaseCache();
  87. return baseCache;
  88. }
  89. @Override
  90. public ObjectReader newReader() {
  91. return new WindowCursor(db);
  92. }
  93. @Override
  94. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  95. throws IOException {
  96. if (id.isComplete())
  97. return Collections.singleton(id.toObjectId());
  98. HashSet<ObjectId> matches = new HashSet<ObjectId>(4);
  99. db.resolve(matches, id);
  100. return matches;
  101. }
  102. public boolean has(AnyObjectId objectId) throws IOException {
  103. return db.has(objectId);
  104. }
  105. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  106. throws MissingObjectException, IncorrectObjectTypeException,
  107. IOException {
  108. final ObjectLoader ldr = db.openObject(this, objectId);
  109. if (ldr == null) {
  110. if (typeHint == OBJ_ANY)
  111. throw new MissingObjectException(objectId.copy(), "unknown");
  112. throw new MissingObjectException(objectId.copy(), typeHint);
  113. }
  114. if (typeHint != OBJ_ANY && ldr.getType() != typeHint)
  115. throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
  116. return ldr;
  117. }
  118. @Override
  119. public Set<ObjectId> getShallowCommits() throws IOException {
  120. return db.getShallowCommits();
  121. }
  122. public long getObjectSize(AnyObjectId objectId, int typeHint)
  123. throws MissingObjectException, IncorrectObjectTypeException,
  124. IOException {
  125. long sz = db.getObjectSize(this, objectId);
  126. if (sz < 0) {
  127. if (typeHint == OBJ_ANY)
  128. throw new MissingObjectException(objectId.copy(), "unknown");
  129. throw new MissingObjectException(objectId.copy(), typeHint);
  130. }
  131. return sz;
  132. }
  133. public LocalObjectToPack newObjectToPack(AnyObjectId objectId, int type) {
  134. return new LocalObjectToPack(objectId, type);
  135. }
  136. public void selectObjectRepresentation(PackWriter packer,
  137. ProgressMonitor monitor, Iterable<ObjectToPack> objects)
  138. throws IOException, MissingObjectException {
  139. for (ObjectToPack otp : objects) {
  140. db.selectObjectRepresentation(packer, otp, this);
  141. monitor.update(1);
  142. }
  143. }
  144. public void copyObjectAsIs(PackOutputStream out, ObjectToPack otp,
  145. boolean validate) throws IOException,
  146. StoredObjectRepresentationNotAvailableException {
  147. LocalObjectToPack src = (LocalObjectToPack) otp;
  148. src.pack.copyAsIs(out, src, validate, this);
  149. }
  150. public void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  151. throws IOException {
  152. for (ObjectToPack otp : list)
  153. out.writeObject(otp);
  154. }
  155. @SuppressWarnings("unchecked")
  156. public Collection<CachedPack> getCachedPacks() throws IOException {
  157. return (Collection<CachedPack>) db.getCachedPacks();
  158. }
  159. /**
  160. * Copy bytes from the window to a caller supplied buffer.
  161. *
  162. * @param pack
  163. * the file the desired window is stored within.
  164. * @param position
  165. * position within the file to read from.
  166. * @param dstbuf
  167. * destination buffer to copy into.
  168. * @param dstoff
  169. * offset within <code>dstbuf</code> to start copying into.
  170. * @param cnt
  171. * number of bytes to copy. This value may exceed the number of
  172. * bytes remaining in the window starting at offset
  173. * <code>pos</code>.
  174. * @return number of bytes actually copied; this may be less than
  175. * <code>cnt</code> if <code>cnt</code> exceeded the number of bytes
  176. * available.
  177. * @throws IOException
  178. * this cursor does not match the provider or id and the proper
  179. * window could not be acquired through the provider's cache.
  180. */
  181. int copy(final PackFile pack, long position, final byte[] dstbuf,
  182. int dstoff, final int cnt) throws IOException {
  183. final long length = pack.length;
  184. int need = cnt;
  185. while (need > 0 && position < length) {
  186. pin(pack, position);
  187. final int r = window.copy(position, dstbuf, dstoff, need);
  188. position += r;
  189. dstoff += r;
  190. need -= r;
  191. }
  192. return cnt - need;
  193. }
  194. public void copyPackAsIs(PackOutputStream out, CachedPack pack,
  195. boolean validate) throws IOException {
  196. ((LocalCachedPack) pack).copyAsIs(out, validate, this);
  197. }
  198. void copyPackAsIs(final PackFile pack, final long length, boolean validate,
  199. final PackOutputStream out) throws IOException {
  200. MessageDigest md = null;
  201. if (validate) {
  202. md = Constants.newMessageDigest();
  203. byte[] buf = out.getCopyBuffer();
  204. pin(pack, 0);
  205. if (window.copy(0, buf, 0, 12) != 12) {
  206. pack.setInvalid();
  207. throw new IOException(JGitText.get().packfileIsTruncated);
  208. }
  209. md.update(buf, 0, 12);
  210. }
  211. long position = 12;
  212. long remaining = length - (12 + 20);
  213. while (0 < remaining) {
  214. pin(pack, position);
  215. int ptr = (int) (position - window.start);
  216. int n = (int) Math.min(window.size() - ptr, remaining);
  217. window.write(out, position, n, md);
  218. position += n;
  219. remaining -= n;
  220. }
  221. if (md != null) {
  222. byte[] buf = new byte[20];
  223. byte[] actHash = md.digest();
  224. pin(pack, position);
  225. if (window.copy(position, buf, 0, 20) != 20) {
  226. pack.setInvalid();
  227. throw new IOException(JGitText.get().packfileIsTruncated);
  228. }
  229. if (!Arrays.equals(actHash, buf)) {
  230. pack.setInvalid();
  231. throw new IOException(MessageFormat.format(
  232. JGitText.get().packfileCorruptionDetected, pack
  233. .getPackFile().getPath()));
  234. }
  235. }
  236. }
  237. /**
  238. * Inflate a region of the pack starting at {@code position}.
  239. *
  240. * @param pack
  241. * the file the desired window is stored within.
  242. * @param position
  243. * position within the file to read from.
  244. * @param dstbuf
  245. * destination buffer the inflater should output decompressed
  246. * data to.
  247. * @param dstoff
  248. * current offset within <code>dstbuf</code> to inflate into.
  249. * @return updated <code>dstoff</code> based on the number of bytes
  250. * successfully inflated into <code>dstbuf</code>.
  251. * @throws IOException
  252. * this cursor does not match the provider or id and the proper
  253. * window could not be acquired through the provider's cache.
  254. * @throws DataFormatException
  255. * the inflater encountered an invalid chunk of data. Data
  256. * stream corruption is likely.
  257. */
  258. int inflate(final PackFile pack, long position, final byte[] dstbuf,
  259. int dstoff) throws IOException, DataFormatException {
  260. prepareInflater();
  261. pin(pack, position);
  262. position += window.setInput(position, inf);
  263. do {
  264. int n = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
  265. if (n == 0) {
  266. if (inf.needsInput()) {
  267. pin(pack, position);
  268. position += window.setInput(position, inf);
  269. } else if (inf.finished())
  270. return dstoff;
  271. else
  272. throw new DataFormatException();
  273. }
  274. dstoff += n;
  275. } while (dstoff < dstbuf.length);
  276. return dstoff;
  277. }
  278. ByteArrayWindow quickCopy(PackFile p, long pos, long cnt)
  279. throws IOException {
  280. pin(p, pos);
  281. if (window instanceof ByteArrayWindow
  282. && window.contains(p, pos + (cnt - 1)))
  283. return (ByteArrayWindow) window;
  284. return null;
  285. }
  286. Inflater inflater() {
  287. prepareInflater();
  288. return inf;
  289. }
  290. private void prepareInflater() {
  291. if (inf == null)
  292. inf = InflaterCache.get();
  293. else
  294. inf.reset();
  295. }
  296. void pin(final PackFile pack, final long position)
  297. throws IOException {
  298. final ByteWindow w = window;
  299. if (w == null || !w.contains(pack, position)) {
  300. // If memory is low, we may need what is in our window field to
  301. // be cleaned up by the GC during the get for the next window.
  302. // So we always clear it, even though we are just going to set
  303. // it again.
  304. //
  305. window = null;
  306. window = WindowCache.get(pack, position);
  307. }
  308. }
  309. int getStreamFileThreshold() {
  310. return WindowCache.getStreamFileThreshold();
  311. }
  312. /** Release the current window cursor. */
  313. public void release() {
  314. window = null;
  315. baseCache = null;
  316. try {
  317. InflaterCache.release(inf);
  318. } finally {
  319. inf = null;
  320. }
  321. }
  322. }