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"
29 #include <dcp/raw_convert.h>
30 #include <dcp/warnings.h>
32 LIBDCP_DISABLE_WARNINGS
34 #include <libavformat/avio.h>
36 LIBDCP_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>
61 using boost::optional;
64 /** @param s Number of seconds to sleep for */
66 dcpomatic_sleep_seconds (int s)
73 dcpomatic_sleep_milliseconds (int ms)
79 /** @return A string of CPU information (model name etc.) */
85 /* This use of ifstream is ok; the filename can never
88 ifstream f ("/proc/cpuinfo");
92 if (boost::algorithm::starts_with (l, "model name")) {
93 string::size_type const c = l.find (':');
94 if (c != string::npos) {
95 info = l.substr (c + 2);
104 boost::filesystem::path
107 return directory_containing_executable().parent_path() / "share" / "dcpomatic2";
111 boost::filesystem::path
112 libdcp_resources_path ()
114 if (auto appdir = getenv("APPDIR")) {
115 return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp";
117 return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp";
122 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
124 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
125 LOG_GENERAL (N_("Probing with %1"), ffprobe);
126 int const r = system (ffprobe.c_str());
127 if (r == -1 || (WIFEXITED(r) && WEXITSTATUS(r) != 0)) {
128 LOG_GENERAL (N_("Could not run ffprobe (system returned %1"), r);
133 list<pair<string, string>>
136 list<pair<string, string>> m;
138 auto f = setmntent ("/etc/mtab", "r");
144 struct mntent* mnt = getmntent (f);
149 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
158 boost::filesystem::path
159 directory_containing_executable ()
161 #if BOOST_VERSION >= 106100
162 return boost::dll::program_location().parent_path();
164 char buffer[PATH_MAX];
165 ssize_t N = readlink ("/proc/self/exe", buffer, PATH_MAX);
166 return boost::filesystem::path(string(buffer, N)).parent_path();
171 boost::filesystem::path
174 auto p = directory_containing_executable() / "dcpomatic2_openssl";
175 if (boost::filesystem::is_regular_file(p)) {
179 return "dcpomatic2_openssl";
183 #ifdef DCPOMATIC_DISK
184 boost::filesystem::path
187 return directory_containing_executable() / "dcpomatic2_disk_writer";
212 start_tool (string executable)
214 auto batch = directory_containing_executable() / executable;
218 int const r = system (batch.string().c_str());
219 exit (WEXITSTATUS (r));
225 start_batch_converter ()
227 start_tool ("dcpomatic2_batch");
234 start_tool ("dcpomatic2_player");
241 return (uint64_t) pthread_self ();
246 avio_open_boost (AVIOContext** s, boost::filesystem::path file, int flags)
248 return avio_open (s, file.c_str(), flags);
252 boost::filesystem::path
255 return getenv("HOME");
259 /** @return true if this process is a 32-bit one running on a 64-bit-capable OS */
263 /* I'm assuming nobody does this on Linux */
269 vector<pair<string, string>>
270 get_mounts (string prefix)
272 vector<pair<string, string>> mounts;
274 std::ifstream f("/proc/mounts");
279 boost::algorithm::split (bits, line, boost::is_any_of(" "));
280 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
281 boost::algorithm::replace_all (bits[1], "\\040", " ");
282 mounts.push_back(make_pair(bits[0], bits[1]));
283 LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
294 vector<Drive> drives;
296 using namespace boost::filesystem;
297 auto mounted_devices = get_mounts("/dev/");
299 for (auto i: directory_iterator("/sys/block")) {
300 string const name = i.path().filename().string();
301 path device_type_file("/sys/block/" + name + "/device/type");
302 optional<string> device_type;
303 if (exists(device_type_file)) {
304 device_type = dcp::file_to_string (device_type_file);
305 boost::trim(*device_type);
307 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
308 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
309 uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(i / "size")) * 512;
313 optional<string> vendor;
315 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
316 boost::trim(*vendor);
318 optional<string> model;
320 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
323 vector<boost::filesystem::path> mount_points;
324 for (auto const& j: mounted_devices) {
325 if (boost::algorithm::starts_with(j.first, "/dev/" + name)) {
326 mount_points.push_back (j.second);
329 drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
330 LOG_DISK_NC(drives.back().log_summary());
341 for (auto i: _mount_points) {
342 int const r = umount(i.string().c_str());
343 LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
352 boost::filesystem::path
353 config_path (optional<string> version)
355 boost::filesystem::path p;
356 p /= g_get_user_config_dir ();
366 disk_write_finished ()
373 dcpomatic::get_process_id ()
375 return dcp::raw_convert<string>(getpid());
380 show_in_file_manager (boost::filesystem::path dir, boost::filesystem::path)
382 int r = system ("which nautilus");
383 if (WEXITSTATUS(r) == 0) {
384 r = system (String::compose("nautilus \"%1\"", dir.string()).c_str());
385 return static_cast<bool>(WEXITSTATUS(r));
387 int r = system ("which konqueror");
388 if (WEXITSTATUS(r) == 0) {
389 r = system (String::compose("konqueror \"%1\"", dir.string()).c_str());
390 return static_cast<bool>(WEXITSTATUS(r));