From ce3f4bd24c330e0cace6a31567e1516dfaa66234 Mon Sep 17 00:00:00 2001
From: Christian Aistleitner <christian@quelltextlich.at>
Date: Sun, 24 Mar 2013 20:42:23 -0400
Subject: [PATCH] Fix NPE when getting user's fork without repository list caching (issue-182)

---
 src/com/gitblit/utils/FileUtils.java |   68 ++++++++++++++++++++++++++++-----
 1 files changed, 57 insertions(+), 11 deletions(-)

diff --git a/src/com/gitblit/utils/FileUtils.java b/src/com/gitblit/utils/FileUtils.java
index a14928f..a21b512 100644
--- a/src/com/gitblit/utils/FileUtils.java
+++ b/src/com/gitblit/utils/FileUtils.java
@@ -99,6 +99,25 @@
 		}
 		return defaultValue;
 	}
+	
+	/**
+	 * Returns the byte [] content of the specified file.
+	 * 
+	 * @param file
+	 * @return the byte content of the file
+	 */
+	public static byte [] readContent(File file) {
+		byte [] buffer = new byte[(int) file.length()];
+		try {
+			BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
+			is.read(buffer,  0,  buffer.length);
+			is.close();
+		} catch (Throwable t) {
+			System.err.println("Failed to read byte content of " + file.getAbsolutePath());
+			t.printStackTrace();
+		}
+		return buffer;
+	}
 
 	/**
 	 * Returns the string content of the specified file.
@@ -157,19 +176,17 @@
 	public static long folderSize(File directory) {
 		if (directory == null || !directory.exists()) {
 			return -1;
-		}
-		if (directory.isFile()) {
-			return directory.length();
-		}
-		long length = 0;
-		for (File file : directory.listFiles()) {
-			if (file.isFile()) {
-				length += file.length();
-			} else {
+		}		
+		if (directory.isDirectory()) {
+			long length = 0;
+			for (File file : directory.listFiles()) {
 				length += folderSize(file);
 			}
+			return length;
+		} else if (directory.isFile()) {
+			return directory.length();
 		}
-		return length;
+		return 0;
 	}
 
 	/**
@@ -226,7 +243,21 @@
 	public static String getRelativePath(File basePath, File path) {
 		File exactBase = getExactFile(basePath);
 		File exactPath = getExactFile(path);
-		return StringUtils.getRelativePath(exactBase.getPath(), exactPath.getPath());
+		if (path.getAbsolutePath().startsWith(basePath.getAbsolutePath())) {
+			// absolute base-path match
+			return StringUtils.getRelativePath(basePath.getAbsolutePath(), path.getAbsolutePath());
+		} else if (exactPath.getPath().startsWith(exactBase.getPath())) {
+			// canonical base-path match
+			return StringUtils.getRelativePath(exactBase.getPath(), exactPath.getPath());
+		} else if (exactPath.getPath().startsWith(basePath.getAbsolutePath())) {
+			// mixed path match
+			return StringUtils.getRelativePath(basePath.getAbsolutePath(), exactPath.getPath());
+		} else if (path.getAbsolutePath().startsWith(exactBase.getPath())) {
+			// mixed path match
+			return StringUtils.getRelativePath(exactBase.getPath(), path.getAbsolutePath());
+		}
+		// no relative relationship
+		return null;
 	}
 	
 	/**
@@ -243,4 +274,19 @@
 			return path.getAbsoluteFile();
 		}
 	}
+
+	public static File resolveParameter(String parameter, File aFolder, String path) {
+		if (aFolder == null) {
+			// strip any parameter reference		
+			path = path.replace(parameter, "").trim();
+			if (path.length() > 0 && path.charAt(0) == '/') {
+				// strip leading /
+				path = path.substring(1);
+			}
+		} else if (path.contains(parameter)) {
+			// replace parameter with path
+			path = path.replace(parameter, aFolder.getAbsolutePath());
+		}
+		return new File(path);
+	}
 }

--
Gitblit v1.9.1