250db3cd5a5ded8b0f98a3eb7799b214e2931d9a
[dcpomatic.git] / src / lib / cross_common.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 "cross.h"
23 #include "compose.hpp"
24 #include "dcpomatic_log.h"
25 #include "warnings.h"
26 #include <dcp/raw_convert.h>
27 DCPOMATIC_DISABLE_WARNINGS
28 #include <libxml++/libxml++.h>
29 DCPOMATIC_ENABLE_WARNINGS
30 #include <boost/algorithm/string.hpp>
31 #include <iostream>
32
33 #include "i18n.h"
34
35
36 using std::map;
37 using std::string;
38 using std::vector;
39 using boost::optional;
40
41
42 Drive::Drive (string xml)
43 {
44         cxml::Document doc;
45         doc.read_string (xml);
46         _device = doc.string_child("Device");
47         for (auto i: doc.node_children("MountPoint")) {
48                 _mount_points.push_back (i->content());
49         }
50         _size = doc.number_child<uint64_t>("Size");
51         _vendor = doc.optional_string_child("Vendor");
52         _model = doc.optional_string_child("Model");
53 }
54
55
56 string
57 Drive::as_xml () const
58 {
59         xmlpp::Document doc;
60         auto root = doc.create_root_node ("Drive");
61         root->add_child("Device")->add_child_text(_device);
62         for (auto i: _mount_points) {
63                 root->add_child("MountPoint")->add_child_text(i.string());
64         }
65         root->add_child("Size")->add_child_text(dcp::raw_convert<string>(_size));
66         if (_vendor) {
67                 root->add_child("Vendor")->add_child_text(*_vendor);
68         }
69         if (_model) {
70                 root->add_child("Model")->add_child_text(*_model);
71         }
72
73         return doc.write_to_string("UTF-8");
74 }
75
76
77 string
78 Drive::description () const
79 {
80         char gb[64];
81         snprintf(gb, 64, "%.1f", _size / 1000000000.0);
82
83         string name;
84         if (_vendor) {
85                 name += *_vendor;
86         }
87         if (_model) {
88                 if (name.size() > 0) {
89                         name += " " + *_model;
90                 } else {
91                         name = *_model;
92                 }
93         }
94         if (name.size() == 0) {
95                 name = _("Unknown");
96         }
97
98         return String::compose(_("%1 (%2 GB) [%3]"), name, gb, _device);
99 }
100
101
102 string
103 Drive::log_summary () const
104 {
105         string mp;
106         for (auto i: _mount_points) {
107                 mp += i.string() + ",";
108         }
109         if (mp.empty()) {
110                 mp = "[none]";
111         } else {
112                 mp = mp.substr (0, mp.length() - 1);
113         }
114
115         return String::compose(
116                 "Device %1 mounted on %2 size %3 vendor %4 model %5",
117                 _device, mp, _size, _vendor.get_value_or("[none]"), _model.get_value_or("[none]")
118                         );
119 }
120
121
122
123 /* This is in _common so we can use it in unit tests */
124 optional<OSXMediaPath>
125 analyse_osx_media_path (string path)
126 {
127         using namespace boost::algorithm;
128
129         if (path.find("/IOHDIXController") != string::npos) {
130                 /* This is a disk image, so we completely ignore it */
131                 LOG_DISK_NC("Ignoring this as it seems to be a disk image");
132                 return {};
133         }
134
135         OSXMediaPath mp;
136         if (starts_with(path, "IODeviceTree:")) {
137                 mp.real = true;
138         } else if (starts_with(path, "IOService:")) {
139                 mp.real = false;
140         } else {
141                 return {};
142         }
143
144         vector<string> bits;
145         split(bits, path, boost::is_any_of("/"));
146         for (auto i: bits) {
147                 if (starts_with(i, "PRT")) {
148                         mp.prt = i;
149                 }
150         }
151
152         return mp;
153 }
154
155
156 /* This is in _common so we can use it in unit tests */
157 vector<Drive>
158 osx_disks_to_drives (vector<OSXDisk> disks)
159 {
160         using namespace boost::algorithm;
161
162         /* Mark disks containing mounted partitions as themselves mounted */
163         for (auto& i: disks) {
164                 if (!i.whole) {
165                         continue;
166                 }
167                 for (auto& j: disks) {
168                         if (!j.mount_points.empty() && starts_with(j.mount_point, i.mount_point)) {
169                                 LOG_DISK("Marking %1 as mounted because %2 is", i.mount_point, j.mount_point);
170                                 std::copy(j.mount_points.begin(), j.mount_points.end(), back_inserter(i.mount_points));
171                         }
172                 }
173         }
174
175         /* Make a map of the PRT codes and mount points of mounted, synthesized disks */
176         map<string, vector<boost::filesystem::path>> mounted_synths;
177         for (auto const& i: disks) {
178                 if (!i.real && !i.mount_points.empty()) {
179                         LOG_DISK("Found a mounted synth %1 with %2", i.mount_point, i.prt);
180                         mounted_synths[i.prt] = i.mount_points;
181                 }
182         }
183
184         /* Mark containers of those mounted synths as themselves mounted */
185         for (auto& i: disks) {
186                 if (i.real) {
187                         auto j = mounted_synths.find(i.prt);
188                         if (j != mounted_synths.end()) {
189                                 LOG_DISK("Marking %1 (%2) as mounted because it contains a mounted synth", i.mount_point, i.prt);
190                                 std::copy(j->second.begin(), j->second.end(), back_inserter(i.mount_points));
191                         }
192                 }
193         }
194
195         vector<Drive> drives;
196         for (auto const& i: disks) {
197                 if (i.whole) {
198                         /* A whole disk that is not a container for a mounted synth */
199                         drives.push_back(Drive(i.mount_point, i.mount_points, i.size, i.vendor, i.model));
200                         LOG_DISK_NC(drives.back().log_summary());
201                 }
202         }
203
204         return drives;
205 }