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 error_code
00067       {
00068         CHROOT_CREATE,   
00069         CHROOT_DEVICE,   
00070         CHROOT_TYPE,     
00071         DEVICE_ABS,      
00072         DEVICE_LOCK,     
00073         DEVICE_NOTBLOCK, 
00074         DEVICE_UNLOCK,   
00075         DIRECTORY_ABS,   
00076         FACET_INVALID,   
00077         FACET_PRESENT,   
00078         FILE_ABS,        
00079         FILE_LOCK,       
00080         FILE_NOTREG,     
00081         FILE_OWNER,      
00082         FILE_PERMS,      
00083         FILE_UNLOCK,     
00084         LOCATION_ABS,    
00085         SESSION_UNLINK,  
00086         SESSION_WRITE    
00087       };
00088 
00090     typedef custom_error<error_code> error;
00091 
00093     typedef std::tr1::shared_ptr<chroot> ptr;
00094 
00096     typedef std::tr1::shared_ptr<const chroot> const_ptr;
00097 
00098   protected:
00100     chroot ();
00101 
00103     chroot (const chroot& rhs);
00104 
00105   public:
00107     virtual ~chroot ();
00108 
00115     static ptr
00116     create (std::string const& type);
00117 
00123     virtual ptr
00124     clone () const = 0;
00125 
00134     virtual chroot::ptr
00135     clone_session (std::string const& session_id,
00136                    std::string const& user,
00137                    bool               root) const = 0;
00138 
00144     virtual chroot::ptr
00145     clone_source () const = 0;
00146 
00152     std::string const&
00153     get_name () const;
00154 
00160     void
00161     set_name (std::string const& name);
00162 
00168     std::string const&
00169     get_session_id () const;
00170 
00177     void
00178     set_session_id (std::string const& session_id);
00179 
00187     std::string const&
00188     get_keyfile_name () const;
00189 
00195     std::string const&
00196     get_description () const;
00197 
00203     void
00204     set_description (std::string const& description);
00205 
00211     std::string const&
00212     get_mount_location () const;
00213 
00219     void
00220     set_mount_location (std::string const& location);
00221 
00222   public:
00231     virtual std::string
00232     get_path () const = 0;
00233 
00240     unsigned int
00241     get_priority () const;
00242 
00252     void
00253     set_priority (unsigned int priority);
00254 
00260     string_list const&
00261     get_users () const;
00262 
00268     void
00269     set_users (string_list const& users);
00270 
00276     string_list const&
00277     get_groups () const;
00278 
00284     void
00285     set_groups (string_list const& groups);
00286 
00294     string_list const&
00295     get_root_users () const;
00296 
00304     void
00305     set_root_users (string_list const& users);
00306 
00314     string_list const&
00315     get_root_groups () const;
00316 
00324     void
00325     set_root_groups (string_list const& groups);
00326 
00333     string_list const&
00334     get_aliases () const;
00335 
00342     void
00343     set_aliases (string_list const& aliases);
00344 
00352     regex const&
00353     get_environment_filter () const;
00354 
00362     void
00363     set_environment_filter (regex const& environment_filter);
00364 
00371     bool
00372     get_active () const;
00373 
00379     bool
00380     get_original () const;
00381 
00387     void
00388     set_original (bool original);
00389 
00395     bool
00396     get_run_setup_scripts () const;
00397 
00398   protected:
00405     void
00406     set_run_setup_scripts (bool run_setup_scripts);
00407 
00408   public:
00416     std::string const&
00417     get_script_config () const;
00418 
00426     void
00427     set_script_config (std::string const& script_config);
00428 
00435     string_list const&
00436     get_command_prefix () const;
00437 
00444     void
00445     set_command_prefix (string_list const& command_prefix);
00446 
00452     virtual std::string const&
00453     get_chroot_type () const = 0;
00454 
00461     void
00462     setup_env (environment& env) const;
00463 
00471     virtual void
00472     setup_env (chroot const& chroot,
00473                environment& env) const = 0;
00474 
00486     void
00487     lock (setup_type type);
00488 
00502     void
00503     unlock (setup_type type,
00504             int        status);
00505 
00506   protected:
00512     virtual void
00513     setup_session_info (bool start);
00514 
00529     virtual void
00530     setup_lock(setup_type type,
00531                bool       lock,
00532                int        status) = 0;
00533 
00534   public:
00535     template <typename T>
00536     std::tr1::shared_ptr<T>
00537     get_facet ();
00538 
00539     template <typename T>
00540     const std::tr1::shared_ptr<const T>
00541     get_facet () const;
00542 
00543     template <typename T>
00544     void
00545     add_facet (std::tr1::shared_ptr<T> facet);
00546 
00547     template <typename T>
00548     void
00549     remove_facet ();
00550 
00551     template <typename T>
00552     void
00553     remove_facet (std::tr1::shared_ptr<T> facet);
00554 
00555     template <typename T>
00556     void
00557     replace_facet (std::tr1::shared_ptr<T> facet);
00558 
00559     string_list
00560     list_facets () const;
00561 
00568     session_flags
00569     get_session_flags () const;
00570 
00578     virtual chroot::session_flags
00579     get_session_flags (chroot const& chroot) const = 0;
00580 
00590     friend std::ostream&
00591     operator << (std::ostream& stream,
00592                  ptr const&    rhs)
00593     {
00594       rhs->print_details(stream);
00595       return stream;
00596     }
00597 
00601     friend
00602     keyfile const&
00603     operator >> (keyfile const& keyfile,
00604                  ptr&           rhs)
00605     {
00606       string_list used;
00607       rhs->set_keyfile(keyfile, used);
00608       keyfile.check_keys(rhs->get_name(), used);
00609       return keyfile;
00610     }
00611 
00615     friend
00616     keyfile&
00617     operator << (keyfile&   keyfile,
00618                  ptr const& rhs)
00619     {
00620       rhs->get_keyfile(keyfile);
00621       return keyfile;
00622     }
00623 
00629     void
00630     get_details (format_detail& detail) const;
00631 
00638     virtual void
00639     get_details (chroot const&  chroot,
00640                  format_detail& detail) const = 0;
00641 
00649     void
00650     print_details (std::ostream& stream) const;
00651 
00659     void
00660     get_keyfile (keyfile& keyfile) const;
00661 
00662   protected:
00671     virtual void
00672     get_keyfile (chroot const& chroot,
00673                  keyfile&      keyfile) const = 0;
00674 
00675   public:
00684     void
00685     set_keyfile (keyfile const& keyfile,
00686                  string_list&   used_keys);
00687 
00688   protected:
00698     virtual void
00699     set_keyfile (chroot&        chroot,
00700                  keyfile const& keyfile,
00701                  string_list&   used_keys) = 0;
00702 
00703   private:
00705     std::string   name;
00707     std::string   session_id;
00709     std::string   description;
00711     unsigned int  priority;
00713     string_list   users;
00715     string_list   groups;
00717     string_list   root_users;
00719     string_list   root_groups;
00721     string_list   aliases;
00723     regex         environment_filter;
00725     std::string   mount_location;
00727     bool          original;
00729     bool          run_setup_scripts;
00731     std::string   script_config;
00733     string_list   command_prefix;
00734 
00735     typedef std::tr1::shared_ptr<chroot_facet> facet_ptr;
00736     typedef std::list<facet_ptr> facet_list;
00737     facet_list facets;
00738   };
00739 
00746   chroot::session_flags
00747   inline operator | (chroot::session_flags const& lhs,
00748                      chroot::session_flags const& rhs)
00749   {
00750     return static_cast<chroot::session_flags>
00751       (static_cast<int>(lhs) | static_cast<int>(rhs));
00752   }
00753 
00760   chroot::session_flags
00761   inline operator & (chroot::session_flags const& lhs,
00762                      chroot::session_flags const& rhs)
00763   {
00764     return static_cast<chroot::session_flags>
00765       (static_cast<int>(lhs) & static_cast<int>(rhs));
00766   }
00767 
00768 }
00769 
00770 #include <sbuild/sbuild-chroot-facet.h>
00771 
00772 namespace sbuild
00773 {
00774 
00775   template <typename T>
00776   std::tr1::shared_ptr<T>
00777   chroot::get_facet ()
00778   {
00779     std::tr1::shared_ptr<T> ret;
00780 
00781     for (facet_list::const_iterator pos = facets.begin();
00782          pos != facets.end();
00783          ++pos)
00784       {
00785         if (ret = std::tr1::dynamic_pointer_cast<T>(*pos))
00786           break;
00787       }
00788 
00789     return ret;
00790   }
00791 
00792   template <typename T>
00793   const std::tr1::shared_ptr<const T>
00794   chroot::get_facet () const
00795   {
00796     std::tr1::shared_ptr<T> ret;
00797 
00798     for (facet_list::const_iterator pos = facets.begin();
00799          pos != facets.end();
00800          ++pos)
00801       {
00802         if (ret = std::tr1::dynamic_pointer_cast<T>(*pos))
00803           break;
00804       }
00805 
00806     return std::tr1::const_pointer_cast<T>(ret);
00807   }
00808 
00809   template <typename T>
00810   void
00811   chroot::add_facet (std::tr1::shared_ptr<T> facet)
00812   {
00813     facet_ptr new_facet = std::tr1::dynamic_pointer_cast<chroot_facet>(facet);
00814     if (!new_facet)
00815       throw error(FACET_INVALID);
00816 
00817     for (facet_list::const_iterator pos = facets.begin();
00818          pos != facets.end();
00819          ++pos)
00820       {
00821         if (std::tr1::dynamic_pointer_cast<T>(*pos))
00822           throw error(FACET_PRESENT);
00823       }
00824 
00825     new_facet->set_chroot(*this);
00826     facets.push_back(new_facet);
00827   }
00828 
00829   template <typename T>
00830   void
00831   chroot::remove_facet ()
00832   {
00833     for (facet_list::iterator pos = facets.begin();
00834          pos != facets.end();
00835          ++pos)
00836       {
00837         if (std::tr1::dynamic_pointer_cast<T>(*pos))
00838           {
00839             facets.erase(pos);
00840             break;
00841           }
00842       }
00843   }
00844 
00845   template <typename T>
00846   void
00847   chroot::remove_facet (std::tr1::shared_ptr<T> facet)
00848   {
00849     remove_facet<T>();
00850   }
00851 
00852   template <typename T>
00853   void
00854   chroot::replace_facet (std::tr1::shared_ptr<T> facet)
00855   {
00856     remove_facet<T>();
00857     add_facet(facet);
00858   }
00859 
00860 }
00861 
00862 #endif /* SBUILD_CHROOT_H */
00863 
00864 /*
00865  * Local Variables:
00866  * mode:C++
00867  * End:
00868  */
Generated on Sat Apr 3 02:43:46 2010 for sbuild by  doxygen 1.6.3