diff options
author | Roger Leigh <rleigh@debian.org> | 2013-04-21 21:51:35 +0100 |
---|---|---|
committer | Roger Leigh <rleigh@debian.org> | 2013-04-21 21:57:33 +0100 |
commit | e520708119bc5bbcb4316e357ba20d364d86f5da (patch) | |
tree | 68e044870ecd66952dc49c02297d801f7ed5bb7b /lib | |
parent | dd9cd34e9e5831812b7e06f9f9bde34aefc36ba3 (diff) | |
download | schroot-e520708119bc5bbcb4316e357ba20d364d86f5da.tar.gz |
sbuild::chroot: Move setup_session_info to session facet
Diffstat (limited to 'lib')
-rw-r--r-- | lib/sbuild/chroot/block-device.cc | 3 | ||||
-rw-r--r-- | lib/sbuild/chroot/btrfs-snapshot.cc | 2 | ||||
-rw-r--r-- | lib/sbuild/chroot/chroot.cc | 59 | ||||
-rw-r--r-- | lib/sbuild/chroot/chroot.h | 8 | ||||
-rw-r--r-- | lib/sbuild/chroot/custom.cc | 2 | ||||
-rw-r--r-- | lib/sbuild/chroot/directory.cc | 3 | ||||
-rw-r--r-- | lib/sbuild/chroot/facet/session.cc | 60 | ||||
-rw-r--r-- | lib/sbuild/chroot/facet/session.h | 11 | ||||
-rw-r--r-- | lib/sbuild/chroot/file.cc | 2 | ||||
-rw-r--r-- | lib/sbuild/chroot/loopback.cc | 3 | ||||
-rw-r--r-- | lib/sbuild/chroot/lvm-snapshot.cc | 2 |
11 files changed, 82 insertions, 73 deletions
diff --git a/lib/sbuild/chroot/block-device.cc b/lib/sbuild/chroot/block-device.cc index c27db24f..d26b494f 100644 --- a/lib/sbuild/chroot/block-device.cc +++ b/lib/sbuild/chroot/block-device.cc @@ -19,6 +19,7 @@ #include <config.h> #include <sbuild/chroot/block-device.h> +#include <sbuild/chroot/facet/session.h> #include <sbuild/chroot/facet/session-clonable.h> #include <sbuild/chroot/facet/source-clonable.h> #ifdef SBUILD_FEATURE_UNION @@ -160,7 +161,7 @@ namespace sbuild (type == SETUP_STOP && lock == false && status == 0)) { bool start = (type == SETUP_START); - setup_session_info(start); + get_facet_strict<facet::session>()->setup_session_info(start); } } diff --git a/lib/sbuild/chroot/btrfs-snapshot.cc b/lib/sbuild/chroot/btrfs-snapshot.cc index 407b70a3..60437e86 100644 --- a/lib/sbuild/chroot/btrfs-snapshot.cc +++ b/lib/sbuild/chroot/btrfs-snapshot.cc @@ -177,7 +177,7 @@ namespace sbuild (type == SETUP_STOP && lock == false && status == 0)) { bool start = (type == SETUP_START); - setup_session_info(start); + get_facet_strict<facet::session>()->setup_session_info(start); } } diff --git a/lib/sbuild/chroot/chroot.cc b/lib/sbuild/chroot/chroot.cc index 85a19458..5dd3600d 100644 --- a/lib/sbuild/chroot/chroot.cc +++ b/lib/sbuild/chroot/chroot.cc @@ -41,22 +41,18 @@ #include <sbuild/chroot/facet/session.h> #include <sbuild/chroot/facet/session-clonable.h> #include <sbuild/chroot/facet/source.h> +#include <sbuild/chroot/facet/storage.h> #include <sbuild/chroot/facet/userdata.h> #ifdef SBUILD_FEATURE_UNSHARE #include <sbuild/chroot/facet/unshare.h> #endif // SBUILD_FEATURE_UNSHARE #include "fdstream.h" -#include "keyfile-writer.h" -#include "lock.h" +#include <cassert> #include <cerrno> #include <map> #include <utility> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> - #include <boost/format.hpp> using boost::format; @@ -552,57 +548,6 @@ namespace sbuild } void - chroot::setup_session_info (bool start) - { - /* Create or unlink session information. */ - std::string file = std::string(SCHROOT_SESSION_DIR) + "/" + get_name(); - - if (start) - { - int fd = open(file.c_str(), O_CREAT|O_EXCL|O_WRONLY, 0664); - if (fd < 0) - throw error(file, SESSION_WRITE, strerror(errno)); - - // Create a stream from the file descriptor. The fd will be - // closed when the stream is destroyed. -#ifdef BOOST_IOSTREAMS_CLOSE_HANDLE_OLD - fdostream output(fd, true); -#else - fdostream output(fd, boost::iostreams::close_handle); -#endif - output.imbue(std::locale::classic()); - - file_lock lock(fd); - try - { - lock.set_lock(lock::LOCK_EXCLUSIVE, 2); - } - catch (lock::error const& e) - { - throw error(file, FILE_LOCK, e); - } - - keyfile details; - get_keyfile(details); - output << keyfile_writer(details); - - try - { - lock.unset_lock(); - } - catch (lock::error const& e) - { - throw error(file, FILE_UNLOCK, e); - } - } - else /* start == false */ - { - if (unlink(file.c_str()) != 0) - throw error(file, SESSION_UNLINK, strerror(errno)); - } - } - - void chroot::lock (setup_type type) { setup_lock(type, true, 0); diff --git a/lib/sbuild/chroot/chroot.h b/lib/sbuild/chroot/chroot.h index bcfdbc52..db41dd1a 100644 --- a/lib/sbuild/chroot/chroot.h +++ b/lib/sbuild/chroot/chroot.h @@ -564,14 +564,6 @@ namespace sbuild protected: /** - * Set up persistent session information. - * - * @param start true if startion, or false if ending a session. - */ - virtual void - setup_session_info (bool start); - - /** * Unlock a chroot during setup. The locking technique (if any) may * vary depending upon the chroot type and setup stage. For * example, during creation of an LVM snapshot a block device diff --git a/lib/sbuild/chroot/custom.cc b/lib/sbuild/chroot/custom.cc index 806d69bf..22ce9b42 100644 --- a/lib/sbuild/chroot/custom.cc +++ b/lib/sbuild/chroot/custom.cc @@ -156,7 +156,7 @@ namespace sbuild { bool start = (type == SETUP_START); - setup_session_info(start); + get_facet_strict<facet::session>()->setup_session_info(start); } } diff --git a/lib/sbuild/chroot/directory.cc b/lib/sbuild/chroot/directory.cc index ed8d70e9..b1037954 100644 --- a/lib/sbuild/chroot/directory.cc +++ b/lib/sbuild/chroot/directory.cc @@ -19,6 +19,7 @@ #include <config.h> #include <sbuild/chroot/directory.h> +#include <sbuild/chroot/facet/session.h> #include <sbuild/chroot/facet/session-clonable.h> #include <sbuild/chroot/facet/source-clonable.h> #ifdef SBUILD_FEATURE_UNION @@ -139,7 +140,7 @@ namespace sbuild (type == SETUP_STOP && lock == false && status == 0)) { bool start = (type == SETUP_START); - setup_session_info(start); + get_facet_strict<facet::session>()->setup_session_info(start); } } diff --git a/lib/sbuild/chroot/facet/session.cc b/lib/sbuild/chroot/facet/session.cc index 40b75f78..71b3caac 100644 --- a/lib/sbuild/chroot/facet/session.cc +++ b/lib/sbuild/chroot/facet/session.cc @@ -21,10 +21,17 @@ #include <sbuild/chroot/chroot.h> #include <sbuild/chroot/config.h> #include <sbuild/chroot/facet/session.h> -#include "format-detail.h" +#include <sbuild/keyfile-writer.h> +#include <sbuild/lock.h> +#include <sbuild/fdstream.h> +#include <sbuild/format-detail.h> #include <cassert> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + #include <boost/format.hpp> using boost::format; @@ -97,6 +104,57 @@ namespace sbuild } void + session::setup_session_info (bool start) + { + /* Create or unlink session information. */ + std::string file = std::string(SCHROOT_SESSION_DIR) + "/" + owner->get_name(); + + if (start) + { + int fd = open(file.c_str(), O_CREAT|O_EXCL|O_WRONLY, 0664); + if (fd < 0) + throw error(file, chroot::SESSION_WRITE, strerror(errno)); + + // Create a stream from the file descriptor. The fd will be + // closed when the stream is destroyed. +#ifdef BOOST_IOSTREAMS_CLOSE_HANDLE_OLD + fdostream output(fd, true); +#else + fdostream output(fd, boost::iostreams::close_handle); +#endif + output.imbue(std::locale::classic()); + + file_lock lock(fd); + try + { + lock.set_lock(lock::LOCK_EXCLUSIVE, 2); + } + catch (lock::error const& e) + { + throw error(file, chroot::FILE_LOCK, e); + } + + keyfile details; + owner->get_keyfile(details); + output << keyfile_writer(details); + + try + { + lock.unset_lock(); + } + catch (lock::error const& e) + { + throw error(file, chroot::FILE_UNLOCK, e); + } + } + else /* start == false */ + { + if (unlink(file.c_str()) != 0) + throw error(file, chroot::SESSION_UNLINK, strerror(errno)); + } + } + + void session::setup_env (chroot const& chroot, environment& env) const { diff --git a/lib/sbuild/chroot/facet/session.h b/lib/sbuild/chroot/facet/session.h index 83682181..8a80f186 100644 --- a/lib/sbuild/chroot/facet/session.h +++ b/lib/sbuild/chroot/facet/session.h @@ -39,6 +39,9 @@ namespace facet class session : public facet { public: + /// Exception type. + typedef chroot::error error; + /// A shared_ptr to a chroot facet object. typedef std::shared_ptr<session> ptr; @@ -100,6 +103,14 @@ namespace facet void set_selected_name (std::string const& name); + /** + * Set up persistent session information. + * + * @param start true if starting, or false if ending a session. + */ + void + setup_session_info (bool start); + virtual void setup_env (chroot const& chroot, environment& env) const; diff --git a/lib/sbuild/chroot/file.cc b/lib/sbuild/chroot/file.cc index c4d948e0..92be8cab 100644 --- a/lib/sbuild/chroot/file.cc +++ b/lib/sbuild/chroot/file.cc @@ -197,7 +197,7 @@ namespace sbuild { bool start = (type == SETUP_START); - setup_session_info(start); + get_facet_strict<facet::session>()->setup_session_info(start); } } diff --git a/lib/sbuild/chroot/loopback.cc b/lib/sbuild/chroot/loopback.cc index d09ecbcd..38c8378a 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/session.h> #include <sbuild/chroot/facet/session-clonable.h> #include <sbuild/chroot/facet/source-clonable.h> #include <sbuild/chroot/facet/mountable.h> @@ -169,7 +170,7 @@ namespace sbuild (type == SETUP_STOP && lock == false && status == 0)) { bool start = (type == SETUP_START); - setup_session_info(start); + get_facet_strict<facet::session>()->setup_session_info(start); } } diff --git a/lib/sbuild/chroot/lvm-snapshot.cc b/lib/sbuild/chroot/lvm-snapshot.cc index d064f5c2..87cda4ec 100644 --- a/lib/sbuild/chroot/lvm-snapshot.cc +++ b/lib/sbuild/chroot/lvm-snapshot.cc @@ -189,7 +189,7 @@ namespace sbuild (type == SETUP_STOP && lock == false && status == 0)) { bool start = (type == SETUP_START); - setup_session_info(start); + get_facet_strict<facet::session>()->setup_session_info(start); } } |