sbuild-chroot.h

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         NAME_INVALID,     
00094         SESSION_UNLINK,   
00095         SESSION_WRITE,    
00096         VERBOSITY_INVALID 
00097       };
00098 
00100     typedef custom_error<error_code> error;
00101 
00103     typedef std::tr1::shared_ptr<chroot> ptr;
00104 
00106     typedef std::tr1::shared_ptr<const chroot> const_ptr;
00107 
00108   protected:
00110     chroot ();
00111 
00113     chroot (const chroot& rhs);
00114 
00115   public:
00117     virtual ~chroot ();
00118 
00125     static ptr
00126     create (std::string const& type);
00127 
00133     virtual ptr
00134     clone () const = 0;
00135 
00144     virtual chroot::ptr
00145     clone_session (std::string const& name,
00146                    std::string const& user,
00147                    bool               root) const = 0;
00148 
00154     virtual chroot::ptr
00155     clone_source () const = 0;
00156 
00162     std::string const&
00163     get_name () const;
00164 
00170     void
00171     set_name (std::string const& name);
00172 
00178     std::string const&
00179     get_description () const;
00180 
00186     void
00187     set_description (std::string const& description);
00188 
00194     std::string const&
00195     get_mount_location () const;
00196 
00202     void
00203     set_mount_location (std::string const& location);
00204 
00205   public:
00214     virtual std::string
00215     get_path () const = 0;
00216 
00223     unsigned int
00224     get_priority () const;
00225 
00235     void
00236     set_priority (unsigned int priority);
00237 
00243     string_list const&
00244     get_users () const;
00245 
00251     void
00252     set_users (string_list const& users);
00253 
00259     string_list const&
00260     get_groups () const;
00261 
00267     void
00268     set_groups (string_list const& groups);
00269 
00277     string_list const&
00278     get_root_users () const;
00279 
00287     void
00288     set_root_users (string_list const& users);
00289 
00297     string_list const&
00298     get_root_groups () const;
00299 
00307     void
00308     set_root_groups (string_list const& groups);
00309 
00316     string_list const&
00317     get_aliases () const;
00318 
00325     void
00326     set_aliases (string_list const& aliases);
00327 
00333     bool
00334     get_preserve_environment () const;
00335 
00341     void
00342     set_preserve_environment (bool preserve_environment);
00343 
00351     regex const&
00352     get_environment_filter () const;
00353 
00361     void
00362     set_environment_filter (regex const& environment_filter);
00363 
00370     bool
00371     get_active () const;
00372 
00378     bool
00379     get_original () const;
00380 
00386     void
00387     set_original (bool original);
00388 
00394     bool
00395     get_run_setup_scripts () const;
00396 
00397   protected:
00404     void
00405     set_run_setup_scripts (bool run_setup_scripts);
00406 
00407   public:
00415     std::string const&
00416     get_script_config () const;
00417 
00425     void
00426     set_script_config (std::string const& script_config);
00427 
00434     string_list const&
00435     get_command_prefix () const;
00436 
00443     void
00444     set_command_prefix (string_list const& command_prefix);
00445 
00451     verbosity
00452     get_verbosity () const;
00453 
00459     const char *
00460     get_verbosity_string () const;
00461 
00467     void
00468     set_verbosity (verbosity verbosity);
00469 
00475     void
00476     set_verbosity (std::string const& verbosity);
00477 
00483     virtual std::string const&
00484     get_chroot_type () const = 0;
00485 
00492     void
00493     setup_env (environment& env) const;
00494 
00502     virtual void
00503     setup_env (chroot const& chroot,
00504                environment& env) const = 0;
00505 
00517     void
00518     lock (setup_type type);
00519 
00533     void
00534     unlock (setup_type type,
00535             int        status);
00536 
00537   protected:
00543     virtual void
00544     setup_session_info (bool start);
00545 
00560     virtual void
00561     setup_lock(setup_type type,
00562                bool       lock,
00563                int        status) = 0;
00564 
00565   public:
00573     template <typename T>
00574     std::tr1::shared_ptr<T>
00575     get_facet ();
00576 
00584     template <typename T>
00585     const std::tr1::shared_ptr<const T>
00586     get_facet () const;
00587 
00593     template <typename T>
00594     void
00595     add_facet (std::tr1::shared_ptr<T> facet);
00596 
00601     template <typename T>
00602     void
00603     remove_facet ();
00604 
00610     template <typename T>
00611     void
00612     remove_facet (std::tr1::shared_ptr<T> facet);
00613 
00619     template <typename T>
00620     void
00621     replace_facet (std::tr1::shared_ptr<T> facet);
00622 
00628     string_list
00629     list_facets () const;
00630 
00637     session_flags
00638     get_session_flags () const;
00639 
00647     virtual chroot::session_flags
00648     get_session_flags (chroot const& chroot) const = 0;
00649 
00659     friend std::ostream&
00660     operator << (std::ostream& stream,
00661                  ptr const&    rhs)
00662     {
00663       rhs->print_details(stream);
00664       return stream;
00665     }
00666 
00674     friend
00675     keyfile const&
00676     operator >> (keyfile const& keyfile,
00677                  ptr&           rhs)
00678     {
00679       string_list used;
00680       rhs->set_keyfile(keyfile, used);
00681       keyfile.check_keys(rhs->get_name(), used);
00682       return keyfile;
00683     }
00684 
00692     friend
00693     keyfile&
00694     operator << (keyfile&   keyfile,
00695                  ptr const& rhs)
00696     {
00697       rhs->get_keyfile(keyfile);
00698       return keyfile;
00699     }
00700 
00706     void
00707     get_details (format_detail& detail) const;
00708 
00715     virtual void
00716     get_details (chroot const&  chroot,
00717                  format_detail& detail) const = 0;
00718 
00726     void
00727     print_details (std::ostream& stream) const;
00728 
00736     void
00737     get_keyfile (keyfile& keyfile) const;
00738 
00739   protected:
00748     virtual void
00749     get_keyfile (chroot const& chroot,
00750                  keyfile&      keyfile) const = 0;
00751 
00752   public:
00761     void
00762     set_keyfile (keyfile const& keyfile,
00763                  string_list&   used_keys);
00764 
00765   protected:
00775     virtual void
00776     set_keyfile (chroot&        chroot,
00777                  keyfile const& keyfile,
00778                  string_list&   used_keys) = 0;
00779 
00780   private:
00782     std::string   name;
00784     std::string   description;
00786     unsigned int  priority;
00788     string_list   users;
00790     string_list   groups;
00792     string_list   root_users;
00794     string_list   root_groups;
00796     string_list   aliases;
00798     bool          preserve_environment;
00800     regex         environment_filter;
00802     std::string   mount_location;
00804     bool          original;
00806     bool          run_setup_scripts;
00808     std::string   script_config;
00810     string_list   command_prefix;
00812     verbosity     message_verbosity;
00813 
00815     typedef std::tr1::shared_ptr<chroot_facet> facet_ptr;
00817     typedef std::list<facet_ptr> facet_list;
00819     facet_list facets;
00820   };
00821 
00828   chroot::session_flags
00829   inline operator | (chroot::session_flags const& lhs,
00830                      chroot::session_flags const& rhs)
00831   {
00832     return static_cast<chroot::session_flags>
00833       (static_cast<int>(lhs) | static_cast<int>(rhs));
00834   }
00835 
00842   chroot::session_flags
00843   inline operator & (chroot::session_flags const& lhs,
00844                      chroot::session_flags const& rhs)
00845   {
00846     return static_cast<chroot::session_flags>
00847       (static_cast<int>(lhs) & static_cast<int>(rhs));
00848   }
00849 
00850 }
00851 
00852 #include <sbuild/sbuild-chroot-facet.h>
00853 
00854 namespace sbuild
00855 {
00856 
00857   template <typename T>
00858   std::tr1::shared_ptr<T>
00859   chroot::get_facet ()
00860   {
00861     std::tr1::shared_ptr<T> ret;
00862 
00863     for (facet_list::const_iterator pos = facets.begin();
00864          pos != facets.end();
00865          ++pos)
00866       {
00867         if (ret = std::tr1::dynamic_pointer_cast<T>(*pos))
00868           break;
00869       }
00870 
00871     return ret;
00872   }
00873 
00874   template <typename T>
00875   const std::tr1::shared_ptr<const T>
00876   chroot::get_facet () const
00877   {
00878     std::tr1::shared_ptr<T> ret;
00879 
00880     for (facet_list::const_iterator pos = facets.begin();
00881          pos != facets.end();
00882          ++pos)
00883       {
00884         if (ret = std::tr1::dynamic_pointer_cast<T>(*pos))
00885           break;
00886       }
00887 
00888     return std::tr1::const_pointer_cast<T>(ret);
00889   }
00890 
00891   template <typename T>
00892   void
00893   chroot::add_facet (std::tr1::shared_ptr<T> facet)
00894   {
00895     facet_ptr new_facet = std::tr1::dynamic_pointer_cast<chroot_facet>(facet);
00896     if (!new_facet)
00897       throw error(FACET_INVALID);
00898 
00899     for (facet_list::const_iterator pos = facets.begin();
00900          pos != facets.end();
00901          ++pos)
00902       {
00903         if (std::tr1::dynamic_pointer_cast<T>(*pos))
00904           throw error(FACET_PRESENT);
00905       }
00906 
00907     new_facet->set_chroot(*this);
00908     facets.push_back(new_facet);
00909   }
00910 
00911   template <typename T>
00912   void
00913   chroot::remove_facet ()
00914   {
00915     for (facet_list::iterator pos = facets.begin();
00916          pos != facets.end();
00917          ++pos)
00918       {
00919         if (std::tr1::dynamic_pointer_cast<T>(*pos))
00920           {
00921             facets.erase(pos);
00922             break;
00923           }
00924       }
00925   }
00926 
00927   template <typename T>
00928   void
00929   chroot::remove_facet (std::tr1::shared_ptr<T> facet)
00930   {
00931     remove_facet<T>();
00932   }
00933 
00934   template <typename T>
00935   void
00936   chroot::replace_facet (std::tr1::shared_ptr<T> facet)
00937   {
00938     remove_facet<T>();
00939     add_facet(facet);
00940   }
00941 
00942 }
00943 
00944 #endif /* SBUILD_CHROOT_H */
00945 
00946 /*
00947  * Local Variables:
00948  * mode:C++
00949  * End:
00950  */

Generated on Tue Jan 18 21:48:56 2011 for sbuild by  doxygen 1.5.6