From cb3a59f88f715484f62816cee44ebb6096f6ad97 Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Tue, 2 Jun 2020 09:13:31 +0100 Subject: [PATCH] Windows recollindex: use wmain to accept unicode command line args. --- src/index/recollindex.cpp | 54 ++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/index/recollindex.cpp b/src/index/recollindex.cpp index 4a563f58..92dced4e 100644 --- a/src/index/recollindex.cpp +++ b/src/index/recollindex.cpp @@ -548,19 +548,29 @@ static void flushIdxReasons() } } +#if defined(_WIN32) +#define WARGTOSTRING(w) wchartoutf8(w) +static vector argstovector(int argc, wchar_t **argv) +#else +#define WARGTOSTRING(w) (w) static vector argstovector(int argc, char **argv) +#endif { + thisprog = path_absolute(WARGTOSTRING(argv[0])); + argc--; argv++; vector args; for (int i = 0; i < argc; i++) { - args.push_back(argv[i]); + args.push_back(WARGTOSTRING(argv[i])); } return args; } + static vector fileToArgs(const string& fn) { string reason, data; if (!file_to_string(fn, data, &reason)) { - cerr << "Failed reading args file " << fn << " errno " << errno << "\n"; + std::cerr << "Failed reading args file " << fn << " reason " << + reason << "\n"; exit(1); } vector args; @@ -568,12 +578,19 @@ static vector fileToArgs(const string& fn) return args; } -int main(int argc, char **argv) +// A bit of history: it's difficult to pass non-ascii parameters +// (e.g. path names) on the command line under Windows without using +// Unicode. It was first thought possible to use a temporary file to +// hold the args, and make sure that the path for this would be ASCII, +// based on using shortpath(). Unfortunately, this does not work in +// all cases, so the second change was to use wmain(), but the +// now largely redundant args-in-file passing trick was kept anyway. +#ifdef _WIN32 +int wmain(int argc, wchar_t *argv[]) +#else +int main(int argc, char *argv[]) +#endif { - string a_config; - int sleepsecs = 60; - vector selpatterns; - // The reexec struct is used by the daemon to shed memory after // the initial indexing pass and to restart when the configuration // changes @@ -582,9 +599,6 @@ int main(int argc, char **argv) o_reexec->init(argc, argv); #endif - thisprog = path_absolute(argv[0]); - argc--; argv++; - vector args = argstovector(argc, argv); // Passing args through a temp file: this is used on Windows to @@ -593,6 +607,9 @@ int main(int argc, char **argv) args = fileToArgs(args[0]); } + vector selpatterns; + int sleepsecs{60}; + string a_config; unsigned int aremain = args.size(); unsigned int argidx = 0; for (; argidx < args.size(); argidx++) { @@ -703,18 +720,13 @@ int main(int argc, char **argv) string rundir; config->getConfParam("idxrundir", rundir); - if (!rundir.compare("tmp")) { - LOGINFO("recollindex: changing current directory to [" << - tmplocation() << "]\n"); - if (chdir(tmplocation().c_str()) < 0) { - LOGERR("chdir(" << tmplocation() << ") failed, errno " << errno << - "\n"); + if (!rundir.empty()) { + if (!rundir.compare("tmp")) { + rundir = tmplocation(); } - } else if (!rundir.empty()) { - LOGINFO("recollindex: changing current directory to [" << rundir << - "]\n"); - if (chdir(rundir.c_str()) < 0) { - LOGERR("chdir(" << rundir << ") failed, errno " << errno << "\n"); + LOGINFO("recollindex: changing current directory to [" <