sbuild  1.6.0
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 
374  stat (const char *file);
375 
380  stat (std::string const& file);
381 
388  stat (std::string const& file,
389  int fd);
390 
395  stat (int fd);
396 
398  virtual ~stat ();
399 
405  void check () const
406  {
407  if (this->errorno)
408  {
409  if (!this->file.empty())
410  throw error(this->file, FILE, std::strerror(this->errorno));
411  else
412  {
413  std::ostringstream str;
414  str << "fd " << fd;
415  throw error(str.str(), FD, std::strerror(this->errorno));
416  }
417  }
418  }
419 
425  struct ::stat const& get_detail()
426  { return this->status; }
427 
432  dev_t
433  device () const
434  { check(); return status.st_dev; }
435 
440  ino_t
441  inode () const
442  { check(); return status.st_ino; }
443 
448  mode_t
449  mode () const
450  { check(); return status.st_mode; }
451 
456  nlink_t
457  links () const
458  { check(); return status.st_nlink; }
459 
464  uid_t
465  uid () const
466  { check(); return status.st_uid; }
467 
472  gid_t
473  gid () const
474  { check(); return status.st_gid; }
475 
480  off_t
481  size () const
482  { check(); return status.st_size; }
483 
488  blksize_t
489  blocksize () const
490  { check(); return status.st_blksize; }
491 
496  blkcnt_t
497  blocks () const
498  { check(); return status.st_blocks; }
499 
504  time_t
505  atime () const
506  { check(); return status.st_atime; }
507 
512  time_t
513  mtime () const
514  { check(); return status.st_mtime; }
515 
520  time_t
521  ctime () const
522  { check(); return status.st_ctime; }
523 
528  inline bool
529  is_regular () const;
530 
535  inline bool
536  is_directory () const;
537 
542  inline bool
543  is_character () const;
544 
549  inline bool
550  is_block () const;
551 
556  inline bool
557  is_fifo () const;
558 
563  inline bool
564  is_link () const;
565 
570  inline bool
571  is_socket () const;
572 
578  inline bool check_mode (mode_bits mask) const;
579 
580  private:
581 
583  std::string file;
585  int fd;
587  int errorno;
589  struct ::stat status;
590  };
591 
599  inline operator | (stat::mode_bits const& lhs,
600  stat::mode_bits const& rhs)
601  {
602  return static_cast<stat::mode_bits>
603  (static_cast<int>(lhs) | static_cast<int>(rhs));
604  }
605 
613  inline operator | (mode_t const& lhs,
614  stat::mode_bits const& rhs)
615  {
616  return static_cast<stat::mode_bits>
617  (lhs | static_cast<int>(rhs));
618  }
619 
627  inline operator | (stat::mode_bits const& lhs,
628  mode_t const& rhs)
629  {
630  return static_cast<stat::mode_bits>
631  (static_cast<int>(lhs) | rhs);
632  }
633 
641  inline operator & (stat::mode_bits const& lhs,
642  stat::mode_bits const& rhs)
643  {
644  return static_cast<stat::mode_bits>
645  (static_cast<int>(lhs) & static_cast<int>(rhs));
646  }
647 
655  inline operator & (mode_t const& lhs,
656  stat::mode_bits const& rhs)
657  {
658  return static_cast<stat::mode_bits>
659  (lhs & static_cast<int>(rhs));
660  }
661 
669  inline operator & (stat::mode_bits const& lhs,
670  mode_t const& rhs)
671  {
672  return static_cast<stat::mode_bits>
673  (static_cast<int>(lhs) & rhs);
674  }
675 
676  inline bool
679 
680  inline bool
683 
684  inline bool
687 
688  inline bool
689  stat::is_block () const
691 
692  inline bool
693  stat::is_fifo () const
695 
696  inline bool
697  stat::is_link () const
699 
700  inline bool
703 
704  inline bool
706  {
707  check();
708  return (static_cast<stat::mode_bits>(status.st_mode) & mask) == mask;
709  }
710 
714  class passwd : public ::passwd
715  {
716  public:
718  typedef std::vector<char> buffer_type;
719 
721  passwd ();
722 
728  passwd (uid_t uid);
729 
735  passwd (const char *name);
736 
742  passwd (std::string const& name);
743 
748  void
749  clear ();
750 
756  void
757  query_uid (uid_t uid);
758 
764  void
765  query_name (const char *name);
766 
772  void
773  query_name (std::string const& name);
774 
778  bool
779  operator ! () const;
780 
781  private:
785  bool valid;
786  };
787 
791  class group : public ::group
792  {
793  public:
795  typedef std::vector<char> buffer_type;
796 
798  group ();
799 
805  group (gid_t gid);
806 
812  group (const char *name);
813 
819  group (std::string const& name);
820 
825  void
826  clear ();
827 
833  void
834  query_gid (gid_t gid);
835 
841  void
842  query_name (const char *name);
843 
849  void
850  query_name (std::string const& name);
851 
855  bool
856  operator ! () const;
857 
858  private:
862  bool valid;
863  };
864 
865 }
866 
867 #endif /* SBUILD_UTIL_H */
868 
869 /*
870  * Local Variables:
871  * mode:C++
872  * End:
873  */