sbuild  1.6.0
sbuild-chroot.h
1 /* Copyright © 2005-2008 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_CHROOT_H
20 #define SBUILD_CHROOT_H
21 
22 #include <sbuild/sbuild-custom-error.h>
23 #include <sbuild/sbuild-environment.h>
24 #include <sbuild/sbuild-format-detail.h>
25 #include <sbuild/sbuild-keyfile.h>
26 #include <sbuild/sbuild-regex.h>
27 #include <sbuild/sbuild-tr1types.h>
28 
29 #include <list>
30 #include <ostream>
31 #include <string>
32 
33 namespace sbuild
34 {
35 
36  class chroot_facet;
37 
45  class chroot
46  {
47  public:
50  {
54  };
55 
58  {
60  SESSION_CREATE = 1 << 0,
61  SESSION_CLONE = 1 << 1,
62  SESSION_PURGE = 1 << 2
63  };
64 
66  enum verbosity
67  {
71  };
72 
75  {
98  };
99 
102 
104  typedef std::shared_ptr<chroot> ptr;
105 
107  typedef std::shared_ptr<const chroot> const_ptr;
108 
109  protected:
111  chroot ();
112 
114  chroot (const chroot& rhs);
115 
116  public:
118  virtual ~chroot ();
119 
126  static ptr
127  create (std::string const& type);
128 
134  virtual ptr
135  clone () const = 0;
136 
146  virtual chroot::ptr
147  clone_session (std::string const& session_id,
148  std::string const& alias,
149  std::string const& user,
150  bool root) const = 0;
151 
157  virtual chroot::ptr
158  clone_source () const = 0;
159 
165  std::string const&
166  get_name () const;
167 
173  void
174  set_name (std::string const& name);
175 
181  std::string const&
182  get_description () const;
183 
189  void
190  set_description (std::string const& description);
191 
197  std::string const&
198  get_mount_location () const;
199 
205  void
206  set_mount_location (std::string const& location);
207 
208  public:
217  virtual std::string
218  get_path () const = 0;
219 
225  string_list const&
226  get_users () const;
227 
233  void
234  set_users (string_list const& users);
235 
241  string_list const&
242  get_groups () const;
243 
249  void
250  set_groups (string_list const& groups);
251 
259  string_list const&
260  get_root_users () const;
261 
269  void
271 
279  string_list const&
280  get_root_groups () const;
281 
289  void
291 
298  string_list const&
299  get_aliases () const;
300 
307  void
309 
315  bool
316  get_preserve_environment () const;
317 
323  void
325 
331  std::string const&
332  get_default_shell () const;
333 
339  void
340  set_default_shell (std::string const& default_shell);
341 
349  regex const&
350  get_environment_filter () const;
351 
359  void
361 
368  bool
369  get_active () const;
370 
376  bool
377  get_original () const;
378 
384  void
385  set_original (bool original);
386 
392  bool
393  get_run_setup_scripts () const;
394 
395  protected:
402  void
404 
405  public:
413  std::string const&
414  get_script_config () const;
415 
423  void
424  set_script_config (std::string const& script_config);
425 
433  std::string const&
434  get_profile () const;
435 
443  void
444  set_profile (std::string const& profile);
445 
452  string_list const&
453  get_command_prefix () const;
454 
461  void
463 
469  verbosity
470  get_verbosity () const;
471 
477  const char *
478  get_verbosity_string () const;
479 
485  void
487 
493  void
494  set_verbosity (std::string const& verbosity);
495 
501  virtual std::string const&
502  get_chroot_type () const = 0;
503 
510  void
511  setup_env (environment& env) const;
512 
520  virtual void
521  setup_env (chroot const& chroot,
522  environment& env) const = 0;
523 
535  void
536  lock (setup_type type);
537 
551  void
552  unlock (setup_type type,
553  int status);
554 
555  protected:
561  virtual void
562  setup_session_info (bool start);
563 
578  virtual void
579  setup_lock(setup_type type,
580  bool lock,
581  int status) = 0;
582 
583  public:
591  template <typename T>
592  std::shared_ptr<T>
593  get_facet ();
594 
602  template <typename T>
603  const std::shared_ptr<const T>
604  get_facet () const;
605 
611  template <typename T>
612  void
613  add_facet (std::shared_ptr<T> facet);
614 
619  template <typename T>
620  void
621  remove_facet ();
622 
628  template <typename T>
629  void
630  remove_facet (std::shared_ptr<T> facet);
631 
637  template <typename T>
638  void
639  replace_facet (std::shared_ptr<T> facet);
640 
647  list_facets () const;
648 
656  get_session_flags () const;
657 
665  virtual chroot::session_flags
666  get_session_flags (chroot const& chroot) const = 0;
667 
677  friend std::ostream&
678  operator << (std::ostream& stream,
679  ptr const& rhs)
680  {
681  rhs->print_details(stream);
682  return stream;
683  }
684 
692  friend
693  keyfile const&
695  ptr& rhs)
696  {
697  rhs->set_keyfile(keyfile);
698  return keyfile;
699  }
700 
708  friend
709  keyfile&
711  ptr const& rhs)
712  {
713  rhs->get_keyfile(keyfile);
714  return keyfile;
715  }
716 
722  void
723  get_details (format_detail& detail) const;
724 
731  virtual void
732  get_details (chroot const& chroot,
733  format_detail& detail) const = 0;
734 
742  void
743  print_details (std::ostream& stream) const;
744 
752  void
753  get_keyfile (keyfile& keyfile) const;
754 
755  protected:
764  virtual void
765  get_keyfile (chroot const& chroot,
766  keyfile& keyfile) const = 0;
767 
768  public:
776  void
777  set_keyfile (keyfile const& keyfile);
778 
779  protected:
789  virtual void
791  keyfile const& keyfile,
792  string_list& used_keys) = 0;
793 
794  private:
796  std::string name;
798  std::string description;
812  std::string default_shell;
816  std::string mount_location;
818  bool original;
822  std::string script_config;
824  std::string profile;
829 
831  typedef std::shared_ptr<chroot_facet> facet_ptr;
833  typedef std::list<facet_ptr> facet_list;
836  };
837 
846  chroot::session_flags const& rhs)
847  {
848  return static_cast<chroot::session_flags>
849  (static_cast<int>(lhs) | static_cast<int>(rhs));
850  }
851 
860  chroot::session_flags const& rhs)
861  {
862  return static_cast<chroot::session_flags>
863  (static_cast<int>(lhs) & static_cast<int>(rhs));
864  }
865 
866 }
867 
868 #include <sbuild/sbuild-chroot-facet.h>
869 
870 namespace sbuild
871 {
872 
873  template <typename T>
874  std::shared_ptr<T>
876  {
877  std::shared_ptr<T> ret;
878 
879  for (facet_list::const_iterator pos = facets.begin();
880  pos != facets.end();
881  ++pos)
882  {
883  if (ret = std::dynamic_pointer_cast<T>(*pos))
884  break;
885  }
886 
887  return ret;
888  }
889 
890  template <typename T>
891  const std::shared_ptr<const T>
893  {
894  std::shared_ptr<T> ret;
895 
896  for (facet_list::const_iterator pos = facets.begin();
897  pos != facets.end();
898  ++pos)
899  {
900  if (ret = std::dynamic_pointer_cast<T>(*pos))
901  break;
902  }
903 
904  return std::const_pointer_cast<T>(ret);
905  }
906 
907  template <typename T>
908  void
909  chroot::add_facet (std::shared_ptr<T> facet)
910  {
911  facet_ptr new_facet = std::dynamic_pointer_cast<chroot_facet>(facet);
912  if (!new_facet)
913  throw error(FACET_INVALID);
914 
915  for (facet_list::const_iterator pos = facets.begin();
916  pos != facets.end();
917  ++pos)
918  {
919  if (std::dynamic_pointer_cast<T>(*pos))
920  throw error(FACET_PRESENT);
921  }
922 
923  new_facet->set_chroot(*this);
924  facets.push_back(new_facet);
925  }
926 
927  template <typename T>
928  void
930  {
931  for (facet_list::iterator pos = facets.begin();
932  pos != facets.end();
933  ++pos)
934  {
935  if (std::dynamic_pointer_cast<T>(*pos))
936  {
937  facets.erase(pos);
938  break;
939  }
940  }
941  }
942 
943  template <typename T>
944  void
945  chroot::remove_facet (std::shared_ptr<T> facet)
946  {
947  remove_facet<T>();
948  }
949 
950  template <typename T>
951  void
952  chroot::replace_facet (std::shared_ptr<T> facet)
953  {
954  remove_facet<T>();
955  add_facet(facet);
956  }
957 
958 }
959 
960 #endif /* SBUILD_CHROOT_H */
961 
962 /*
963  * Local Variables:
964  * mode:C++
965  * End:
966  */