File matrix-synapse-proper-paths.patch of Package matrix-synapse

From bfae10ce312a2eb9724229bb203be64e09a95b3f Mon Sep 17 00:00:00 2001
From: Oleg Girko <ol@infoserver.lv>
Date: Wed, 25 Nov 2015 01:32:19 +0000
Subject: [PATCH] Use proper paths for uploads, database, pid and log files.

Signed-off-by: Oleg Girko <ol@infoserver.lv>
---
 synapse/config/_base.py       | 21 +++++++++++++++++++++
 synapse/config/database.py    |  4 ++--
 synapse/config/logger.py      |  2 +-
 synapse/config/repository.py  |  2 +-
 synapse/config/server.py      |  4 ++--
 tests/config/test_database.py |  4 ++--
 tests/config/test_generate.py |  3 ++-
 7 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/synapse/config/_base.py b/synapse/config/_base.py
index 1265738dc..0f1a62c7d 100644
--- a/synapse/config/_base.py
+++ b/synapse/config/_base.py
@@ -176,6 +176,27 @@ class Config:
     def abspath(file_path: str) -> str:
         return os.path.abspath(file_path) if file_path else file_path
 
+    @staticmethod
+    def runpath(file_path: str) -> str:
+        if file_path:
+            return os.path.join("/var/run/synapse", file_path)
+        else:
+            return file_path
+
+    @staticmethod
+    def varpath(file_path: str) -> str:
+        if file_path:
+            return os.path.join("/var/lib/synapse", file_path)
+        else:
+            return file_path
+
+    @staticmethod
+    def logpath(file_path: str) -> str:
+        if file_path:
+            return os.path.join("/var/log/synapse", file_path)
+        else:
+            return file_path
+
     @classmethod
     def path_exists(cls, file_path: str) -> bool:
         return path_exists(file_path)
diff --git a/synapse/config/database.py b/synapse/config/database.py
index 06ccf15cd..07fc46b8f 100644
--- a/synapse/config/database.py
+++ b/synapse/config/database.py
@@ -166,7 +166,7 @@ class DatabaseConfig(Config):
 
     def generate_config_section(self, data_dir_path, **kwargs) -> str:
         return DEFAULT_CONFIG % {
-            "database_path": os.path.join(data_dir_path, "homeserver.db")
+            "database_path": self.varpath("homeserver.db")
         }
 
     def read_arguments(self, args: argparse.Namespace) -> None:
@@ -198,7 +198,7 @@ class DatabaseConfig(Config):
     def set_databasepath(self, database_path: str) -> None:
 
         if database_path != ":memory:":
-            database_path = self.abspath(database_path)
+            database_path = self.varpath(database_path)
 
         self.databases[0].config["args"]["database"] = database_path
 
diff --git a/synapse/config/logger.py b/synapse/config/logger.py
index ea69b9bd9..fbbc141ec 100644
--- a/synapse/config/logger.py
+++ b/synapse/config/logger.py
@@ -190,7 +190,7 @@ class LoggingConfig(Config):
     def generate_files(self, config: Dict[str, Any], config_dir_path: str) -> None:
         log_config = config.get("log_config")
         if log_config and not os.path.exists(log_config):
-            log_file = self.abspath("homeserver.log")
+            log_file = self.logpath("homeserver.log")
             print(
                 "Generating log config file %s which will log to %s"
                 % (log_config, log_file)
diff --git a/synapse/config/repository.py b/synapse/config/repository.py
index 1980351e7..5e8390f12 100644
--- a/synapse/config/repository.py
+++ b/synapse/config/repository.py
@@ -224,7 +224,7 @@ class ContentRepositoryConfig(Config):
             ) or ["en"]
 
     def generate_config_section(self, data_dir_path, **kwargs):
-        media_store = os.path.join(data_dir_path, "media_store")
+        media_store = self.varpath("media_store")
 
         formatted_thumbnail_sizes = "".join(
             THUMBNAIL_SIZE_YAML % s for s in DEFAULT_THUMBNAIL_SIZES
diff --git a/synapse/config/server.py b/synapse/config/server.py
index 1de2dea9b..bd932509e 100644
--- a/synapse/config/server.py
+++ b/synapse/config/server.py
@@ -258,7 +258,7 @@ class ServerConfig(Config):
         except ValueError as e:
             raise ConfigError(str(e))
 
-        self.pid_file = self.abspath(config.get("pid_file"))
+        self.pid_file = self.runpath(config.get("pid_file"))
         self.web_client_location = config.get("web_client_location", None)
         self.soft_file_limit = config.get("soft_file_limit", 0)
         self.daemonize = config.get("daemonize")
@@ -696,7 +696,7 @@ class ServerConfig(Config):
             bind_port = 8448
             unsecure_port = 8008
 
-        pid_file = os.path.join(data_dir_path, "homeserver.pid")
+        pid_file = self.runpath("homeserver.pid")
 
         # Bring DEFAULT_ROOM_VERSION into the local-scope for use in the
         # default config string
diff --git a/tests/config/test_database.py b/tests/config/test_database.py
index 9eca10bbe..c488337f5 100644
--- a/tests/config/test_database.py
+++ b/tests/config/test_database.py
@@ -22,12 +22,12 @@ from tests import unittest
 class DatabaseConfigTestCase(unittest.TestCase):
     def test_database_configured_correctly(self):
         conf = yaml.safe_load(
-            DatabaseConfig().generate_config_section(data_dir_path="/data_dir_path")
+            DatabaseConfig().generate_config_section(data_dir_path="/var/lib/synapse")
         )
 
         expected_database_conf = {
             "name": "sqlite3",
-            "args": {"database": "/data_dir_path/homeserver.db"},
+            "args": {"database": "/var/lib/synapse/homeserver.db"},
         }
 
         self.assertEqual(conf["database"], expected_database_conf)
diff --git a/tests/config/test_generate.py b/tests/config/test_generate.py
index fdfbb0e38..0d40754a4 100644
--- a/tests/config/test_generate.py
+++ b/tests/config/test_generate.py
@@ -20,6 +20,7 @@ from contextlib import redirect_stdout
 from io import StringIO
 
 from synapse.config.homeserver import HomeServerConfig
+from synapse.config._base import Config
 
 from tests import unittest
 
@@ -53,7 +54,7 @@ class ConfigGenerationTestCase(unittest.TestCase):
 
         self.assert_log_filename_is(
             os.path.join(self.dir, "lemurs.win.log.config"),
-            os.path.join(os.getcwd(), "homeserver.log"),
+            Config.logpath("homeserver.log"),
         )
 
     def assert_log_filename_is(self, log_config_file, expected):
-- 
2.34.1