source: trunk/debathena/third/schroot/sbuild/sbuild-chroot-facet-session-clonable.cc @ 24167

Revision 24167, 5.4 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
RevLine 
[24167]1/* Copyright © 2005-2009  Roger Leigh <rleigh@debian.org>
2 *
3 * schroot is free software: you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * schroot is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 *********************************************************************/
18
19#include <config.h>
20
21#include "sbuild-chroot.h"
22#include "sbuild-chroot-facet-session.h"
23#include "sbuild-chroot-facet-session-clonable.h"
24#include "sbuild-chroot-facet-source-clonable.h"
25#include "sbuild-chroot-plain.h"
26#ifdef SBUILD_FEATURE_LVMSNAP
27#include "sbuild-chroot-lvm-snapshot.h"
28#endif // SBUILD_FEATURE_LVMSNAP
29#ifdef SBUILD_FEATURE_UNION
30#include "sbuild-chroot-facet-union.h"
31#endif // SBUILD_FEATURE_UNION
32#include "sbuild-format-detail.h"
33
34#include <cassert>
35
36#include <boost/format.hpp>
37
38using boost::format;
39using std::endl;
40using namespace sbuild;
41
42chroot_facet_session_clonable::chroot_facet_session_clonable ():
43  chroot_facet()
44{
45}
46
47chroot_facet_session_clonable::~chroot_facet_session_clonable ()
48{
49}
50
51chroot_facet_session_clonable::ptr
52chroot_facet_session_clonable::create ()
53{
54  return ptr(new chroot_facet_session_clonable());
55}
56
57chroot_facet::ptr
58chroot_facet_session_clonable::clone () const
59{
60  return ptr(new chroot_facet_session_clonable(*this));
61}
62
63std::string const&
64chroot_facet_session_clonable::get_name () const
65{
66  static const std::string name("session");
67
68  return name;
69}
70
71void
72chroot_facet_session_clonable::clone_session_setup (chroot::ptr&       clone,
73                                                    std::string const& session_id,
74                                                    std::string const& user,
75                                                    bool               root) const
76{
77  clone->remove_facet<chroot_facet_session_clonable>();
78  clone->add_facet(chroot_facet_session::create());
79
80  // Disable session, delete aliases.
81  chroot_facet_session::ptr session(clone->get_facet<chroot_facet_session>());
82  assert(session);
83  if (session)
84    {
85      clone->set_session_id(session_id);
86      assert(clone->get_session_id() == session_id);
87      clone->set_description
88        (clone->get_description() + ' ' + _("(session chroot)"));
89
90      string_list empty_list;
91      string_list allowed_users;
92      if (!user.empty())
93        allowed_users.push_back(user);
94
95      if (root)
96        {
97          clone->set_users(empty_list);
98          clone->set_root_users(allowed_users);
99        }
100      else
101        {
102          clone->set_users(allowed_users);
103          clone->set_root_users(empty_list);
104        }
105      clone->set_groups(empty_list);
106      clone->set_root_groups(empty_list);
107
108      session->get_session_flags(*clone); // For testing.
109    }
110
111  log_debug(DEBUG_INFO)
112    << format("Cloned session %1%")
113    % clone->get_name() << endl;
114
115  // Disable source cloning.
116  clone->remove_facet<chroot_facet_source_clonable>();
117
118  /* If a chroot mount location has not yet been set, and the
119     chroot is not a plain chroot, set a mount location with the
120     session id.  Only set for non-plain chroots which run
121     setup scripts. */
122  {
123    std::tr1::shared_ptr<chroot_plain> plain(std::tr1::dynamic_pointer_cast<chroot_plain>(clone));
124
125    if (clone->get_mount_location().empty() && !plain)
126      {
127        log_debug(DEBUG_NOTICE) << "Setting mount location" << endl;
128        std::string location(std::string(SCHROOT_MOUNT_DIR) + "/" +
129                             session_id);
130        clone->set_mount_location(location);
131      }
132  }
133
134  log_debug(DEBUG_NOTICE)
135    << format("Mount Location: %1%") % clone->get_mount_location()
136    << endl;
137
138#ifdef SBUILD_FEATURE_LVMSNAP
139  /* LVM devices need the snapshot device name specifying. */
140  std::tr1::shared_ptr<chroot_lvm_snapshot> snapshot(std::tr1::dynamic_pointer_cast<chroot_lvm_snapshot>(clone));
141  if (snapshot && !snapshot->get_device().empty())
142    {
143      std::string device(dirname(snapshot->get_device(), '/'));
144      device += "/" + clone->get_session_id();
145      snapshot->set_snapshot_device(device);
146    }
147#endif // SBUILD_FEATURE_LVMSNAP
148
149#ifdef SBUILD_FEATURE_UNION
150  /* Filesystem unions need the overlay directory specifying. */
151  chroot_facet_union::ptr puni(clone->get_facet<chroot_facet_union>());
152
153  if (puni)
154    {
155      std::string overlay = puni->get_union_overlay_directory();
156      overlay += "/" + clone->get_session_id();
157      puni->set_union_overlay_directory(overlay);
158
159      std::string underlay = puni->get_union_underlay_directory();
160      underlay += "/" + clone->get_session_id();
161      puni->set_union_underlay_directory(underlay);
162    }
163#endif // SBUILD_FEATURE_UNION
164}
165
166void
167chroot_facet_session_clonable::setup_env (chroot const& chroot,
168                                          environment&  env) const
169{
170}
171
172chroot::session_flags
173chroot_facet_session_clonable::get_session_flags (chroot const& chroot) const
174{
175  return chroot::SESSION_CREATE;
176}
177
178void
179chroot_facet_session_clonable::get_details (chroot const&  chroot,
180                                            format_detail& detail) const
181{
182}
183
184void
185chroot_facet_session_clonable::get_keyfile (chroot const& chroot,
186                                            keyfile&      keyfile) const
187{
188}
189
190void
191chroot_facet_session_clonable::set_keyfile (chroot&        chroot,
192                                            keyfile const& keyfile,
193                                            string_list&   used_keys)
194{
195}
Note: See TracBrowser for help on using the repository browser.