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 #include <boost/algorithm/string.hpp>
33 #if BOOST_VERSION >= 106100
34 #include <boost/dll/runtime_symbol_info.hpp>
38 #include <sys/types.h>
39 #include <sys/mount.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
56 using boost::optional;
59 /** @return A string of CPU information (model name etc.) */
65 /* This use of ifstream is ok; the filename can never
68 ifstream f ("/proc/cpuinfo");
72 if (boost::algorithm::starts_with (l, "model name")) {
73 string::size_type const c = l.find (':');
74 if (c != string::npos) {
75 info = l.substr (c + 2);
84 boost::filesystem::path
87 return directory_containing_executable().parent_path() / "share" / "dcpomatic2";
91 boost::filesystem::path
92 libdcp_resources_path ()
94 if (auto appdir = getenv("APPDIR")) {
95 return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp";
97 return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp";
102 run_ffprobe (boost::filesystem::path content, boost::filesystem::path out)
104 string ffprobe = "ffprobe \"" + content.string() + "\" 2> \"" + out.string() + "\"";
105 LOG_GENERAL (N_("Probing with %1"), ffprobe);
106 int const r = system (ffprobe.c_str());
107 if (r == -1 || (WIFEXITED(r) && WEXITSTATUS(r) != 0)) {
108 LOG_GENERAL (N_("Could not run ffprobe (system returned %1"), r);
113 list<pair<string, string>>
116 list<pair<string, string>> m;
118 auto f = setmntent ("/etc/mtab", "r");
124 struct mntent* mnt = getmntent (f);
129 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
138 boost::filesystem::path
139 directory_containing_executable ()
141 #if BOOST_VERSION >= 106100
142 return boost::dll::program_location().parent_path();
144 char buffer[PATH_MAX];
145 ssize_t N = readlink ("/proc/self/exe", buffer, PATH_MAX);
146 return boost::filesystem::path(string(buffer, N)).parent_path();
151 boost::filesystem::path
154 auto p = directory_containing_executable() / "dcpomatic2_openssl";
155 if (boost::filesystem::is_regular_file(p)) {
159 return "dcpomatic2_openssl";
163 #ifdef DCPOMATIC_DISK
164 boost::filesystem::path
167 return directory_containing_executable() / "dcpomatic2_disk_writer";
192 start_tool (string executable)
194 auto batch = directory_containing_executable() / executable;
198 int const r = system (batch.string().c_str());
199 exit (WEXITSTATUS (r));
205 start_batch_converter ()
207 start_tool ("dcpomatic2_batch");
214 start_tool ("dcpomatic2_player");
219 vector<pair<string, string>>
220 get_mounts (string prefix)
222 vector<pair<string, string>> mounts;
224 std::ifstream f("/proc/mounts");
229 boost::algorithm::split (bits, line, boost::is_any_of(" "));
230 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
231 boost::algorithm::replace_all (bits[1], "\\040", " ");
232 mounts.push_back(make_pair(bits[0], bits[1]));
233 LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
244 vector<Drive> drives;
246 using namespace boost::filesystem;
247 auto mounted_devices = get_mounts("/dev/");
249 for (auto i: directory_iterator("/sys/block")) {
250 string const name = i.path().filename().string();
251 path device_type_file("/sys/block/" + name + "/device/type");
252 optional<string> device_type;
253 if (exists(device_type_file)) {
254 device_type = dcp::file_to_string (device_type_file);
255 boost::trim(*device_type);
257 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
258 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
259 uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(i / "size")) * 512;
263 optional<string> vendor;
265 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
266 boost::trim(*vendor);
268 optional<string> model;
270 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
273 vector<boost::filesystem::path> mount_points;
274 for (auto const& j: mounted_devices) {
275 if (boost::algorithm::starts_with(j.first, "/dev/" + name)) {
276 mount_points.push_back (j.second);
279 drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
280 LOG_DISK_NC(drives.back().log_summary());
291 for (auto i: _mount_points) {
292 int const r = umount(i.string().c_str());
293 LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
302 boost::filesystem::path
303 config_path (optional<string> version)
305 boost::filesystem::path p;
306 p /= g_get_user_config_dir ();
316 disk_write_finished ()
322 show_in_file_manager (boost::filesystem::path dir, boost::filesystem::path)
324 int r = system ("which nautilus");
325 if (WEXITSTATUS(r) == 0) {
326 r = system (String::compose("nautilus \"%1\"", dir.string()).c_str());
327 return static_cast<bool>(WEXITSTATUS(r));
329 int r = system ("which konqueror");
330 if (WEXITSTATUS(r) == 0) {
331 r = system (String::compose("konqueror \"%1\"", dir.string()).c_str());
332 return static_cast<bool>(WEXITSTATUS(r));