From 4822638f024e13f3418f2bb04b8d66e4e6261b8b Mon Sep 17 00:00:00 2001 From: Simon Rose Date: Mon, 4 Nov 2024 15:30:46 +0100 Subject: [PATCH] Improve relative path check for msi For posix-ish systems, we previously checked that a path was relative by simpy checking if it included a '/' character. This meant that you could not, for example, do ``` $ cat foo.substitutions file rel/to/bar.template { } $ msi -I /some/path foo.substitutions ``` where our template file is located at `/some/path/rel/to/bar.template`. Note that relateive paths work differently on Windows, so we carve out an exception there. --- modules/database/src/ioc/dbtemplate/Makefile | 1 + modules/database/src/ioc/dbtemplate/msi.cpp | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/modules/database/src/ioc/dbtemplate/Makefile b/modules/database/src/ioc/dbtemplate/Makefile index 99c46530fd..ffc5917be1 100644 --- a/modules/database/src/ioc/dbtemplate/Makefile +++ b/modules/database/src/ioc/dbtemplate/Makefile @@ -14,6 +14,7 @@ SRC_DIRS += $(IOCDIR)/dbtemplate PROD_HOST += msi msi_SRCS = msi.cpp +msi_LIBS_WIN32 = Shlwapi DOCS += msi.md INC += dbLoadTemplate.h diff --git a/modules/database/src/ioc/dbtemplate/msi.cpp b/modules/database/src/ioc/dbtemplate/msi.cpp index 88c5d160d9..f107f2460a 100644 --- a/modules/database/src/ioc/dbtemplate/msi.cpp +++ b/modules/database/src/ioc/dbtemplate/msi.cpp @@ -27,6 +27,10 @@ #include #include +#ifdef _WIN32 +#include +#endif + #define MAX_BUFFER_SIZE 4096 #define MAX_DEPS 1024 @@ -493,6 +497,14 @@ static void inputErrPrint(const inputData *const pinputData) EXIT; } +static int isPathRelative(const char * const path) { +#ifdef _WIN32 + return path && PathIsRelativeA(path); +#else + return path && path[0] != '/'; +#endif +} + static void inputOpenFile(inputData *pinputData, const char * const filename) { std::list& pathList = pinputData->pathList; @@ -505,7 +517,7 @@ static void inputOpenFile(inputData *pinputData, const char * const filename) STEP("Using stdin"); fp = stdin; } - else if (pathList.empty() || strchr(filename, '/')){ + else if (pathList.empty() || !isPathRelative(filename)){ STEPS("Opening ", filename); fp = fopen(filename, "r"); }