summaryrefslogtreecommitdiff
path: root/lib/sbuild/chroot/facet/custom.cc
blob: 523df3dd7bc792e00c467638674184fb1221ae42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* Copyright © 2005-2013  Roger Leigh <rleigh@debian.org>
 *
 * schroot is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * schroot is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *********************************************************************/

#include <config.h>

#include <sbuild/chroot/facet/factory.h>
#include <sbuild/chroot/facet/custom.h>
#include <sbuild/chroot/facet/mountable.h>
#include <sbuild/chroot/facet/session-clonable.h>
#include <sbuild/chroot/facet/session.h>
#include <sbuild/chroot/facet/source-clonable.h>
#include <sbuild/format-detail.h>

#include <cassert>
#include <cerrno>

#include <boost/format.hpp>

using std::endl;
using boost::format;
using namespace sbuild;

namespace sbuild
{
  namespace chroot
  {
    namespace facet
    {

      namespace
      {

        factory::facet_info custom_info =
          {
            "custom",
            N_("Support for ‘custom’ chroots"),
            false,
            []() -> facet::ptr { return custom::create(); }
          };

        factory custom_register(custom_info);

      }

      custom::custom ():
        storage(),
        purgeable(false)
      {
      }

      custom::custom (const custom& rhs):
        storage(rhs),
        purgeable(rhs.purgeable)
      {
      }

      custom::~custom ()
      {
      }

      std::string const&
      custom::get_name () const
      {
        static const std::string name("custom");

        return name;
      }

      custom::ptr
      custom::create ()
      {
        return ptr(new custom());
      }

      facet::ptr
      custom::clone () const
      {
        return ptr(new custom(*this));
      }

      void
      custom::set_session_cloneable (bool cloneable)
      {
        if (cloneable)
          owner->add_facet(session_clonable::create());
        else
          owner->remove_facet<session_clonable>();
      }

      void
      custom::set_session_purgeable (bool purgeable)
      {
        this->purgeable = purgeable;
      }

      bool
      custom::get_session_purgeable () const
      {
        return this->purgeable;
      }

      void
      custom::set_source_cloneable (bool cloneable)
      {
        if (cloneable)
          owner->add_facet(source_clonable::create());
        else
          owner->remove_facet<source_clonable>();
      }

      std::string
      custom::get_path () const
      {
        // TODO: Allow customisation?  Or require use of mount location?
        return owner->get_mount_location();
      }

      void
      custom::setup_env (environment& env) const
      {
        storage::setup_env(env);
      }

      void
      custom::setup_lock (chroot::setup_type type,
                          bool               lock,
                          int                status)
      {
        /* By default, custom chroots do no locking. */
        /* Create or unlink session information. */
        if ((type == chroot::SETUP_START && lock == true) ||
            (type == chroot::SETUP_STOP && lock == false && status == 0))
          {

            bool start = (type == chroot::SETUP_START);
            owner->get_facet_strict<session>()->setup_session_info(start);
          }
      }

      facet::session_flags
      custom::get_session_flags () const
      {
        session_flags flags = SESSION_NOFLAGS;

        // TODO: Only set if purge is set.

        if (owner->get_facet<session>() &&
            get_session_purgeable())
          flags = SESSION_PURGE;

        return flags;
      }

      void
      custom::get_details (format_detail& detail) const
      {
        storage::get_details(detail);
      }

      void
      custom::get_used_keys (string_list& used_keys) const
      {
        storage::get_used_keys(used_keys);

        used_keys.push_back("custom-cloneable");
        used_keys.push_back("custom-purgeable");
        used_keys.push_back("custom-source-cloneable");
      }

      void
      custom::get_keyfile (keyfile& keyfile) const
      {
        storage::get_keyfile(keyfile);

        keyfile::set_object_value(*this,
                                  &custom::get_session_purgeable,
                                  keyfile, owner->get_name(),
                                  "custom-session-purgeable");
      }

      void
      custom::set_keyfile (keyfile const& keyfile)
      {
        storage::set_keyfile(keyfile);

        bool is_session = static_cast<bool>(owner->get_facet<session>());

        keyfile::get_object_value(*this, &custom::set_session_cloneable,
                                  keyfile, owner->get_name(), "custom-session-cloneable",
                                  is_session ?
                                  keyfile::PRIORITY_DISALLOWED :
                                  keyfile::PRIORITY_OPTIONAL);

        keyfile::get_object_value(*this, &custom::set_session_purgeable,
                                  keyfile, owner->get_name(), "custom-session-purgeable",
                                  keyfile::PRIORITY_OPTIONAL);

        keyfile::get_object_value(*this, &custom::set_source_cloneable,
                                  keyfile, owner->get_name(), "custom-source-cloneable",
                                  is_session ?
                                  keyfile::PRIORITY_DISALLOWED :
                                  keyfile::PRIORITY_OPTIONAL);
      }
    }
  }
}