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 "dcpomatic_log.h"
27 #include "exceptions.h"
30 #include <dcp/raw_convert.h>
32 DCPOMATIC_DISABLE_WARNINGS
34 #include <libavformat/avio.h>
36 DCPOMATIC_ENABLE_WARNINGS
37 #include <boost/algorithm/string.hpp>
38 #if BOOST_VERSION >= 106100
39 #include <boost/dll/runtime_symbol_info.hpp>
43 #include <sys/types.h>
44 #include <sys/mount.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
62 using boost::optional;
65 /** @param s Number of seconds to sleep for */
67 dcpomatic_sleep_seconds (int s)
74 dcpomatic_sleep_milliseconds (int ms)
80 /** @return A string of CPU information (model name etc.) */
86 /* This use of ifstream is ok; the filename can never
89 ifstream f ("/proc/cpuinfo");
93 if (boost::algorithm::starts_with (l, "model name")) {
94 string::size_type const c = l.find (':');
95 if (c != string::npos) {
96 info = l.substr (c + 2);
105 boost::filesystem::path
108 return directory_containing_executable().parent_path() / "share" / "dcpomatic2";
112 boost::filesystem::path
115 if (auto appdir = getenv("APPDIR")) {
116 return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp" / "xsd";
118 return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp" / "xsd";
122 boost::filesystem::path
125 if (auto appdir = getenv("APPDIR")) {
126 return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp" / "tags";
128 return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp" / "tags";
133 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
135 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
136 LOG_GENERAL (N_("Probing with %1"), ffprobe);
137 int const r = system (ffprobe.c_str());
138 if (r == -1 || (WIFEXITED(r) && WEXITSTATUS(r) != 0)) {
139 LOG_GENERAL (N_("Could not run ffprobe (system returned %1"), r);
144 list<pair<string, string>>
147 list<pair<string, string>> m;
149 auto f = setmntent ("/etc/mtab", "r");
155 struct mntent* mnt = getmntent (f);
160 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
169 boost::filesystem::path
170 directory_containing_executable ()
172 #if BOOST_VERSION >= 106100
173 return boost::dll::program_location().parent_path();
175 char buffer[PATH_MAX];
176 ssize_t N = readlink ("/proc/self/exe", buffer, PATH_MAX);
177 return boost::filesystem::path(string(buffer, N)).parent_path();
182 boost::filesystem::path
185 auto p = directory_containing_executable() / "dcpomatic2_openssl";
186 if (boost::filesystem::is_regular_file(p)) {
190 return "dcpomatic2_openssl";
194 #ifdef DCPOMATIC_DISK
195 boost::filesystem::path
198 return directory_containing_executable() / "dcpomatic2_disk_writer";
203 /* Apparently there is no way to create an ofstream using a UTF-8
204 filename under Windows. We are hence reduced to using fopen
208 fopen_boost (boost::filesystem::path p, string t)
210 return fopen(p.c_str(), t.c_str());
215 dcpomatic_fseek (FILE* stream, int64_t offset, int whence)
217 return fseek (stream, offset, whence);
241 start_tool (string executable)
243 auto batch = directory_containing_executable() / executable;
247 int const r = system (batch.string().c_str());
248 exit (WEXITSTATUS (r));
254 start_batch_converter ()
256 start_tool ("dcpomatic2_batch");
263 start_tool ("dcpomatic2_player");
270 return (uint64_t) pthread_self ();
275 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
277 return avio_open (s, file.c_str(), flags);
281 boost::filesystem::path
284 return getenv("HOME");
288 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
292 /* I'm assuming nobody does this on Linux */
298 vector<pair<string, string>>
299 get_mounts (string prefix)
301 vector<pair<string, string>> mounts;
303 std::ifstream f("/proc/mounts");
308 boost::algorithm::split (bits, line, boost::is_any_of(" "));
309 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
310 boost::algorithm::replace_all (bits[1], "\\040", " ");
311 mounts.push_back(make_pair(bits[0], bits[1]));
312 LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
323 vector<Drive> drives;
325 using namespace boost::filesystem;
326 auto mounted_devices = get_mounts("/dev/");
328 for (auto i: directory_iterator("/sys/block")) {
329 string const name = i.path().filename().string();
330 path device_type_file("/sys/block/" + name + "/device/type");
331 optional<string> device_type;
332 if (exists(device_type_file)) {
333 device_type = dcp::file_to_string (device_type_file);
334 boost::trim(*device_type);
336 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
337 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
338 uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(i / "size")) * 512;
342 optional<string> vendor;
344 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
345 boost::trim(*vendor);
347 optional<string> model;
349 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
352 vector<boost::filesystem::path> mount_points;
353 for (auto const& j: mounted_devices) {
354 if (boost::algorithm::starts_with(j.first, "/dev/" + name)) {
355 mount_points.push_back (j.second);
358 drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
359 LOG_DISK_NC(drives.back().log_summary());
370 for (auto i: _mount_points) {
371 int const r = umount(i.string().c_str());
372 LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
381 boost::filesystem::path
382 config_path (optional<string> version)
384 boost::filesystem::path p;
385 p /= g_get_user_config_dir ();
395 disk_write_finished ()
402 dcpomatic::get_process_id ()
404 return dcp::raw_convert<string>(getpid());
408 boost::filesystem::path
409 fix_long_path (boost::filesystem::path path)
416 show_in_file_manager (boost::filesystem::path dir, boost::filesystem::path)
418 int r = system ("which nautilus");
419 if (WEXITSTATUS(r) == 0) {
420 r = system (String::compose("nautilus \"%1\"", dir.string()).c_str());
421 return static_cast<bool>(WEXITSTATUS(r));
423 int r = system ("which konqueror");
424 if (WEXITSTATUS(r) == 0) {
425 r = system (String::compose("konqueror \"%1\"", dir.string()).c_str());
426 return static_cast<bool>(WEXITSTATUS(r));