2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
23 #include "compose.hpp"
25 #include "dcpomatic_log.h"
27 #include "exceptions.h"
28 #include <dcp/filesystem.h>
29 #include <dcp/raw_convert.h>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/regex.hpp>
33 #if BOOST_VERSION >= 106100
34 #include <boost/dll/runtime_symbol_info.hpp>
36 #include <ApplicationServices/ApplicationServices.h>
37 #include <sys/sysctl.h>
38 #include <mach-o/dyld.h>
39 #include <IOKit/pwr_mgt/IOPMLib.h>
40 #include <IOKit/storage/IOMedia.h>
41 #include <DiskArbitration/DADisk.h>
42 #include <DiskArbitration/DiskArbitration.h>
43 #include <CoreFoundation/CFURL.h>
44 #include <sys/types.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
62 using std::runtime_error;
64 using std::shared_ptr;
65 using boost::optional;
69 /** @return A string of CPU information (model name etc.) */
76 size_t N = sizeof (buffer);
77 if (sysctlbyname("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
85 boost::filesystem::path
86 directory_containing_executable ()
88 return dcp::filesystem::canonical(boost::dll::program_location()).parent_path();
92 boost::filesystem::path
95 return directory_containing_executable().parent_path() / "Resources";
99 boost::filesystem::path
100 libdcp_resources_path ()
102 return resources_path();
107 run_ffprobe(boost::filesystem::path content, boost::filesystem::path out, bool err, string args)
109 auto path = directory_containing_executable () / "ffprobe";
110 if (!dcp::filesystem::exists(path)) {
111 /* This is a hack but we need ffprobe during tests */
112 path = "/Users/ci/workspace/bin/ffprobe";
114 string const redirect = err ? "2>" : ">";
116 auto const ffprobe = String::compose("\"%1\" %2 \"%3\" %4 \"%5\"", path, args.empty() ? " " : args, content.string(), redirect, out.string());
117 LOG_GENERAL (N_("Probing with %1"), ffprobe);
118 system (ffprobe.c_str());
123 list<pair<string, string>>
130 boost::filesystem::path
133 return directory_containing_executable() / "openssl";
137 #ifdef DCPOMATIC_DISK
138 /* Note: this isn't actually used at the moment as the disk writer is started as a service */
139 boost::filesystem::path
142 return directory_containing_executable() / "dcpomatic2_disk_writer";
156 boost::mutex::scoped_lock lm (_mutex);
157 IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
163 boost::mutex::scoped_lock lm (_mutex);
164 IOPMAssertionRelease (_assertion_id);
169 start_tool (string executable, string app)
171 auto exe_path = directory_containing_executable();
172 exe_path = exe_path.parent_path(); // Contents
173 exe_path = exe_path.parent_path(); // DCP-o-matic 2.app
174 exe_path = exe_path.parent_path(); // Applications
176 exe_path /= "Contents";
178 exe_path /= executable;
182 LOG_GENERAL ("start_tool %1 %2 with path %3", executable, app, exe_path.string());
183 int const r = system (exe_path.string().c_str());
184 exit (WEXITSTATUS (r));
185 } else if (pid == -1) {
186 LOG_ERROR_NC("Fork failed in start_tool");
192 start_batch_converter ()
194 start_tool ("dcpomatic2_batch", "DCP-o-matic\\ 2\\ Batch\\ Converter.app");
201 start_tool ("dcpomatic2_player", "DCP-o-matic\\ 2\\ Player.app");
205 static optional<string>
206 get_vendor (CFDictionaryRef& description)
208 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceVendorKey);
213 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
219 boost::algorithm::trim (s);
224 static optional<string>
225 get_model (CFDictionaryRef& description)
227 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceModelKey);
232 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
238 boost::algorithm::trim (s);
243 static optional<OSXMediaPath>
244 analyse_media_path (CFDictionaryRef& description)
246 using namespace boost::algorithm;
248 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionMediaPathKey);
250 LOG_DISK_NC("There is no MediaPathKey (no dictionary value)");
254 auto path_key_cstr = CFStringGetCStringPtr((CFStringRef) str, kCFStringEncodingUTF8);
255 if (!path_key_cstr) {
256 LOG_DISK_NC("There is no MediaPathKey (no cstring)");
260 string path(path_key_cstr);
261 LOG_DISK("MediaPathKey is %1", path);
262 return analyse_osx_media_path (path);
267 is_whole_drive (DADiskRef& disk)
269 io_service_t service = DADiskCopyIOMedia (disk);
270 CFTypeRef whole_media_ref = IORegistryEntryCreateCFProperty (service, CFSTR(kIOMediaWholeKey), kCFAllocatorDefault, 0);
271 bool whole_media = false;
272 if (whole_media_ref) {
273 whole_media = CFBooleanGetValue((CFBooleanRef) whole_media_ref);
274 CFRelease (whole_media_ref);
276 IOObjectRelease (service);
281 static optional<boost::filesystem::path>
282 mount_point (CFDictionaryRef& description)
284 auto volume_path_key = (CFURLRef) CFDictionaryGetValue (description, kDADiskDescriptionVolumePathKey);
285 if (!volume_path_key) {
289 char mount_path_buffer[1024];
290 if (!CFURLGetFileSystemRepresentation(volume_path_key, false, (UInt8 *) mount_path_buffer, sizeof(mount_path_buffer))) {
293 return boost::filesystem::path(mount_path_buffer);
297 /* Here follows some rather intricate and (probably) fragile code to find the list of available
298 * "real" drives on macOS that we might want to write a DCP to.
300 * We use the Disk Arbitration framework to give us a series of mount_points (/dev/disk0, /dev/disk1,
301 * /dev/disk1s1 and so on) and we use the API to gather useful information about these mount_points into
302 * a vector of Disk structs.
304 * Then we read the Disks that we found and try to derive a list of drives that we should offer to the
305 * user, with details of whether those drives are currently mounted or not.
307 * At the basic level we find the "disk"-level mount_points, looking at whether any of their partitions are mounted.
309 * This is complicated enormously by recent-ish macOS versions' habit of making `synthesized' volumes which
310 * reflect data in `real' partitions. So, for example, we might have a real (physical) drive /dev/disk2 with
311 * a partition /dev/disk2s2 whose content is made into a synthesized /dev/disk3, itself containing some partitions
312 * which are mounted. /dev/disk2s2 is not considered to be mounted, in this case. So we need to know that
313 * disk2s2 is related to disk3 so we can consider disk2s2 as mounted if any parts of disk3 are. In order to do
314 * this I am taking the first two parts of the IODeviceTree and seeing if they exist anywhere in a
315 * IOService identifier. If they do, I am assuming the IOService device is on the matching IODeviceTree device.
317 * Lots of this is guesswork and may be broken. In my defence the documentation that I have been able to
318 * unearth is, to put it impolitely, crap.
322 disk_appeared (DADiskRef disk, void* context)
324 auto bsd_name = DADiskGetBSDName (disk);
326 LOG_DISK_NC("Disk with no BSDName appeared");
329 LOG_DISK("%1 appeared", bsd_name);
333 this_disk.device = string("/dev/") + bsd_name;
334 LOG_DISK("Device is %1", this_disk.device);
336 CFDictionaryRef description = DADiskCopyDescription (disk);
338 this_disk.vendor = get_vendor (description);
339 this_disk.model = get_model (description);
340 LOG_DISK("Vendor/model: %1 %2", this_disk.vendor.get_value_or("[none]"), this_disk.model.get_value_or("[none]"));
342 auto media_path = analyse_media_path (description);
344 LOG_DISK("Finding media path for %1 failed", bsd_name);
348 this_disk.media_path = *media_path;
349 this_disk.whole = is_whole_drive (disk);
350 auto mp = mount_point (description);
352 this_disk.mount_points.push_back (*mp);
356 "%1 %2 mounted at %3",
357 this_disk.media_path.real ? "Real" : "Synth",
358 this_disk.whole ? "whole" : "part",
359 mp ? mp->string() : "[nowhere]"
362 auto media_size_cstr = CFDictionaryGetValue (description, kDADiskDescriptionMediaSizeKey);
363 if (!media_size_cstr) {
364 LOG_DISK_NC("Could not read media size");
368 CFNumberGetValue ((CFNumberRef) media_size_cstr, kCFNumberLongType, &this_disk.size);
369 CFRelease (description);
371 reinterpret_cast<vector<OSXDisk>*>(context)->push_back(this_disk);
378 using namespace boost::algorithm;
379 vector<OSXDisk> disks;
381 LOG_DISK_NC("Drive::get() starts");
383 auto session = DASessionCreate(kCFAllocatorDefault);
388 LOG_DISK_NC("Drive::get() has session");
390 DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &disks);
391 auto run_loop = CFRunLoopGetCurrent ();
392 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
393 CFRunLoopStop (run_loop);
394 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
395 DAUnregisterCallback(session, (void *) disk_appeared, &disks);
398 auto drives = osx_disks_to_drives(disks);
400 LOG_DISK("Drive::get() found %1 drives:", drives.size());
401 for (auto const& drive: drives) {
402 LOG_DISK("%1 %2 mounted=%3", drive.description(), drive.device(), drive.mounted() ? "yes" : "no");
409 boost::filesystem::path
410 config_path (optional<string> version)
412 boost::filesystem::path p;
413 p /= g_get_home_dir ();
416 p /= "com.dcpomatic";
427 bool success = false;
428 bool callback = false;
432 void done_callback(DADiskRef, DADissenterRef dissenter, void* context)
434 LOG_DISK_NC("Unmount finished");
435 auto state = reinterpret_cast<UnmountState*>(context);
436 state->callback = true;
438 LOG_DISK("Error: %1", DADissenterGetStatus(dissenter));
440 LOG_DISK_NC("Successful");
441 state->success = true;
449 LOG_DISK_NC("Unmount operation started");
451 auto session = DASessionCreate(kCFAllocatorDefault);
456 auto disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, _device.c_str());
460 LOG_DISK("Requesting unmount of %1 from %2", _device, thread_id());
462 DADiskUnmount(disk, kDADiskUnmountOptionWhole, &done_callback, &state);
465 CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
466 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
467 CFRunLoopStop (run_loop);
468 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 5, 0);
471 if (!state.callback) {
472 LOG_DISK_NC("End of unmount: timeout");
474 LOG_DISK("End of unmount: %1", state.success ? "success" : "failure");
476 return state.success;
481 disk_write_finished ()
488 make_foreground_application ()
490 ProcessSerialNumber serial;
491 LIBDCP_DISABLE_WARNINGS
492 GetCurrentProcess (&serial);
493 LIBDCP_ENABLE_WARNINGS
494 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
499 show_in_file_manager (boost::filesystem::path, boost::filesystem::path select)
501 int r = system (String::compose("open -R \"%1\"", select.string()).c_str());
502 return static_cast<bool>(WEXITSTATUS(r));