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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.NullProgressMonitor;
  67. import org.eclipse.jgit.lib.ObjectId;
  68. import org.eclipse.jgit.lib.ObjectIdRef;
  69. import org.eclipse.jgit.lib.ObjectInserter;
  70. import org.eclipse.jgit.lib.ProgressMonitor;
  71. import org.eclipse.jgit.lib.Ref;
  72. import org.eclipse.jgit.revwalk.RevCommit;
  73. import org.eclipse.jgit.revwalk.RevFlag;
  74. import org.eclipse.jgit.revwalk.RevObject;
  75. import org.eclipse.jgit.revwalk.RevWalk;
  76. import org.eclipse.jgit.storage.file.PackLock;
  77. import org.eclipse.jgit.util.IO;
  78. import org.eclipse.jgit.util.RawParseUtils;
  79. /**
  80. * Fetch connection for bundle based classes. It used by
  81. * instances of {@link TransportBundle}
  82. */
  83. class BundleFetchConnection extends BaseFetchConnection {
  84. private final Transport transport;
  85. InputStream bin;
  86. final Map<ObjectId, String> prereqs = new HashMap<ObjectId, String>();
  87. private String lockMessage;
  88. private PackLock packLock;
  89. BundleFetchConnection(Transport transportBundle, final InputStream src) throws TransportException {
  90. transport = transportBundle;
  91. bin = new BufferedInputStream(src);
  92. try {
  93. switch (readSignature()) {
  94. case 2:
  95. readBundleV2();
  96. break;
  97. default:
  98. throw new TransportException(transport.uri, JGitText.get().notABundle);
  99. }
  100. } catch (TransportException err) {
  101. close();
  102. throw err;
  103. } catch (IOException err) {
  104. close();
  105. throw new TransportException(transport.uri, err.getMessage(), err);
  106. } catch (RuntimeException err) {
  107. close();
  108. throw new TransportException(transport.uri, err.getMessage(), err);
  109. }
  110. }
  111. private int readSignature() throws IOException {
  112. final String rev = readLine(new byte[1024]);
  113. if (TransportBundle.V2_BUNDLE_SIGNATURE.equals(rev))
  114. return 2;
  115. throw new TransportException(transport.uri, JGitText.get().notABundle);
  116. }
  117. private void readBundleV2() throws IOException {
  118. final byte[] hdrbuf = new byte[1024];
  119. final LinkedHashMap<String, Ref> avail = new LinkedHashMap<String, Ref>();
  120. for (;;) {
  121. String line = readLine(hdrbuf);
  122. if (line.length() == 0)
  123. break;
  124. if (line.charAt(0) == '-') {
  125. ObjectId id = ObjectId.fromString(line.substring(1, 41));
  126. String shortDesc = null;
  127. if (line.length() > 42)
  128. shortDesc = line.substring(42);
  129. prereqs.put(id, shortDesc);
  130. continue;
  131. }
  132. final String name = line.substring(41, line.length());
  133. final ObjectId id = ObjectId.fromString(line.substring(0, 40));
  134. final Ref prior = avail.put(name, new ObjectIdRef.Unpeeled(
  135. Ref.Storage.NETWORK, name, id));
  136. if (prior != null)
  137. throw duplicateAdvertisement(name);
  138. }
  139. available(avail);
  140. }
  141. private PackProtocolException duplicateAdvertisement(final String name) {
  142. return new PackProtocolException(transport.uri,
  143. MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, name));
  144. }
  145. private String readLine(final byte[] hdrbuf) throws IOException {
  146. bin.mark(hdrbuf.length);
  147. final int cnt = bin.read(hdrbuf);
  148. int lf = 0;
  149. while (lf < cnt && hdrbuf[lf] != '\n')
  150. lf++;
  151. bin.reset();
  152. IO.skipFully(bin, lf);
  153. if (lf < cnt && hdrbuf[lf] == '\n')
  154. IO.skipFully(bin, 1);
  155. return RawParseUtils.decode(Constants.CHARSET, hdrbuf, 0, lf);
  156. }
  157. public boolean didFetchTestConnectivity() {
  158. return false;
  159. }
  160. @Override
  161. protected void doFetch(final ProgressMonitor monitor,
  162. final Collection<Ref> want, final Set<ObjectId> have)
  163. throws TransportException {
  164. verifyPrerequisites();
  165. try {
  166. ObjectInserter ins = transport.local.newObjectInserter();
  167. try {
  168. PackParser parser = ins.newPackParser(bin);
  169. parser.setAllowThin(true);
  170. parser.setObjectChecking(transport.isCheckFetchedObjects());
  171. parser.setLockMessage(lockMessage);
  172. packLock = parser.parse(NullProgressMonitor.INSTANCE);
  173. ins.flush();
  174. } finally {
  175. ins.release();
  176. }
  177. } catch (IOException err) {
  178. close();
  179. throw new TransportException(transport.uri, err.getMessage(), err);
  180. } catch (RuntimeException err) {
  181. close();
  182. throw new TransportException(transport.uri, err.getMessage(), err);
  183. }
  184. }
  185. public void setPackLockMessage(final String message) {
  186. lockMessage = message;
  187. }
  188. public Collection<PackLock> getPackLocks() {
  189. if (packLock != null)
  190. return Collections.singleton(packLock);
  191. return Collections.<PackLock> emptyList();
  192. }
  193. private void verifyPrerequisites() throws TransportException {
  194. if (prereqs.isEmpty())
  195. return;
  196. final RevWalk rw = new RevWalk(transport.local);
  197. try {
  198. final RevFlag PREREQ = rw.newFlag("PREREQ");
  199. final RevFlag SEEN = rw.newFlag("SEEN");
  200. final Map<ObjectId, String> missing = new HashMap<ObjectId, String>();
  201. final List<RevObject> commits = new ArrayList<RevObject>();
  202. for (final Map.Entry<ObjectId, String> e : prereqs.entrySet()) {
  203. ObjectId p = e.getKey();
  204. try {
  205. final RevCommit c = rw.parseCommit(p);
  206. if (!c.has(PREREQ)) {
  207. c.add(PREREQ);
  208. commits.add(c);
  209. }
  210. } catch (MissingObjectException notFound) {
  211. missing.put(p, e.getValue());
  212. } catch (IOException err) {
  213. throw new TransportException(transport.uri, MessageFormat
  214. .format(JGitText.get().cannotReadCommit, p.name()),
  215. err);
  216. }
  217. }
  218. if (!missing.isEmpty())
  219. throw new MissingBundlePrerequisiteException(transport.uri,
  220. missing);
  221. for (final Ref r : transport.local.getAllRefs().values()) {
  222. try {
  223. rw.markStart(rw.parseCommit(r.getObjectId()));
  224. } catch (IOException readError) {
  225. // If we cannot read the value of the ref skip it.
  226. }
  227. }
  228. int remaining = commits.size();
  229. try {
  230. RevCommit c;
  231. while ((c = rw.next()) != null) {
  232. if (c.has(PREREQ)) {
  233. c.add(SEEN);
  234. if (--remaining == 0)
  235. break;
  236. }
  237. }
  238. } catch (IOException err) {
  239. throw new TransportException(transport.uri,
  240. JGitText.get().cannotReadObject, err);
  241. }
  242. if (remaining > 0) {
  243. for (final RevObject o : commits) {
  244. if (!o.has(SEEN))
  245. missing.put(o, prereqs.get(o));
  246. }
  247. throw new MissingBundlePrerequisiteException(transport.uri,
  248. missing);
  249. }
  250. } finally {
  251. rw.release();
  252. }
  253. }
  254. @Override
  255. public void close() {
  256. if (bin != null) {
  257. try {
  258. bin.close();
  259. } catch (IOException ie) {
  260. // Ignore close failures.
  261. } finally {
  262. bin = null;
  263. }
  264. }
  265. }
  266. }