Add 'directory' configuration option in etc/lfs-storage-fs.config

Configuration example:
[storage]
	directory = /var/data/git-lfs-fs

Change-Id: I9bbd4c9b65b2b437484e7f51a239d629f32c77b8
Signed-off-by: Jacek Centkowski <geminica.programs@gmail.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfslocal/LocalLargeFileRepository.java b/src/main/java/com/googlesource/gerrit/plugins/lfslocal/LocalLargeFileRepository.java
index 41b3c14..f0da120 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfslocal/LocalLargeFileRepository.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfslocal/LocalLargeFileRepository.java
@@ -14,20 +14,49 @@
 
 package com.googlesource.gerrit.plugins.lfslocal;
 
-import java.io.IOException;
-import java.nio.file.Path;
-
-import org.eclipse.jgit.lfs.server.fs.FileLfsRepository;
-
+import com.google.common.base.Strings;
 import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl;
 import com.google.gerrit.extensions.annotations.PluginData;
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.server.config.PluginConfigFactory;
 import com.google.inject.Inject;
 
-public class LocalLargeFileRepository extends FileLfsRepository {
+import org.eclipse.jgit.lfs.server.fs.FileLfsRepository;
+import org.eclipse.jgit.lib.Config;
 
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+public class LocalLargeFileRepository extends FileLfsRepository {
   @Inject
-  LocalLargeFileRepository(@PluginCanonicalWebUrl String url,
-      @PluginData Path dataDir) throws IOException {
-    super(url, dataDir);
+  LocalLargeFileRepository(PluginConfigFactory cfg,
+      @PluginName String pluginName,
+      @PluginCanonicalWebUrl String url,
+      @PluginData Path defaultDataDir) throws IOException {
+    super(url, getOrCreateDataDir(cfg, pluginName, defaultDataDir));
+  }
+
+  private static Path getOrCreateDataDir(PluginConfigFactory cfgFactory,
+      String pluginName, Path defaultDataDir) throws IOException {
+    Config cfg = cfgFactory.getGlobalPluginConfig(pluginName);
+    String dataDir = cfg.getString("storage", null, "directory");
+    if (Strings.isNullOrEmpty(dataDir)) {
+      return defaultDataDir;
+    }
+
+    // note that the following method not only creates missing
+    // directory/directories but throws exception when path
+    // exists and points to file
+    Path ensured = Files.createDirectories(Paths.get(dataDir));
+
+    // we should at least make sure that directory is readable
+    if (!Files.isReadable(ensured)) {
+      throw new IOException(
+          "Path '" + ensured.toAbsolutePath() + "' cannot be accessed");
+    }
+
+    return ensured;
   }
 }
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index 63e1cbb..3c54ff6 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -1,4 +1 @@
-This plugin provides a local file system storage for the LFS service.
-
-The large files are stored under the plugin's data directory
-$GERRIT_SITE/data/lfs-storage-fs.
+This plugin provides a local file system storage for the LFS service.
\ No newline at end of file
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index f44d0a4..2dfeef5 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -1,4 +1,16 @@
-@PLUGIN@ Configuration
+Plugin @PLUGIN@ configuration
 ======================
 
-This plugin has no configuration.
+The following options can be configured in `$GERRIT_SITE/etc/@PLUGIN@.config`
+
+Section `storage`
+-------------------------
+
+```
+  [storage]
+    directory = [path to data directory]
+```
+
+Path to directory where large files should be stored.
+If not configured, defaults to the plugin's data directory:
+`$GERRIT_SITE/data/@PLUGIN@`
\ No newline at end of file