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

From 58357977badcb96be070c5ce8cbc05328a1a3e14 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 08e2c2c54..4135ce4e1 100644
--- a/synapse/config/_base.py
+++ b/synapse/config/_base.py
@@ -180,6 +180,27 @@ class Config:
     def abspath(file_path):
         return os.path.abspath(file_path) if file_path else file_path
 
+    @staticmethod
+    def runpath(file_path):
+        if file_path:
+            return os.path.join("/var/run/synapse", file_path)
+        else:
+            return file_path
+
+    @staticmethod
+    def varpath(file_path):
+        if file_path:
+            return os.path.join("/var/lib/synapse", file_path)
+        else:
+            return file_path
+
+    @staticmethod
+    def logpath(file_path):
+        if file_path:
+            return os.path.join("/var/log/synapse", file_path)
+        else:
+            return file_path
+
     @classmethod
     def path_exists(cls, file_path):
         return path_exists(file_path)
diff --git a/synapse/config/database.py b/synapse/config/database.py
index c76ef1e1d..1ff3e199d 100644
--- a/synapse/config/database.py
+++ b/synapse/config/database.py
@@ -160,7 +160,7 @@ class DatabaseConfig(Config):
 
     def generate_config_section(self, data_dir_path, **kwargs):
         return DEFAULT_CONFIG % {
-            "database_path": os.path.join(data_dir_path, "homeserver.db")
+            "database_path": self.varpath("homeserver.db")
         }
 
     def read_arguments(self, args):
@@ -192,7 +192,7 @@ class DatabaseConfig(Config):
     def set_databasepath(self, database_path):
 
         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 813076dfe..ba5e37a1e 100644
--- a/synapse/config/logger.py
+++ b/synapse/config/logger.py
@@ -171,7 +171,7 @@ class LoggingConfig(Config):
     def generate_files(self, config, config_dir_path):
         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 c78a83abe..a4d26b1ef 100644
--- a/synapse/config/repository.py
+++ b/synapse/config/repository.py
@@ -207,7 +207,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 c290a35a9..5295f1e1f 100644
--- a/synapse/config/server.py
+++ b/synapse/config/server.py
@@ -228,7 +228,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")
@@ -726,7 +726,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.31.1