Add a missing noncopyable.
[dcpomatic.git] / src / lib / cross_linux.cc
index 8c81c50081470353fd508aeb2fdc75c237a1a6f7..36683a0cd1cac9551a74935b32c4895b906ea99d 100644 (file)
@@ -24,6 +24,7 @@
 #include "dcpomatic_log.h"
 #include "config.h"
 #include "exceptions.h"
+#include "dcpomatic_log.h"
 #include <dcp/raw_convert.h>
 #include <glib.h>
 extern "C" {
@@ -31,12 +32,14 @@ extern "C" {
 }
 #include <boost/algorithm/string.hpp>
 #include <boost/foreach.hpp>
+#include <boost/function.hpp>
 #ifdef DCPOMATIC_DISK
 #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>
@@ -56,6 +59,7 @@ using std::cout;
 using std::runtime_error;
 using boost::shared_ptr;
 using boost::optional;
+using boost::function;
 
 /** @param s Number of seconds to sleep for */
 void
@@ -256,25 +260,37 @@ running_32_on_64 ()
        return false;
 }
 
-vector<Drive>
-get_drives ()
+
+static
+vector<pair<string, string> >
+get_mounts (string prefix)
 {
-       vector<Drive> drives;
+       vector<pair<string, string> > mounts;
 
-       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]);
+               if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
+                       mounts.push_back(make_pair(bits[0], bits[1]));
+                       LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
                }
        }
 
+       return mounts;
+}
+
+
+vector<Drive>
+Drive::get ()
+{
+       vector<Drive> drives;
+
+       using namespace boost::filesystem;
+       vector<pair<string, string> > mounted_devices = get_mounts("/dev/");
+
        for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
                string const name = i->path().filename().string();
                path device_type_file("/sys/block/" + name + "/device/type");
@@ -289,7 +305,6 @@ get_drives ()
                        if (size == 0) {
                                continue;
                        }
-                       bool mounted = false;
                        optional<string> vendor;
                        try {
                                vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
@@ -300,19 +315,35 @@ 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 (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);
                                }
                        }
-                       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;
 }
 
+
+bool
+Drive::unmount ()
+{
+       BOOST_FOREACH (boost::filesystem::path 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;
+}
+
+
 void
 unprivileged ()
 {
@@ -342,3 +373,11 @@ config_path ()
        p /= "dcpomatic2";
        return p;
 }
+
+
+void
+disk_write_finished ()
+{
+
+}
+