summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRoger Leigh <rleigh@debian.org>2013-04-28 19:22:39 +0100
committerRoger Leigh <rleigh@debian.org>2013-05-04 17:17:17 +0100
commit94bd4d909b83ff719600ad716fdbddd3db0d5835 (patch)
treea194badc91e5e5d1bfa77d5bef8cbedea4ba2039 /lib
parent82df78af4c5fa44642ed36f549f84267ec67f2c9 (diff)
downloadschroot-94bd4d909b83ff719600ad716fdbddd3db0d5835.tar.gz
sbuild::chroot: Migrate loopback to a storage facet
Diffstat (limited to 'lib')
-rw-r--r--lib/sbuild/Makefile.am2
-rw-r--r--lib/sbuild/chroot/facet/fsunion.h1
-rw-r--r--lib/sbuild/chroot/facet/loopback.cc338
-rw-r--r--lib/sbuild/chroot/facet/loopback.h189
-rw-r--r--lib/sbuild/chroot/facet/session-clonable.cc8
-rw-r--r--lib/sbuild/chroot/loopback.cc131
-rw-r--r--lib/sbuild/chroot/loopback.h54
7 files changed, 267 insertions, 456 deletions
diff --git a/lib/sbuild/Makefile.am b/lib/sbuild/Makefile.am
index 498a9971..51279724 100644
--- a/lib/sbuild/Makefile.am
+++ b/lib/sbuild/Makefile.am
@@ -30,6 +30,7 @@ lib_sbuild_public_h_sources = \
lib/sbuild/chroot/facet/file.h \
lib/sbuild/chroot/facet/directory.h \
lib/sbuild/chroot/facet/directory-base.h \
+ lib/sbuild/chroot/facet/loopback.h \
lib/sbuild/chroot/facet/mountable.h \
lib/sbuild/chroot/facet/personality.h \
lib/sbuild/chroot/facet/plain.h \
@@ -123,6 +124,7 @@ lib_sbuild_public_cc_sources = \
lib/sbuild/chroot/facet/facet.cc \
lib/sbuild/chroot/facet/file.cc \
lib/sbuild/chroot/facet/directory-base.cc \
+ lib/sbuild/chroot/facet/loopback.cc \
lib/sbuild/chroot/facet/mountable.cc \
lib/sbuild/chroot/facet/personality.cc \
lib/sbuild/chroot/facet/plain.cc \
diff --git a/lib/sbuild/chroot/facet/fsunion.h b/lib/sbuild/chroot/facet/fsunion.h
index 2cb1627b..e8a901d2 100644
--- a/lib/sbuild/chroot/facet/fsunion.h
+++ b/lib/sbuild/chroot/facet/fsunion.h
@@ -20,6 +20,7 @@
#ifndef SBUILD_CHROOT_FACET_FSUNION_H
#define SBUILD_CHROOT_FACET_FSUNION_H
+#include <sbuild/chroot/chroot.h>
#include <sbuild/chroot/facet/facet.h>
namespace sbuild
diff --git a/lib/sbuild/chroot/facet/loopback.cc b/lib/sbuild/chroot/facet/loopback.cc
index d09ecbcd..22a4601d 100644
--- a/lib/sbuild/chroot/facet/loopback.cc
+++ b/lib/sbuild/chroot/facet/loopback.cc
@@ -18,23 +18,18 @@
#include <config.h>
-#include <sbuild/chroot/loopback.h>
-#include <sbuild/chroot/facet/session-clonable.h>
-#include <sbuild/chroot/facet/source-clonable.h>
-#include <sbuild/chroot/facet/mountable.h>
-#ifdef SBUILD_FEATURE_UNION
#include <sbuild/chroot/facet/fsunion.h>
-#endif // SBUILD_FEATURE_UNION
+#include <sbuild/chroot/facet/loopback.h>
+#include <sbuild/chroot/facet/mountable.h>
+#include <sbuild/chroot/facet/session.h>
#include "format-detail.h"
-#include "lock.h"
-#include "util.h"
#include <cassert>
#include <cerrno>
-#include <cstring>
#include <boost/format.hpp>
+using std::endl;
using boost::format;
using namespace sbuild;
@@ -42,181 +37,162 @@ namespace sbuild
{
namespace chroot
{
-
- loopback::loopback ():
- chroot(),
- filename()
- {
- add_facet(facet::mountable::create());
+ namespace facet
+ {
+
+ loopback::loopback ():
+ storage(),
+ filename()
+ {
+ }
+
+ loopback::loopback (const loopback& rhs):
+ storage(rhs),
+ filename(rhs.filename)
+ {
+ }
+
+ loopback::~loopback ()
+ {
+ }
+
+ void
+ loopback::set_chroot (chroot& chroot)
+ {
+ storage::set_chroot(chroot);
+ if (!owner->get_facet<mountable>())
+ owner->add_facet(mountable::create());
#ifdef SBUILD_FEATURE_UNION
- add_facet(facet::fsunion::create());
+ if (!owner->get_facet<fsunion>())
+ owner->add_facet(fsunion::create());
#endif // SBUILD_FEATURE_UNION
- }
-
- loopback::~loopback ()
- {
- }
+ }
+
+ std::string const&
+ loopback::get_name () const
+ {
+ static const std::string name("loopback");
+
+ return name;
+ }
+
+ loopback::ptr
+ loopback::create ()
+ {
+ return ptr(new loopback());
+ }
+
+ facet::ptr
+ loopback::clone () const
+ {
+ return ptr(new loopback(*this));
+ }
+
+ std::string const&
+ loopback::get_filename () const
+ {
+ return this->filename;
+ }
+
+ void
+ loopback::set_filename (std::string const& filename)
+ {
+ if (!is_absname(filename))
+ throw error(filename, chroot::FILE_ABS);
+
+ this->filename = filename;
+ }
+
+ std::string
+ loopback::get_path () const
+ {
+ mountable::const_ptr pmnt
+ (owner->get_facet<mountable>());
+
+ std::string path(owner->get_mount_location());
+
+ if (pmnt)
+ path += pmnt->get_location();
+
+ return path;
+ }
+
+ void
+ loopback::setup_env (chroot const& chroot,
+ environment& env) const
+ {
+ storage::setup_env(chroot, env);
+
+ env.add("CHROOT_FILE", get_filename());
+ }
+
+ void
+ loopback::setup_lock (chroot::setup_type type,
+ bool lock,
+ int status)
+ {
+ // Check ownership and permissions.
+ if (type == chroot::SETUP_START && lock == true)
+ {
+ stat file_status(this->filename);
+
+ // NOTE: taken from chroot_config::check_security.
+ if (file_status.uid() != 0)
+ throw error(this->filename, chroot::FILE_OWNER);
+ if (file_status.check_mode(stat::PERM_OTHER_WRITE))
+ throw error(this->filename, chroot::FILE_PERMS);
+ if (!file_status.is_regular())
+ throw error(this->filename, chroot::FILE_NOTREG);
+ }
+
+ /* 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);
+ }
+ }
+
+ void
+ loopback::get_details (chroot const& chroot,
+ format_detail& detail) const
+ {
+ storage::get_details(chroot, detail);
+
+ if (!this->filename.empty())
+ detail.add(_("File"), get_filename());
+ }
+
+ void
+ loopback::get_used_keys (string_list& used_keys) const
+ {
+ storage::get_used_keys(used_keys);
+
+ used_keys.push_back("file");
+ }
+
+ void
+ loopback::get_keyfile (chroot const& chroot,
+ keyfile& keyfile) const
+ {
+ storage::get_keyfile(chroot, keyfile);
+
+ keyfile::set_object_value(*this, &loopback::get_filename,
+ keyfile, chroot.get_name(), "file");
+ }
+
+ void
+ loopback::set_keyfile (chroot& chroot,
+ keyfile const& keyfile)
+ {
+ storage::set_keyfile(chroot, keyfile);
+
+ keyfile::get_object_value(*this, &loopback::set_filename,
+ keyfile, chroot.get_name(), "file",
+ keyfile::PRIORITY_REQUIRED);
+ }
- loopback::loopback (const loopback& rhs):
- chroot(rhs),
- filename(rhs.filename)
- {
- }
-
- chroot::chroot::ptr
- loopback::clone () const
- {
- return ptr(new loopback(*this));
}
-
- chroot::chroot::ptr
- loopback::clone_session (std::string const& session_id,
- std::string const& alias,
- std::string const& user,
- bool root) const
- {
- facet::session_clonable::const_ptr psess
- (get_facet<facet::session_clonable>());
- assert(psess);
-
- ptr session(new loopback(*this));
- psess->clone_session_setup(*this, session, session_id, alias, user, root);
-
- return session;
- }
-
- chroot::chroot::ptr
- loopback::clone_source () const
- {
- ptr clone(new loopback(*this));
-
- facet::source_clonable::const_ptr psrc
- (get_facet<facet::source_clonable>());
- assert(psrc);
-
- psrc->clone_source_setup(*this, clone);
-
- return clone;
- }
-
- std::string const&
- loopback::get_filename () const
- {
- return this->filename;
- }
-
- void
- loopback::set_filename (std::string const& filename)
- {
- if (!is_absname(filename))
- throw error(filename, FILE_ABS);
-
- this->filename = filename;
- }
-
- std::string
- loopback::get_path () const
- {
- facet::mountable::const_ptr pmnt
- (get_facet<facet::mountable>());
-
- std::string path(get_mount_location());
-
- if (pmnt)
- path += pmnt->get_location();
-
- return path;
- }
-
- std::string const&
- loopback::get_chroot_type () const
- {
- static const std::string type("loopback");
-
- return type;
- }
-
- void
- loopback::setup_env (chroot const& chroot,
- environment& env) const
- {
- chroot::setup_env(chroot, env);
-
- env.add("CHROOT_FILE", get_filename());
- }
-
- void
- loopback::setup_lock (chroot::setup_type type,
- bool lock,
- int status)
- {
- // Check ownership and permissions.
- if (type == SETUP_START && lock == true)
- {
- stat file_status(this->filename);
-
- // NOTE: taken from chroot_config::check_security.
- if (file_status.uid() != 0)
- throw error(this->filename, FILE_OWNER);
- if (file_status.check_mode(stat::PERM_OTHER_WRITE))
- throw error(this->filename, FILE_PERMS);
- if (!file_status.is_regular())
- throw error(this->filename, FILE_NOTREG);
- }
-
- /* Create or unlink session information. */
- if ((type == SETUP_START && lock == true) ||
- (type == SETUP_STOP && lock == false && status == 0))
- {
- bool start = (type == SETUP_START);
- setup_session_info(start);
- }
- }
-
- chroot::chroot::session_flags
- loopback::get_session_flags (chroot const& chroot) const
- {
- return SESSION_NOFLAGS;
- }
-
- void
- loopback::get_details (chroot const& chroot,
- format_detail& detail) const
- {
- chroot::get_details(chroot, detail);
-
- if (!this->filename.empty())
- detail.add(_("File"), get_filename());
- }
-
- void
- loopback::get_used_keys (string_list& used_keys) const
- {
- chroot::get_used_keys(used_keys);
-
- used_keys.push_back("file");
- }
-
- void
- loopback::get_keyfile (chroot const& chroot,
- keyfile& keyfile) const
- {
- chroot::get_keyfile(chroot, keyfile);
-
- keyfile::set_object_value(*this, &loopback::get_filename,
- keyfile, get_name(), "file");
- }
-
- void
- loopback::set_keyfile (chroot& chroot,
- keyfile const& keyfile)
- {
- chroot::set_keyfile(chroot, keyfile);
-
- keyfile::get_object_value(*this, &loopback::set_filename,
- keyfile, get_name(), "file",
- keyfile::PRIORITY_REQUIRED);
- }
-
}
}
diff --git a/lib/sbuild/chroot/facet/loopback.h b/lib/sbuild/chroot/facet/loopback.h
index 6b09df7d..b2ba6f25 100644
--- a/lib/sbuild/chroot/facet/loopback.h
+++ b/lib/sbuild/chroot/facet/loopback.h
@@ -16,111 +16,120 @@
*
*********************************************************************/
-#ifndef SBUILD_CHROOT_LOOPBACK_H
-#define SBUILD_CHROOT_LOOPBACK_H
+#ifndef SBUILD_CHROOT_FACET_LOOPBACK_H
+#define SBUILD_CHROOT_FACET_LOOPBACK_H
-#include <sbuild/config.h>
#include <sbuild/chroot/chroot.h>
+#include <sbuild/chroot/facet/storage.h>
namespace sbuild
{
namespace chroot
{
- /**
- * A chroot stored in a file for loopback mounting.
- *
- * The file will be mounted on demand.
- */
- class loopback : public chroot
+ namespace facet
{
- public:
- /// Exception type.
- typedef chroot::error error;
-
- protected:
- /// The constructor.
- loopback ();
-
- /// The copy constructor.
- loopback (const loopback& rhs);
-
- friend class chroot;
-
- public:
- /// The destructor.
- virtual ~loopback ();
-
- virtual chroot::ptr
- clone () const;
-
- virtual chroot::ptr
- clone_session (std::string const& session_id,
- std::string const& alias,
- std::string const& user,
- bool root) const;
-
- virtual chroot::ptr
- clone_source () const;
-
- /**
- * Get the filename containing the chroot.
- *
- * @returns the filename.
- */
- std::string const&
- get_filename () const;
/**
- * Set the filename containing the chroot.
+ * A chroot stored in a file for loopback mounting.
*
- * @param filename the filename.
+ * The file will be mounted on demand.
*/
- void
- set_filename (std::string const& filename);
-
- std::string const&
- get_chroot_type () const;
-
- virtual std::string
- get_path () const;
-
- virtual void
- setup_env (chroot const& chroot,
- environment& env) const;
-
- virtual session_flags
- get_session_flags (chroot const& chroot) const;
-
- protected:
- virtual void
- setup_lock (chroot::setup_type type,
- bool lock,
- int status);
-
- virtual void
- get_details (chroot const& chroot,
- format_detail& detail) const;
-
- virtual void
- get_used_keys (string_list& used_keys) const;
-
- virtual void
- get_keyfile (chroot const& chroot,
- keyfile& keyfile) const;
-
- virtual void
- set_keyfile (chroot& chroot,
- keyfile const& keyfile);
-
- private:
- /// The file to use.
- std::string filename;
- };
-
+ class loopback : public storage
+ {
+ public:
+ /// Exception type.
+ typedef chroot::error error;
+
+ /// A shared_ptr to a chroot facet object.
+ typedef std::shared_ptr<loopback> ptr;
+
+ /// A shared_ptr to a const chroot facet object.
+ typedef std::shared_ptr<const loopback> const_ptr;
+
+ protected:
+ /// The constructor.
+ loopback ();
+
+ /// The copy constructor.
+ loopback (const loopback& rhs);
+
+ void
+ set_chroot (chroot& chroot);
+
+ friend class chroot;
+
+ public:
+ /// The destructor.
+ virtual ~loopback ();
+
+ virtual std::string const&
+ get_name () const;
+
+ /**
+ * Create a chroot facet.
+ *
+ * @returns a shared_ptr to the new chroot facet.
+ */
+ static ptr
+ create ();
+
+ facet::ptr
+ clone () const;
+
+ /**
+ * Get the filename containing the chroot.
+ *
+ * @returns the filename.
+ */
+ std::string const&
+ get_filename () const;
+
+ /**
+ * Set the filename containing the chroot.
+ *
+ * @param filename the filename.
+ */
+ void
+ set_filename (std::string const& filename);
+
+ virtual std::string
+ get_path () const;
+
+ virtual void
+ setup_env (chroot const& chroot,
+ environment& env) const;
+
+ protected:
+ virtual void
+ setup_lock (chroot::setup_type type,
+ bool lock,
+ int status);
+
+ virtual void
+ get_details (chroot const& chroot,
+ format_detail& detail) const;
+
+ virtual void
+ get_used_keys (string_list& used_keys) const;
+
+ virtual void
+ get_keyfile (chroot const& chroot,
+ keyfile& keyfile) const;
+
+ virtual void
+ set_keyfile (chroot& chroot,
+ keyfile const& keyfile);
+
+ private:
+ /// The file to use.
+ std::string filename;
+ };
+
+ }
}
}
-#endif /* SBUILD_CHROOT_LOOPBACK_H */
+#endif /* SBUILD_CHROOT_FACET_LOOPBACK_H */
/*
* Local Variables:
diff --git a/lib/sbuild/chroot/facet/session-clonable.cc b/lib/sbuild/chroot/facet/session-clonable.cc
index 6b6efc18..6479e61b 100644
--- a/lib/sbuild/chroot/facet/session-clonable.cc
+++ b/lib/sbuild/chroot/facet/session-clonable.cc
@@ -31,7 +31,7 @@
#include <sbuild/chroot/facet/lvm-snapshot.h>
#endif // SBUILD_FEATURE_LVMSNAP
#ifdef SBUILD_FEATURE_LOOPBACK
-#include <sbuild/chroot/loopback.h>
+#include <sbuild/chroot/facet/loopback.h>
#endif // SBUILD_FEATURE_LOOPBACK
#ifdef SBUILD_FEATURE_BTRFSSNAP
#include <sbuild/chroot/facet/btrfs-snapshot.h>
@@ -169,13 +169,13 @@ namespace sbuild
#ifdef SBUILD_FEATURE_LOOPBACK
/* Loopback chroots need the mount device name specifying. */
- std::shared_ptr<loopback> loopback(std::dynamic_pointer_cast<loopback>(clone));
- if (loopback)
+ loopback::ptr loop(clone->get_facet<loopback>());
+ if (loop)
{
mountable::ptr pmnt
(clone->get_facet<mountable>());
if (pmnt)
- pmnt->set_mount_device(loopback->get_filename());
+ pmnt->set_mount_device(loop->get_filename());
}
#endif // SBUILD_FEATURE_LOOPBACK
diff --git a/lib/sbuild/chroot/loopback.cc b/lib/sbuild/chroot/loopback.cc
index 38c8378a..c53746d0 100644
--- a/lib/sbuild/chroot/loopback.cc
+++ b/lib/sbuild/chroot/loopback.cc
@@ -19,6 +19,7 @@
#include <config.h>
#include <sbuild/chroot/loopback.h>
+#include <sbuild/chroot/facet/loopback.h>
#include <sbuild/chroot/facet/session.h>
#include <sbuild/chroot/facet/session-clonable.h>
#include <sbuild/chroot/facet/source-clonable.h>
@@ -45,13 +46,9 @@ namespace sbuild
{
loopback::loopback ():
- chroot(),
- filename()
+ chroot()
{
- add_facet(facet::mountable::create());
-#ifdef SBUILD_FEATURE_UNION
- add_facet(facet::fsunion::create());
-#endif // SBUILD_FEATURE_UNION
+ add_facet(facet::loopback::create());
}
loopback::~loopback ()
@@ -59,8 +56,7 @@ namespace sbuild
}
loopback::loopback (const loopback& rhs):
- chroot(rhs),
- filename(rhs.filename)
+ chroot(rhs)
{
}
@@ -100,124 +96,5 @@ namespace sbuild
return clone;
}
- std::string const&
- loopback::get_filename () const
- {
- return this->filename;
- }
-
- void
- loopback::set_filename (std::string const& filename)
- {
- if (!is_absname(filename))
- throw error(filename, FILE_ABS);
-
- this->filename = filename;
- }
-
- std::string
- loopback::get_path () const
- {
- facet::mountable::const_ptr pmnt
- (get_facet<facet::mountable>());
-
- std::string path(get_mount_location());
-
- if (pmnt)
- path += pmnt->get_location();
-
- return path;
- }
-
- std::string const&
- loopback::get_chroot_type () const
- {
- static const std::string type("loopback");
-
- return type;
- }
-
- void
- loopback::setup_env (chroot const& chroot,
- environment& env) const
- {
- chroot::setup_env(chroot, env);
-
- env.add("CHROOT_FILE", get_filename());
- }
-
- void
- loopback::setup_lock (chroot::setup_type type,
- bool lock,
- int status)
- {
- // Check ownership and permissions.
- if (type == SETUP_START && lock == true)
- {
- stat file_status(this->filename);
-
- // NOTE: taken from chroot_config::check_security.
- if (file_status.uid() != 0)
- throw error(this->filename, FILE_OWNER);
- if (file_status.check_mode(stat::PERM_OTHER_WRITE))
- throw error(this->filename, FILE_PERMS);
- if (!file_status.is_regular())
- throw error(this->filename, FILE_NOTREG);
- }
-
- /* Create or unlink session information. */
- if ((type == SETUP_START && lock == true) ||
- (type == SETUP_STOP && lock == false && status == 0))
- {
- bool start = (type == SETUP_START);
- get_facet_strict<facet::session>()->setup_session_info(start);
- }
- }
-
- chroot::chroot::session_flags
- loopback::get_session_flags (chroot const& chroot) const
- {
- return SESSION_NOFLAGS;
- }
-
- void
- loopback::get_details (chroot const& chroot,
- format_detail& detail) const
- {
- chroot::get_details(chroot, detail);
-
- if (!this->filename.empty())
- detail.add(_("File"), get_filename());
- }
-
- void
- loopback::get_used_keys (string_list& used_keys) const
- {
- chroot::get_used_keys(used_keys);
-
- used_keys.push_back("file");
- }
-
- void
- loopback::get_keyfile (chroot const& chroot,
- keyfile& keyfile) const
- {
- chroot::get_keyfile(chroot, keyfile);
-
- keyfile::set_object_value(*this, &loopback::get_filename,
- keyfile, get_name(), "file");
- }
-
- void
- loopback::set_keyfile (chroot& chroot,
- keyfile const& keyfile)
- {
- chroot::set_keyfile(chroot, keyfile);
-
- keyfile::get_object_value(*this, &loopback::set_filename,
- keyfile, get_name(), "file",
- keyfile::PRIORITY_REQUIRED);
- }
-
}
}
diff --git a/lib/sbuild/chroot/loopback.h b/lib/sbuild/chroot/loopback.h
index 6b09df7d..8082ec65 100644
--- a/lib/sbuild/chroot/loopback.h
+++ b/lib/sbuild/chroot/loopback.h
@@ -61,60 +61,6 @@ namespace sbuild
virtual chroot::ptr
clone_source () const;
-
- /**
- * Get the filename containing the chroot.
- *
- * @returns the filename.
- */
- std::string const&
- get_filename () const;
-
- /**
- * Set the filename containing the chroot.
- *
- * @param filename the filename.
- */
- void
- set_filename (std::string const& filename);
-
- std::string const&
- get_chroot_type () const;
-
- virtual std::string
- get_path () const;
-
- virtual void
- setup_env (chroot const& chroot,
- environment& env) const;
-
- virtual session_flags
- get_session_flags (chroot const& chroot) const;
-
- protected:
- virtual void
- setup_lock (chroot::setup_type type,
- bool lock,
- int status);
-
- virtual void
- get_details (chroot const& chroot,
- format_detail& detail) const;
-
- virtual void
- get_used_keys (string_list& used_keys) const;
-
- virtual void
- get_keyfile (chroot const& chroot,
- keyfile& keyfile) const;
-
- virtual void
- set_keyfile (chroot& chroot,
- keyfile const& keyfile);
-
- private:
- /// The file to use.
- std::string filename;
};
}