00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include "sbuild-chroot-block-device.h"
00023 #include "sbuild-format-detail.h"
00024 #include "sbuild-lock.h"
00025
00026 #include <cerrno>
00027 #include <cstring>
00028
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <sys/sysmacros.h>
00032 #include <unistd.h>
00033
00034 #include <boost/format.hpp>
00035
00036 using boost::format;
00037 using namespace sbuild;
00038
00039 chroot_block_device::chroot_block_device ():
00040 chroot(),
00041 device(),
00042 mount_options()
00043 {
00044 }
00045
00046 chroot_block_device::~chroot_block_device ()
00047 {
00048 }
00049
00050 sbuild::chroot::ptr
00051 chroot_block_device::clone () const
00052 {
00053 return ptr(new chroot_block_device(*this));
00054 }
00055
00056 std::string const&
00057 chroot_block_device::get_device () const
00058 {
00059 return this->device;
00060 }
00061
00062 void
00063 chroot_block_device::set_device (std::string const& device)
00064 {
00065 if (!is_absname(device))
00066 throw error(device, DEVICE_ABS);
00067
00068 this->device = device;
00069 }
00070
00071 std::string const&
00072 chroot_block_device::get_mount_device () const
00073 {
00074 return this->device;
00075 }
00076
00077 std::string const&
00078 chroot_block_device::get_mount_options () const
00079 {
00080 return this->mount_options;
00081 }
00082
00083 void
00084 chroot_block_device::set_mount_options (std::string const& mount_options)
00085 {
00086 this->mount_options = mount_options;
00087 }
00088
00089 std::string const&
00090 chroot_block_device::get_location () const
00091 {
00092 return chroot::get_location();
00093 }
00094
00095 void
00096 chroot_block_device::set_location (std::string const& location)
00097 {
00098 if (!location.empty() && !is_absname(location))
00099 throw error(location, LOCATION_ABS);
00100
00101 chroot::set_location(location);
00102 }
00103
00104 std::string const&
00105 chroot_block_device::get_chroot_type () const
00106 {
00107 static const std::string type("block-device");
00108
00109 return type;
00110 }
00111
00112 void
00113 chroot_block_device::setup_env (environment& env)
00114 {
00115 this->chroot::setup_env(env);
00116
00117 env.add("CHROOT_DEVICE", get_device());
00118 env.add("CHROOT_MOUNT_OPTIONS", get_mount_options());
00119 }
00120
00121 void
00122 chroot_block_device::setup_lock (chroot::setup_type type,
00123 bool lock,
00124 int status)
00125 {
00126 struct stat statbuf;
00127
00128
00129 if (type == EXEC_START || type == EXEC_STOP)
00130 return;
00131
00132
00133 if ((type == SETUP_START && lock == false) ||
00134 (type == SETUP_STOP && lock == true))
00135 return;
00136
00137 if (stat(this->device.c_str(), &statbuf) == -1)
00138 {
00139 throw error(get_device(), DEVICE_STAT, strerror(errno));
00140 }
00141 else if (!S_ISBLK(statbuf.st_mode))
00142 {
00143 throw error(get_device(), DEVICE_NOTBLOCK);
00144 }
00145 else
00146 {
00147 sbuild::device_lock dlock(this->device);
00148 if (lock)
00149 {
00150 try
00151 {
00152 dlock.set_lock(lock::LOCK_EXCLUSIVE, 15);
00153 }
00154 catch (sbuild::lock::error const& e)
00155 {
00156 throw error(get_device(), DEVICE_LOCK, e);
00157 }
00158 }
00159 else
00160 {
00161 try
00162 {
00163 dlock.unset_lock();
00164 }
00165 catch (sbuild::lock::error const& e)
00166 {
00167 throw error(get_device(), DEVICE_UNLOCK, e);
00168 }
00169 }
00170 }
00171 }
00172
00173 sbuild::chroot::session_flags
00174 chroot_block_device::get_session_flags () const
00175 {
00176 return static_cast<session_flags>(0);
00177 }
00178
00179 void
00180 chroot_block_device::get_details (format_detail& detail) const
00181 {
00182 this->chroot::get_details(detail);
00183
00184 if (!this->device.empty())
00185 detail.add(_("Device"), get_device());
00186 if (!this->mount_options.empty())
00187 detail.add(_("Mount Options"), get_mount_options());
00188 }
00189
00190 void
00191 chroot_block_device::get_keyfile (keyfile& keyfile) const
00192 {
00193 chroot::get_keyfile(keyfile);
00194
00195 keyfile::set_object_value(*this, &chroot_block_device::get_device,
00196 keyfile, get_name(), "device");
00197
00198 keyfile::set_object_value(*this, &chroot_block_device::get_mount_options,
00199 keyfile, get_name(), "mount-options");
00200
00201 keyfile::set_object_value(*this, &chroot_block_device::get_location,
00202 keyfile, get_name(), "location");
00203 }
00204
00205 void
00206 chroot_block_device::set_keyfile (keyfile const& keyfile)
00207 {
00208 chroot::set_keyfile(keyfile);
00209
00210 keyfile::get_object_value(*this, &chroot_block_device::set_device,
00211 keyfile, get_name(), "device",
00212 keyfile::PRIORITY_REQUIRED);
00213
00214 keyfile::get_object_value(*this, &chroot_block_device::set_mount_options,
00215 keyfile, get_name(), "mount-options",
00216 keyfile::PRIORITY_OPTIONAL);
00217
00218 keyfile::get_object_value(*this, &chroot_block_device::set_location,
00219 keyfile, get_name(), "location",
00220 keyfile::PRIORITY_OPTIONAL);
00221 }