1 | /* Copyright © 2008-2009 Jan-Marek Glogowski <glogow@fbihome.de> |
---|
2 | * Copyright © 2009 Roger Leigh <rleigh@debian.org> |
---|
3 | * |
---|
4 | * schroot is free software: you can redistribute it and/or modify it |
---|
5 | * under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * schroot is distributed in the hope that it will be useful, but |
---|
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | * General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see |
---|
16 | * <http://www.gnu.org/licenses/>. |
---|
17 | * |
---|
18 | *********************************************************************/ |
---|
19 | |
---|
20 | #include <config.h> |
---|
21 | |
---|
22 | #include "sbuild-chroot.h" |
---|
23 | #include "sbuild-chroot-facet-union.h" |
---|
24 | #include "sbuild-chroot-facet-source-clonable.h" |
---|
25 | |
---|
26 | #include <cassert> |
---|
27 | |
---|
28 | using boost::format; |
---|
29 | using std::endl; |
---|
30 | using namespace sbuild; |
---|
31 | |
---|
32 | namespace |
---|
33 | { |
---|
34 | typedef std::pair<chroot_facet_union::error_code,const char *> emap; |
---|
35 | |
---|
36 | /** |
---|
37 | * This is a list of the supported error codes. It's used to |
---|
38 | * construct the real error codes map. |
---|
39 | */ |
---|
40 | emap init_errors[] = |
---|
41 | { |
---|
42 | // TRANSLATORS: %1% = chroot fs type |
---|
43 | emap(chroot_facet_union::UNION_TYPE_UNKNOWN, N_("Unknown filesystem union type '%1%'")), |
---|
44 | emap(chroot_facet_union::UNION_OVERLAY_ABS, N_("Union overlay must have an absolute path")), |
---|
45 | emap(chroot_facet_union::UNION_UNDERLAY_ABS, N_("Union underlay must have an absolute path")) |
---|
46 | }; |
---|
47 | } |
---|
48 | |
---|
49 | template<> |
---|
50 | error<chroot_facet_union::error_code>::map_type |
---|
51 | error<chroot_facet_union::error_code>::error_strings |
---|
52 | (init_errors, |
---|
53 | init_errors + (sizeof(init_errors) / sizeof(init_errors[0]))); |
---|
54 | |
---|
55 | chroot_facet_union::chroot_facet_union (): |
---|
56 | chroot_facet(), |
---|
57 | union_type("none"), |
---|
58 | union_overlay_directory(SCHROOT_OVERLAY_DIR), |
---|
59 | union_underlay_directory(SCHROOT_UNDERLAY_DIR) |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | chroot_facet_union::~chroot_facet_union () |
---|
64 | { |
---|
65 | } |
---|
66 | |
---|
67 | chroot_facet_union::ptr |
---|
68 | chroot_facet_union::create () |
---|
69 | { |
---|
70 | return ptr(new chroot_facet_union()); |
---|
71 | } |
---|
72 | |
---|
73 | chroot_facet::ptr |
---|
74 | chroot_facet_union::clone () const |
---|
75 | { |
---|
76 | return ptr(new chroot_facet_union(*this)); |
---|
77 | } |
---|
78 | |
---|
79 | std::string const& |
---|
80 | chroot_facet_union::get_name () const |
---|
81 | { |
---|
82 | static const std::string name("union"); |
---|
83 | |
---|
84 | return name; |
---|
85 | } |
---|
86 | |
---|
87 | void |
---|
88 | chroot_facet_union::clone_source_setup (chroot::ptr& clone) const |
---|
89 | { |
---|
90 | const chroot *base = dynamic_cast<const chroot *>(this->owner); |
---|
91 | assert(base); |
---|
92 | |
---|
93 | chroot_facet_source_clonable::const_ptr psrc |
---|
94 | (base->get_facet<chroot_facet_source_clonable>()); |
---|
95 | if (psrc) |
---|
96 | psrc->clone_source_setup(clone); |
---|
97 | |
---|
98 | chroot_facet_union::ptr puni |
---|
99 | (clone->get_facet<sbuild::chroot_facet_union>()); |
---|
100 | if (puni) |
---|
101 | puni->set_union_type("none"); |
---|
102 | } |
---|
103 | |
---|
104 | bool |
---|
105 | chroot_facet_union::get_union_configured () const |
---|
106 | { |
---|
107 | return get_union_type() != "none"; |
---|
108 | } |
---|
109 | |
---|
110 | std::string const& |
---|
111 | chroot_facet_union::get_union_overlay_directory () const |
---|
112 | { |
---|
113 | return this->union_overlay_directory; |
---|
114 | } |
---|
115 | |
---|
116 | void |
---|
117 | chroot_facet_union::set_union_overlay_directory |
---|
118 | (std::string const& directory) |
---|
119 | { |
---|
120 | if (!is_absname(directory)) |
---|
121 | throw error(directory, UNION_OVERLAY_ABS); |
---|
122 | |
---|
123 | this->union_overlay_directory = directory; |
---|
124 | } |
---|
125 | |
---|
126 | std::string const& |
---|
127 | chroot_facet_union::get_union_underlay_directory () const |
---|
128 | { |
---|
129 | return this->union_underlay_directory; |
---|
130 | } |
---|
131 | |
---|
132 | void |
---|
133 | chroot_facet_union::set_union_underlay_directory |
---|
134 | (std::string const& directory) |
---|
135 | { |
---|
136 | if (!is_absname(directory)) |
---|
137 | throw error(directory, UNION_UNDERLAY_ABS); |
---|
138 | |
---|
139 | this->union_underlay_directory = directory; |
---|
140 | } |
---|
141 | |
---|
142 | std::string const& |
---|
143 | chroot_facet_union::get_union_type () const |
---|
144 | { |
---|
145 | return this->union_type; |
---|
146 | } |
---|
147 | |
---|
148 | void |
---|
149 | chroot_facet_union::set_union_type (std::string const& type) |
---|
150 | { |
---|
151 | if (type == "aufs" || |
---|
152 | type == "unionfs" || |
---|
153 | type == "none") |
---|
154 | this->union_type = type; |
---|
155 | else |
---|
156 | throw error(type, UNION_TYPE_UNKNOWN); |
---|
157 | |
---|
158 | chroot *base = dynamic_cast<chroot *>(this->owner); |
---|
159 | assert(base); |
---|
160 | |
---|
161 | if (this->union_type != "none") |
---|
162 | { |
---|
163 | if (!base->get_facet<chroot_facet_source_clonable>()) |
---|
164 | base->add_facet(chroot_facet_source_clonable::create()); |
---|
165 | } |
---|
166 | else |
---|
167 | base->remove_facet<chroot_facet_source_clonable>(); |
---|
168 | } |
---|
169 | |
---|
170 | std::string const& |
---|
171 | chroot_facet_union::get_union_mount_options () const |
---|
172 | { |
---|
173 | return union_mount_options; |
---|
174 | } |
---|
175 | |
---|
176 | void |
---|
177 | chroot_facet_union::set_union_mount_options |
---|
178 | (std::string const& union_mount_options) |
---|
179 | { |
---|
180 | this->union_mount_options = union_mount_options; |
---|
181 | } |
---|
182 | |
---|
183 | void |
---|
184 | chroot_facet_union::setup_env (chroot const& chroot, |
---|
185 | environment& env) const |
---|
186 | { |
---|
187 | env.add("CHROOT_UNION_TYPE", get_union_type()); |
---|
188 | if (get_union_configured()) |
---|
189 | { |
---|
190 | env.add("CHROOT_UNION_MOUNT_OPTIONS", |
---|
191 | get_union_mount_options()); |
---|
192 | env.add("CHROOT_UNION_OVERLAY_DIRECTORY", |
---|
193 | get_union_overlay_directory()); |
---|
194 | env.add("CHROOT_UNION_UNDERLAY_DIRECTORY", |
---|
195 | get_union_underlay_directory()); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | sbuild::chroot::session_flags |
---|
200 | chroot_facet_union::get_session_flags (chroot const& chroot) const |
---|
201 | { |
---|
202 | sbuild::chroot::session_flags flags = sbuild::chroot::SESSION_NOFLAGS; |
---|
203 | |
---|
204 | if (get_union_configured() && chroot.get_active()) |
---|
205 | flags = sbuild::chroot::SESSION_PURGE; |
---|
206 | |
---|
207 | return flags; |
---|
208 | } |
---|
209 | |
---|
210 | void |
---|
211 | chroot_facet_union::get_details (chroot const& chroot, |
---|
212 | format_detail& detail) const |
---|
213 | { |
---|
214 | detail.add(_("Filesystem union type"), get_union_type()); |
---|
215 | if (get_union_configured()) |
---|
216 | { |
---|
217 | if (!this->union_mount_options.empty()) |
---|
218 | detail.add(_("Filesystem union mount options"), |
---|
219 | get_union_mount_options()); |
---|
220 | if (!this->union_overlay_directory.empty()) |
---|
221 | detail.add(_("Filesystem union overlay directory"), |
---|
222 | get_union_overlay_directory()); |
---|
223 | if (!this->union_underlay_directory.empty()) |
---|
224 | detail.add(_("Filesystem union underlay directory"), |
---|
225 | get_union_underlay_directory()); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | void |
---|
230 | chroot_facet_union::get_keyfile (chroot const& chroot, |
---|
231 | keyfile& keyfile) const |
---|
232 | { |
---|
233 | keyfile::set_object_value(*this, &chroot_facet_union::get_union_type, |
---|
234 | keyfile, chroot.get_keyfile_name(), "union-type"); |
---|
235 | |
---|
236 | if (get_union_configured()) |
---|
237 | { |
---|
238 | keyfile::set_object_value(*this, |
---|
239 | &chroot_facet_union::get_union_mount_options, |
---|
240 | keyfile, chroot.get_keyfile_name(), |
---|
241 | "union-mount-options"); |
---|
242 | |
---|
243 | keyfile::set_object_value(*this, |
---|
244 | &chroot_facet_union::get_union_overlay_directory, |
---|
245 | keyfile, chroot.get_keyfile_name(), |
---|
246 | "union-overlay-directory"); |
---|
247 | |
---|
248 | keyfile::set_object_value(*this, |
---|
249 | &chroot_facet_union::get_union_underlay_directory, |
---|
250 | keyfile, chroot.get_keyfile_name(), |
---|
251 | "union-underlay-directory"); |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | void |
---|
256 | chroot_facet_union::set_keyfile (chroot& chroot, |
---|
257 | keyfile const& keyfile, |
---|
258 | string_list& used_keys) |
---|
259 | { |
---|
260 | keyfile::get_object_value(*this, &chroot_facet_union::set_union_type, |
---|
261 | keyfile, chroot.get_keyfile_name(), "union-type", |
---|
262 | keyfile::PRIORITY_OPTIONAL); |
---|
263 | used_keys.push_back("union-type"); |
---|
264 | |
---|
265 | // If we are a union, add specific source options here. |
---|
266 | chroot_facet_source_clonable::ptr psrc |
---|
267 | (chroot.get_facet<chroot_facet_source_clonable>()); |
---|
268 | if (psrc) |
---|
269 | psrc->set_keyfile(chroot, keyfile, used_keys); |
---|
270 | |
---|
271 | keyfile::get_object_value(*this, |
---|
272 | &chroot_facet_union::set_union_mount_options, |
---|
273 | keyfile, chroot.get_keyfile_name(), "union-mount-options", |
---|
274 | keyfile::PRIORITY_OPTIONAL); |
---|
275 | used_keys.push_back("union-mount-options"); |
---|
276 | |
---|
277 | keyfile::get_object_value(*this, |
---|
278 | &chroot_facet_union::set_union_overlay_directory, |
---|
279 | keyfile, chroot.get_keyfile_name(), |
---|
280 | "union-overlay-directory", |
---|
281 | (chroot.get_active() && get_union_configured())? |
---|
282 | keyfile::PRIORITY_REQUIRED : |
---|
283 | keyfile::PRIORITY_OPTIONAL); |
---|
284 | used_keys.push_back("union-overlay-directory"); |
---|
285 | |
---|
286 | keyfile::get_object_value(*this, |
---|
287 | &chroot_facet_union::set_union_underlay_directory, |
---|
288 | keyfile, chroot.get_keyfile_name(), |
---|
289 | "union-underlay-directory", |
---|
290 | (chroot.get_active() && get_union_configured())? |
---|
291 | keyfile::PRIORITY_REQUIRED : |
---|
292 | keyfile::PRIORITY_OPTIONAL); |
---|
293 | used_keys.push_back("union-underlay-directory"); |
---|
294 | } |
---|