Move some common methods out to cross_unix.cc
[dcpomatic.git] / src / lib / cross_linux.cc
index 8c81c50081470353fd508aeb2fdc75c237a1a6f7..919927fdacddd53cf14b7e05c79d9052718b5a76 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
-#include "cross.h"
+
 #include "compose.hpp"
-#include "log.h"
-#include "dcpomatic_log.h"
 #include "config.h"
+#include "cross.h"
+#include "dcpomatic_log.h"
+#include "dcpomatic_log.h"
 #include "exceptions.h"
+#include "log.h"
 #include <dcp/raw_convert.h>
+#include <dcp/warnings.h>
 #include <glib.h>
-extern "C" {
-#include <libavformat/avio.h>
-}
 #include <boost/algorithm/string.hpp>
-#include <boost/foreach.hpp>
-#ifdef DCPOMATIC_DISK
+#if BOOST_VERSION >= 106100
 #include <boost/dll/runtime_symbol_info.hpp>
 #endif
 #include <unistd.h>
 #include <mntent.h>
 #include <sys/types.h>
+#include <sys/mount.h>
 #include <ifaddrs.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -44,31 +44,17 @@ extern "C" {
 
 #include "i18n.h"
 
-using std::pair;
-using std::list;
+
+using std::cerr;
+using std::cout;
 using std::ifstream;
-using std::string;
-using std::wstring;
+using std::list;
 using std::make_pair;
+using std::pair;
+using std::string;
 using std::vector;
-using std::cerr;
-using std::cout;
-using std::runtime_error;
-using boost::shared_ptr;
 using boost::optional;
 
-/** @param s Number of seconds to sleep for */
-void
-dcpomatic_sleep_seconds (int s)
-{
-       sleep (s);
-}
-
-void
-dcpomatic_sleep_milliseconds (int ms)
-{
-       usleep (ms * 1000);
-}
 
 /** @return A string of CPU information (model name etc.) */
 string
@@ -94,30 +80,42 @@ cpu_info ()
        return info;
 }
 
+
 boost::filesystem::path
