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