BOOST_FOREACH.
[dcpomatic.git] / src / lib / cross_windows.cc
index 130086898abcd3ebb81f1932f9019ca58c8f2d21..0ab56bb6b24f9e4ca24a069402d9d06b475bb943 100644 (file)
@@ -31,18 +31,19 @@ extern "C" {
 #include <libavformat/avio.h>
 }
 #include <boost/algorithm/string.hpp>
-#include <boost/foreach.hpp>
 #include <boost/dll/runtime_symbol_info.hpp>
 #include <windows.h>
 #include <winternl.h>
 #include <winioctl.h>
 #include <ntdddisk.h>
 #include <setupapi.h>
+#include <fileapi.h>
 #undef DATADIR
 #include <shlwapi.h>
 #include <shellapi.h>
 #include <fcntl.h>
 #include <fstream>
+#include <map>
 
 #include "i18n.h"
 
@@ -56,9 +57,12 @@ using std::vector;
 using std::cerr;
 using std::cout;
 using std::runtime_error;
-using boost::shared_ptr;
+using std::map;
+using std::shared_ptr;
 using boost::optional;
 
+static std::vector<pair<HANDLE, string> > locked_volumes;
+
 /** @param s Number of seconds to sleep for */
 void
 dcpomatic_sleep_seconds (int s)
@@ -122,9 +126,7 @@ run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
        }
 
        wchar_t dir[512];
-       GetModuleFileName (GetModuleHandle (0), dir, sizeof (dir));
-       PathRemoveFileSpec (dir);
-       SetCurrentDirectory (dir);
+       MultiByteToWideChar (CP_UTF8, 0, directory_containing_executable().string().c_str(), -1, dir, sizeof(dir));
 
        STARTUPINFO startup_info;
        ZeroMemory (&startup_info, sizeof (startup_info));
@@ -143,7 +145,7 @@ run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
 
        PROCESS_INFORMATION process_info;
        ZeroMemory (&process_info, sizeof (process_info));
-       if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &startup_info, &process_info)) {
+       if (!CreateProcess (0, command, 0, 0, TRUE, CREATE_NO_WINDOW, 0, dir, &startup_info, &process_info)) {
                LOG_ERROR_NC (N_("ffprobe call failed (could not CreateProcess)"));
                return;
        }
@@ -180,29 +182,50 @@ 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
+resources_path ()
+{
+       return directory_containing_executable().parent_path();
+}
+
+
+boost::filesystem::path
+xsd_path ()
+{
+       return directory_containing_executable().parent_path() / "xsd";
+}
+
+
 boost::filesystem::path
-shared_path ()
+tags_path ()
 {
-       return executable_path().parent_path();
+       return directory_containing_executable().parent_path() / "tags";
 }
 
+
 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
@@ -239,10 +262,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));
@@ -256,18 +280,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 ()
 {
@@ -317,12 +344,6 @@ home_directory ()
        return boost::filesystem::path(getenv("HOMEDRIVE")) / boost::filesystem::path(getenv("HOMEPATH"));
 }
 
-string
-command_and_read (string)
-{
-       return "";
-}
-
 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
 bool
 running_32_on_64 ()
@@ -415,12 +436,28 @@ get_device_number (HDEVINFO device_info, SP_DEVINFO_DATA* device_info_data)
        return device_number.DeviceNumber;
 }
 
+typedef map<int, vector<boost::filesystem::path> > MountPoints;
+
 /** Take a volume path (with a trailing \) and add any disk numbers related to that volume
  *  to @ref disks.
  */
 static void
-add_volume_disk_number (wchar_t* volume, vector<int>& disks)
+add_volume_mount_points (wchar_t* volume, MountPoints& mount_points)
 {
+       LOG_DISK("Looking at %1", wchar_to_utf8(volume));
+
+       wchar_t volume_path_names[512];
+       vector<boost::filesystem::path> mp;
+       DWORD returned;
+       if (GetVolumePathNamesForVolumeNameW(volume, volume_path_names, sizeof(volume_path_names) / sizeof(wchar_t), &returned)) {
+               wchar_t* p = volume_path_names;
+               while (*p != L'\0') {
+                       mp.push_back (wchar_to_utf8(p));
+                       LOG_DISK ("Found mount point %1", wchar_to_utf8(p));
+                       p += wcslen(p) + 1;
+               }
+       }
+
        /* Strip trailing \ */
        size_t const len = wcslen (volume);
        DCPOMATIC_ASSERT (len > 0);
@@ -442,41 +479,39 @@ add_volume_disk_number (wchar_t* volume, vector<int>& disks)
                return;
        }
        DCPOMATIC_ASSERT (extents.NumberOfDiskExtents == 1);
-       return disks.push_back (extents.Extents[0].DiskNumber);
+
+       mount_points[extents.Extents[0].DiskNumber] = mp;
 }
 
