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

From 55ff63215b5da392ae523b256735fe37c438358f 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 adce34c03a..ac0bce67b4 100644
--- a/synapse/config/_base.py
+++ b/synapse/config/_base.py
@@ -254,6 +254,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 c4ca63a1fa..5f795b235d 100644
--- a/synapse/config/database.py
+++ b/synapse/config/database.py
@@ -131,7 +131,7 @@ class DatabaseConfig(Config):
 
     def generate_config_section(self, data_dir_path: str, **kwargs: Any) -> 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:
@@ -162,7 +162,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 fca0b08d6d..1e3ae8ea3f 100644
--- a/synapse/config/logger.py
+++ b/synapse/config/logger.py
@@ -191,7 +191,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 97ce6de528..1a05c99186 100644
--- a/synapse/config/repository.py
+++ b/synapse/config/repository.py
@@ -278,5 +278,5 @@ class ContentRepositoryConfig(Config):
 
     def generate_config_section(self, data_dir_path: str, **kwargs: Any) -> str:
         assert data_dir_path is not None
-        media_store = os.path.join(data_dir_path, "media_store")
+        media_store = self.varpath("media_store")
         return f"media_store_path: {media_store}"
diff --git a/synapse/config/server.py b/synapse/config/server.py
index fd52c0475c..798f38cca9 100644
--- a/synapse/config/server.py
+++ b/synapse/config/server.py
@@ -306,7 +306,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.soft_file_limit = config.get("soft_file_limit", 0)
         self.daemonize = bool(config.get("daemonize"))
         self.print_pidfile = bool(config.get("print_pidfile"))
@@ -799,7 +799,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")
 
         secure_listeners = []
         unsecure_listeners = []
diff --git a/tests/config/test_database.py b/tests/config/test_database.py
index b46519f84a..2052c84f33 100644
--- a/tests/config/test_database.py
+++ b/tests/config/test_database.py
@@ -28,12 +28,12 @@ from tests import unittest
 class DatabaseConfigTestCase(unittest.TestCase):
     def test_database_configured_correctly(self) -> None:
         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 5ab96e16e1..2a3be04d3a 100644
--- a/tests/config/test_generate.py
+++ b/tests/config/test_generate.py
@@ -27,6 +27,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
 
@@ -60,7 +61,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: str, expected: str) -> None:
-- 
2.46.0