sbuild-chroot.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2008  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software: you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation, either version 3 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>.
00016  *
00017  *********************************************************************/
00018 
00019 #ifndef SBUILD_CHROOT_H
00020 #define SBUILD_CHROOT_H
00021 
00022 #include <sbuild/sbuild-custom-error.h>
00023 #include <sbuild/sbuild-environment.h>
00024 #include <sbuild/sbuild-format-detail.h>
00025 #include <sbuild/sbuild-keyfile.h>
00026 #include <sbuild/sbuild-regex.h>
00027 #include <sbuild/sbuild-tr1types.h>
00028 
00029 #include <list>
00030 #include <ostream>
00031 #include <string>
00032 
00033 namespace sbuild
00034 {
00035 
00036   class chroot_facet;
00037 
00045   class chroot
00046   {
00047   public:
00049     enum setup_type
00050       {
00051         SETUP_START,   
00052         SETUP_RECOVER, 
00053         SETUP_STOP     
00054       };
00055 
00057     enum session_flags
00058       {
00059         SESSION_NOFLAGS = 0,      
00060         SESSION_CREATE  = 1 << 0, 
00061         SESSION_CLONE   = 1 << 1, 
00062         SESSION_PURGE   = 1 << 2  
00063       };
00064 
00066     enum verbosity
00067       {
00068         VERBOSITY_QUIET,  
00069         VERBOSITY_NORMAL, 
00070         VERBOSITY_VERBOSE 
00071       };
00072 
00074     enum error_code
00075       {
00076         CHROOT_CREATE,    
00077         CHROOT_DEVICE,    
00078         CHROOT_TYPE,      
00079         DEVICE_ABS,       
00080         DEVICE_LOCK,      
00081         DEVICE_NOTBLOCK,  
00082         DEVICE_UNLOCK,    
00083         DIRECTORY_ABS,    
00084         FACET_INVALID,    
00085         FACET_PRESENT,    
00086         FILE_ABS,         
00087         FILE_LOCK,        
00088         FILE_NOTREG,      
00089         FILE_OWNER,       
00090         FILE_PERMS,       
00091         FILE_UNLOCK,      
00092         LOCATION_ABS,     
00093         SESSION_UNLINK,   
00094         SESSION_WRITE,    
00095         VERBOSITY_INVALID 
00096       };
00097 
00099     typedef custom_error<error_code> error;
00100 
00102     typedef std::tr1::shared_ptr<chroot> ptr;
00103 
00105     typedef std::tr1::shared_ptr<const chroot> const_ptr;
00106 
00107   protected:
00109     chroot ();
00110 
00112     chroot (const chroot& rhs);
00113 
00114   public:
00116     virtual ~chroot ();
00117 
00124     static ptr
00125     create (std::string const& type);
00126 
00132     virtual ptr
00133     clone () const = 0;
00134 
00143     virtual chroot::ptr
00144     clone_session (std::string const& session_id,
00145                    std::string const& user,
00146                    bool               root) const = 0;
00147 
00153     virtual chroot::ptr
00154     clone_source () const = 0;
00155 
00161     std::string const&
00162     get_name () const;
00163 
00169     void
00170     set_name (std::string const& name);
00171 
00177     std::string const&
00178     get_session_id () const;
00179 
00186     void
00187     set_session_id (std::string const& session_id);
00188 
00196     std::string const&
00197     get_keyfile_name () const;
00198 
00204     std::string const&
00205     get_description () const;
00206 
00212     void
00213     set_description (std::string const& description);
00214 
00220     std::string const&
00221     get_mount_location () const;
00222 
00228     void
00229     set_mount_location (std::string const& location);
00230 
00231   public:
00240     virtual std::string
00241     get_path () const = 0;
00242 
00249     unsigned int
00250     get_priority () const;
00251 
00261     void
00262     set_priority (unsigned int priority);
00263 
00269     string_list const&
00270     get_users () const;
00271 
00277     void
00278     set_users (string_list const& users);
00279 
00285     string_list const&
00286     get_groups () const;
00287 
00293     void
00294     set_groups (string_list const& groups);
00295 
00303     string_list const&
00304     get_root_users () const;
00305 
00313     void
00314     set_root_users (string_list const& users);
00315 
00323     string_list const&
00324     get_root_groups () const;
00325 
00333     void
00334     set_root_groups (string_list const& groups);
00335 
00342     string_list const&
00343     get_aliases () const;
00344 
00351     void
00352     set_aliases (string_list const& aliases);
00353 
00359     bool
00360     get_preserve_environment () const;
00361 
00367     void
00368     set_preserve_environment (bool preserve_environment);
00369 
00377     regex const&
00378     get_environment_filter () const;
00379 
00387     void
00388     set_environment_filter (regex const& environment_filter);
00389 
00396     bool
00397     get_active () const;
00398 
00404     bool
00405     get_original () const;
00406 
00412     void
00413     set_original (bool original);
00414 
00420     bool
00421     get_run_setup_scripts () const;
00422 
00423   protected:
00430     void
00431     set_run_setup_scripts (bool run_setup_scripts);
00432 
00433   public:
00441     std::string const&
00442     get_script_config () const;
00443 
00451     void
00452     set_script_config (std::string const& script_config);
00453 
00460     string_list const&
00461     get_command_prefix () const;
00462 
00469     void
00470     set_command_prefix (string_list const& command_prefix);
00471 
00477     verbosity
00478     get_verbosity () const;
00479 
00485     const char *
00486     get_verbosity_string () const;
00487 
00493     void
00494     set_verbosity (verbosity verbosity);
00495 
00501     void
00502     set_verbosity (std::string const& verbosity);
00503 
00509     virtual std::string const&
00510     get_chroot_type () const = 0;
00511 
00518     void
00519     setup_env (environment& env) const;
00520 
00528     virtual void
00529     setup_env (chroot const& chroot,
00530                environment& env) const = 0;
00531 
00543     void
00544     lock (setup_type type);
00545 
00559     void
00560     unlock (setup_type type,
00561             int        status);
00562 
00563   protected:
00569     virtual void
00570     setup_session_info (bool start);
00571 
00586     virtual void
00587     setup_lock(setup_type type,
00588                bool       lock,
00589                int        status) = 0;
00590 
00591   public:
00592     template <typename T>
00593     std::tr1::shared_ptr<T>
00594     get_facet ();
00595 
00596     template <typename T>
00597     const std::tr1::shared_ptr<const T>
00598     get_facet () const;
00599 
00600     template <typename T>
00601     void
00602     add_facet (std::tr1::shared_ptr<T> facet);
00603 
00604     template <typename T>
00605     void
00606     remove_facet ();
00607 
00608     template <typename T>
00609     void
00610     remove_facet (std::tr1::shared_ptr<T> facet);
00611 
00612     template <typename T>
00613     void
00614     replace_facet (std::tr1::shared_ptr<T> facet);
00615 
00616     string_list
00617     list_facets () const;
00618 
00625     session_flags
00626     get_session_flags () const;
00627 
00635     virtual chroot::session_flags
00636     get_session_flags (chroot const& chroot) const = 0;
00637 
00647     friend std::ostream&
00648     operator << (std::ostream& stream,
00649                  ptr const&    rhs)
00650     {
00651       rhs->print_details(stream);
00652       return stream;
00653     }
00654 
00658     friend
00659     keyfile const&
00660     operator >> (keyfile const& keyfile,
00661                  ptr&           rhs)
00662     {
00663       string_list used;
00664       rhs->set_keyfile(keyfile, used);
00665       keyfile.check_keys(rhs->get_name(), used);
00666       return keyfile;
00667     }
00668 
00672     friend
00673     keyfile&
00674     operator << (keyfile&   keyfile,
00675                  ptr const& rhs)
00676     {
00677       rhs->get_keyfile(keyfile);
00678       return keyfile;
00679     }
00680 
00686     void
00687     get_details (format_detail& detail) const;
00688 
00695     virtual void
00696     get_details (chroot const&  chroot,
00697                  format_detail& detail) const = 0;
00698 
00706     void
00707     print_details (std::ostream& stream) const;
00708 
00716     void
00717     get_keyfile (keyfile& keyfile) const;
00718 
00719   protected:
00728     virtual void
00729     get_keyfile (chroot const& chroot,
00730                  keyfile&      keyfile) const = 0;
00731 
00732   public:
00741     void
00742     set_keyfile (keyfile const& keyfile,
00743                  string_list&   used_keys);
00744 
00745   protected:
00755     virtual void
00756     set_keyfile (chroot&        chroot,
00757                  keyfile const& keyfile,
00758                  string_list&   used_keys) = 0;
00759 
00760   private:
00762     std::string   name;
00764     std::string   session_id;
00766     std::string   description;
00768     unsigned int  priority;
00770     string_list   users;
00772     string_list   groups;
00774     string_list   root_users;
00776     string_list   root_groups;
00778     string_list   aliases;
00780     bool          preserve_environment;
00782     regex         environment_filter;
00784     std::string   mount_location;
00786     bool          original;
00788     bool          run_setup_scripts;
00790     std::string   script_config;
00792     string_list   command_prefix;
00794     verbosity     message_verbosity;
00795 
00796     typedef std::tr1::shared_ptr<chroot_facet> facet_ptr;
00797     typedef std::list<facet_ptr> facet_list;
00799     facet_list facets;
00800   };
00801 
00808   chroot::session_flags
00809   inline operator | (chroot::session_flags const& lhs,
00810                      chroot::session_flags const& rhs)
00811   {
00812     return static_cast<chroot::session_flags>
00813       (static_cast<int>(lhs) | static_cast<int>(rhs));
00814   }
00815 
00822   chroot::session_flags
00823   inline operator & (chroot::session_flags const& lhs,
00824                      chroot::session_flags const& rhs)
00825   {
00826     return static_cast<chroot::session_flags>
00827       (static_cast<int>(lhs) & static_cast<int>(rhs));
00828   }
00829 
00830 }
00831 
00832 #include <sbuild/sbuild-chroot-facet.h>
00833 
00834 namespace sbuild
00835 {
00836 
00837   template <typename T>
00838   std::tr1::shared_ptr<T>
00839   chroot::get_facet ()
00840   {
00841     std::tr1::shared_ptr<T> ret;
00842 
00843     for (facet_list::const_iterator pos = facets.begin();
00844          pos != facets.end();
00845          ++pos)
00846       {
00847         if (ret = std::tr1::dynamic_pointer_cast<T>(*pos))
00848           break;
00849       }
00850 
00851     return ret;
00852   }
00853 
00854   template <typename T>
00855   const std::tr1::shared_ptr<const T>
00856   chroot::get_facet () const
00857   {
00858     std::tr1::shared_ptr<T> ret;
00859 
00860     for (facet_list::const_iterator pos = facets.begin();
00861          pos != facets.end();
00862          ++pos)
00863       {
00864         if (ret = std::tr1::dynamic_pointer_cast<T>(*pos))
00865           break;
00866       }
00867 
00868     return std::tr1::const_pointer_cast<T>(ret);
00869   }
00870 
00871   template <typename T>
00872   void
00873   chroot::add_facet (std::tr1::shared_ptr<T> facet)
00874   {
00875     facet_ptr new_facet = std::tr1::dynamic_pointer_cast<chroot_facet>(facet);
00876     if (!new_facet)
00877       throw error(FACET_INVALID);
00878 
00879     for (facet_list::const_iterator pos = facets.begin();
00880          pos != facets.end();
00881          ++pos)
00882       {
00883         if (std::tr1::dynamic_pointer_cast<T>(*pos))
00884           throw error(FACET_PRESENT);
00885       }
00886 
00887     new_facet->set_chroot(*this);
00888     facets.push_back(new_facet);
00889   }
00890 
00891   template <typename T>
00892   void
00893   chroot::remove_facet ()
00894   {
00895     for (facet_list::iterator pos = facets.begin();
00896          pos != facets.end();
00897          ++pos)
00898       {
00899         if (std::tr1::dynamic_pointer_cast<T>(*pos))
00900           {
00901             facets.erase(pos);
00902             break;
00903           }
00904       }
00905   }
00906 
00907   template <typename T>
00908   void
00909   chroot::remove_facet (std::tr1::shared_ptr<T> facet)
00910   {
00911     remove_facet<T>();
00912   }
00913 
00914   template <typename T>
00915   void
00916   chroot::replace_facet (std::tr1::shared_ptr<T> facet)
00917   {
00918     remove_facet<T>();
00919     add_facet(facet);
00920   }
00921 
00922 }
00923 
00924 #endif /* SBUILD_CHROOT_H */
00925 
00926 /*
00927  * Local Variables:
00928  * mode:C++
00929  * End:
00930  */
Generated on Sat Jul 10 22:15:59 2010 for sbuild by  doxygen 1.6.3