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/>.
22 #include "compose.hpp"
25 #include "dcpomatic_log.h"
26 #include "exceptions.h"
29 #include <dcp/filesystem.h>
30 #include <dcp/raw_convert.h>
32 #include <boost/algorithm/string.hpp>
33 #include <boost/regex.hpp>
34 #if BOOST_VERSION >= 106100
35 #include <boost/dll/runtime_symbol_info.hpp>
37 #include <ApplicationServices/ApplicationServices.h>
38 #include <sys/sysctl.h>
39 #include <mach-o/dyld.h>
40 #include <IOKit/pwr_mgt/IOPMLib.h>
41 #include <IOKit/storage/IOMedia.h>
42 #include <DiskArbitration/DADisk.h>
43 #include <DiskArbitration/DiskArbitration.h>
44 #include <CoreFoundation/CFURL.h>
45 #include <sys/types.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
63 using std::runtime_error;
65 using std::shared_ptr;
66 using boost::optional;
70 /** @return A string of CPU information (model name etc.) */
77 size_t N = sizeof (buffer);
78 if (sysctlbyname("machdep.cpu.brand_string", buffer, &N, 0, 0) == 0) {
86 boost::filesystem::path
87 directory_containing_executable ()
89 return dcp::filesystem::canonical(boost::dll::program_location()).parent_path();
93 boost::filesystem::path
96 return directory_containing_executable().parent_path() / "Resources";
100 boost::filesystem::path
101 libdcp_resources_path ()
103 return resources_path();
108 run_ffprobe(boost::filesystem::path content, boost::filesystem::path out, bool err, string args)
110 auto path = directory_containing_executable () / "ffprobe";
111 if (!dcp::filesystem::exists(path)) {
112 /* This is a hack but we need ffprobe during tests */
113 path = "/Users/ci/workspace/bin/ffprobe";
115 string const redirect = err ? "2>" : ">";
117 auto const ffprobe = String::compose("\"%1\" %2 \"%3\" %4 \"%5\"", path, args.empty() ? " " : args, content.string(), redirect, out.string());
118 LOG_GENERAL (N_("Probing with %1"), ffprobe);
119 system (ffprobe.c_str());
124 list<pair<string, string>>
131 boost::filesystem::path
134 return directory_containing_executable() / "openssl";
138 #ifdef DCPOMATIC_DISK
139 /* Note: this isn't actually used at the moment as the disk writer is started as a service */
140 boost::filesystem::path
143 return directory_containing_executable() / "dcpomatic2_disk_writer";
157 boost::mutex::scoped_lock lm (_mutex);
158 IOPMAssertionCreateWithName (kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR ("Encoding DCP"), &_assertion_id);
164 boost::mutex::scoped_lock lm (_mutex);
165 IOPMAssertionRelease (_assertion_id);
170 start_tool (string executable, string app)
172 boost::algorithm::replace_all(app, " ", "\\ ");
174 auto exe_path = directory_containing_executable();
175 exe_path = exe_path.parent_path(); // Contents
176 exe_path = exe_path.parent_path(); // DCP-o-matic 2.app
177 exe_path = exe_path.parent_path(); // Applications
179 exe_path /= "Contents";
181 exe_path /= executable;
185 LOG_GENERAL ("start_tool %1 %2 with path %3", executable, app, exe_path.string());
186 int const r = system (exe_path.string().c_str());
187 exit (WEXITSTATUS (r));
188 } else if (pid == -1) {
189 LOG_ERROR_NC("Fork failed in start_tool");
195 start_batch_converter ()
197 start_tool("dcpomatic2_batch", variant::dcpomatic_batch_converter_app());
204 start_tool("dcpomatic2_player", variant::dcpomatic_player_app());
208 static optional<string>
209 get_vendor (CFDictionaryRef& description)
211 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceVendorKey);
216 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
222 boost::algorithm::trim (s);
227 static optional<string>
228 get_model (CFDictionaryRef& description)
230 void const* str = CFDictionaryGetValue (description, kDADiskDescriptionDeviceModelKey);
235 auto c_str = CFStringGetCStringPtr ((CFStringRef) str, kCFStringEncodingUTF8);
241 boost::algorithm::trim (s);
246 static optional<boost::filesystem::path>
247 mount_point (CFDictionaryRef& description)
249 auto volume_path_key = (CFURLRef) CFDictionaryGetValue (description, kDADiskDescriptionVolumePathKey);
250 if (!volume_path_key) {
254 char mount_path_buffer[1024];
255 if (!CFURLGetFileSystemRepresentation(volume_path_key, false, (UInt8 *) mount_path_buffer, sizeof(mount_path_buffer))) {
258 return boost::filesystem::path(mount_path_buffer);
263 get_bool(CFDictionaryRef& description, void const* key)
265 auto value = CFDictionaryGetValue(description, key);
270 return CFBooleanGetValue(reinterpret_cast<CFBooleanRef>(value));
275 disk_appeared (DADiskRef disk, void* context)
277 auto bsd_name = DADiskGetBSDName (disk);
279 LOG_DISK_NC("Disk with no BSDName appeared");
282 LOG_DISK("%1 appeared", bsd_name);
286 this_disk.device = string("/dev/") + bsd_name;
287 LOG_DISK("Device is %1", this_disk.device);
289 CFDictionaryRef description = DADiskCopyDescription (disk);
291 this_disk.vendor = get_vendor (description);
292 this_disk.model = get_model (description);
293 LOG_DISK("Vendor/model: %1 %2", this_disk.vendor.get_value_or("[none]"), this_disk.model.get_value_or("[none]"));
295 auto mp = mount_point (description);
297 this_disk.mount_points.push_back (*mp);
300 auto media_size_cstr = CFDictionaryGetValue (description, kDADiskDescriptionMediaSizeKey);
301 if (!media_size_cstr) {
302 LOG_DISK_NC("Could not read media size");
306 this_disk.system = get_bool(description, kDADiskDescriptionDeviceInternalKey) && !get_bool(description, kDADiskDescriptionMediaRemovableKey);
307 this_disk.writeable = get_bool(description, kDADiskDescriptionMediaWritableKey);
308 this_disk.partition = string(bsd_name).find("s", 5) != std::string::npos;
311 "%1 %2 %3 %4 mounted at %5",
313 this_disk.system ? "system" : "non-system",
314 this_disk.writeable ? "writeable" : "read-only",
315 this_disk.partition ? "partition" : "drive",
316 mp ? mp->string() : "[nowhere]"
319 CFNumberGetValue ((CFNumberRef) media_size_cstr, kCFNumberLongType, &this_disk.size);
320 CFRelease (description);
322 reinterpret_cast<vector<OSXDisk>*>(context)->push_back(this_disk);
329 using namespace boost::algorithm;
330 vector<OSXDisk> disks;
332 LOG_DISK_NC("Drive::get() starts");
334 auto session = DASessionCreate(kCFAllocatorDefault);
339 LOG_DISK_NC("Drive::get() has session");
341 DARegisterDiskAppearedCallback (session, NULL, disk_appeared, &disks);
342 auto run_loop = CFRunLoopGetCurrent ();
343 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
344 CFRunLoopStop (run_loop);
345 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.05, 0);
346 DAUnregisterCallback(session, (void *) disk_appeared, &disks);
349 vector<Drive> drives;
350 for (auto const& disk: disks) {
351 if (!disk.system && !disk.partition && disk.writeable) {
352 drives.push_back({disk.device, disk.mount_points, disk.size, disk.vendor, disk.model});
356 LOG_DISK("Drive::get() found %1 drives:", drives.size());
357 for (auto const& drive: drives) {
358 LOG_DISK("%1 %2 mounted=%3", drive.description(), drive.device(), drive.mounted() ? "yes" : "no");
365 boost::filesystem::path
366 config_path (optional<string> version)
368 boost::filesystem::path p;
369 p /= g_get_home_dir ();
372 p /= "com.dcpomatic";
383 bool success = false;
384 bool callback = false;
388 void done_callback(DADiskRef, DADissenterRef dissenter, void* context)
390 LOG_DISK_NC("Unmount finished");
391 auto state = reinterpret_cast<UnmountState*>(context);
392 state->callback = true;
394 LOG_DISK("Error: %1", DADissenterGetStatus(dissenter));
396 LOG_DISK_NC("Successful");
397 state->success = true;
405 LOG_DISK_NC("Unmount operation started");
407 auto session = DASessionCreate(kCFAllocatorDefault);
412 auto disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, _device.c_str());
416 LOG_DISK("Requesting unmount of %1 from %2", _device, thread_id());
418 DADiskUnmount(disk, kDADiskUnmountOptionWhole, &done_callback, &state);
421 CFRunLoopRef run_loop = CFRunLoopGetCurrent ();
422 DASessionScheduleWithRunLoop (session, run_loop, kCFRunLoopDefaultMode);
423 CFRunLoopStop (run_loop);
424 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 5, 0);
427 if (!state.callback) {
428 LOG_DISK_NC("End of unmount: timeout");
430 LOG_DISK("End of unmount: %1", state.success ? "success" : "failure");
432 return state.success;
437 disk_write_finished ()
444 make_foreground_application ()
446 ProcessSerialNumber serial;
447 LIBDCP_DISABLE_WARNINGS
448 GetCurrentProcess (&serial);
449 LIBDCP_ENABLE_WARNINGS
450 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
455 show_in_file_manager (boost::filesystem::path, boost::filesystem::path select)
457 int r = system (String::compose("open -R \"%1\"", select.string()).c_str());
458 return static_cast<bool>(WEXITSTATUS(r));