summaryrefslogtreecommitdiff
path: root/lib/sbuild/chroot/facet/facet.h
blob: d5c25b8bc4134579ee4f23713d25eb5b785615ab (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
/* 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/>.
 *
 *********************************************************************/

#ifndef SBUILD_CHROOT_FACET_FACET_H
#define SBUILD_CHROOT_FACET_FACET_H

#include <sbuild/environment.h>
#include <sbuild/format-detail.h>
#include <sbuild/keyfile.h>
#include <sbuild/types.h>
#include <sbuild/chroot/chroot.h>

#include <string>

namespace sbuild
{
  namespace chroot
  {
    class chroot;

    /**
     * Facets of functionality and state of individual chroots
     */
    namespace facet
    {

      /**
       * Base class for all facets.  This class provides the basic
       * interfaces and properties required for all facets.  All
       * facets must be derived from this type.  Note that this class
       * can not be used independently of a chroot, and most methods
       * can not be called until the instance is installed in a chroot
       * instance using chroot::add_facet() or related methods.
       */
      class facet
      {
      public:
        /// Chroot session properties.
        enum session_flags
          {
            SESSION_NOFLAGS = 0,      ///< No flags are set.
            SESSION_CREATE  = 1 << 0, ///< The chroot supports session creation.
            SESSION_CLONE   = 1 << 1, ///< The chroot supports cloning.
            SESSION_PURGE   = 1 << 2  ///< The chroot should be purged.
          };

        /// A shared_ptr to a chroot facet object.
        typedef std::shared_ptr<facet> ptr;

        /// A shared_ptr to a const chroot facet object.
        typedef std::shared_ptr<const facet> const_ptr;

      protected:
        /// The constructor.
        facet ();

        /**
         * Set containing chroot.  The copy parameter is used to
         * inform the facet if it is new or a copy; this is intended
         * to allow additional facets to only be added if the facet is
         * new, for example, to avoid re-adding removed facets when
         * copying a chroot.
         *
         * @param chroot the chroot containing this facet.
         * @param copy true if the facet has been copied, or false if
         * this is a new instance.
         */
        virtual void
        set_chroot (chroot& chroot,
                    bool    copy = false);

        friend class ::sbuild::chroot::chroot;

      public:
        /// The destructor.
        virtual ~facet ();

        /**
         * Copy the chroot facet.  This is a virtual copy constructor.
         *
         * @returns a shared_ptr to the new copy of the chroot facet.
         */
        virtual ptr
        clone () const = 0;

        /**
         * Get the name of the chroot facet.
         *
         * @returns the chroot facet name.
         */
        virtual std::string const&
        get_name () const = 0;

        /**
         * Set environment.  Set the environment that the setup scripts
         * will see during execution.
         *
         * @param env the environment to set.
         */
        virtual void
        setup_env (environment& env) const;

        /**
         * Get the session flags of the chroot.  These determine how the
         * Session controlling the chroot will operate.
         *
         * @param chroot the chroot to use.
         * @returns the session flags.
         */
        virtual session_flags
        get_session_flags () const;

        /**
         * Get detailed information about the chroot for output.
         *
         * @param detail the details to output to.
         */
        virtual void
        get_details (format_detail& detail) const;

        /**
         * Get a list of the keys used during keyfile parsing.
         *
         * @returns a list of key names.
         */
        virtual void
        get_used_keys (string_list& used_keys) const;

        /**
         * Copy the chroot properties into a keyfile.  The keyfile group
         * with the name of the chroot will be set; if it already exists,
         * it will be removed before setting it.
         *
         * @param keyfile the keyfile to use.
         */
        virtual void
        get_keyfile (keyfile& keyfile) const;

        /**
         * Set the chroot properties from a keyfile.  The chroot name must
         * have previously been set, so that the correct keyfile group may
         * be determined.
         *
         * @param keyfile the keyfile to get the properties from.
         */
        virtual void
        set_keyfile (const keyfile& keyfile);

      protected:
        /// Chroot owning this facet.
        chroot *owner;
      };

      /**
       * Bitwise-OR of specifed session properties
       * @param lhs session properties
       * @param rhs session properties
       * @returns result of OR.
       */
      facet::session_flags
      inline operator | (const facet::session_flags& lhs,
                         const facet::session_flags& rhs)
      {
        return static_cast<facet::session_flags>
          (static_cast<int>(lhs) | static_cast<int>(rhs));
      }

      /**
       * Bitwise-AND of specifed session properties
       * @param lhs session properties
       * @param rhs session properties
       * @returns result of AND.
       */
      facet::session_flags
      inline operator & (const facet::session_flags& lhs,
                         const facet::session_flags& rhs)
      {
        return static_cast<facet::session_flags>
          (static_cast<int>(lhs) & static_cast<int>(rhs));
      }

    }
  }
}

#endif /* SBUILD_CHROOT_FACET_FACET_H */

/*
 * Local Variables:
 * mode:C++
 * End:
 */