sbuild  1.6.5
sbuild-util.h
1 /* Copyright © 2005-2007 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_UTIL_H
20 #define SBUILD_UTIL_H
21 
22 #include <sbuild/sbuild-environment.h>
23 #include <sbuild/sbuild-error.h>
24 #include <sbuild/sbuild-regex.h>
25 #include <sbuild/sbuild-types.h>
26 
27 #include <string>
28 #include <cerrno>
29 #include <cstring>
30 
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <pwd.h>
34 #include <grp.h>
35 #include <unistd.h>
36 
37 namespace sbuild
38 {
39 
47  std::string
48  basename (std::string name);
49 
57  std::string
58  dirname (std::string name);
59 
67  std::string
68  normalname (std::string name);
69 
77  bool
78  is_absname (std::string const& name);
79 
88  bool
89  is_valid_sessionname (std::string const& name);
90 
100  bool
101  is_valid_filename (std::string const& name,
102  bool lsb_mode = true);
103 
110  std::string
111  getcwd ();
112 
113 
121  std::string
123 
132  std::string
133  string_list_to_string (string_list const& list,
134  std::string const& separator);
135 
150  template <typename S>
151  std::vector<S>
152  split_string (S const& value,
153  S const& separator)
154  {
155  std::vector<S> ret;
156 
157  // Skip any separators at the start
158  typename S::size_type last_pos =
159  value.find_first_not_of(separator, 0);
160  // Find first separator.
161  typename S::size_type pos = value.find_first_of(separator, last_pos);
162 
163  while (pos !=S::npos || last_pos != S::npos)
164  {
165  // Add to list
166  ret.push_back(value.substr(last_pos, pos - last_pos));
167  // Find next
168  last_pos = value.find_first_not_of(separator, pos);
169  pos = value.find_first_of(separator, last_pos);
170  }
171 
172  return ret;
173  }
174 
186  std::vector<std::string>
187  split_string (std::string const& value,
188  std::string const& separator);
189 
204  template <typename S>
205  std::vector<S>
206  split_string_strict (S const& value,
207  S const& separator)
208  {
209  std::vector<S> ret;
210 
211  // Skip any separators at the start
212  typename S::size_type last_pos = 0;
213  // Find first separator.
214  typename S::size_type pos = value.find_first_of(separator, last_pos);
215 
216  while (pos !=S::npos || last_pos != S::npos)
217  {
218  // Add to list
219  if (pos == std::string::npos)
220  // Entire string from last_pos
221  ret.push_back(value.substr(last_pos, pos));
222  else
223  // Between pos and last_pos
224  ret.push_back(value.substr(last_pos, pos - last_pos));
225 
226  // Find next
227  last_pos = pos + separator.length();
228  pos = value.find_first_of(separator, last_pos);
229  }
230 
231  return ret;
232  }
233 
245  std::vector<std::string>
246  split_string_strict (std::string const& value,
247  std::string const& separator);
248 
258  std::wstring
259  widen_string (std::string const& str,
260  std::locale locale);
261 
271  std::string
272  narrow_string (std::wstring const& str,
273  std::locale locale);
274 
285  std::string
286  find_program_in_path (std::string const& program,
287  std::string const& path,
288  std::string const& prefix);
289 
298  char **
299  string_list_to_strv (string_list const& str);
300 
308  void
309  strv_delete (char **strv);
310 
321  int
322  exec (std::string const& file,
323  string_list const& command,
324  environment const& env);
325 
329  class stat
330  {
331  public:
334  {
336  FD
337  };
338 
341  {
342  FILE_TYPE_MASK = S_IFMT,
343  FILE_TYPE_SOCKET = S_IFSOCK,
344  FILE_TYPE_LINK = S_IFLNK,
345  FILE_TYPE_REGULAR = S_IFREG,
346  FILE_TYPE_BLOCK = S_IFBLK,
349  FILE_TYPE_FIFO = S_IFIFO,
350  PERM_SETUID = S_ISUID,
351  PERM_SETGIT = S_ISGID,
352  PERM_STICKY = S_ISVTX,
353  PERM_USER_MASK = S_IRWXU,
354  PERM_USER_READ = S_IRUSR,
355  PERM_USER_WRITE = S_IWUSR,
356  PERM_USER_EXECUTE = S_IXUSR,
357  PERM_GROUP_MASK = S_IRWXG,
358  PERM_GROUP_READ = S_IRGRP,
359  PERM_GROUP_WRITE = S_IWGRP,
360  PERM_GROUP_EXECUTE = S_IXGRP,
361  PERM_OTHER_MASK = S_IRWXO,
362  PERM_OTHER_READ = S_IROTH,
363  PERM_OTHER_WRITE = S_IWOTH,
365  };
366 
369 
375  stat (const char *file,
376  bool link = false);
377 
383  stat (std::string const& file,
384  bool link = false);
385 
392  stat (std::string const& file,
393  int fd);
394 
399  stat (int fd);
400 
402  virtual ~stat ();
403 
409  void check () const
410  {
411  if (this->errorno)
412  {
413  if (!this->file.empty())
414  throw error(this->file, FILE, std::strerror(this->errorno));
415  else
416  {
417  std::ostringstream str;
418  str << "fd " << fd;
419  throw error(str.str(), FD, std::strerror(this->errorno));
420  }
421  }
422  }
423 
429  struct ::stat const& get_detail()
430  { return this->status; }
431 
436  dev_t
437  device () const
438  { check(); return status.st_dev; }
439 
444  ino_t
445  inode () const
446  { check(); return status.st_ino; }
447 
452  mode_t
453  mode () const
454  { check(); return status.st_mode; }
455 
460  nlink_t
461  links () const
462  { check(); return status.st_nlink; }
463 
468  uid_t
469  uid () const
470  { check(); return status.st_uid; }
471 
476  gid_t
477  gid () const
478  { check(); return status.st_gid; }
479 
484  off_t
485  size () const
486  { check(); return status.st_size; }
487 
492  blksize_t
493  blocksize () const
494  { check(); return status.st_blksize; }
495 
500  blkcnt_t
501  blocks () const
502  { check(); return status.st_blocks; }
503 
508  time_t
509  atime () const
510  { check(); return status.st_atime; }
511 
516  time_t
517  mtime () const
518  { check(); return status.st_mtime; }
519 
524  time_t
525  ctime () const
526  { check(); return status.st_ctime; }
527 
532  inline bool
533  is_regular () const;
534 
539  inline bool
540  is_directory () const;
541 
546  inline bool
547  is_character () const;
548 
553  inline bool
554  is_block () const;
555 
560  inline bool
561  is_fifo () const;
562 
567  inline bool
568  is_link () const;
569 
574  inline bool
575  is_socket () const;
576 
582  inline bool check_mode (mode_bits mask) const;
583 
584  private:
585 
587  std::string file;
589  int fd;
591  int errorno;
593  struct ::stat status;
594  };
595 
603  inline operator | (stat::mode_bits const& lhs,
604  stat::mode_bits const& rhs)
605  {
606  return static_cast<stat::mode_bits>
607  (static_cast<int>(lhs) | static_cast<int>(rhs));
608  }
609 
617  inline operator | (mode_t const& lhs,
618  stat::mode_bits const& rhs)
619  {
620  return static_cast<stat::mode_bits>
621  (lhs | static_cast<int>(rhs));
622  }
623 
631  inline operator | (stat::mode_bits const& lhs,
632  mode_t const& rhs)
633  {
634  return static_cast<stat::mode_bits>
635  (static_cast<int>(lhs) | rhs);
636  }
637 
645  inline operator & (stat::mode_bits const& lhs,
646  stat::mode_bits const& rhs)
647  {
648  return static_cast<stat::mode_bits>
649  (static_cast<int>(lhs) & static_cast<int>(rhs));
650  }
651 
659  inline operator & (mode_t const& lhs,
660  stat::mode_bits const& rhs)
661  {
662  return static_cast<stat::mode_bits>
663  (lhs & static_cast<int>(rhs));
664  }
665 
673  inline operator & (stat::mode_bits const& lhs,
674  mode_t const& rhs)
675  {
676  return static_cast<stat::mode_bits>
677  (static_cast<int>(lhs) & rhs);
678  }
679 
680  inline bool
683 
684  inline bool
687 
688  inline bool
691 
692  inline bool
693  stat::is_block () const
695 
696  inline bool
697  stat::is_fifo () const
699 
700  inline bool
701  stat::is_link () const
703 
704  inline bool
707 
708  inline bool
710  {
711  check();
712  return (static_cast<stat::mode_bits>(status.st_mode) & mask) == mask;
713  }
714 
718  class passwd : public ::passwd
719  {
720  public:
722  typedef std::vector<char> buffer_type;
723 
725  passwd ();
726 
732  passwd (uid_t uid);
733 
739  passwd (const char *name);
740 
746  passwd (std::string const& name);
747 
752  void
753  clear ();
754 
760  void
761  query_uid (uid_t uid);
762 
768  void
769  query_name (const char *name);
770 
776  void
777  query_name (std::string const& name);
778 
782  bool
783  operator ! () const;
784 
785  private:
789  bool valid;
790  };
791 
795  class group : public ::group
796  {
797  public:
799  typedef std::vector<char> buffer_type;
800 
802  group ();
803 
809  group (gid_t gid);
810 
816  group (const char *name);
817 
823  group (std::string const& name);
824 
829  void
830  clear ();
831 
837  void
838  query_gid (gid_t gid);
839 
845  void
846  query_name (const char *name);
847 
853  void
854  query_name (std::string const& name);
855 
859  bool
860  operator ! () const;
861 
862  private:
866  bool valid;
867  };
868 
869 }
870 
871 #endif /* SBUILD_UTIL_H */
872 
873 /*
874  * Local Variables:
875  * mode:C++
876  * End:
877  */