source: trunk/debathena/third/schroot/sbuild/sbuild-chroot-file.cc @ 24167

Revision 24167, 5.2 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
1/* Copyright © 2005-2008  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-file.h"
22#include "sbuild-chroot-facet-session-clonable.h"
23#include "sbuild-chroot-facet-source-clonable.h"
24#include "sbuild-format-detail.h"
25#include "sbuild-lock.h"
26
27#include <cassert>
28#include <cerrno>
29#include <cstring>
30
31#include <boost/format.hpp>
32
33using boost::format;
34using namespace sbuild;
35
36chroot_file::chroot_file ():
37  chroot(),
38  file(),
39  repack(false)
40{
41  add_facet(sbuild::chroot_facet_source_clonable::create());
42}
43
44chroot_file::chroot_file (const chroot_file& rhs):
45  chroot(rhs),
46  file(rhs.file),
47  repack(rhs.repack)
48{
49}
50
51chroot_file::~chroot_file ()
52{
53}
54
55sbuild::chroot::ptr
56chroot_file::clone () const
57{
58  return ptr(new chroot_file(*this));
59}
60
61sbuild::chroot::ptr
62chroot_file::clone_session (std::string const& session_id,
63                            std::string const& user,
64                            bool               root) const
65{
66  chroot_facet_session_clonable::const_ptr psess
67    (get_facet<chroot_facet_session_clonable>());
68  assert(psess);
69
70  ptr session(new chroot_file(*this));
71  psess->clone_session_setup(session, session_id, user, root);
72
73  return session;
74}
75
76sbuild::chroot::ptr
77chroot_file::clone_source () const
78{
79  chroot_file *clone_file = new chroot_file(*this);
80  ptr clone(clone_file);
81
82  chroot_facet_source_clonable::const_ptr psrc
83    (get_facet<chroot_facet_source_clonable>());
84  assert(psrc);
85
86  psrc->clone_source_setup(clone);
87  clone_file->repack = true;
88
89  return clone;
90}
91
92std::string const&
93chroot_file::get_file () const
94{
95  return this->file;
96}
97
98void
99chroot_file::set_file (std::string const& file)
100{
101  if (!is_absname(file))
102    throw error(file, FILE_ABS);
103
104  this->file = file;
105}
106
107bool
108chroot_file::get_file_repack () const
109{
110  return this->repack;
111}
112
113void
114chroot_file::set_file_repack (bool repack)
115{
116  this->repack = repack;
117}
118
119std::string
120chroot_file::get_path () const
121{
122  return get_mount_location();
123}
124
125std::string const&
126chroot_file::get_chroot_type () const
127{
128  static const std::string type("file");
129
130  return type;
131}
132
133void
134chroot_file::setup_env (chroot const& chroot,
135                        environment&  env) const
136{
137  chroot::setup_env(chroot, env);
138
139  env.add("CHROOT_FILE", get_file());
140  env.add("CHROOT_FILE_REPACK", this->repack);
141  env.add("CHROOT_FILE_UNPACK_DIR", SCHROOT_FILE_UNPACK_DIR);
142}
143
144void
145chroot_file::setup_lock (chroot::setup_type type,
146                         bool               lock,
147                         int                status)
148{
149  // Check ownership and permissions.
150  if (type == SETUP_START && lock == true)
151    {
152      stat file_status(this->file);
153
154      // NOTE: taken from chroot_config::check_security.
155      if (file_status.uid() != 0)
156        throw error(this->file, FILE_OWNER);
157      if (file_status.check_mode(stat::PERM_OTHER_WRITE))
158        throw error(this->file, FILE_PERMS);
159      if (!file_status.is_regular())
160        throw error(this->file, FILE_NOTREG);
161    }
162
163  /* By default, file chroots do no locking. */
164  /* Create or unlink session information. */
165  if ((type == SETUP_START && lock == true) ||
166      (type == SETUP_STOP && lock == false && status == 0))
167    {
168
169      bool start = (type == SETUP_START);
170      setup_session_info(start);
171    }
172}
173
174sbuild::chroot::session_flags
175chroot_file::get_session_flags (chroot const& chroot) const
176{
177  session_flags flags = SESSION_NOFLAGS;
178
179  if (get_active())
180    flags = SESSION_PURGE;
181
182  return flags;
183}
184
185void
186chroot_file::get_details (chroot const&  chroot,
187                          format_detail& detail) const
188{
189  chroot::get_details(chroot, detail);
190
191  if (!this->file.empty())
192    detail
193      .add(_("File"), get_file())
194      .add(_("File Repack"), this->repack);
195}
196
197void
198chroot_file::get_keyfile (chroot const& chroot,
199                          keyfile&      keyfile) const
200{
201  chroot::get_keyfile(chroot, keyfile);
202
203  keyfile::set_object_value(*this, &chroot_file::get_file,
204                            keyfile, get_keyfile_name(), "file");
205
206  if (get_active())
207    keyfile::set_object_value(*this, &chroot_file::get_file_repack,
208                              keyfile, get_keyfile_name(), "file-repack");
209}
210
211void
212chroot_file::set_keyfile (chroot&        chroot,
213                          keyfile const& keyfile,
214                          string_list&   used_keys)
215{
216  chroot::set_keyfile(chroot, keyfile, used_keys);
217
218  keyfile::get_object_value(*this, &chroot_file::set_file,
219                            keyfile, get_keyfile_name(), "file",
220                            keyfile::PRIORITY_REQUIRED);
221  used_keys.push_back("file");
222
223  keyfile::get_object_value(*this, &chroot_file::set_file_repack,
224                            keyfile, get_keyfile_name(), "file-repack",
225                            get_active() ?
226                            keyfile::PRIORITY_REQUIRED :
227                            keyfile::PRIORITY_DISALLOWED);
228  used_keys.push_back("file-repack");
229}
Note: See TracBrowser for help on using the repository browser.