Fix fopen() on windows to cope with long filenames (part of #1755).
[dcpomatic.git] / src / lib / cross_linux.cc
index e17f188b9f8aacece46cfa29eb7cd884ab3d9797..816573230adc1dabfb71f002fa83791e190b6482 100644 (file)
@@ -31,7 +31,6 @@ extern "C" {
 #include <libavformat/avio.h>
 }
 #include <boost/algorithm/string.hpp>
-#include <boost/foreach.hpp>
 #include <boost/function.hpp>
 #if BOOST_VERSION >= 106100
 #include <boost/dll/runtime_symbol_info.hpp>
@@ -57,7 +56,7 @@ using std::vector;
 using std::cerr;
 using std::cout;
 using std::runtime_error;
-using boost::shared_ptr;
+using std::shared_ptr;
 using boost::optional;
 using boost::function;
 
@@ -99,15 +98,32 @@ cpu_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
+xsd_path ()
+{
+       if (auto appdir = getenv("APPDIR")) {
+               return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp" / "xsd";
+       }
+       return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp" / "xsd";
+}
+
+
+boost::filesystem::path
+tags_path ()
+{
+       if (auto appdir = getenv("APPDIR")) {
+               return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp" / "tags";
        }
-       return boost::filesystem::canonical (LINUX_SHARE_PREFIX);
+       return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp" / "tags";
 }
 
+
 void
 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
 {
@@ -257,29 +273,6 @@ home_directory ()
        return getenv("HOME");
 }
 
-string
-command_and_read (string cmd)
-{
-       FILE* pipe = popen (cmd.c_str(), "r");
-       if (!pipe) {
-               throw runtime_error ("popen failed");
-       }
-
-       string result;
-       char buffer[128];
-       try {
-               while (fgets(buffer, sizeof(buffer), pipe)) {
-                       result += buffer;
-               }
-       } catch (...) {
-               pclose (pipe);
-               throw;
-       }
-
-       pclose (pipe);
-       return result;
-}
-
 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
 bool
 running_32_on_64 ()
@@ -318,10 +311,10 @@ Drive::get ()
        vector<Drive> drives;
 
        using namespace boost::filesystem;
-       vector<pair<string, string> > mounted_devices = get_mounts("/dev/");
+       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)) {
@@ -330,7 +323,7 @@ Drive::get ()
                }
                /* 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;
                        }
@@ -345,9 +338,9 @@ Drive::get ()
                                boost::trim(*model);
                        } catch (...) {}
                        vector<boost::filesystem::path> mount_points;
-                       for (vector<pair<string, string> >::const_iterator j = mounted_devices.begin(); j != mounted_devices.end(); ++j) {
-                               if (boost::algorithm::starts_with(j->first, "/dev/" + name)) {
-                                       mount_points.push_back (j->second);
+                       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/" + name, mount_points, size, vendor, model));
@@ -362,7 +355,8 @@ Drive::get ()
 bool
 Drive::unmount ()
 {
-       BOOST_FOREACH (boost::filesystem::path i, _mount_points) {
+       for (auto i: _mount_points) {
+               PrivilegeEscalator esc;
                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) {
@@ -385,16 +379,23 @@ unprivileged ()
        }
 }
 
+
+bool PrivilegeEscalator::test = false;
+
 PrivilegeEscalator::~PrivilegeEscalator ()
 {
-       unprivileged ();
+       if (!test) {
+               unprivileged ();
+       }
 }
 
 PrivilegeEscalator::PrivilegeEscalator ()
 {
-       int const r = seteuid(0);
-       if (r < 0) {
-               throw PrivilegeError (String::compose("seteuid() call failed with %1", errno));
+       if (!test) {
+               int const r = seteuid(0);
+               if (r < 0) {
+                       throw PrivilegeError (String::compose("seteuid() call failed with %1", errno));
+               }
        }
 }
 
@@ -414,3 +415,16 @@ disk_write_finished ()
 
 }
 
+string
+dcpomatic::get_process_id ()
+{
+       return dcp::raw_convert<string>(getpid());
+}
+
+
+boost::filesystem::path
+fix_long_path (boost::filesystem::path path)
+{
+       return path;
+}
+