From f6109ecd5d05bef6f06d82cd26bed358455979ab Mon Sep 17 00:00:00 2001
From: David Ostrovsky <david@ostrovsky.org>
Date: Thu, 10 Apr 2014 18:58:09 -0400
Subject: [PATCH] Add JGit gc command

---
 src/main/java/com/gitblit/transport/ssh/FileKeyManager.java |   55 +++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java b/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java
index 1eb470b..defb4a3 100644
--- a/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java
+++ b/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java
@@ -21,6 +21,8 @@
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.commons.codec.binary.Base64;
 import org.apache.sshd.common.util.Buffer;
@@ -33,17 +35,20 @@
 import com.google.common.io.Files;
 
 /**
- * Manages SSH keys on the filesystem.
+ * Manages public keys on the filesystem.
  *
  * @author James Moger
  *
  */
-public class FileKeyManager implements IKeyManager {
+public class FileKeyManager extends IPublicKeyManager {
 
 	protected final IRuntimeManager runtimeManager;
 
+	protected final Map<File, Long> lastModifieds;
+
 	public FileKeyManager(IRuntimeManager runtimeManager) {
 		this.runtimeManager = runtimeManager;
+		this.lastModifieds = new ConcurrentHashMap<File, Long>();
 	}
 
 	@Override
@@ -54,6 +59,7 @@
 
 	@Override
 	public FileKeyManager start() {
+		log.info(toString());
 		return this;
 	}
 
@@ -68,15 +74,34 @@
 	}
 
 	@Override
-	public List<PublicKey> getKeys(String username) {
+	protected boolean isStale(String username) {
+		File keystore = getKeystore(username);
+		if (!keystore.exists()) {
+			// keystore may have been deleted
+			return true;
+		}
+
+		if (lastModifieds.containsKey(keystore)) {
+			// compare modification times
+			long lastModified = lastModifieds.get(keystore);
+			return lastModified != keystore.lastModified();
+		}
+
+		// assume stale
+		return true;
+	}
+
+	@Override
+	protected List<PublicKey> getKeysImpl(String username) {
 		try {
-			File keys = getKeystore(username);
-			if (!keys.exists()) {
+			log.info("loading keystore for {}", username);
+			File keystore = getKeystore(username);
+			if (!keystore.exists()) {
 				return null;
 			}
-			if (keys.exists()) {
+			if (keystore.exists()) {
 				List<PublicKey> list = new ArrayList<PublicKey>();
-				for (String entry : Files.readLines(keys, Charsets.ISO_8859_1)) {
+				for (String entry : Files.readLines(keystore, Charsets.ISO_8859_1)) {
 					if (entry.trim().length() == 0) {
 						// skip blanks
 						continue;
@@ -93,6 +118,8 @@
 				if (list.isEmpty()) {
 					return null;
 				}
+
+				lastModifieds.put(keystore, keystore.lastModified());
 				return list;
 			}
 		} catch (IOException e) {
@@ -140,6 +167,9 @@
 			// write keystore
 			String content = Joiner.on("\n").join(lines).trim().concat("\n");
 			Files.write(content, keystore, Charsets.ISO_8859_1);
+
+			lastModifieds.remove(keystore);
+			keyCache.invalidate(username);
 			return true;
 		} catch (IOException e) {
 			throw new RuntimeException("Cannot add ssh key", e);
@@ -183,6 +213,9 @@
 					String content = Joiner.on("\n").join(lines).trim().concat("\n");
 					Files.write(content, keystore, Charsets.ISO_8859_1);
 				}
+
+				lastModifieds.remove(keystore);
+				keyCache.invalidate(username);
 				return true;
 			}
 		} catch (IOException e) {
@@ -193,7 +226,13 @@
 
 	@Override
 	public boolean removeAllKeys(String username) {
-		return getKeystore(username).delete();
+		File keystore = getKeystore(username);
+		if (keystore.delete()) {
+			lastModifieds.remove(keystore);
+			keyCache.invalidate(username);
+			return true;
+		}
+		return false;
 	}
 
 	protected File getKeystore(String username) {

--
Gitblit v1.9.1