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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 static java.nio.charset.StandardCharsets.UTF_8;
  49. import java.io.BufferedInputStream;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.text.MessageFormat;
  53. import java.util.ArrayList;
  54. import java.util.Collection;
  55. import java.util.Collections;
  56. import java.util.HashMap;
  57. import java.util.LinkedHashMap;
  58. import java.util.List;
  59. import java.util.Map;
  60. import java.util.Set;
  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.internal.JGitText;
  66. import org.eclipse.jgit.internal.storage.file.PackLock;
  67. import org.eclipse.jgit.lib.NullProgressMonitor;
  68. import org.eclipse.jgit.lib.ObjectId;
  69. import org.eclipse.jgit.lib.ObjectIdRef;
  70. import org.eclipse.jgit.lib.ObjectInserter;
  71. import org.eclipse.jgit.lib.ProgressMonitor;
  72. import org.eclipse.jgit.lib.Ref;
  73. import org.eclipse.jgit.revwalk.RevCommit;
  74. import org.eclipse.jgit.revwalk.RevFlag;
  75. import org.eclipse.jgit.revwalk.RevObject;
  76. import org.eclipse.jgit.revwalk.RevWalk;
  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<>();
  87. private String lockMessage;
  88. private PackLock packLock;
  89. BundleFetchConnection(Transport transportBundle, 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<>();
  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(String name) {
  142. return new PackProtocolException(transport.uri,
  143. MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, name));
  144. }
  145. private String readLine(byte[] hdrbuf) throws IOException {
  146. StringBuilder line = new StringBuilder();
  147. boolean done = false;
  148. while (!done) {
  149. bin.mark(hdrbuf.length);
  150. final int cnt = bin.read(hdrbuf);
  151. int lf = 0;
  152. while (lf < cnt && hdrbuf[lf] != '\n')
  153. lf++;
  154. bin.reset();
  155. IO.skipFully(bin, lf);
  156. if (lf < cnt && hdrbuf[lf] == '\n') {
  157. IO.skipFully(bin, 1);
  158. done = true;
  159. }
  160. line.append(RawParseUtils.decode(UTF_8, hdrbuf, 0, lf));
  161. }
  162. return line.toString();
  163. }
  164. /** {@inheritDoc} */
  165. @Override
  166. public boolean didFetchTestConnectivity() {
  167. return false;
  168. }
  169. /** {@inheritDoc} */
  170. @Override
  171. protected void doFetch(final ProgressMonitor monitor,
  172. final Collection<Ref> want, final Set<ObjectId> have)
  173. throws TransportException {
  174. verifyPrerequisites();
  175. try {
  176. try (ObjectInserter ins = transport.local.newObjectInserter()) {
  177. PackParser parser = ins.newPackParser(bin);
  178. parser.setAllowThin(true);
  179. parser.setObjectChecker(transport.getObjectChecker());
  180. parser.setLockMessage(lockMessage);
  181. packLock = parser.parse(NullProgressMonitor.INSTANCE);
  182. ins.flush();
  183. }
  184. } catch (IOException err) {
  185. close();
  186. throw new TransportException(transport.uri, err.getMessage(), err);
  187. } catch (RuntimeException err) {
  188. close();
  189. throw new TransportException(transport.uri, err.getMessage(), err);
  190. }
  191. }
  192. /** {@inheritDoc} */
  193. @Override
  194. public void setPackLockMessage(String message) {
  195. lockMessage = message;
  196. }
  197. /** {@inheritDoc} */
  198. @Override
  199. public Collection<PackLock> getPackLocks() {
  200. if (packLock != null)
  201. return Collections.singleton(packLock);
  202. return Collections.<PackLock> emptyList();
  203. }
  204. private void verifyPrerequisites() throws TransportException {
  205. if (prereqs.isEmpty())
  206. return;
  207. try (RevWalk rw = new RevWalk(transport.local)) {
  208. final RevFlag PREREQ = rw.newFlag("PREREQ"); //$NON-NLS-1$
  209. final RevFlag SEEN = rw.newFlag("SEEN"); //$NON-NLS-1$
  210. final Map<ObjectId, String> missing = new HashMap<>();
  211. final List<RevObject> commits = new ArrayList<>();
  212. for (Map.Entry<ObjectId, String> e : prereqs.entrySet()) {
  213. ObjectId p = e.getKey();
  214. try {
  215. final RevCommit c = rw.parseCommit(p);
  216. if (!c.has(PREREQ)) {
  217. c.add(PREREQ);
  218. commits.add(c);
  219. }
  220. } catch (MissingObjectException notFound) {
  221. missing.put(p, e.getValue());
  222. } catch (IOException err) {
  223. throw new TransportException(transport.uri, MessageFormat
  224. .format(JGitText.get().cannotReadCommit, p.name()),
  225. err);
  226. }
  227. }
  228. if (!missing.isEmpty())
  229. throw new MissingBundlePrerequisiteException(transport.uri,
  230. missing);
  231. List<Ref> localRefs;
  232. try {
  233. localRefs = transport.local.getRefDatabase().getRefs();
  234. } catch (IOException e) {
  235. throw new TransportException(transport.uri, e.getMessage(), e);
  236. }
  237. for (Ref r : localRefs) {
  238. try {
  239. rw.markStart(rw.parseCommit(r.getObjectId()));
  240. } catch (IOException readError) {
  241. // If we cannot read the value of the ref skip it.
  242. }
  243. }
  244. int remaining = commits.size();
  245. try {
  246. RevCommit c;
  247. while ((c = rw.next()) != null) {
  248. if (c.has(PREREQ)) {
  249. c.add(SEEN);
  250. if (--remaining == 0)
  251. break;
  252. }
  253. }
  254. } catch (IOException err) {
  255. throw new TransportException(transport.uri,
  256. JGitText.get().cannotReadObject, err);
  257. }
  258. if (remaining > 0) {
  259. for (RevObject o : commits) {
  260. if (!o.has(SEEN))
  261. missing.put(o, prereqs.get(o));
  262. }
  263. throw new MissingBundlePrerequisiteException(transport.uri,
  264. missing);
  265. }
  266. }
  267. }
  268. /** {@inheritDoc} */
  269. @Override
  270. public void close() {
  271. if (bin != null) {
  272. try {
  273. bin.close();
  274. } catch (IOException ie) {
  275. // Ignore close failures.
  276. } finally {
  277. bin = null;
  278. }
  279. }
  280. }
  281. }