sbuild-util.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2007  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software: you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation, either version 3 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>.
00016  *
00017  *********************************************************************/
00018 
00019 #ifndef SBUILD_UTIL_H
00020 #define SBUILD_UTIL_H
00021 
00022 #include <sbuild/sbuild-environment.h>
00023 #include <sbuild/sbuild-error.h>
00024 #include <sbuild/sbuild-types.h>
00025 
00026 #include <string>
00027 
00028 #include <sys/types.h>
00029 #include <sys/stat.h>
00030 #include <unistd.h>
00031 
00032 namespace sbuild
00033 {
00034 
00043   std::string
00044   basename (std::string name,
00045             char        separator = '/');
00046 
00055   std::string
00056   dirname (std::string name,
00057            char        separator = '/');
00058 
00067   std::string
00068   normalname (std::string name,
00069               char        separator = '/');
00070 
00078   bool
00079   is_absname (std::string const& name);
00080 
00089   std::string
00090   string_list_to_string (string_list const& list,
00091                          std::string const& separator);
00092 
00104   string_list
00105   split_string (std::string const& value,
00106                 std::string const& separator);
00107 
00117   std::wstring
00118   widen_string (std::string const& str,
00119                 std::locale        locale);
00120 
00130   std::string
00131   narrow_string (std::wstring const& str,
00132                  std::locale         locale);
00133 
00144   std::string
00145   find_program_in_path (std::string const& program,
00146                         std::string const& path,
00147                         std::string const& prefix);
00148 
00156   char **
00157   string_list_to_strv (string_list const& str);
00158 
00166   void
00167   strv_delete (char **strv);
00168 
00179   int
00180   exec (std::string const& file,
00181         string_list const& command,
00182         environment const& env);
00183 
00187   class stat
00188   {
00189   public:
00191     enum error_code
00192       {
00193         FILE, 
00194         FD    
00195       };
00196 
00198     enum mode_bits
00199       {
00200         FILE_TYPE_MASK      = S_IFMT,   
00201         FILE_TYPE_SOCKET    = S_IFSOCK, 
00202         FILE_TYPE_LINK      = S_IFLNK,  
00203         FILE_TYPE_REGULAR   = S_IFREG,  
00204         FILE_TYPE_BLOCK     = S_IFBLK,  
00205         FILE_TYPE_DIRECTORY = S_IFDIR,  
00206         FILE_TYPE_CHARACTER = S_IFCHR,  
00207         FILE_TYPE_FIFO      = S_IFIFO,  
00208         PERM_SETUID         = S_ISUID,  
00209         PERM_SETGIT         = S_ISGID,  
00210         PERM_STICKY         = S_ISVTX,  
00211         PERM_USER_MASK      = S_IRWXU,  
00212         PERM_USER_READ      = S_IRUSR,  
00213         PERM_USER_WRITE     = S_IWUSR,  
00214         PERM_USER_EXECUTE   = S_IXUSR,  
00215         PERM_GROUP_MASK     = S_IRWXG,  
00216         PERM_GROUP_READ     = S_IRGRP,  
00217         PERM_GROUP_WRITE    = S_IWGRP,  
00218         PERM_GROUP_EXECUTE  = S_IXGRP,  
00219         PERM_OTHER_MASK     = S_IRWXO,  
00220         PERM_OTHER_READ     = S_IROTH,  
00221         PERM_OTHER_WRITE    = S_IWOTH,  
00222         PERM_OTHER_EXECUTE  = S_IXOTH   
00223       };
00224 
00226     typedef custom_error<error_code> error;
00227 
00232     stat (std::string const& file);
00233 
00240     stat (std::string const& file,
00241           int                fd);
00242 
00247     stat (int fd);
00248 
00250     virtual ~stat ();
00251 
00257     void check () const
00258     {
00259       if (this->errorno)
00260         {
00261           if (!this->file.empty())
00262             throw error(this->file, FILE, strerror(this->errorno));
00263           else
00264             {
00265               std::ostringstream str;
00266               str << "fd " << fd;
00267               throw error(str.str(), FD, strerror(this->errorno));
00268             }
00269         }
00270     }
00271 
00277     struct ::stat const& get_detail()
00278     { return this->status; }
00279 
00284     dev_t
00285     device () const
00286     { check(); return status.st_dev; }
00287 
00292     ino_t
00293     inode () const
00294     { check(); return status.st_ino; }
00295 
00300     mode_t
00301     mode () const
00302     { check(); return status.st_mode; }
00303 
00308     nlink_t
00309     links () const
00310     { check(); return status.st_nlink; }
00311 
00316     uid_t
00317     uid () const
00318     { check(); return status.st_uid; }
00319 
00324     gid_t
00325     gid () const
00326     { check(); return status.st_gid; }
00327 
00332     off_t
00333     size () const
00334     { check(); return status.st_size; }
00335 
00340     blksize_t
00341     blocksize () const
00342     { check(); return status.st_blksize; }
00343 
00348     blkcnt_t
00349     blocks () const
00350     { check(); return status.st_blocks; }
00351 
00356     time_t
00357     atime () const
00358     { check(); return status.st_atime; }
00359 
00364     time_t
00365     mtime () const
00366     { check(); return status.st_mtime; }
00367 
00372     time_t
00373     ctime () const
00374     { check(); return status.st_ctime; }
00375 
00380     inline bool
00381     is_regular () const;
00382 
00387     inline bool
00388     is_directory () const;
00389 
00394     inline bool
00395     is_character () const;
00396 
00401     inline bool
00402     is_block () const;
00403 
00408     inline bool
00409     is_fifo () const;
00410 
00415     inline bool
00416     is_link () const;
00417 
00422     inline bool
00423     is_socket () const;
00424 
00430     inline bool check_mode (mode_bits mask) const;
00431 
00432   private:
00433 
00435     std::string file;
00437     int fd;
00439     int errorno;
00441     struct ::stat status;
00442   };
00443 
00450   stat::mode_bits
00451   inline operator | (stat::mode_bits const& lhs,
00452               stat::mode_bits const& rhs)
00453   {
00454     return static_cast<stat::mode_bits>
00455       (static_cast<int>(lhs) | static_cast<int>(rhs));
00456   }
00457 
00464   stat::mode_bits
00465   inline operator | (mode_t const&          lhs,
00466               stat::mode_bits const& rhs)
00467   {
00468     return static_cast<stat::mode_bits>
00469       (lhs | static_cast<int>(rhs));
00470   }
00471 
00478   stat::mode_bits
00479   inline operator | (stat::mode_bits const& lhs,
00480               mode_t const&          rhs)
00481   {
00482     return static_cast<stat::mode_bits>
00483       (static_cast<int>(lhs) | rhs);
00484   }
00485 
00492   stat::mode_bits
00493   inline operator & (stat::mode_bits const& lhs,
00494               stat::mode_bits const& rhs)
00495   {
00496     return static_cast<stat::mode_bits>
00497       (static_cast<int>(lhs) & static_cast<int>(rhs));
00498   }
00499 
00506   stat::mode_bits
00507   inline operator & (mode_t const&          lhs,
00508               stat::mode_bits const& rhs)
00509   {
00510     return static_cast<stat::mode_bits>
00511       (lhs & static_cast<int>(rhs));
00512   }
00513 
00520   stat::mode_bits
00521   inline operator & (stat::mode_bits const& lhs,
00522               mode_t const&          rhs)
00523   {
00524     return static_cast<stat::mode_bits>
00525       (static_cast<int>(lhs) & rhs);
00526   }
00527 
00528   inline bool
00529   stat::is_regular () const
00530   { return check_mode(FILE_TYPE_REGULAR & FILE_TYPE_MASK); }
00531 
00532   inline bool
00533   stat::is_directory () const
00534   { return check_mode(FILE_TYPE_DIRECTORY & FILE_TYPE_MASK); }
00535 
00536   inline bool
00537   stat::is_character () const
00538   { return check_mode(FILE_TYPE_CHARACTER & FILE_TYPE_MASK); }
00539 
00540   inline bool
00541   stat::is_block () const
00542   { return check_mode(FILE_TYPE_BLOCK & FILE_TYPE_MASK); }
00543 
00544   inline bool
00545   stat::is_fifo () const
00546   { return check_mode(FILE_TYPE_FIFO & FILE_TYPE_MASK); }
00547 
00548   inline bool
00549   stat::is_link () const
00550   { return check_mode(FILE_TYPE_LINK & FILE_TYPE_MASK); }
00551 
00552   inline bool
00553   stat::is_socket () const
00554   { return check_mode(FILE_TYPE_SOCKET & FILE_TYPE_MASK); }
00555 
00556   inline bool
00557   stat::check_mode (mode_bits mask) const
00558   {
00559     check();
00560     return (static_cast<stat::mode_bits>(status.st_mode) & mask) == mask;
00561   }
00562 
00563 }
00564 
00565 #endif /* SBUILD_UTIL_H */
00566 
00567 /*
00568  * Local Variables:
00569  * mode:C++
00570  * End:
00571  */

Generated on Sun Mar 22 22:18:14 2009 for sbuild by  doxygen 1.5.8