From: Carl Hetherington Date: Wed, 3 Jun 2020 10:21:28 +0000 (+0200) Subject: Clean up handling of paths relative to the executable. X-Git-Tag: v2.15.78~29 X-Git-Url: https://git.carlh.net/gitweb/?p=dcpomatic.git;a=commitdiff_plain;h=7552a04c443c9c641ac580585f6d88900bf84d04 Clean up handling of paths relative to the executable. --- diff --git a/src/lib/cross.h b/src/lib/cross.h index 114581269..4d3d2b2c7 100644 --- a/src/lib/cross.h +++ b/src/lib/cross.h @@ -49,17 +49,14 @@ extern boost::filesystem::path openssl_path (); #ifdef DCPOMATIC_DISK extern boost::filesystem::path disk_writer_path (); #endif -#ifdef DCPOMATIC_OSX -extern boost::filesystem::path app_contents (); -#endif #ifdef DCPOMATIC_WINDOWS extern void maybe_open_console (); #endif extern boost::filesystem::path shared_path (); extern FILE * fopen_boost (boost::filesystem::path, std::string); extern int dcpomatic_fseek (FILE *, int64_t, int); -extern void start_batch_converter (boost::filesystem::path dcpomatic); -extern void start_player (boost::filesystem::path dcpomatic); +extern void start_batch_converter (); +extern void start_player (); extern uint64_t thread_id (); extern int avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags); extern boost::filesystem::path home_directory (); @@ -67,6 +64,7 @@ extern std::string command_and_read (std::string cmd); extern bool running_32_on_64 (); extern void unprivileged (); extern boost::filesystem::path config_path (); +extern boost::filesystem::path directory_containing_executable (); class PrivilegeEscalator { diff --git a/src/lib/cross_linux.cc b/src/lib/cross_linux.cc index c8b890bf1..7b807a441 100644 --- a/src/lib/cross_linux.cc +++ b/src/lib/cross_linux.cc @@ -140,20 +140,36 @@ mount_info () return m; } + +boost::filesystem::path +directory_containing_executable () +{ +#if BOOST_VERSION >= 106100 + return boost::dll::program_location().parent_path(); +#else + char buffer[PATH_MAX]; + ssize_t N = readlink ("/proc/self/exe", buffer, PATH_MAX); + return boost::filesystem::path(string(buffer, N)).parent_path(); +#endif +} + + boost::filesystem::path openssl_path () { - return "dcpomatic2_openssl"; + return directory_containing_executable() / "dcpomatic2_openssl"; } + #ifdef DCPOMATIC_DISK boost::filesystem::path disk_writer_path () { - return boost::dll::program_location().parent_path() / "dcpomatic2_disk_writer"; + return directory_containing_executable() / "dcpomatic2_disk_writer"; } #endif + /* Apparently there is no way to create an ofstream using a UTF-8 filename under Windows. We are hence reduced to using fopen with this wrapper. @@ -186,10 +202,11 @@ Waker::~Waker () } + void -start_tool (boost::filesystem::path dcpomatic, string executable, string) +start_tool (string executable) { - boost::filesystem::path batch = dcpomatic.parent_path() / executable; + boost::filesystem::path batch = directory_containing_executable() / executable; pid_t pid = fork (); if (pid == 0) { @@ -198,18 +215,21 @@ start_tool (boost::filesystem::path dcpomatic, string executable, string) } } + void -start_batch_converter (boost::filesystem::path dcpomatic) +start_batch_converter () { - start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app"); + start_tool ("dcpomatic2_batch"); } + void -start_player (boost::filesystem::path dcpomatic) +start_player () { - start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app"); + start_tool ("dcpomatic2_player"); } + uint64_t thread_id () { diff --git a/src/lib/cross_osx.cc b/src/lib/cross_osx.cc index a15ad0322..574cc8660 100644 --- a/src/lib/cross_osx.cc +++ b/src/lib/cross_osx.cc @@ -32,6 +32,9 @@ extern "C" { #include #include #include +#if BOOST_VERSION >= 106100 +#include +#endif #include #include #include @@ -91,14 +94,13 @@ cpu_info () return info; } -/** @return Path of the Contents directory in the .app */ + boost::filesystem::path -app_contents () +directory_containing_executable () { - /* Could use boost::dll::program_location().parent_path().parent_path() but that - * boost library isn't in the 10.6 environment we're currently using and I don't - * really want to change it right now. - */ +#if BOOST_VERSION >= 106100 + return boost::dll::program_location().parent_path(); +#else uint32_t size = 1024; char buffer[size]; if (_NSGetExecutablePath (buffer, &size)) { @@ -107,29 +109,29 @@ app_contents () boost::filesystem::path path (buffer); path = boost::filesystem::canonical (path); - path = path.parent_path (); - path = path.parent_path (); - return path; + return path.parent_path (); +#endif } + boost::filesystem::path shared_path () { - return app_contents() / "Resources"; + return directory_containing_executable().parent_path() / "Resources"; } + void run_ffprobe (boost::filesystem::path content, boost::filesystem::path out) { - boost::filesystem::path path = app_contents(); - path /= "MacOS"; - path /= "ffprobe"; + boost::filesystem::path path = directory_containing_executable () / "ffprobe"; string ffprobe = "\"" + path.string() + "\" \"" + content.string() + "\" 2> \"" + out.string() + "\""; LOG_GENERAL (N_("Probing with %1"), ffprobe); system (ffprobe.c_str ()); } + list > mount_info () { @@ -140,21 +142,16 @@ mount_info () boost::filesystem::path openssl_path () { - boost::filesystem::path path = app_contents(); - path /= "MacOS"; - path /= "openssl"; - return path; + return directory_containing_executable() / "openssl"; } + #ifdef DCPOMATIC_DISK /* Note: this isn't actually used at the moment as the disk writer is started as a service */ boost::filesystem::path disk_writer_path () { - boost::filesystem::path path = app_contents(); - path /= "MacOS"; - path /= "dcpomatic2_disk_writer"; - return path; + return directory_containing_executable() / "dcpomatic2_disk_writer"; } #endif @@ -196,9 +193,9 @@ Waker::~Waker () } void -start_tool (boost::filesystem::path dcpomatic, string executable, string app) +start_tool (string executable, string app) { - boost::filesystem::path batch = dcpomatic.parent_path (); + boost::filesystem::path batch = directory_containing_executable(); batch = batch.parent_path (); // MacOS batch = batch.parent_path (); // Contents batch = batch.parent_path (); // DCP-o-matic.app @@ -215,18 +212,21 @@ start_tool (boost::filesystem::path dcpomatic, string executable, string app) } } + void -start_batch_converter (boost::filesystem::path dcpomatic) +start_batch_converter () { - start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app"); + start_tool ("dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app"); } + void -start_player (boost::filesystem::path dcpomatic) +start_player () { - start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app"); + start_tool ("dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app"); } + uint64_t thread_id () { diff --git a/src/lib/cross_windows.cc b/src/lib/cross_windows.cc index 8ccc790d6..d732e9fe5 100644 --- a/src/lib/cross_windows.cc +++ b/src/lib/cross_windows.cc @@ -127,8 +127,7 @@ run_ffprobe (boost::filesystem::path content, boost::filesystem::path out) } wchar_t dir[512]; - GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir)); - PathRemoveFileSpec (dir); + MultiByteToWideChar (CP_UTF8, 0, directory_containing_executable().string().c_str(), -1, dir, sizeof(dir)); SetCurrentDirectory (dir); STARTUPINFO startup_info; @@ -185,32 +184,37 @@ mount_info () return m; } -static boost::filesystem::path -executable_path () + +boost::filesystem::path +directory_containing_executable () { return boost::dll::program_location().parent_path(); } + boost::filesystem::path shared_path () { - return executable_path().parent_path(); + return directory_containing_executable().parent_path(); } + boost::filesystem::path openssl_path () { - return executable_path() / "openssl.exe"; + return directory_containing_executable() / "openssl.exe"; } + #ifdef DCPOMATIC_DISK boost::filesystem::path disk_writer_path () { - return executable_path() / "dcpomatic2_disk_writer.exe"; + return directory_containing_executable() / "dcpomatic2_disk_writer.exe"; } #endif + /* Apparently there is no way to create an ofstream using a UTF-8 filename under Windows. We are hence reduced to using fopen with this wrapper. @@ -246,10 +250,11 @@ Waker::~Waker () } + void -start_tool (boost::filesystem::path dcpomatic, string executable, string) +start_tool (string executable) { - boost::filesystem::path batch = dcpomatic.parent_path() / executable; + boost::filesystem::path batch = directory_containing_executable() / executable; STARTUPINFO startup_info; ZeroMemory (&startup_info, sizeof (startup_info)); @@ -263,18 +268,21 @@ start_tool (boost::filesystem::path dcpomatic, string executable, string) CreateProcess (0, cmd, 0, 0, FALSE, 0, 0, 0, &startup_info, &process_info); } + void -start_batch_converter (boost::filesystem::path dcpomatic) +start_batch_converter () { - start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app"); + start_tool ("dcpomatic2_batch"); } + void -start_player (boost::filesystem::path dcpomatic) +start_player () { - start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app"); + start_tool ("dcpomatic2_player"); } + uint64_t thread_id () { diff --git a/src/lib/util.cc b/src/lib/util.cc index bff205cfb..e0efbc6f5 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -362,7 +362,7 @@ dcpomatic_setup () /* Add our library directory to the libltdl search path so that xmlsec can find xmlsec1-openssl. */ - boost::filesystem::path lib = app_contents (); + boost::filesystem::path lib = directory_containing_executable().parent_path(); lib /= "Frameworks"; setenv ("LTDL_LIBRARY_PATH", lib.c_str (), 1); #endif diff --git a/src/tools/dcpomatic.cc b/src/tools/dcpomatic.cc index 3168197d5..cef7cf602 100644 --- a/src/tools/dcpomatic.cc +++ b/src/tools/dcpomatic.cc @@ -812,7 +812,7 @@ private: } /** @return false if we succeeded, true if not */ - bool send_to_other_tool (int port, function start, string message) + bool send_to_other_tool (int port, function start, string message) { /* i = 0; try to connect via socket i = 1; try again, and then try to start the tool @@ -838,7 +838,7 @@ private: } if (i == 1) { - start (wx_to_std (wxStandardPaths::Get().GetExecutablePath())); + start (); } dcpomatic_sleep_seconds (1); @@ -864,7 +864,7 @@ private: _film->write_metadata (); - if (send_to_other_tool (BATCH_JOB_PORT, bind (&start_batch_converter, _1), _film->directory()->string())) { + if (send_to_other_tool (BATCH_JOB_PORT, &start_batch_converter, _film->directory()->string())) { error_dialog (this, _("Could not find batch converter.")); } } @@ -875,7 +875,7 @@ private: return; } - if (send_to_other_tool (PLAYER_PLAY_PORT, bind (&start_player, _1), _film->dir(_film->dcp_name(false)).string())) { + if (send_to_other_tool (PLAYER_PLAY_PORT, &start_player, _film->dir(_film->dcp_name(false)).string())) { error_dialog (this, _("Could not find player.")); } }