-/* Return a list of disk numbers that contain volumes; i.e. a list of disk numbers that should
- * not be offered as targets to write to as they are "mounted" (whatever that means on Windows).
- */
-vector<int>
-disk_numbers_with_volumes ()
+MountPoints
+find_mount_points ()
 {
-       vector<int> disks;
+       MountPoints mount_points;
 
        wchar_t volume_name[512];
        HANDLE volume = FindFirstVolumeW (volume_name, sizeof(volume_name) / sizeof(wchar_t));
        if (volume == INVALID_HANDLE_VALUE) {
-               return disks;
+               return MountPoints();
        }
 
-       add_volume_disk_number (volume_name, disks);
+       add_volume_mount_points (volume_name, mount_points);
        while (true) {
                if (!FindNextVolumeW(volume, volume_name, sizeof(volume_name) / sizeof(wchar_t))) {
                        break;
                }
-               add_volume_disk_number (volume_name, disks);
+               add_volume_mount_points (volume_name, mount_points);
        }
        FindVolumeClose (volume);
 
-       return disks;
+       return mount_points;
 }
 
 vector<Drive>
-get_drives ()
+Drive::get ()
 {
        vector<Drive> drives;
 
-       vector<int> disks_to_ignore = disk_numbers_with_volumes ();
+       MountPoints mount_points = find_mount_points ();
 
        /* Get a `device information set' containing information about all disks */
        HDEVINFO device_info = SetupDiGetClassDevsA (&GUID_DEVICE_INTERFACE_DISK, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
@@ -525,9 +560,18 @@ get_drives ()
                                &geom, sizeof(geom), &returned, 0
                                );
 
-               if (r && find(disks_to_ignore.begin(), disks_to_ignore.end(), *device_number) == disks_to_ignore.end()) {
+               LOG_DISK("Having a look through %1 locked volumes", locked_volumes.size());
+               bool locked = false;
+               for (vector<pair<HANDLE, string> >::const_iterator i = locked_volumes.begin(); i != locked_volumes.end(); ++i) {
+                       if (i->second == physical_drive) {
+                               locked = true;
+                       }
+               }
+
+               if (r) {
                        uint64_t const disk_size = geom.Cylinders.QuadPart * geom.TracksPerCylinder * geom.SectorsPerTrack * geom.BytesPerSector;
-                       drives.push_back (Drive(physical_drive, disk_size, false, friendly_name, optional<string>()));
+                       drives.push_back (Drive(physical_drive, locked ? vector<boost::filesystem::path>() : mount_points[*device_number], disk_size, friendly_name, optional<string>()));
+                       LOG_DISK("Added drive %1%2", drives.back().log_summary(), locked ? "(locked by us)" : "");
                }
 
                CloseHandle (device);
@@ -536,6 +580,36 @@ get_drives ()
        return drives;
 }
 
+
+bool
+Drive::unmount ()
+{
+       LOG_DISK("Unmounting %1 with %2 mount points", _device, _mount_points.size());
+       DCPOMATIC_ASSERT (_mount_points.size() == 1);
+       string const device_name = String::compose ("\\\\.\\%1", _mount_points.front());
+       string const truncated = device_name.substr (0, device_name.length() - 1);
+       //LOG_DISK("Actually opening %1", _device);
+       //HANDLE device = CreateFileA (_device.c_str(), (GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
+       LOG_DISK("Actually opening %1", truncated);
+       HANDLE device = CreateFileA (truncated.c_str(), (GENERIC_READ | GENERIC_WRITE), FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
+       if (device == INVALID_HANDLE_VALUE) {
+               LOG_DISK("Could not open %1 for unmount (%2)", truncated, GetLastError());
+               return false;
+       }
+       DWORD returned;
+       BOOL r = DeviceIoControl (device, FSCTL_LOCK_VOLUME, 0, 0, 0, 0, &returned, 0);
+       if (!r) {
+               LOG_DISK("Unmount of %1 failed (%2)", truncated, GetLastError());
+               return false;
+       }
+
+       LOG_DISK("Unmount of %1 succeeded", _device);
+       locked_volumes.push_back (make_pair(device, _device));
+
+       return true;
+}
+
+
 boost::filesystem::path
 config_path ()
 {
@@ -544,3 +618,19 @@ config_path ()
        p /= "dcpomatic2";
        return p;
 }
+
+void
+disk_write_finished ()
+{
+       for (vector<pair<HANDLE, string> >::const_iterator i = locked_volumes.begin(); i != locked_volumes.end(); ++i) {
+               CloseHandle (i->first);
+       }
+}
+
+
+string
+dcpomatic::get_process_id ()
+{
+       return dcp::raw_convert<string>(GetCurrentProcessId());
+}
+