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.internal.storage.file.PackLock;
  35. import org.eclipse.jgit.lib.NullProgressMonitor;
  36. import org.eclipse.jgit.lib.ObjectId;
  37. import org.eclipse.jgit.lib.ObjectIdRef;
  38. import org.eclipse.jgit.lib.ObjectInserter;
  39. import org.eclipse.jgit.lib.ProgressMonitor;
  40. import org.eclipse.jgit.lib.Ref;
  41. import org.eclipse.jgit.revwalk.RevCommit;
  42. import org.eclipse.jgit.revwalk.RevFlag;
  43. import org.eclipse.jgit.revwalk.RevObject;
  44. import org.eclipse.jgit.revwalk.RevWalk;
  45. import org.eclipse.jgit.util.IO;
  46. import org.eclipse.jgit.util.RawParseUtils;

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

  52.     private final Transport transport;

  53.     InputStream bin;

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

  55.     private String lockMessage;

  56.     private PackLock packLock;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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