sbuild  1.7.1
chroot.h
1 /* Copyright © 2005-2013 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_CHROOT_H
20 #define SBUILD_CHROOT_CHROOT_H
21 
22 #include <sbuild/chroot/facet/facet.h>
23 #include <sbuild/custom-error.h>
24 #include <sbuild/environment.h>
25 #include <sbuild/format-detail.h>
26 #include <sbuild/keyfile.h>
27 #include <sbuild/regex.h>
28 #include <sbuild/util.h>
29 
30 #include <list>
31 #include <memory>
32 #include <ostream>
33 #include <string>
34 
35 namespace sbuild
36 {
40  namespace chroot
41  {
42 
50  class chroot
51  {
52  public:
55  {
61  };
62 
64  enum verbosity
65  {
69  };
70 
73  {
97  };
98 
101 
103  typedef std::shared_ptr<chroot> ptr;
104 
106  typedef std::shared_ptr<const chroot> const_ptr;
107 
109  typedef std::shared_ptr<facet::facet> facet_ptr;
110 
112  typedef std::list<facet_ptr> facet_list;
113 
115  chroot ();
116 
118  chroot (const chroot& rhs);
119 
121  virtual ~chroot ();
122 
129  static chroot::ptr
130  create (const std::string& type);
131 
138  clone () const;
139 
150  clone_session (const std::string& session_id,
151  const std::string& alias,
152  const std::string& user,
153  bool root) const;
154 
161  clone_source () const;
162 
168  std::string const&
169  get_name () const;
170 
176  void
177  set_name (const std::string& name);
178 
184  std::string const&
185  get_description () const;
186 
192  void
193  set_description (const std::string& description);
194 
200  std::string const&
201  get_mount_location () const;
202 
208  void
209  set_mount_location (const std::string& location);
210 
211  public:
221  std::string
222  get_path () const;
223 
229  string_list const&
230  get_users () const;
231 
237  void
238  set_users (const string_list& users);
239 
245  string_list const&
246  get_groups () const;
247 
253  void
254  set_groups (const string_list& groups);
255 
263  string_list const&
264  get_root_users () const;
265 
273  void
275 
283  string_list const&
284  get_root_groups () const;
285 
293  void
295 
302  string_list const&
303  get_aliases () const;
304 
311  void
313 
319  bool
320  get_preserve_environment () const;
321 
327  void
329 
335  std::string const&
336  get_default_shell () const;
337 
343  void
344  set_default_shell (const std::string& default_shell);
345 
353  regex const&
354  get_environment_filter () const;
355 
363  void
365 
372  bool
373  get_active () const;
374 
380  bool
381  get_original () const;
382 
388  void
389  set_original (bool original);
390 
396  bool
397  get_run_setup_scripts () const;
398 
399  public:
407  std::string const&
408  get_script_config () const;
409 
417  void
418  set_script_config (const std::string& script_config);
419 
427  std::string const&
428  get_profile () const;
429 
437  void
438  set_profile (const std::string& profile);
439 
446  string_list const&
447  get_command_prefix () const;
448 
455  void
457 
463  verbosity
464  get_verbosity () const;
465 
471  const char *
472  get_verbosity_string () const;
473 
479  void
481 
487  void
488  set_verbosity (const std::string& verbosity);
489 
496  std::string const&
497  get_chroot_type () const;
498 
505  void
506  setup_env (environment& env) const;
507 
519  void
520  lock (setup_type type);
521 
535  void
536  unlock (setup_type type,
537  int status);
538 
539  protected:
555  void
556  setup_lock(setup_type type,
557  bool lock,
558  int status);
559 
560  public:
568  template <typename T>
569  std::shared_ptr<T>
570  get_facet ();
571 
579  template <typename T>
580  const std::shared_ptr<const T>
581  get_facet () const;
582 
590  template <typename T>
591  std::shared_ptr<T>
592  get_facet_strict ();
593 
601  template <typename T>
602  const std::shared_ptr<const T>
603  get_facet_strict () const;
604 
610  facet_list&
611  get_facets ();
612 
618  const facet_list&
619  get_facets () const;
620 
628  template <typename T>
629  void
630  add_facet (std::shared_ptr<T> facet,
631  bool copy = false);
632 
642  void
644  bool copy = false);
645 
650  template <typename T>
651  void
652  remove_facet ();
653 
659  template <typename T>
660  void
661  remove_facet (std::shared_ptr<T> facet);
662 
668  template <typename T>
669  void
670  replace_facet (std::shared_ptr<T> facet);
671 
678  list_facets () const;
679 
687  get_session_flags () const;
688 
698  friend std::ostream&
699  operator << (std::ostream& stream,
700  const ptr& rhs)
701  {
702  rhs->print_details(stream);
703  return stream;
704  }
705 
713  friend
714  keyfile const&
716  ptr& rhs)
717  {
718  rhs->set_keyfile(keyfile);
719  return keyfile;
720  }
721 
729  friend
730  keyfile&
732  const ptr& rhs)
733  {
734  rhs->get_keyfile(keyfile);
735  return keyfile;
736  }
737 
743  void
744  get_details (format_detail& detail) const;
745 
753  void
754  print_details (std::ostream& stream) const;
755 
763  void
764  get_keyfile (keyfile& keyfile) const;
765 
766  public:
774  void
775  set_keyfile (const keyfile& keyfile);
776 
783  get_used_keys () const;
784 
785  private:
787  std::string name;
789  std::string description;
803  std::string default_shell;
807  std::string mount_location;
809  bool original;
811  std::string script_config;
813  std::string profile;
818 
821  };
822 
823  }
824 }
825 
826 namespace sbuild
827 {
828  namespace chroot
829  {
830 
831  template <typename T>
832  std::shared_ptr<T>
833  chroot::get_facet ()
834  {
835  std::shared_ptr<T> ret;
836 
837  for (const auto& facet : facets)
838  {
839  if (ret = std::dynamic_pointer_cast<T>(facet))
840  break;
841  }
842 
843  return ret;
844  }
845 
846  template <typename T>
847  const std::shared_ptr<const T>
848  chroot::get_facet () const
849  {
850  std::shared_ptr<T> ret;
851 
852  for (const auto& facet : facets)
853  {
854  if (ret = std::dynamic_pointer_cast<T>(facet))
855  break;
856  }
857 
858  return std::const_pointer_cast<T>(ret);
859  }
860 
861  template <typename T>
862  std::shared_ptr<T>
863  chroot::get_facet_strict ()
864  {
865  auto ret = get_facet<T>();
866 
867  if (!ret)
868  throw error(type_name<T>(), FACET_ABSENT);
869 
870  return ret;
871  }
872 
873  template <typename T>
874  const std::shared_ptr<const T>
875  chroot::get_facet_strict () const
876  {
877  auto ret = get_facet<T>();
878 
879  if (!ret)
880  throw error(type_name<T>(), FACET_ABSENT);
881 
882  return ret;
883  }
884 
885  template <typename T>
886  void
887  chroot::add_facet (std::shared_ptr<T> facet,
888  bool copy)
889  {
890  facet_ptr new_facet = std::dynamic_pointer_cast<facet::facet>(facet);
891  if (!new_facet)
892  throw error(type_name<T>(), FACET_INVALID);
893 
894  for (const auto& facet : facets)
895  {
896  if (std::dynamic_pointer_cast<T>(facet))
897  throw error(type_name<T>(), FACET_PRESENT);
898  }
899 
900  new_facet->set_chroot(*this, copy);
901  facets.push_back(new_facet);
902  }
903 
904  inline void
905  chroot::add_facet_by_name (facet::facet::ptr facet,
906  bool copy)
907  {
908  for (facet_list::iterator lfacet = facets.begin();
909  lfacet != facets.end();)
910  {
911  facet_list::iterator current = lfacet++;
912  if ((*current)->get_name() == facet->get_name())
913  facets.erase(current);
914  }
915 
916  facet->set_chroot(*this, copy);
917  facets.push_back(facet);
918  }
919 
920  template <typename T>
921  void
922  chroot::remove_facet ()
923  {
924  for (facet_list::iterator facet = facets.begin();
925  facet != facets.end();
926  ++facet)
927  {
928  if (std::dynamic_pointer_cast<T>(*facet))
929  {
930  facets.erase(facet);
931  break;
932  }
933  }
934  }
935 
936  template <typename T>
937  void
938  chroot::remove_facet (std::shared_ptr<T> facet)
939  {
940  remove_facet<T>();
941  }
942 
943  template <typename T>
944  void
945  chroot::replace_facet (std::shared_ptr<T> facet)
946  {
947  remove_facet<T>();
948  add_facet(facet);
949  }
950 
951  }
952 }
953 
954 #endif /* SBUILD_CHROOT_CHROOT_H */
955 
956 /*
957  * Local Variables:
958  * mode:C++
959  * End:
960  */