sbuild  1.6.10
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:
787  buffer_type buffer;
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:
864  buffer_type buffer;
866  bool valid;
867  };
868 
869 }
870 
871 #endif /* SBUILD_UTIL_H */
872 
873 /*
874  * Local Variables:
875  * mode:C++
876  * End:
877  */
virtual ~stat()
The destructor.
Definition: sbuild-util.cc:566
Other execute permission.
Definition: sbuild-util.h:364
custom_error< error_code > error
Exception type.
Definition: sbuild-util.h:368
mode_bits
Mode bits.
Definition: sbuild-util.h:340
dev_t device() const
Get the device the file resides on.
Definition: sbuild-util.h:437
Named pipe (FIFO) file type.
Definition: sbuild-util.h:349
time_t mtime() const
Get the file modification time.
Definition: sbuild-util.h:517
Directory file type.
Definition: sbuild-util.h:347
std::wstring widen_string(std::string const &str, std::locale locale)
Widen a string.
Definition: sbuild-util.cc:332
std::string unique_identifier()
Get a unique string for use as a session identifier.
Definition: sbuild-util.cc:235
Failed to stat file descriptor.
Definition: sbuild-util.h:336
Symbolic link file type.
Definition: sbuild-util.h:344
bool is_character() const
Is the file a character device?
Definition: sbuild-util.h:689
struct::stat const & get_detail()
Get the struct stat used internally.
Definition: sbuild-util.h:429
Mask for user permissions.
Definition: sbuild-util.h:353
Debian source builder components.
Definition: sbuild-auth-null.h:24
int exec(std::string const &file, string_list const &command, environment const &env)
execve wrapper.
Definition: sbuild-util.cc:490
bool is_block() const
Is the file a block device?
Definition: sbuild-util.h:693
std::string getcwd()
Get the current working directory.
Definition: sbuild-util.cc:220
std::string basename(std::string name)
Strip the directory path from a filename.
Definition: sbuild-util.cc:100
std::vector< char > buffer_type
A buffer for reentrant passwd functions.
Definition: sbuild-util.h:722
mode_t mode() const
Get the mode of the file.
Definition: sbuild-util.h:453
chroot::session_flags operator&(chroot::session_flags const &lhs, chroot::session_flags const &rhs)
Bitwise-AND of specifed session properties.
Definition: sbuild-chroot.h:860
void check() const
Check if the file status was obtained.
Definition: sbuild-util.h:409
User execute permission.
Definition: sbuild-util.h:356
bool is_regular() const
Is the file a regular file?
Definition: sbuild-util.h:681
Block device file type.
Definition: sbuild-util.h:346
Character device file type.
Definition: sbuild-util.h:348
Container of environment variables.
Definition: sbuild-environment.h:38
Sticky permission.
Definition: sbuild-util.h:352
blksize_t blocksize() const
Get the file block size.
Definition: sbuild-util.h:493
bool check_mode(mode_bits mask) const
Check if particular mode bits are set.
Definition: sbuild-util.h:709
buffer_type buffer
Query result buffer.
Definition: sbuild-util.h:864
Set user ID permission.
Definition: sbuild-util.h:350
Get file status.
Definition: sbuild-util.h:329
nlink_t links() const
Get the number of hard links to the file.
Definition: sbuild-util.h:461
std::vector< S > split_string(S const &value, S const &separator)
Split a string into a string_list.
Definition: sbuild-util.h:152
time_t atime() const
Get the file access time.
Definition: sbuild-util.h:509
Socket file type.
Definition: sbuild-util.h:343
struct::stat status
The stat(2) results.
Definition: sbuild-util.h:593
chroot::session_flags operator|(chroot::session_flags const &lhs, chroot::session_flags const &rhs)
Bitwise-OR of specifed session properties.
Definition: sbuild-chroot.h:846
bool valid
Object validity.
Definition: sbuild-util.h:789
Other write permission.
Definition: sbuild-util.h:363
std::string file
The filename being checked (if specified).
Definition: sbuild-util.h:587
Mask for group permissions.
Definition: sbuild-util.h:357
bool is_valid_filename(std::string const &name, bool lsb_mode=true)
Check if a filename matches the allowed pattern(s).
Definition: sbuild-util.cc:191
bool is_directory() const
Is the file a directory?
Definition: sbuild-util.h:685
std::vector< std::string > string_list
A string vector.
Definition: sbuild-types.h:38
error_code
Error codes.
Definition: sbuild-util.h:333
Custom error.
Definition: sbuild-custom-error.h:32
Group execute permission.
Definition: sbuild-util.h:360
off_t size() const
Get the file size.
Definition: sbuild-util.h:485
bool is_absname(std::string const &name)
Check if a pathname is absolute.
Definition: sbuild-util.cc:164
Group write permission.
Definition: sbuild-util.h:359
System group database entry.
Definition: sbuild-util.h:795
uid_t uid() const
Get the user id owning the file.
Definition: sbuild-util.h:469
Mask for other permissions.
Definition: sbuild-util.h:361
bool is_socket() const
Is the file a socket?
Definition: sbuild-util.h:705
stat(const char *file, bool link=false)
The constructor.
Definition: sbuild-util.cc:507
int fd
The file descriptor being checked (if specified).
Definition: sbuild-util.h:589
bool is_fifo() const
Is the file a named pipe (FIFO)?
Definition: sbuild-util.h:697
Group read permission.
Definition: sbuild-util.h:358
std::string normalname(std::string name)
Normalise a pathname.
Definition: sbuild-util.cc:150
blkcnt_t blocks() const
Get the file block count.
Definition: sbuild-util.h:501
void strv_delete(char **strv)
Delete a string vector.
Definition: sbuild-util.cc:482
std::string dirname(std::string name)
Strip the fileame from a pathname.
Definition: sbuild-util.cc:125
bool is_valid_sessionname(std::string const &name)
Check if a filename matches the allowed pattern(s).
Definition: sbuild-util.cc:173
ino_t inode() const
Get the inode of the file.
Definition: sbuild-util.h:445
std::vector< S > split_string_strict(S const &value, S const &separator)
Split a string into a string_list.
Definition: sbuild-util.h:206
std::string narrow_string(std::wstring const &str, std::locale locale)
Narrow a string.
Definition: sbuild-util.cc:379
buffer_type buffer
Query result buffer.
Definition: sbuild-util.h:787
std::vector< char > buffer_type
A buffer for reentrant group functions.
Definition: sbuild-util.h:799
System passwd database entry.
Definition: sbuild-util.h:718
char ** string_list_to_strv(string_list const &str)
Create a string vector from a string_list.
Definition: sbuild-util.cc:464
Mask for file type bit fields.
Definition: sbuild-util.h:342
int errorno
The error number set after stat(2) error.
Definition: sbuild-util.h:591
std::string string_list_to_string(string_list const &list, std::string const &separator)
Convert a string_list into a string.
Definition: sbuild-util.cc:256
User write permission.
Definition: sbuild-util.h:355
Regular file type.
Definition: sbuild-util.h:345
std::string find_program_in_path(std::string const &program, std::string const &path, std::string const &prefix)
Find a program in the PATH search path.
Definition: sbuild-util.cc:426
gid_t gid() const
Get the group id owning the file.
Definition: sbuild-util.h:477
User read permission.
Definition: sbuild-util.h:354
bool valid
Object validity.
Definition: sbuild-util.h:866
time_t ctime() const
Get the file creation time.
Definition: sbuild-util.h:525
Set group ID permission.
Definition: sbuild-util.h:351
bool is_link() const
Is the file a symbolic link?
Definition: sbuild-util.h:701
Failed to stat file.
Definition: sbuild-util.h:335
Other read permission.
Definition: sbuild-util.h:362