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.

BundleFetchConnection.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  3. * Copyright (C) 2008-2010, Google Inc.
  4. * Copyright (C) 2008-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2009, Sasa Zivkov <sasa.zivkov@sap.com>
  6. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  7. * and other copyright owners as documented in the project's IP log.
  8. *
  9. * This program and the accompanying materials are made available
  10. * under the terms of the Eclipse Distribution License v1.0 which
  11. * accompanies this distribution, is reproduced below, and is
  12. * available at http://www.eclipse.org/org/documents/edl-v10.php
  13. *
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or
  17. * without modification, are permitted provided that the following
  18. * conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials provided
  26. * with the distribution.
  27. *
  28. * - Neither the name of the Eclipse Foundation, Inc. nor the
  29. * names of its contributors may be used to endorse or promote
  30. * products derived from this software without specific prior
  31. * written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  34. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  36. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  37. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  38. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  40. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  43. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  44. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  45. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. package org.eclipse.jgit.transport;
  48. import java.io.BufferedInputStream;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.text.MessageFormat;
  52. import java.util.ArrayList;
  53. import java.util.Collection;
  54. import java.util.Collections;
  55. import java.util.HashMap;
  56. import java.util.LinkedHashMap;
  57. import java.util.List;
  58. import java.util.Map;
  59. import java.util.Set;
  60. import org.eclipse.jgit.JGitText;
  61. import org.eclipse.jgit.errors.MissingBundlePrerequisiteException;
  62. import org.eclipse.jgit.errors.MissingObjectException;
  63. import org.eclipse.jgit.errors.PackProtocolException;
  64. import org.eclipse.jgit.errors.TransportException;
  65. import org.eclipse.jgit.lib.Constants;
  66. import org.eclipse.jgit.lib.ObjectId;
  67. import org.eclipse.jgit.lib.ObjectIdRef;
  68. import org.eclipse.jgit.lib.ProgressMonitor;
  69. import org.eclipse.jgit.lib.Ref;
  70. import org.eclipse.jgit.revwalk.RevCommit;
  71. import org.eclipse.jgit.revwalk.RevFlag;
  72. import org.eclipse.jgit.revwalk.RevObject;
  73. import org.eclipse.jgit.revwalk.RevWalk;
  74. import org.eclipse.jgit.storage.file.PackLock;
  75. import org.eclipse.jgit.util.IO;
  76. import org.eclipse.jgit.util.RawParseUtils;
  77. /**
  78. * Fetch connection for bundle based classes. It used by
  79. * instances of {@link TransportBundle}
  80. */
  81. class BundleFetchConnection extends BaseFetchConnection {
  82. private final Transport transport;
  83. InputStream bin;
  84. final Map<ObjectId, String> prereqs = new HashMap<ObjectId, String>();
  85. private String lockMessage;
  86. private PackLock packLock;
  87. BundleFetchConnection(Transport transportBundle, final InputStream src) throws TransportException {
  88. transport = transportBundle;
  89. bin = new BufferedInputStream(src, IndexPack.BUFFER_SIZE);
  90. try {
  91. switch (readSignature()) {
  92. case 2:
  93. readBundleV2();
  94. break;
  95. default:
  96. throw new TransportException(transport.uri, JGitText.get().notABundle);
  97. }
  98. } catch (TransportException err) {
  99. close();
  100. throw err;
  101. } catch (IOException err) {
  102. close();
  103. throw new TransportException(transport.uri, err.getMessage(), err);
  104. } catch (RuntimeException err) {
  105. close();
  106. throw new TransportException(transport.uri, err.getMessage(), err);
  107. }
  108. }
  109. private int readSignature() throws IOException {
  110. final String rev = readLine(new byte[1024]);
  111. if (TransportBundle.V2_BUNDLE_SIGNATURE.equals(rev))
  112. return 2;
  113. throw new TransportException(transport.uri, JGitText.get().notABundle);
  114. }
  115. private void readBundleV2() throws IOException {
  116. final byte[] hdrbuf = new byte[1024];
  117. final LinkedHashMap<String, Ref> avail = new LinkedHashMap<String, Ref>();
  118. for (;;) {
  119. String line = readLine(hdrbuf);
  120. if (line.length() == 0)
  121. break;
  122. if (line.charAt(0) == '-') {
  123. ObjectId id = ObjectId.fromString(line.substring(1, 41));
  124. String shortDesc = null;
  125. if (line.length() > 42)
  126. shortDesc = line.substring(42);
  127. prereqs.put(id, shortDesc);
  128. continue;
  129. }
  130. final String name = line.substring(41, line.length());
  131. final ObjectId id = ObjectId.fromString(line.substring(0, 40));
  132. final Ref prior = avail.put(name, new ObjectIdRef.Unpeeled(
  133. Ref.Storage.NETWORK, name, id));
  134. if (prior != null)
  135. throw duplicateAdvertisement(name);
  136. }
  137. available(avail);
  138. }
  139. private PackProtocolException duplicateAdvertisement(final String name) {
  140. return new PackProtocolException(transport.uri,
  141. MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, name));
  142. }
  143. private String readLine(final byte[] hdrbuf) throws IOException {
  144. bin.mark(hdrbuf.length);
  145. final int cnt = bin.read(hdrbuf);
  146. int lf = 0;
  147. while (lf < cnt && hdrbuf[lf] != '\n')
  148. lf++;
  149. bin.reset();
  150. IO.skipFully(bin, lf);
  151. if (lf < cnt && hdrbuf[lf] == '\n')
  152. IO.skipFully(bin, 1);
  153. return RawParseUtils.decode(Constants.CHARSET, hdrbuf, 0, lf);
  154. }
  155. public boolean didFetchTestConnectivity() {
  156. return false;
  157. }
  158. @Override
  159. protected void doFetch(final ProgressMonitor monitor,
  160. final Collection<Ref> want, final Set<ObjectId> have)
  161. throws TransportException {
  162. verifyPrerequisites();
  163. try {
  164. final IndexPack ip = newIndexPack();
  165. ip.index(monitor);
  166. packLock = ip.renameAndOpenPack(lockMessage);
  167. } catch (IOException err) {
  168. close();
  169. throw new TransportException(transport.uri, err.getMessage(), err);
  170. } catch (RuntimeException err) {
  171. close();
  172. throw new TransportException(transport.uri, err.getMessage(), err);
  173. }
  174. }
  175. public void setPackLockMessage(final String message) {
  176. lockMessage = message;
  177. }
  178. public Collection<PackLock> getPackLocks() {
  179. if (packLock != null)
  180. return Collections.singleton(packLock);
  181. return Collections.<PackLock> emptyList();
  182. }
  183. private IndexPack newIndexPack() throws IOException {
  184. final IndexPack ip = IndexPack.create(transport.local, bin);
  185. ip.setFixThin(true);
  186. ip.setObjectChecking(transport.isCheckFetchedObjects());
  187. return ip;
  188. }
  189. private void verifyPrerequisites() throws TransportException {
  190. if (prereqs.isEmpty())
  191. return;
  192. final RevWalk rw = new RevWalk(transport.local);
  193. try {
  194. final RevFlag PREREQ = rw.newFlag("PREREQ");
  195. final RevFlag SEEN = rw.newFlag("SEEN");
  196. final Map<ObjectId, String> missing = new HashMap<ObjectId, String>();
  197. final List<RevObject> commits = new ArrayList<RevObject>();
  198. for (final Map.Entry<ObjectId, String> e : prereqs.entrySet()) {
  199. ObjectId p = e.getKey();
  200. try {
  201. final RevCommit c = rw.parseCommit(p);
  202. if (!c.has(PREREQ)) {
  203. c.add(PREREQ);
  204. commits.add(c);
  205. }
  206. } catch (MissingObjectException notFound) {
  207. missing.put(p, e.getValue());
  208. } catch (IOException err) {
  209. throw new TransportException(transport.uri, MessageFormat
  210. .format(JGitText.get().cannotReadCommit, p.name()),
  211. err);
  212. }
  213. }
  214. if (!missing.isEmpty())
  215. throw new MissingBundlePrerequisiteException(transport.uri,
  216. missing);
  217. for (final Ref r : transport.local.getAllRefs().values()) {
  218. try {
  219. rw.markStart(rw.parseCommit(r.getObjectId()));
  220. } catch (IOException readError) {
  221. // If we cannot read the value of the ref skip it.
  222. }
  223. }
  224. int remaining = commits.size();
  225. try {
  226. RevCommit c;
  227. while ((c = rw.next()) != null) {
  228. if (c.has(PREREQ)) {
  229. c.add(SEEN);
  230. if (--remaining == 0)
  231. break;
  232. }
  233. }
  234. } catch (IOException err) {
  235. throw new TransportException(transport.uri,
  236. JGitText.get().cannotReadObject, err);
  237. }
  238. if (remaining > 0) {
  239. for (final RevObject o : commits) {
  240. if (!o.has(SEEN))
  241. missing.put(o, prereqs.get(o));
  242. }
  243. throw new MissingBundlePrerequisiteException(transport.uri,
  244. missing);
  245. }
  246. } finally {
  247. rw.release();
  248. }
  249. }
  250. @Override
  251. public void close() {
  252. if (bin != null) {
  253. try {
  254. bin.close();
  255. } catch (IOException ie) {
  256. // Ignore close failures.
  257. } finally {
  258. bin = null;
  259. }
  260. }
  261. }
  262. }