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.

BasePackConnection.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.transport;
  47. import java.io.EOFException;
  48. import java.io.IOException;
  49. import java.io.InputStream;
  50. import java.io.OutputStream;
  51. import java.util.HashSet;
  52. import java.util.LinkedHashMap;
  53. import java.util.Set;
  54. import org.eclipse.jgit.errors.NoRemoteRepositoryException;
  55. import org.eclipse.jgit.errors.PackProtocolException;
  56. import org.eclipse.jgit.errors.RemoteRepositoryException;
  57. import org.eclipse.jgit.errors.TransportException;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.ObjectIdRef;
  60. import org.eclipse.jgit.lib.Ref;
  61. import org.eclipse.jgit.lib.Repository;
  62. import org.eclipse.jgit.util.io.InterruptTimer;
  63. import org.eclipse.jgit.util.io.TimeoutInputStream;
  64. import org.eclipse.jgit.util.io.TimeoutOutputStream;
  65. /**
  66. * Base helper class for pack-based operations implementations. Provides partial
  67. * implementation of pack-protocol - refs advertising and capabilities support,
  68. * and some other helper methods.
  69. *
  70. * @see BasePackFetchConnection
  71. * @see BasePackPushConnection
  72. */
  73. abstract class BasePackConnection extends BaseConnection {
  74. /** The repository this transport fetches into, or pushes out of. */
  75. protected final Repository local;
  76. /** Remote repository location. */
  77. protected final URIish uri;
  78. /** A transport connected to {@link #uri}. */
  79. protected final Transport transport;
  80. /** Low-level input stream, if a timeout was configured. */
  81. protected TimeoutInputStream timeoutIn;
  82. /** Low-level output stream, if a timeout was configured. */
  83. protected TimeoutOutputStream timeoutOut;
  84. /** Timer to manage {@link #timeoutIn} and {@link #timeoutOut}. */
  85. private InterruptTimer myTimer;
  86. /** Input stream reading from the remote. */
  87. protected InputStream in;
  88. /** Output stream sending to the remote. */
  89. protected OutputStream out;
  90. /** Packet line decoder around {@link #in}. */
  91. protected PacketLineIn pckIn;
  92. /** Packet line encoder around {@link #out}. */
  93. protected PacketLineOut pckOut;
  94. /** Send {@link PacketLineOut#end()} before closing {@link #out}? */
  95. protected boolean outNeedsEnd;
  96. /** True if this is a stateless RPC connection. */
  97. protected boolean statelessRPC;
  98. /** Capability tokens advertised by the remote side. */
  99. private final Set<String> remoteCapablities = new HashSet<String>();
  100. /** Extra objects the remote has, but which aren't offered as refs. */
  101. protected final Set<ObjectId> additionalHaves = new HashSet<ObjectId>();
  102. BasePackConnection(final PackTransport packTransport) {
  103. transport = (Transport) packTransport;
  104. local = transport.local;
  105. uri = transport.uri;
  106. }
  107. /**
  108. * Configure this connection with the directional pipes.
  109. *
  110. * @param myIn
  111. * input stream to receive data from the peer. Caller must ensure
  112. * the input is buffered, otherwise read performance may suffer.
  113. * @param myOut
  114. * output stream to transmit data to the peer. Caller must ensure
  115. * the output is buffered, otherwise write performance may
  116. * suffer.
  117. */
  118. protected final void init(InputStream myIn, OutputStream myOut) {
  119. final int timeout = transport.getTimeout();
  120. if (timeout > 0) {
  121. final Thread caller = Thread.currentThread();
  122. myTimer = new InterruptTimer(caller.getName() + "-Timer");
  123. timeoutIn = new TimeoutInputStream(myIn, myTimer);
  124. timeoutOut = new TimeoutOutputStream(myOut, myTimer);
  125. timeoutIn.setTimeout(timeout * 1000);
  126. timeoutOut.setTimeout(timeout * 1000);
  127. myIn = timeoutIn;
  128. myOut = timeoutOut;
  129. }
  130. in = myIn;
  131. out = myOut;
  132. pckIn = new PacketLineIn(in);
  133. pckOut = new PacketLineOut(out);
  134. outNeedsEnd = true;
  135. }
  136. /**
  137. * Reads the advertised references through the initialized stream.
  138. * <p>
  139. * Subclass implementations may call this method only after setting up the
  140. * input and output streams with {@link #init(InputStream, OutputStream)}.
  141. * <p>
  142. * If any errors occur, this connection is automatically closed by invoking
  143. * {@link #close()} and the exception is wrapped (if necessary) and thrown
  144. * as a {@link TransportException}.
  145. *
  146. * @throws TransportException
  147. * the reference list could not be scanned.
  148. */
  149. protected void readAdvertisedRefs() throws TransportException {
  150. try {
  151. readAdvertisedRefsImpl();
  152. } catch (TransportException err) {
  153. close();
  154. throw err;
  155. } catch (IOException err) {
  156. close();
  157. throw new TransportException(err.getMessage(), err);
  158. } catch (RuntimeException err) {
  159. close();
  160. throw new TransportException(err.getMessage(), err);
  161. }
  162. }
  163. private void readAdvertisedRefsImpl() throws IOException {
  164. final LinkedHashMap<String, Ref> avail = new LinkedHashMap<String, Ref>();
  165. for (;;) {
  166. String line;
  167. try {
  168. line = pckIn.readString();
  169. } catch (EOFException eof) {
  170. if (avail.isEmpty())
  171. throw noRepository();
  172. throw eof;
  173. }
  174. if (line == PacketLineIn.END)
  175. break;
  176. if (line.startsWith("ERR ")) {
  177. // This is a customized remote service error.
  178. // Users should be informed about it.
  179. throw new RemoteRepositoryException(uri, line.substring(4));
  180. }
  181. if (avail.isEmpty()) {
  182. final int nul = line.indexOf('\0');
  183. if (nul >= 0) {
  184. // The first line (if any) may contain "hidden"
  185. // capability values after a NUL byte.
  186. for (String c : line.substring(nul + 1).split(" "))
  187. remoteCapablities.add(c);
  188. line = line.substring(0, nul);
  189. }
  190. }
  191. String name = line.substring(41, line.length());
  192. if (avail.isEmpty() && name.equals("capabilities^{}")) {
  193. // special line from git-receive-pack to show
  194. // capabilities when there are no refs to advertise
  195. continue;
  196. }
  197. final ObjectId id = ObjectId.fromString(line.substring(0, 40));
  198. if (name.equals(".have")) {
  199. additionalHaves.add(id);
  200. } else if (name.endsWith("^{}")) {
  201. name = name.substring(0, name.length() - 3);
  202. final Ref prior = avail.get(name);
  203. if (prior == null)
  204. throw new PackProtocolException(uri, "advertisement of "
  205. + name + "^{} came before " + name);
  206. if (prior.getPeeledObjectId() != null)
  207. throw duplicateAdvertisement(name + "^{}");
  208. avail.put(name, new ObjectIdRef.PeeledTag(
  209. Ref.Storage.NETWORK, name, prior.getObjectId(), id));
  210. } else {
  211. final Ref prior = avail.put(name, new ObjectIdRef.PeeledNonTag(
  212. Ref.Storage.NETWORK, name, id));
  213. if (prior != null)
  214. throw duplicateAdvertisement(name);
  215. }
  216. }
  217. available(avail);
  218. }
  219. /**
  220. * Create an exception to indicate problems finding a remote repository. The
  221. * caller is expected to throw the returned exception.
  222. *
  223. * Subclasses may override this method to provide better diagnostics.
  224. *
  225. * @return a TransportException saying a repository cannot be found and
  226. * possibly why.
  227. */
  228. protected TransportException noRepository() {
  229. return new NoRemoteRepositoryException(uri, "not found.");
  230. }
  231. protected boolean isCapableOf(final String option) {
  232. return remoteCapablities.contains(option);
  233. }
  234. protected boolean wantCapability(final StringBuilder b, final String option) {
  235. if (!isCapableOf(option))
  236. return false;
  237. b.append(' ');
  238. b.append(option);
  239. return true;
  240. }
  241. private PackProtocolException duplicateAdvertisement(final String name) {
  242. return new PackProtocolException(uri, "duplicate advertisements of "
  243. + name);
  244. }
  245. @Override
  246. public void close() {
  247. if (out != null) {
  248. try {
  249. if (outNeedsEnd) {
  250. outNeedsEnd = false;
  251. pckOut.end();
  252. }
  253. out.close();
  254. } catch (IOException err) {
  255. // Ignore any close errors.
  256. } finally {
  257. out = null;
  258. pckOut = null;
  259. }
  260. }
  261. if (in != null) {
  262. try {
  263. in.close();
  264. } catch (IOException err) {
  265. // Ignore any close errors.
  266. } finally {
  267. in = null;
  268. pckIn = null;
  269. }
  270. }
  271. if (myTimer != null) {
  272. try {
  273. myTimer.terminate();
  274. } finally {
  275. myTimer = null;
  276. timeoutIn = null;
  277. timeoutOut = null;
  278. }
  279. }
  280. }
  281. /** Tell the peer we are disconnecting, if it cares to know. */
  282. protected void endOut() {
  283. if (outNeedsEnd && out != null) {
  284. try {
  285. outNeedsEnd = false;
  286. pckOut.end();
  287. } catch (IOException e) {
  288. try {
  289. out.close();
  290. } catch (IOException err) {
  291. // Ignore any close errors.
  292. } finally {
  293. out = null;
  294. pckOut = null;
  295. }
  296. }
  297. }
  298. }
  299. }