Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TransportAmazonS3.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.io.BufferedReader;
  45. import java.io.File;
  46. import java.io.FileNotFoundException;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import java.net.URLConnection;
  51. import java.text.MessageFormat;
  52. import java.util.ArrayList;
  53. import java.util.Collection;
  54. import java.util.Collections;
  55. import java.util.EnumSet;
  56. import java.util.HashSet;
  57. import java.util.Map;
  58. import java.util.Properties;
  59. import java.util.Set;
  60. import java.util.TreeMap;
  61. import org.eclipse.jgit.errors.NotSupportedException;
  62. import org.eclipse.jgit.errors.TransportException;
  63. import org.eclipse.jgit.internal.JGitText;
  64. import org.eclipse.jgit.lib.Constants;
  65. import org.eclipse.jgit.lib.ObjectId;
  66. import org.eclipse.jgit.lib.ObjectIdRef;
  67. import org.eclipse.jgit.lib.ProgressMonitor;
  68. import org.eclipse.jgit.lib.Ref;
  69. import org.eclipse.jgit.lib.Ref.Storage;
  70. import org.eclipse.jgit.lib.Repository;
  71. import org.eclipse.jgit.lib.SymbolicRef;
  72. /**
  73. * Transport over the non-Git aware Amazon S3 protocol.
  74. * <p>
  75. * This transport communicates with the Amazon S3 servers (a non-free commercial
  76. * hosting service that users must subscribe to). Some users may find transport
  77. * to and from S3 to be a useful backup service.
  78. * <p>
  79. * The transport does not require any specialized Git support on the remote
  80. * (server side) repository, as Amazon does not provide any such support.
  81. * Repository files are retrieved directly through the S3 API, which uses
  82. * extended HTTP/1.1 semantics. This make it possible to read or write Git data
  83. * from a remote repository that is stored on S3.
  84. * <p>
  85. * Unlike the HTTP variant (see {@link TransportHttp}) we rely upon being able
  86. * to list objects in a bucket, as the S3 API supports this function. By listing
  87. * the bucket contents we can avoid relying on <code>objects/info/packs</code>
  88. * or <code>info/refs</code> in the remote repository.
  89. * <p>
  90. * Concurrent pushing over this transport is not supported. Multiple concurrent
  91. * push operations may cause confusion in the repository state.
  92. *
  93. * @see WalkFetchConnection
  94. * @see WalkPushConnection
  95. */
  96. public class TransportAmazonS3 extends HttpTransport implements WalkTransport {
  97. static final String S3_SCHEME = "amazon-s3"; //$NON-NLS-1$
  98. static final TransportProtocol PROTO_S3 = new TransportProtocol() {
  99. public String getName() {
  100. return "Amazon S3"; //$NON-NLS-1$
  101. }
  102. public Set<String> getSchemes() {
  103. return Collections.singleton(S3_SCHEME);
  104. }
  105. public Set<URIishField> getRequiredFields() {
  106. return Collections.unmodifiableSet(EnumSet.of(URIishField.USER,
  107. URIishField.HOST, URIishField.PATH));
  108. }
  109. public Set<URIishField> getOptionalFields() {
  110. return Collections.unmodifiableSet(EnumSet.of(URIishField.PASS));
  111. }
  112. public Transport open(URIish uri, Repository local, String remoteName)
  113. throws NotSupportedException {
  114. return new TransportAmazonS3(local, uri);
  115. }
  116. };
  117. /** User information necessary to connect to S3. */
  118. private final AmazonS3 s3;
  119. /** Bucket the remote repository is stored in. */
  120. private final String bucket;
  121. /**
  122. * Key prefix which all objects related to the repository start with.
  123. * <p>
  124. * The prefix does not start with "/".
  125. * <p>
  126. * The prefix does not end with "/". The trailing slash is stripped during
  127. * the constructor if a trailing slash was supplied in the URIish.
  128. * <p>
  129. * All files within the remote repository start with
  130. * <code>keyPrefix + "/"</code>.
  131. */
  132. private final String keyPrefix;
  133. TransportAmazonS3(final Repository local, final URIish uri)
  134. throws NotSupportedException {
  135. super(local, uri);
  136. Properties props = loadProperties();
  137. if (!props.contains("tmpdir") && local.getDirectory() != null) //$NON-NLS-1$
  138. props.put("tmpdir", local.getDirectory().getPath()); //$NON-NLS-1$
  139. s3 = new AmazonS3(props);
  140. bucket = uri.getHost();
  141. String p = uri.getPath();
  142. if (p.startsWith("/")) //$NON-NLS-1$
  143. p = p.substring(1);
  144. if (p.endsWith("/")) //$NON-NLS-1$
  145. p = p.substring(0, p.length() - 1);
  146. keyPrefix = p;
  147. }
  148. private Properties loadProperties() throws NotSupportedException {
  149. if (local.getDirectory() != null) {
  150. File propsFile = new File(local.getDirectory(), uri.getUser());
  151. if (propsFile.isFile())
  152. return loadPropertiesFile(propsFile);
  153. }
  154. File propsFile = new File(local.getFS().userHome(), uri.getUser());
  155. if (propsFile.isFile())
  156. return loadPropertiesFile(propsFile);
  157. Properties props = new Properties();
  158. String user = uri.getUser();
  159. String pass = uri.getPass();
  160. if (user != null && pass != null) {
  161. props.setProperty("accesskey", user); //$NON-NLS-1$
  162. props.setProperty("secretkey", pass); //$NON-NLS-1$
  163. } else
  164. throw new NotSupportedException(MessageFormat.format(
  165. JGitText.get().cannotReadFile, propsFile));
  166. return props;
  167. }
  168. private static Properties loadPropertiesFile(File propsFile)
  169. throws NotSupportedException {
  170. try {
  171. return AmazonS3.properties(propsFile);
  172. } catch (IOException e) {
  173. throw new NotSupportedException(MessageFormat.format(
  174. JGitText.get().cannotReadFile, propsFile), e);
  175. }
  176. }
  177. @Override
  178. public FetchConnection openFetch() throws TransportException {
  179. final DatabaseS3 c = new DatabaseS3(bucket, keyPrefix + "/objects"); //$NON-NLS-1$
  180. final WalkFetchConnection r = new WalkFetchConnection(this, c);
  181. r.available(c.readAdvertisedRefs());
  182. return r;
  183. }
  184. @Override
  185. public PushConnection openPush() throws TransportException {
  186. final DatabaseS3 c = new DatabaseS3(bucket, keyPrefix + "/objects"); //$NON-NLS-1$
  187. final WalkPushConnection r = new WalkPushConnection(this, c);
  188. r.available(c.readAdvertisedRefs());
  189. return r;
  190. }
  191. @Override
  192. public void close() {
  193. // No explicit connections are maintained.
  194. }
  195. class DatabaseS3 extends WalkRemoteObjectDatabase {
  196. private final String bucketName;
  197. private final String objectsKey;
  198. DatabaseS3(final String b, final String o) {
  199. bucketName = b;
  200. objectsKey = o;
  201. }
  202. private String resolveKey(String subpath) {
  203. if (subpath.endsWith("/")) //$NON-NLS-1$
  204. subpath = subpath.substring(0, subpath.length() - 1);
  205. String k = objectsKey;
  206. while (subpath.startsWith(ROOT_DIR)) {
  207. k = k.substring(0, k.lastIndexOf('/'));
  208. subpath = subpath.substring(3);
  209. }
  210. return k + "/" + subpath; //$NON-NLS-1$
  211. }
  212. @Override
  213. URIish getURI() {
  214. URIish u = new URIish();
  215. u = u.setScheme(S3_SCHEME);
  216. u = u.setHost(bucketName);
  217. u = u.setPath("/" + objectsKey); //$NON-NLS-1$
  218. return u;
  219. }
  220. @Override
  221. Collection<WalkRemoteObjectDatabase> getAlternates() throws IOException {
  222. try {
  223. return readAlternates(INFO_ALTERNATES);
  224. } catch (FileNotFoundException err) {
  225. // Fall through.
  226. }
  227. return null;
  228. }
  229. @Override
  230. WalkRemoteObjectDatabase openAlternate(final String location)
  231. throws IOException {
  232. return new DatabaseS3(bucketName, resolveKey(location));
  233. }
  234. @Override
  235. Collection<String> getPackNames() throws IOException {
  236. final HashSet<String> have = new HashSet<String>();
  237. have.addAll(s3.list(bucket, resolveKey("pack"))); //$NON-NLS-1$
  238. final Collection<String> packs = new ArrayList<String>();
  239. for (final String n : have) {
  240. if (!n.startsWith("pack-") || !n.endsWith(".pack")) //$NON-NLS-1$ //$NON-NLS-2$
  241. continue;
  242. final String in = n.substring(0, n.length() - 5) + ".idx"; //$NON-NLS-1$
  243. if (have.contains(in))
  244. packs.add(n);
  245. }
  246. return packs;
  247. }
  248. @Override
  249. FileStream open(final String path) throws IOException {
  250. final URLConnection c = s3.get(bucket, resolveKey(path));
  251. final InputStream raw = c.getInputStream();
  252. final InputStream in = s3.decrypt(c);
  253. final int len = c.getContentLength();
  254. return new FileStream(in, raw == in ? len : -1);
  255. }
  256. @Override
  257. void deleteFile(final String path) throws IOException {
  258. s3.delete(bucket, resolveKey(path));
  259. }
  260. @Override
  261. OutputStream writeFile(final String path,
  262. final ProgressMonitor monitor, final String monitorTask)
  263. throws IOException {
  264. return s3.beginPut(bucket, resolveKey(path), monitor, monitorTask);
  265. }
  266. @Override
  267. void writeFile(final String path, final byte[] data) throws IOException {
  268. s3.put(bucket, resolveKey(path), data);
  269. }
  270. Map<String, Ref> readAdvertisedRefs() throws TransportException {
  271. final TreeMap<String, Ref> avail = new TreeMap<String, Ref>();
  272. readPackedRefs(avail);
  273. readLooseRefs(avail);
  274. readRef(avail, Constants.HEAD);
  275. return avail;
  276. }
  277. private void readLooseRefs(final TreeMap<String, Ref> avail)
  278. throws TransportException {
  279. try {
  280. for (final String n : s3.list(bucket, resolveKey(ROOT_DIR
  281. + "refs"))) //$NON-NLS-1$
  282. readRef(avail, "refs/" + n); //$NON-NLS-1$
  283. } catch (IOException e) {
  284. throw new TransportException(getURI(), JGitText.get().cannotListRefs, e);
  285. }
  286. }
  287. private Ref readRef(final TreeMap<String, Ref> avail, final String rn)
  288. throws TransportException {
  289. final String s;
  290. String ref = ROOT_DIR + rn;
  291. try {
  292. final BufferedReader br = openReader(ref);
  293. try {
  294. s = br.readLine();
  295. } finally {
  296. br.close();
  297. }
  298. } catch (FileNotFoundException noRef) {
  299. return null;
  300. } catch (IOException err) {
  301. throw new TransportException(getURI(), MessageFormat.format(
  302. JGitText.get().transportExceptionReadRef, ref), err);
  303. }
  304. if (s == null)
  305. throw new TransportException(getURI(), MessageFormat.format(JGitText.get().transportExceptionEmptyRef, rn));
  306. if (s.startsWith("ref: ")) { //$NON-NLS-1$
  307. final String target = s.substring("ref: ".length()); //$NON-NLS-1$
  308. Ref r = avail.get(target);
  309. if (r == null)
  310. r = readRef(avail, target);
  311. if (r == null)
  312. r = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, target, null);
  313. r = new SymbolicRef(rn, r);
  314. avail.put(r.getName(), r);
  315. return r;
  316. }
  317. if (ObjectId.isId(s)) {
  318. final Ref r = new ObjectIdRef.Unpeeled(loose(avail.get(rn)),
  319. rn, ObjectId.fromString(s));
  320. avail.put(r.getName(), r);
  321. return r;
  322. }
  323. throw new TransportException(getURI(), MessageFormat.format(JGitText.get().transportExceptionBadRef, rn, s));
  324. }
  325. private Storage loose(final Ref r) {
  326. if (r != null && r.getStorage() == Storage.PACKED)
  327. return Storage.LOOSE_PACKED;
  328. return Storage.LOOSE;
  329. }
  330. @Override
  331. void close() {
  332. // We do not maintain persistent connections.
  333. }
  334. }
  335. }