FS.getFileStoreAttributes: cancel failed task executed asynchronously

to avoid unnecessary computations if the task getting
FileStoreAttributes asynchronously timed out, cancelled or failed with
another exceptions.

Change-Id: I127a3e2f3710fc5a8742c61f513576ee2d84baed
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
index 59bbacf..6a40fad 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
@@ -363,6 +363,7 @@ public static FileStoreAttributes get(Path path) {
 
 		private static FileStoreAttributes getFileStoreAttributes(Path dir) {
 			FileStore s;
+			CompletableFuture<Optional<FileStoreAttributes>> f = null;
 			try {
 				if (Files.exists(dir)) {
 					s = Files.getFileStore(dir);
@@ -385,7 +386,7 @@ private static FileStoreAttributes getFileStoreAttributes(Path dir) {
 					return FALLBACK_FILESTORE_ATTRIBUTES;
 				}
 
-				CompletableFuture<Optional<FileStoreAttributes>> f = CompletableFuture
+				f = CompletableFuture
 						.supplyAsync(() -> {
 							Lock lock = locks.computeIfAbsent(s,
 									l -> new ReentrantLock());
@@ -455,10 +456,13 @@ private static FileStoreAttributes getFileStoreAttributes(Path dir) {
 				}
 				// fall through and return fallback
 			} catch (IOException | ExecutionException | CancellationException e) {
+				cancel(f);
 				LOG.error(e.getMessage(), e);
 			} catch (TimeoutException | SecurityException e) {
+				cancel(f);
 				// use fallback
 			} catch (InterruptedException e) {
+				cancel(f);
 				LOG.error(e.getMessage(), e);
 				Thread.currentThread().interrupt();
 			}
@@ -467,6 +471,13 @@ private static FileStoreAttributes getFileStoreAttributes(Path dir) {
 			return FALLBACK_FILESTORE_ATTRIBUTES;
 		}
 
+		private static void cancel(
+				CompletableFuture<Optional<FileStoreAttributes>> f) {
+			if (f != null) {
+				f.cancel(true);
+			}
+		}
+
 		@SuppressWarnings("boxing")
 		private static Duration measureMinimalRacyInterval(Path dir) {
 			LOG.debug("{}: start measure minimal racy interval in {}", //$NON-NLS-1$