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<boost::filesystem::path>
244 mount_point (CFDictionaryRef& description)
246 auto volume_path_key = (CFURLRef) CFDictionaryGetValue (description, kDADiskDescriptionVolumePathKey);
247 if (!volume_path_key) {
251 char mount_path_buffer[1024];
252 if (!CFURLGetFileSystemRepresentation(volume_path_key, false, (UInt8 *) mount_path_buffer, sizeof(mount_path_buffer))) {
255 return boost::filesystem::path(mount_path_buffer);
260 get_bool(CFDictionaryRef& description, void const* key)
262 auto value = CFDictionaryGetValue(description, key);
267 return CFBooleanGetValue(reinterpret_cast<CFBooleanRef>(value));
272 disk_appeared (DADiskRef disk, void* context)
274 auto bsd_name = DADiskGetBSDName (disk);
276 LOG_DISK_NC("Disk with no BSDName appeared");
279 LOG_DISK("%1 appeared", bsd_name);
283 this_disk.device = string("/dev/") + bsd_name;
284 LOG_DISK("Device is %1", this_disk.device);
286 CFDictionaryRef description = DADiskCopyDescription (disk);
288 this_disk.vendor = get_vendor (description);
289 this_disk.model = get_model (description);
290 LOG_DISK("Vendor/model: %1 %2", this_disk.vendor.get_value_or("[none]"), this_disk.model.get_value_or("[none]"));
292 auto mp = mount_point (description);
294 this_disk.mount_points.push_back (*mp);
297 auto media_size_cstr = CFDictionaryGetValue (description, kDADiskDescriptionMediaSizeKey);
298 if (!media_size_cstr) {
299 LOG_DISK_NC("Could not read media size");
303 this_disk.system = get_bool(description, kDADiskDescriptionDeviceInternalKey) && !get_bool(description, kDADiskDescriptionMediaRemovableKey);
304 this_disk.writeable = get_bool(description, kDADiskDescriptionMediaWritableKey);
305 this_disk.partition = string(bsd_name).find("s", 5) != std::string::npos;
308 "%1 %2 %3 %4 mounted at %5",
310 this_disk.system ? "system" : "non-system",
311 this_disk.writeable ? "writeable" : "read-only",
312 this_disk.partition ? "partition" : "drive",
313 mp ? mp->string() : "[nowhere]"
316 CFNumberGetValue ((CFNumberRef) media_size_cstr, kCFNumberLongType, &this_disk.size);
317 CFRelease (description);
319 reinterpret_cast<vector<OSXDisk>*>(context)->push_back(this_disk);
326 using namespace boost::algorithm;
327 vector<OSXDisk> disks;
329 LOG_DISK_NC("Drive::get() starts");
331 auto session = DASessionCreate(kCFAllocatorDefault);
336 LOG_DISK_NC("Drive::get() has session");
338 DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &disks);
339 auto run_loop = CFRunLoopGetCurrent ();
340 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
341 CFRunLoopStop (run_loop);
342 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
343 DAUnregisterCallback(session, (void *) disk_appeared, &disks);
346 vector<Drive> drives;
347 for (auto const& disk: disks) {
348 if (!disk.system && !disk.partition && disk.writeable) {
349 drives.push_back({disk.device, disk.mount_points, disk.size, disk.vendor, disk.model});
353 LOG_DISK("Drive::get() found %1 drives:", drives.size());
354 for (auto const& drive: drives) {
355 LOG_DISK("%1 %2 mounted=%3", drive.description(), drive.device(), drive.mounted() ? "yes" : "no");
362 boost::filesystem::path
363 config_path (optional<string> version)
365 boost::filesystem::path p;
366 p /= g_get_home_dir ();
369 p /= "com.dcpomatic";
380 bool success = false;
381 bool callback = false;
385 void done_callback(DADiskRef, DADissenterRef dissenter, void* context)
387 LOG_DISK_NC("Unmount finished");
388 auto state = reinterpret_cast<UnmountState*>(context);
389 state->callback = true;
391 LOG_DISK("Error: %1", DADissenterGetStatus(dissenter));
393 LOG_DISK_NC("Successful");
394 state->success = true;
402 LOG_DISK_NC("Unmount operation started");
404 auto session = DASessionCreate(kCFAllocatorDefault);
409 auto disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, _device.c_str());
413 LOG_DISK("Requesting unmount of %1 from %2", _device, thread_id());
415 DADiskUnmount(disk, kDADiskUnmountOptionWhole, &done_callback, &state);
418 CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
419 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
420 CFRunLoopStop (run_loop);
421 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 5, 0);
424 if (!state.callback) {
425 LOG_DISK_NC("End of unmount: timeout");
427 LOG_DISK("End of unmount: %1", state.success ? "success" : "failure");
429 return state.success;
434 disk_write_finished ()
441 make_foreground_application ()
443 ProcessSerialNumber serial;
444 LIBDCP_DISABLE_WARNINGS
445 GetCurrentProcess (&serial);
446 LIBDCP_ENABLE_WARNINGS
447 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
452 show_in_file_manager (boost::filesystem::path, boost::filesystem::path select)
454 int r = system (String::compose("open -R \"%1\"", select.string()).c_str());
455 return static_cast<bool>(WEXITSTATUS(r));