BundleFetchConnection.java

  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> and others
  7.  *
  8.  * This program and the accompanying materials are made available under the
  9.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  10.  * https://www.eclipse.org/org/documents/edl-v10.php.
  11.  *
  12.  * SPDX-License-Identifier: BSD-3-Clause
  13.  */

  14. package org.eclipse.jgit.transport;

  15. import static java.nio.charset.StandardCharsets.UTF_8;

  16. import java.io.BufferedInputStream;
  17. import java.io.EOFException;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.text.MessageFormat;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.LinkedHashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Set;

  29. import org.eclipse.jgit.errors.MissingBundlePrerequisiteException;
  30. import org.eclipse.jgit.errors.MissingObjectException;
  31. import org.eclipse.jgit.errors.PackProtocolException;
  32. import org.eclipse.jgit.errors.TransportException;
  33. import org.eclipse.jgit.internal.JGitText;
  34. import org.eclipse.jgit.lib.NullProgressMonitor;
  35. import org.eclipse.jgit.lib.ObjectId;
  36. import org.eclipse.jgit.lib.ObjectIdRef;
  37. import org.eclipse.jgit.lib.ObjectInserter;
  38. import org.eclipse.jgit.lib.ProgressMonitor;
  39. import org.eclipse.jgit.lib.Ref;
  40. import org.eclipse.jgit.revwalk.RevCommit;
  41. import org.eclipse.jgit.revwalk.RevFlag;
  42. import org.eclipse.jgit.revwalk.RevObject;
  43. import org.eclipse.jgit.revwalk.RevWalk;
  44. import org.eclipse.jgit.util.IO;
  45. import org.eclipse.jgit.util.RawParseUtils;

  46. /**
  47.  * Fetch connection for bundle based classes. It used by
  48.  * instances of {@link TransportBundle}
  49.  */
  50. class BundleFetchConnection extends BaseFetchConnection {

  51.     private final Transport transport;

  52.     InputStream bin;

  53.     final Map<ObjectId, String> prereqs = new HashMap<>();

  54.     private String lockMessage;

  55.     private PackLock packLock;

  56.     BundleFetchConnection(Transport transportBundle, InputStream src) throws TransportException {
  57.         transport = transportBundle;
  58.         bin = new BufferedInputStream(src);
  59.         try {
  60.             switch (readSignature()) {
  61.             case 2:
  62.                 readBundleV2();
  63.                 break;
  64.             default:
  65.                 throw new TransportException(transport.uri, JGitText.get().notABundle);
  66.             }
  67.         } catch (TransportException err) {
  68.             close();
  69.             throw err;
  70.         } catch (IOException | RuntimeException err) {
  71.             close();
  72.             throw new TransportException(transport.uri, err.getMessage(), err);
  73.         }
  74.     }

  75.     private int readSignature() throws IOException {
  76.         final String rev = readLine(new byte[1024]);
  77.         if (TransportBundle.V2_BUNDLE_SIGNATURE.equals(rev))
  78.             return 2;
  79.         throw new TransportException(transport.uri, JGitText.get().notABundle);
  80.     }

  81.     private void readBundleV2() throws IOException {
  82.         final byte[] hdrbuf = new byte[1024];
  83.         final LinkedHashMap<String, Ref> avail = new LinkedHashMap<>();
  84.         for (;;) {
  85.             String line = readLine(hdrbuf);
  86.             if (line.length() == 0)
  87.                 break;

  88.             if (line.charAt(0) == '-') {
  89.                 ObjectId id = ObjectId.fromString(line.substring(1, 41));
  90.                 String shortDesc = null;
  91.                 if (line.length() > 42)
  92.                     shortDesc = line.substring(42);
  93.                 prereqs.put(id, shortDesc);
  94.                 continue;
  95.             }

  96.             final String name = line.substring(41, line.length());
  97.             final ObjectId id = ObjectId.fromString(line.substring(0, 40));
  98.             final Ref prior = avail.put(name, new ObjectIdRef.Unpeeled(
  99.                     Ref.Storage.NETWORK, name, id));
  100.             if (prior != null)
  101.                 throw duplicateAdvertisement(name);
  102.         }
  103.         available(avail);
  104.     }

  105.     private PackProtocolException duplicateAdvertisement(String name) {
  106.         return new PackProtocolException(transport.uri,
  107.                 MessageFormat.format(JGitText.get().duplicateAdvertisementsOf, name));
  108.     }

  109.     private String readLine(byte[] hdrbuf) throws IOException {
  110.         StringBuilder line = new StringBuilder();
  111.         boolean done = false;
  112.         while (!done) {
  113.             bin.mark(hdrbuf.length);
  114.             final int cnt = bin.read(hdrbuf);
  115.             if (cnt < 0) {
  116.                 throw new EOFException(JGitText.get().shortReadOfBlock);
  117.             }
  118.             int lf = 0;
  119.             while (lf < cnt && hdrbuf[lf] != '\n') {
  120.                 lf++;
  121.             }
  122.             bin.reset();
  123.             IO.skipFully(bin, lf);
  124.             if (lf < cnt && hdrbuf[lf] == '\n') {
  125.                 IO.skipFully(bin, 1);
  126.                 done = true;
  127.             }
  128.             line.append(RawParseUtils.decode(UTF_8, hdrbuf, 0, lf));
  129.         }
  130.         return line.toString();
  131.     }

  132.     /** {@inheritDoc} */
  133.     @Override
  134.     public boolean didFetchTestConnectivity() {
  135.         return false;
  136.     }

  137.     /** {@inheritDoc} */
  138.     @Override
  139.     protected void doFetch(final ProgressMonitor monitor,
  140.             final Collection<Ref> want, final Set<ObjectId> have)
  141.             throws TransportException {
  142.         verifyPrerequisites();
  143.         try {
  144.             try (ObjectInserter ins = transport.local.newObjectInserter()) {
  145.                 PackParser parser = ins.newPackParser(bin);
  146.                 parser.setAllowThin(true);
  147.                 parser.setObjectChecker(transport.getObjectChecker());
  148.                 parser.setLockMessage(lockMessage);
  149.                 packLock = parser.parse(NullProgressMonitor.INSTANCE);
  150.                 ins.flush();
  151.             }
  152.         } catch (IOException | RuntimeException err) {
  153.             close();
  154.             throw new TransportException(transport.uri, err.getMessage(), err);
  155.         }
  156.     }

  157.     /** {@inheritDoc} */
  158.     @Override
  159.     public void setPackLockMessage(String message) {
  160.         lockMessage = message;
  161.     }

  162.     /** {@inheritDoc} */
  163.     @Override
  164.     public Collection<PackLock> getPackLocks() {
  165.         if (packLock != null)
  166.             return Collections.singleton(packLock);
  167.         return Collections.<PackLock> emptyList();
  168.     }

  169.     private void verifyPrerequisites() throws TransportException {
  170.         if (prereqs.isEmpty())
  171.             return;

  172.         try (RevWalk rw = new RevWalk(transport.local)) {
  173.             final RevFlag PREREQ = rw.newFlag("PREREQ"); //$NON-NLS-1$
  174.             final RevFlag SEEN = rw.newFlag("SEEN"); //$NON-NLS-1$

  175.             final Map<ObjectId, String> missing = new HashMap<>();
  176.             final List<RevObject> commits = new ArrayList<>();
  177.             for (Map.Entry<ObjectId, String> e : prereqs.entrySet()) {
  178.                 ObjectId p = e.getKey();
  179.                 try {
  180.                     final RevCommit c = rw.parseCommit(p);
  181.                     if (!c.has(PREREQ)) {
  182.                         c.add(PREREQ);
  183.                         commits.add(c);
  184.                     }
  185.                 } catch (MissingObjectException notFound) {
  186.                     missing.put(p, e.getValue());
  187.                 } catch (IOException err) {
  188.                     throw new TransportException(transport.uri, MessageFormat
  189.                             .format(JGitText.get().cannotReadCommit, p.name()),
  190.                             err);
  191.                 }
  192.             }
  193.             if (!missing.isEmpty())
  194.                 throw new MissingBundlePrerequisiteException(transport.uri,
  195.                         missing);

  196.             List<Ref> localRefs;
  197.             try {
  198.                 localRefs = transport.local.getRefDatabase().getRefs();
  199.             } catch (IOException e) {
  200.                 throw new TransportException(transport.uri, e.getMessage(), e);
  201.             }
  202.             for (Ref r : localRefs) {
  203.                 try {
  204.                     rw.markStart(rw.parseCommit(r.getObjectId()));
  205.                 } catch (IOException readError) {
  206.                     // If we cannot read the value of the ref skip it.
  207.                 }
  208.             }

  209.             int remaining = commits.size();
  210.             try {
  211.                 RevCommit c;
  212.                 while ((c = rw.next()) != null) {
  213.                     if (c.has(PREREQ)) {
  214.                         c.add(SEEN);
  215.                         if (--remaining == 0)
  216.                             break;
  217.                     }
  218.                 }
  219.             } catch (IOException err) {
  220.                 throw new TransportException(transport.uri,
  221.                         JGitText.get().cannotReadObject, err);
  222.             }

  223.             if (remaining > 0) {
  224.                 for (RevObject o : commits) {
  225.                     if (!o.has(SEEN))
  226.                         missing.put(o, prereqs.get(o));
  227.                 }
  228.                 throw new MissingBundlePrerequisiteException(transport.uri,
  229.                         missing);
  230.             }
  231.         }
  232.     }

  233.     /** {@inheritDoc} */
  234.     @Override
  235.     public void close() {
  236.         if (bin != null) {
  237.             try {
  238.                 bin.close();
  239.             } catch (IOException ie) {
  240.                 // Ignore close failures.
  241.             } finally {
  242.                 bin = null;
  243.             }
  244.         }
  245.     }
  246. }