[v6,15/16] config: Make configuration file configurable
diff mbox series

Message ID 20241206171148.1189292-16-mzamazal@redhat.com
State New
Headers show
Series
  • Add global configuration file
Related show

Commit Message

Milan Zamazal Dec. 6, 2024, 5:11 p.m. UTC
To support easy switching of configurations, let's introduce
LIBCAMERA_CONFIG_NAME` environment variable, which denotes:

- The path of the configuration file to load if it is an absolute path; or
- the path of the configuration file to load, relative to the configuration
  directories.

If such a configuration file doesn't exist, no custom configuration is
loaded.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 Documentation/runtime_configuration.rst     |  8 +++++++
 src/libcamera/base/global_configuration.cpp | 26 ++++++++++++++++-----
 2 files changed, 28 insertions(+), 6 deletions(-)

Patch
diff mbox series

diff --git a/Documentation/runtime_configuration.rst b/Documentation/runtime_configuration.rst
index e52754e5..59211169 100644
--- a/Documentation/runtime_configuration.rst
+++ b/Documentation/runtime_configuration.rst
@@ -19,6 +19,14 @@  order:
 - LIBCAMERA_SYSCONF_DIR/configuration.yaml
 - /etc/libcamera/configuration.yaml
 
+The default name of the configuration file, configuration.yaml, can be
+overridden in LIBCAMERA_CONFIG_NAME environment variable. The variable
+can specify just an alternative configuration file name to be looked up
+in the locations above, or it can contain a whole relative or absolute
+path. If an absolute path is specified then it is the only location that
+is used; if the given file doesn't exist then no configuration file is
+read.
+
 The first configuration file found wins, contingent configuration files
 in other locations are ignored.
 
diff --git a/src/libcamera/base/global_configuration.cpp b/src/libcamera/base/global_configuration.cpp
index e69951b4..f35b3dae 100644
--- a/src/libcamera/base/global_configuration.cpp
+++ b/src/libcamera/base/global_configuration.cpp
@@ -60,13 +60,27 @@  bool loadFile(const std::filesystem::path &fileName)
 }
 
 const std::vector<std::filesystem::path>
-	globalConfigurationFiles = {
-		std::filesystem::path(LIBCAMERA_SYSCONF_DIR) / "configuration.yaml",
-		std::filesystem::path("/etc/libcamera/configuration.yaml"),
+	globalConfigurationDirectories = {
+		std::filesystem::path(LIBCAMERA_SYSCONF_DIR),
+		std::filesystem::path("/etc/libcamera"),
 	};
 
 void load()
 {
+	const char *libcameraConfigName =
+		utils::secure_getenv("LIBCAMERA_CONFIG_NAME");
+	if (!libcameraConfigName)
+		libcameraConfigName = "";
+	std::filesystem::path configName(libcameraConfigName);
+
+	if (configName.is_absolute()) {
+		loadFile(configName);
+		return;
+	}
+
+	if (configName.empty())
+		configName = std::filesystem::path("configuration.yaml");
+
 	std::filesystem::path userConfigurationDirectory;
 	char *xdgConfigHome = utils::secure_getenv("XDG_CONFIG_HOME");
 	if (xdgConfigHome) {
@@ -80,13 +94,13 @@  void load()
 
 	if (!userConfigurationDirectory.empty()) {
 		std::filesystem::path user_configuration_file =
-			userConfigurationDirectory / "libcamera" / "configuration.yaml";
+			userConfigurationDirectory / "libcamera" / configName;
 		if (loadFile(user_configuration_file))
 			return;
 	}
 
-	for (const auto &path : globalConfigurationFiles)
-		if (loadFile(path))
+	for (const auto &path : globalConfigurationDirectories)
+		if (loadFile(path / configName))
 			return;
 }