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

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