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

From f42015bf11f13f0225c0470d6332e14cc3a42101 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 fdfbee98a1..f3fbf1f6dc 100644
--- a/synapse/config/_base.py
+++ b/synapse/config/_base.py
@@ -264,6 +264,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 8e9d253820..bc566fa4b1 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 4af73627be..35d0d31fb4 100644
--- a/synapse/config/logger.py
+++ b/synapse/config/logger.py
@@ -189,7 +189,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 c87442aace..3a7e0e2878 100644
--- a/synapse/config/repository.py
+++ b/synapse/config/repository.py
@@ -312,5 +312,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 ca94c224ea..bad6f291fc 100644
--- a/synapse/config/server.py
+++ b/synapse/config/server.py
@@ -416,7 +416,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"))
@@ -931,7 +931,7 @@ class ServerConfig(Config):
         listeners: list[dict] | None = None,
         **kwargs: Any,
     ) -> str:
-        pid_file = os.path.join(data_dir_path, "homeserver.pid")
+        pid_file = self.runpath("homeserver.pid")
 
         http_bindings = "[]"
         private_addresses = ["::1", "127.0.0.1"]
diff --git a/tests/config/test_database.py b/tests/config/test_database.py
index 3fa5fff2b2..57d9a9a791 100644
--- a/tests/config/test_database.py
+++ b/tests/config/test_database.py
@@ -30,13 +30,13 @@ class DatabaseConfigTestCase(unittest.TestCase):
     def test_database_configured_correctly(self) -> None:
         conf = yaml.safe_load(
             DatabaseConfig(RootConfig()).generate_config_section(
-                data_dir_path="/data_dir_path"
+                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.53.0