Supporters update.
[dcpomatic.git] / src / lib / cross_linux.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "compose.hpp"
23 #include "config.h"
24 #include "cross.h"
25 #include "dcpomatic_log.h"
26 #include "dcpomatic_log.h"
27 #include "exceptions.h"
28 #include "log.h"
29 #include <dcp/filesystem.h>
30 #include <dcp/raw_convert.h>
31 #include <dcp/warnings.h>
32 #include <glib.h>
33 #include <boost/algorithm/string.hpp>
34 #if BOOST_VERSION >= 106100
35 #include <boost/dll/runtime_symbol_info.hpp>
36 #endif
37 #include <unistd.h>
38 #include <mntent.h>
39 #include <sys/types.h>
40 #include <sys/mount.h>
41 #include <ifaddrs.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <fstream>
45
46 #include "i18n.h"
47
48
49 using std::cerr;
50 using std::cout;
51 using std::ifstream;
52 using std::list;
53 using std::make_pair;
54 using std::pair;
55 using std::string;
56 using std::vector;
57 using boost::optional;
58
59
60 /** @return A string of CPU information (model name etc.) */
61 string
62 cpu_info ()
63 {
64         string info;
65
66         /* This use of ifstream is ok; the filename can never
67            be non-Latin
68         */
69         ifstream f ("/proc/cpuinfo");
70         while (f.good ()) {
71                 string l;
72                 getline (f, l);
73                 if (boost::algorithm::starts_with (l, "model name")) {
74                         string::size_type const c = l.find (':');
75                         if (c != string::npos) {
76                                 info = l.substr (c + 2);
77                         }
78                 }
79         }
80
81         return info;
82 }
83
84
85 boost::filesystem::path
86 resources_path ()
87 {
88         return directory_containing_executable().parent_path() / "share" / "dcpomatic2";
89 }
90
91
92 boost::filesystem::path
93 libdcp_resources_path ()
94 {
95         if (auto appdir = getenv("APPDIR")) {
96                 return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp";
97         }
98         return dcp::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp";
99 }
100
101
102 void
103 run_ffprobe(boost::filesystem::path content, boost::filesystem::path out, bool err, string args)
104 {
105         string const redirect = err ? "2>" : ">";
106         auto const ffprobe = String::compose("ffprobe %1 \"%2\" %3 \"%4\"", args.empty() ? " " : args, content.string(), redirect, out.string());
107         LOG_GENERAL (N_("Probing with %1"), ffprobe);
108         int const r = system (ffprobe.c_str());
109         if (r == -1 || (WIFEXITED(r) && WEXITSTATUS(r) != 0)) {
110                 LOG_GENERAL (N_("Could not run ffprobe (system returned %1"), r);
111         }
112 }
113
114
115 list<pair<string, string>>
116 mount_info ()
117 {
118         list<pair<string, string>> m;
119
120         auto f = setmntent ("/etc/mtab", "r");
121         if (!f) {
122                 return m;
123         }
124
125         while (true) {
126                 struct mntent* mnt = getmntent (f);
127                 if (!mnt) {
128                         break;
129                 }
130
131                 m.push_back (make_pair (mnt->mnt_dir, mnt->mnt_type));
132         }
133
134         endmntent (f);
135
136         return m;
137 }
138
139
140 boost::filesystem::path
141 directory_containing_executable ()
142 {
143 #if BOOST_VERSION >= 106100
144         return boost::dll::program_location().parent_path();
145 #else
146         char buffer[PATH_MAX];
147         ssize_t N = readlink ("/proc/self/exe", buffer, PATH_MAX);
148         return boost::filesystem::path(string(buffer, N)).parent_path();
149 #endif
150 }
151
152
153 boost::filesystem::path
154 openssl_path ()
155 {
156         auto p = directory_containing_executable() / "dcpomatic2_openssl";
157         if (dcp::filesystem::is_regular_file(p)) {
158                 return p;
159         }
160
161         return "dcpomatic2_openssl";
162 }
163
164
165 #ifdef DCPOMATIC_DISK
166 boost::filesystem::path
167 disk_writer_path ()
168 {
169         return directory_containing_executable() / "dcpomatic2_disk_writer";
170 }
171 #endif
172
173
174 void
175 Waker::nudge ()
176 {
177
178 }
179
180
181 Waker::Waker ()
182 {
183
184 }
185
186
187 Waker::~Waker ()
188 {
189
190 }
191
192
193 void
194 start_tool (string executable)
195 {
196         auto batch = directory_containing_executable() / executable;
197
198         pid_t pid = fork ();
199         if (pid == 0) {
200                 int const r = system (batch.string().c_str());
201                 exit (WEXITSTATUS (r));
202         }
203 }
204
205
206 void
207 start_batch_converter ()
208 {
209         start_tool ("dcpomatic2_batch");
210 }
211
212
213 void
214 start_player ()
215 {
216         start_tool ("dcpomatic2_player");
217 }
218
219
220 static
221 vector<pair<string, string>>
222 get_mounts (string prefix)
223 {
224         vector<pair<string, string>> mounts;
225
226         std::ifstream f("/proc/mounts");
227         string line;
228         while (f.good()) {
229                 getline(f, line);
230                 vector<string> bits;
231                 boost::algorithm::split (bits, line, boost::is_any_of(" "));
232                 if (bits.size() > 1 && boost::algorithm::starts_with(bits[0], prefix)) {
233                         boost::algorithm::replace_all (bits[1], "\\040", " ");
234                         mounts.push_back(make_pair(bits[0], bits[1]));
235                         LOG_DISK("Found mounted device %1 from prefix %2", bits[0], prefix);
236                 }
237         }
238
239         return mounts;
240 }
241
242
243 vector<Drive>
244 Drive::get ()
245 {
246         vector<Drive> drives;
247
248         using namespace boost::filesystem;
249         auto mounted_devices = get_mounts("/dev/");
250
251         for (auto i: directory_iterator("/sys/block")) {
252                 string const name = i.path().filename().string();
253                 path device_type_file("/sys/block/" + name + "/device/type");
254                 optional<string> device_type;
255                 if (exists(device_type_file)) {
256                         device_type = dcp::file_to_string (device_type_file);
257                         boost::trim(*device_type);
258                 }
259                 /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
260                 if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
261                         uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(i / "size")) * 512;
262                         if (size == 0) {
263                                 continue;
264                         }
265                         optional<string> vendor;
266                         try {
267                                 vendor = dcp::file_to_string("/sys/block/" + name + "/device/vendor");
268                                 boost::trim(*vendor);
269                         } catch (...) {}
270                         optional<string> model;
271                         try {
272                                 model = dcp::file_to_string("/sys/block/" + name + "/device/model");
273                                 boost::trim(*model);
274                         } catch (...) {}
275                         vector<boost::filesystem::path> mount_points;
276                         for (auto const& j: mounted_devices) {
277                                 if (boost::algorithm::starts_with(j.first, "/dev/" + name)) {
278                                         mount_points.push_back (j.second);
279                                 }
280                         }
281                         drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
282                         LOG_DISK_NC(drives.back().log_summary());
283                 }
284         }
285
286         return drives;
287 }
288
289
290 bool
291 Drive::unmount ()
292 {
293         for (auto i: _mount_points) {
294                 int const r = umount(i.string().c_str());
295                 LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
296                 if (r == -1) {
297                         return false;
298                 }
299         }
300         return true;
301 }
302
303
304 boost::filesystem::path
305 config_path (optional<string> version)
306 {
307         boost::filesystem::path p;
308         p /= g_get_user_config_dir ();
309         p /= "dcpomatic2";
310         if (version) {
311                 p /= *version;
312         }
313         return p;
314 }
315
316
317 void
318 disk_write_finished ()
319 {
320
321 }
322
323 bool
324 show_in_file_manager (boost::filesystem::path dir, boost::filesystem::path)
325 {
326         int r = system ("which nautilus");
327         if (WEXITSTATUS(r) == 0) {
328                 r = system (String::compose("nautilus \"%1\"", dir.string()).c_str());
329                 return static_cast<bool>(WEXITSTATUS(r));
330         } else {
331                 int r = system ("which konqueror");
332                 if (WEXITSTATUS(r) == 0) {
333                         r = system (String::compose("konqueror \"%1\"", dir.string()).c_str());
334                         return static_cast<bool>(WEXITSTATUS(r));
335                 }
336         }
337
338         return true;
339 }
340