-shared_path ()
+resources_path ()
 {
-       char const * p = getenv ("DCPOMATIC_LINUX_SHARE_PREFIX");
-       if (p) {
-               return p;
+       return directory_containing_executable().parent_path() / "share" / "dcpomatic2";
+}
+
+
+boost::filesystem::path
+libdcp_resources_path ()
+{
+       if (auto appdir = getenv("APPDIR")) {
+               return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp";
        }
-       return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
+       return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp";
 }
 
+
 void
 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
 {
        string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
        LOG_GENERAL (N_("Probing with %1"), ffprobe);
-        system (ffprobe.c_str ());
+       int const r = system (ffprobe.c_str());
+       if (r == -1 || (WIFEXITED(r) && WEXITSTATUS(r) != 0)) {
+               LOG_GENERAL (N_("Could not run ffprobe (system returned %1"), r);
+       }
 }
 
-list<pair<string, string> >
+
+list<pair<string, string>>
 mount_info ()
 {
-       list<pair<string, string> > m;
+       list<pair<string, string>> m;
 
-       FILE* f = setmntent ("/etc/mtab", "r");
+       auto f = setmntent ("/etc/mtab", "r");
        if (!f) {
                return m;
        }
@@ -136,35 +134,40 @@ 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 ()
 {
+       auto p = directory_containing_executable() / "dcpomatic2_openssl";
+       if (boost::filesystem::is_regular_file(p)) {
+               return p;
+       }
+
        return "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.
-*/
-FILE *
-fopen_boost (boost::filesystem::path p, string t)
-{
-        return fopen (p.c_str(), t.c_str ());
-}
-
-int
-dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
-{
-       return fseek (stream, offset, whence);
-}
 
 void
 Waker::nudge ()
@@ -172,20 +175,23 @@ Waker::nudge ()
 
 }
 
+
 Waker::Waker ()
 {
 
 }
 
+
 Waker::~Waker ()
 {
 
 }
 
+
 void
-start_tool (boost::filesystem::path dcpomatic, string executable, string)
+start_tool (string executable)
 {
-       boost::filesystem::path batch = dcpomatic.parent_path() / executable;
+       auto batch = directory_containing_executable() / executable;
 
        pid_t pid = fork ();
        if (pid == 0) {
@@ -194,89 +200,54 @@ start_tool (boost::filesystem::path dcpomatic, string executable, string)
        }
 }
 
-void
-start_batch_converter (boost::filesystem::path dcpomatic)
-{
-       start_tool (dcpomatic, "dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
-}
 
 void
-start_player (boost::filesystem::path dcpomatic)
+start_batch_converter ()
 {
-       start_tool (dcpomatic, "dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
+       start_tool ("dcpomatic2_batch");
 }
 
-uint64_t
-thread_id ()
-{
-       return (uint64_t) pthread_self ();
-}
 
-int
-avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
+void
+start_player ()
 {
-       return avio_open (s, file.c_str(), flags);
+       start_tool ("dcpomatic2_player");
 }
 
 
-boost::filesystem::path
-home_directory ()
+static
+vector<pair<string, string>>
+get_mounts (string prefix)
 {
-               return getenv("HOME");
-}
-
-string
-command_and_read (string cmd)
-{
-       FILE* pipe = popen (cmd.c_str(), "r");
-       if (!pipe) {
-               throw runtime_error ("popen failed");
-       }
+       vector<pair<string, string>> mounts;
 
-       string result;
-       char buffer[128];
-       try {
-               while (fgets(buffer, sizeof(buffer), pipe)) {
-                       result += buffer;
+       std::ifstream f("/proc/mounts");
+       string line;
+       while (f.good()) {
+               getline(f, line);
+               vector<string> bits;
+               boost::algorithm::split (bits, line, boost::is_any_of(" "));
+               if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
+                       boost::algorithm::replace_all (bits[1], "\\040", " ");
+                       mounts.push_back(make_pair(bits[0], bits[1]));
+                       LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
                }
-       } catch (...) {
-               pclose (pipe);
-               throw;
        }
 
-       pclose (pipe);
-       return result;
+       return mounts;
 }
 
-/** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
-bool
-running_32_on_64 ()
-{
-       /* I'm assuming nobody does this on Linux */
-       return false;
-}
 
 vector<Drive>
-get_drives ()
+Drive::get ()
 {
        vector<Drive> drives;
 
        using namespace boost::filesystem;
-       list<string> mounted_devices;
-       std::ifstream f("/proc/mounts");
-       string line;
-       while (f.good()) {
-               getline(f, line);
-               vector<string> bits;
-               boost::algorithm::split (bits, line, boost::is_any_of(" "));
-               if (bits.size() > 0 && boost::algorithm::starts_with(bits[0], "/dev/")) {
-                       mounted_devices.push_back(bits[0]);
-                       LOG_DISK("Mounted device %1", bits[0]);
-               }
-       }
+       auto mounted_devices = get_mounts("/dev/");
 
-       for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
-               string const name = i->path().filename().string();
+       for (auto i: directory_iterator("/sys/block")) {
+               string const name = i.path().filename().string();
                path device_type_file("/sys/block/" + name + "/device/type");
                optional<string> device_type;
                if (exists(device_type_file)) {
@@ -285,11 +256,10 @@ get_drives ()
                }
                /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
                if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
-                       uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
+                       uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(i / "size")) * 512;
                        if (size == 0) {
                                continue;
                        }
-                       bool mounted = false;
                        optional<string> vendor;
                        try {
                                vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
@@ -300,45 +270,69 @@ get_drives ()
                                model = dcp::file_to_string("/sys/block/" + name + "/device/model");
                                boost::trim(*model);
                        } catch (...) {}
-                       BOOST_FOREACH (string j, mounted_devices) {
-                               if (boost::algorithm::starts_with(j, "/dev/" + name)) {
-                                       mounted = true;
+                       vector<boost::filesystem::path> mount_points;
+                       for (auto const& j: mounted_devices) {
+                               if (boost::algorithm::starts_with(j.first, "/dev/" + name)) {
+                                       mount_points.push_back (j.second);
                                }
                        }
-                       drives.push_back(Drive("/dev/" + i->path().filename().string(), size, mounted, vendor, model));
-                       LOG_DISK("Block device %1 size %2 %3 vendor %4 model %5", name, size, mounted ? "mounted" : "not mounted", vendor.get_value_or("[none]"), model.get_value_or("[none]"));
+                       drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
+                       LOG_DISK_NC(drives.back().log_summary());
                }
        }
 
        return drives;
 }
 
-void
-unprivileged ()
-{
-       uid_t ruid, euid, suid;
-       if (getresuid(&ruid, &euid, &suid) == -1) {
-               cerr << "getresuid() failed.\n";
-               exit (EXIT_FAILURE);
-       }
-       seteuid (ruid);
-}
 
-PrivilegeEscalator::~PrivilegeEscalator ()
+bool
+Drive::unmount ()
 {
-       unprivileged ();
+       for (auto i: _mount_points) {
+               int const r = umount(i.string().c_str());
+               LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
+               if (r == -1) {
+                       return false;
+               }
+       }
+       return true;
 }
 
-PrivilegeEscalator::PrivilegeEscalator ()
-{
-       seteuid (0);
-}
 
 boost::filesystem::path
-config_path ()
+config_path (optional<string> version)
 {
        boost::filesystem::path p;
        p /= g_get_user_config_dir ();
        p /= "dcpomatic2";
+       if (version) {
+               p /= *version;
+       }
        return p;
 }
+
+
+void
+disk_write_finished ()
+{
+
+}
+
+bool
+show_in_file_manager (boost::filesystem::path dir, boost::filesystem::path)
+{
+       int r = system ("which nautilus");
+       if (WEXITSTATUS(r) == 0) {
+               r = system (String::compose("nautilus \"%1\"", dir.string()).c_str());
+               return static_cast<bool>(WEXITSTATUS(r));
+       } else {
+               int r = system ("which konqueror");
+               if (WEXITSTATUS(r) == 0) {
+                       r = system (String::compose("konqueror \"%1\"", dir.string()).c_str());
+                       return static_cast<bool>(WEXITSTATUS(r));
+               }
+       }
+
+       return true;
+}
+