From a4a60ea2023bf8380df43a0e1452471a6e12d322 Mon Sep 17 00:00:00 2001 From: Matthias Klumpp Date: Tue, 2 Feb 2021 19:39:03 +0100 Subject: [PATCH] Ensure export directory paths are sane, absolute paths all the time --- src/app.d | 10 +++++----- src/asgen/config.d | 37 +++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/app.d b/src/app.d index 255b0386..070c980b 100644 --- a/src/app.d +++ b/src/app.d @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016-2017 Matthias Klumpp + * Copyright (C) 2016-2021 Matthias Klumpp * * Licensed under the GNU Lesser General Public License Version 3 * @@ -54,7 +54,7 @@ Application Options: --force Force action. -w|--workspace Define the workspace location. -c|--config Use the given configuration file. - -e|--exportDir Directory to export data to."; + --export-dir Override the workspace root export directory."; version (unittest) { void main () {} @@ -96,7 +96,7 @@ void main(string[] args) bool showVersion; bool forceAction; string wdir; - string edir; + string exportDir; string configFname; // parse command-line options @@ -108,7 +108,7 @@ void main(string[] args) "force", &forceAction, "workspace|w", &wdir, "config|c", &configFname, - "exportDir|e", &edir); + "export-dir", &exportDir); } catch (Exception e) { writeln ("Unable to parse parameters: ", e.msg); exit (1); @@ -143,7 +143,7 @@ void main(string[] args) } try { - conf.loadFromFile (configFname, wdir, edir); + conf.loadFromFile (configFname, wdir, exportDir); } catch (Exception e) { writefln ("Unable to load configuration: %s", e.msg); exit (4); diff --git a/src/asgen/config.d b/src/asgen/config.d index 7dabf464..7ed01d16 100644 --- a/src/asgen/config.d +++ b/src/asgen/config.d @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016-2020 Matthias Klumpp + * Copyright (C) 2016-2021 Matthias Klumpp * * Licensed under the GNU Lesser General Public License Version 3 * @@ -22,7 +22,7 @@ module asgen.config; import std.stdio; import std.array; import std.string : format, toLower; -import std.path : dirName, buildPath, buildNormalizedPath; +import std.path : dirName, buildPath, buildNormalizedPath, isAbsolute, absolutePath; import std.conv : to; import std.json; import std.typecons; @@ -289,6 +289,8 @@ public: // allow overriding the workspace location if (!enforcedWorkspaceDir.empty) workspaceDir = enforcedWorkspaceDir; + if (!workspaceDir.isAbsolute) + workspaceDir = workspaceDir.absolutePath; this.projectName = "Unknown"; if ("ProjectName" in root) @@ -304,18 +306,22 @@ public: if ("HtmlBaseUrl" in root) this.htmlBaseUrl = root["HtmlBaseUrl"].str; - // set the default export directory locations, allow people to override them in the config - exportDir = buildPath (workspaceDir, "export"); - - // allow overriding the export location - if (!enforcedExportDir.empty) + // set root export directory + if (enforcedExportDir.empty) { + exportDir = buildPath (workspaceDir, "export"); + } else { exportDir = enforcedExportDir; + logInfo ("Using data export directory root from the command-line: %s", exportDir); + } + if (!exportDir.isAbsolute) + exportDir = exportDir.absolutePath; - mediaExportDir = buildPath (exportDir, "media"); - dataExportDir = buildPath (exportDir, "data"); - hintsExportDir = buildPath (exportDir, "hints"); - htmlExportDir = buildPath (exportDir, "html"); - auto extraMetainfoDir = buildPath (workspaceDir, "extra-metainfo"); + // set the default export directory locations, allow people to override them in the config + // (we convert the relative to absolute paths later) + mediaExportDir = "media"; + dataExportDir = "data"; + hintsExportDir = "hints"; + htmlExportDir = "html"; if ("ExportDirs" in root) { auto edirs = root["ExportDirs"].object; @@ -339,7 +345,14 @@ public: } } + // convert export directory paths to absolute paths if necessary + mediaExportDir = mediaExportDir.isAbsolute? mediaExportDir : buildNormalizedPath (exportDir, mediaExportDir); + dataExportDir = dataExportDir.isAbsolute? dataExportDir : buildNormalizedPath (exportDir, dataExportDir); + hintsExportDir = hintsExportDir.isAbsolute? hintsExportDir : buildNormalizedPath (exportDir, hintsExportDir); + htmlExportDir = htmlExportDir.isAbsolute? htmlExportDir : buildNormalizedPath (exportDir, htmlExportDir); + // a place where external metainfo data can be injected + auto extraMetainfoDir = buildPath (workspaceDir, "extra-metainfo"); if ("ExtraMetainfoDir" in root) extraMetainfoDir = root["ExtraMetainfoDir"].str;