From: Thomas Jarosch Date: Mon, 7 Apr 2008 13:17:07 +0000 (+0000) Subject: libi2ncommon: (tomj) modified filefunc according to our new style guide. X-Git-Tag: v2.6~189 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=a287a3060aae9ab81b6c9668e8f2dfb246602cac;p=libi2ncommon libi2ncommon: (tomj) modified filefunc according to our new style guide. --- diff --git a/src/daemonfunc.cpp b/src/daemonfunc.cpp index 274c6a5..5f874ef 100644 --- a/src/daemonfunc.cpp +++ b/src/daemonfunc.cpp @@ -14,7 +14,10 @@ #include "stringfunc.hxx" #include "filefunc.hxx" -namespace i2n +// TODO: Remove me once libi2ncommon is completly switched to I2n +using namespace i2n; + +namespace I2n { namespace daemon { @@ -82,7 +85,7 @@ bool pid_of(const std::string& name, std::vector< pid_t >& result) std::vector< pid_t > fuzz1_result; std::vector< pid_t > fuzz2_result; result.clear(); - if (!i2n::getDir("/proc", entries)) return false; + if (!get_dir("/proc", entries)) return false; for(std::vector< std::string >::const_iterator it= entries.begin(); it != entries.end(); ++it) @@ -91,16 +94,16 @@ bool pid_of(const std::string& name, std::vector< pid_t >& result) if (! stringTo(*it, pid)) continue; std::string base_path= std::string("/proc/") + *it; std::string exe_path= base_path + "/exe"; - Stat stat(exe_path, false); - if (not stat or not stat.isLink()) continue; - std::string real_exe= readLink(exe_path); + I2n::Stat stat(exe_path, false); + if (not stat or not stat.is_link()) continue; + std::string real_exe= read_link(exe_path); if (real_exe == name) { result.push_back( pid ); continue; } - std::string proc_stat= readFile( base_path + "/stat"); + std::string proc_stat= read_file( base_path + "/stat"); if (proc_stat.empty()) continue; // process vanished //TODO some more fuzz tests here?! (cmdline, stat(us)) diff --git a/src/filefunc.cpp b/src/filefunc.cpp index 47e9838..a37d0ae 100644 --- a/src/filefunc.cpp +++ b/src/filefunc.cpp @@ -25,7 +25,10 @@ #include "filefunc.hxx" #include "stringfunc.hxx" -namespace i2n +// TODO: Remove me once libi2ncommon is completly switched to I2n +using namespace i2n; + +namespace I2n { using namespace std; @@ -35,9 +38,9 @@ using namespace std; */ Stat::Stat() -: m_valid(false) +: Valid(false) { - clear(); + clear(); } // eo Stat::Stat() @@ -59,9 +62,9 @@ Stat::~Stat() */ void Stat::recheck() { - if (! m_path.empty()) + if (! Path.empty()) { - stat(m_path, m_follow_links); + stat(Path, FollowLinks); } } // eo Stat::recheck() @@ -75,34 +78,34 @@ void Stat::recheck() void Stat::stat(const std::string& path, bool follow_links) { clear(); - m_path= path; - m_follow_links= follow_links; + Path= path; + FollowLinks= follow_links; struct stat stat_info; int res; res = ( follow_links ? ::stat(path.c_str(), &stat_info) : ::lstat(path.c_str(), &stat_info) ); if ( 0 == res ) { - m_device = stat_info.st_dev; - m_inode = stat_info.st_ino; - m_mode = (stat_info.st_mode & ~(S_IFMT)); - m_num_links = stat_info.st_nlink; - m_uid = stat_info.st_uid; - m_gid = stat_info.st_gid; - m_device_type = stat_info.st_rdev; - m_size = stat_info.st_size; - m_atime = stat_info.st_atime; - m_mtime = stat_info.st_mtime; - m_ctime = stat_info.st_atime; - - m_is_link= S_ISLNK( stat_info.st_mode ); - m_is_regular= S_ISREG( stat_info.st_mode ); - m_is_directory= S_ISDIR( stat_info.st_mode ); - m_is_character_device= S_ISCHR( stat_info.st_mode ); - m_is_block_device= S_ISBLK( stat_info.st_mode ); - m_is_fifo= S_ISFIFO( stat_info.st_mode ); - m_is_socket= S_ISSOCK( stat_info.st_mode ); + Device = stat_info.st_dev; + Inode = stat_info.st_ino; + Mode = (stat_info.st_mode & ~(S_IFMT)); + NumLinks = stat_info.st_nlink; + Uid = stat_info.st_uid; + Gid = stat_info.st_gid; + DeviceType = stat_info.st_rdev; + Size = stat_info.st_size; + Atime = stat_info.st_atime; + Mtime = stat_info.st_mtime; + Ctime = stat_info.st_atime; + + IsLink= S_ISLNK( stat_info.st_mode ); + IsRegular= S_ISREG( stat_info.st_mode ); + IsDirectory= S_ISDIR( stat_info.st_mode ); + IsCharacterDevice= S_ISCHR( stat_info.st_mode ); + IsBlockDevice= S_ISBLK( stat_info.st_mode ); + IsFifo= S_ISFIFO( stat_info.st_mode ); + IsSocket= S_ISSOCK( stat_info.st_mode ); } - m_valid = (0 == res); + Valid = (0 == res); } // eo Stat::stat(const std::string&,bool) @@ -111,28 +114,28 @@ void Stat::stat(const std::string& path, bool follow_links) */ void Stat::clear() { - m_path.clear(); - m_valid= false; - - m_device = 0; - m_inode = 0; - m_mode = 0; - m_num_links = 0; - m_uid = 0; - m_gid = 0; - m_device_type = 0; - m_size = 0; - m_atime = 0; - m_mtime = 0; - m_ctime = 0; - - m_is_link= false; - m_is_regular= false; - m_is_directory= false; - m_is_character_device= false; - m_is_block_device= false; - m_is_fifo= false; - m_is_socket= false; + Path.clear(); + Valid= false; + + Device = 0; + Inode = 0; + Mode = 0; + NumLinks = 0; + Uid = 0; + Gid = 0; + DeviceType = 0; + Size = 0; + Atime = 0; + Mtime = 0; + Ctime = 0; + + IsLink= false; + IsRegular= false; + IsDirectory= false; + IsCharacterDevice= false; + IsBlockDevice= false; + IsFifo= false; + IsSocket= false; } // eo Stat::clear() @@ -144,12 +147,12 @@ void Stat::clear() * The "same file" means that the files are located on the same device and use the same inode. * They might still have two different directory entries (different paths)! */ -bool Stat::isSameAs(const Stat& rhs) +bool Stat::is_same_as(const Stat& rhs) { - return m_valid and rhs.m_valid - and ( m_device == rhs.m_device) - and ( m_inode == rhs.m_inode); -} // eo Stat::isSameAs(const Stat& rhs); + return Valid and rhs.Valid + and ( Device == rhs.Device) + and ( Inode == rhs.Inode); +} // eo Stat::is_same_as(const Stat& rhs); /** @@ -159,13 +162,13 @@ bool Stat::isSameAs(const Stat& rhs) * * "Same device" means that the devices have the same type and the same major and minor id. */ -bool Stat::isSameDeviceAs(const Stat& rhs) +bool Stat::is_same_device_as(const Stat& rhs) { - return isDevice() and rhs.isDevice() - and ( m_is_block_device == rhs.m_is_block_device ) - and ( m_is_character_device == rhs.m_is_character_device ) - and ( m_device_type == rhs.m_device_type); -} // eo Stat::isSameDeviceAs(const Stat&) + return is_device() and rhs.is_device() + and ( IsBlockDevice == rhs.IsBlockDevice ) + and ( IsCharacterDevice == rhs.IsCharacterDevice ) + and ( DeviceType == rhs.DeviceType); +} // eo Stat::is_same_device_as(const Stat&) /** * @brief check existence of a path. @@ -223,7 +226,7 @@ long file_size (const string &name) * @param path path which should be tested. * @return the last modification time or 0 if the path doen't exist. */ -time_t fileMTime(const std::string& path) +time_t file_mtime(const std::string& path) { struct stat stat_info; int res = ::stat(path.c_str(), &stat_info); @@ -239,7 +242,7 @@ time_t fileMTime(const std::string& path) * @param include_dot_names determines if dot-files should be included in the list. * @return @a true if reading the directory was succesful, @a false on error. */ -bool getDir( +bool get_dir( const std::string& path, std::vector< std::string >& result, bool include_dot_names ) @@ -270,10 +273,10 @@ bool getDir( * @param include_dot_names determines if dot-files should be included in the list. * @return the list of names (empty on error). */ -std::vector< std::string > getDir(const std::string& path, bool include_dot_names ) +std::vector< std::string > get_dir(const std::string& path, bool include_dot_names ) { std::vector< std::string > result; - getDir(path,result,include_dot_names); + get_dir(path,result,include_dot_names); return result; } // eo getDir(const std::string&,bool) @@ -321,8 +324,8 @@ bool symlink(const std::string& target, const std::string& link_name, bool force Stat link_name_stat(link_name, false); if (target_stat.exists() && link_name_stat.exists()) { - if (link_name_stat.isSameAs(target_stat) - or link_name_stat.isSameDeviceAs(target_stat) ) + if (link_name_stat.is_same_as(target_stat) + or link_name_stat.is_same_device_as(target_stat) ) { return false; } @@ -356,11 +359,11 @@ bool symlink(const std::string& target, const std::string& link_name, bool force * @param path path to the symbolic link * @return the target of the link or an empty string on error. */ -std::string readLink(const std::string& path) +std::string read_link(const std::string& path) { errno= 0; Stat stat(path,false); - if (!stat || !stat.isLink()) + if (!stat || !stat.is_link()) { return std::string(); } @@ -386,10 +389,10 @@ std::string readLink(const std::string& path) * @param path path to the file. * @return the content of the file as string (empty if file could be opened). */ -std::string readFile(const std::string& path) +std::string read_file(const std::string& path) { Stat stat(path); - if (!stat.isReg()) + if (!stat.is_reg()) { return std::string(); } @@ -424,7 +427,7 @@ std::string readFile(const std::string& path) * * A simple (q'n'd) function for writing a string to a file. */ -bool writeFile(const std::string& path, const std::string& data) +bool write_file(const std::string& path, const std::string& data) { std::ofstream f( path.c_str(), std::ios::out | std::ios::binary | std::ios::trunc); if (f.good()) @@ -488,7 +491,7 @@ std::string dirname(const std::string& path) * @return the normalized path. */ -std::string normalizePath(const std::string& path) +std::string normalize_path(const std::string& path) { if (path.empty()) { @@ -576,12 +579,12 @@ bool chmod(const std::string& path, int mode) * the validity of user and group within the system is not checked. * This is intentional since this way we can use id's which are not assigned. */ -bool chown(const std::string& path, const i2n::User& user, const i2n::Group& group) +bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group) { - uid_t uid= user.uid; + uid_t uid= user.Uid; if (uid<0) return false; - gid_t gid= group.gid; - if (gid<0) gid= user.gid; + gid_t gid= group.Gid; + if (gid<0) gid= user.Gid; if (gid<0) return false; int res= ::chown( path.c_str(), uid, gid); return (res==0); diff --git a/src/filefunc.hxx b/src/filefunc.hxx index 0b62cbb..b2216af 100644 --- a/src/filefunc.hxx +++ b/src/filefunc.hxx @@ -9,9 +9,8 @@ #include "userfunc.hxx" -namespace i2n +namespace I2n { - /** * @brief helper class representing a file state. * @@ -19,106 +18,105 @@ namespace i2n */ class Stat { - public: - Stat(); - Stat(const std::string& path, bool follow_links= true); - ~Stat(); - - void recheck(); - - bool isValid() const { return m_valid; } - - bool exists() const { return m_valid; } - std::string path() const { return m_path; } - - dev_t device() const { return m_device; } - ino_t inode() const { return m_inode; } - mode_t mode() const { return m_mode; } - nlink_t nlink() const { return m_num_links; } - uid_t uid() const { return m_uid; } - gid_t gid() const { return m_gid; } - dev_t rdev() const { return m_device_type; } - off_t size() const { return m_size; } - time_t atime() const { return m_atime; } - time_t mtime() const { return m_mtime; } - time_t ctime() const { return m_ctime; } - - nlink_t numHardLinks() const { return m_num_links; } - dev_t deviceType() const { return m_device_type; } - time_t lastModifiedTime() const { return m_mtime; } - time_t createdTime() const { return m_ctime; } - - /* - ** unix style like type queries: - */ - - bool isLnk() const { return m_is_link; } - bool isReg() const { return m_is_regular; } - bool isDir() const { return m_is_directory; } - bool isChr() const { return m_is_character_device; } - bool isBlk() const { return m_is_block_device; } - bool isFifo() const { return m_is_fifo; } - bool isSock() const { return m_is_socket; } - - /* - ** readable style type queries: - */ - - bool isLink () const { return m_is_link; } - bool isRegular() const { return m_is_regular; } - bool isRegularFile() const { return m_is_regular; } - bool isDirectory() const { return m_is_directory; } - bool isCharacterDevice() const {return m_is_character_device; } - bool isBlockDevice() const { return m_is_block_device; } - bool isFIFO() const { return m_is_fifo; } - bool isSocket() const { return m_is_socket; } - - bool isDevice() const { return m_is_character_device or m_is_block_device; } - - - /* - ** "high level" queries - */ - - bool isSameAs(const Stat& rhs); - bool isSameDeviceAs(const Stat& rhs); - - /* - ** convenience methods - */ - - operator bool() const { return m_valid; } - - protected: - - void stat(const std::string& path, bool follow_links= true); - void clear(); - - protected: - std::string m_path; - bool m_follow_links; - - bool m_valid; - - dev_t m_device; - ino_t m_inode; - mode_t m_mode; - nlink_t m_num_links; - uid_t m_uid; - gid_t m_gid; - dev_t m_device_type; - off_t m_size; - time_t m_atime; - time_t m_mtime; - time_t m_ctime; - - bool m_is_link : 1; - bool m_is_regular : 1; - bool m_is_directory : 1; - bool m_is_character_device : 1; - bool m_is_block_device : 1; - bool m_is_fifo : 1; - bool m_is_socket : 1; +public: + Stat(); + Stat(const std::string& path, bool follow_links= true); + ~Stat(); + + void recheck(); + + bool is_valid() const { return Valid; } + + bool exists() const { return Valid; } + std::string path() const { return Path; } + + dev_t device() const { return Device; } + ino_t inode() const { return Inode; } + mode_t mode() const { return Mode; } + nlink_t nlink() const { return NumLinks; } + uid_t uid() const { return Uid; } + gid_t gid() const { return Gid; } + dev_t rdev() const { return DeviceType; } + off_t size() const { return Size; } + time_t atime() const { return Atime; } + time_t mtime() const { return Mtime; } + time_t ctime() const { return Ctime; } + + nlink_t num_hard_links() const { return NumLinks; } + dev_t device_type() const { return DeviceType; } + time_t last_modified_time() const { return Mtime; } + time_t created_time() const { return Ctime; } + + /* + ** unix style like type queries: + */ + + bool is_lnk() const { return IsLink; } + bool is_reg() const { return IsRegular; } + bool is_dir() const { return IsDirectory; } + bool is_chr() const { return IsCharacterDevice; } + bool is_blk() const { return IsBlockDevice; } + bool is_fifo() const { return IsFifo; } + bool is_sock() const { return IsSocket; } + + /* + ** readable style type queries: + */ + + bool is_link () const { return IsLink; } + bool is_regular() const { return IsRegular; } + bool is_regular_file() const { return IsRegular; } + bool is_directory() const { return IsDirectory; } + bool is_character_device() const {return IsCharacterDevice; } + bool is_block_device() const { return IsBlockDevice; } + bool is_socket() const { return IsSocket; } + + bool is_device() const { return IsCharacterDevice or IsBlockDevice; } + + + /* + ** "high level" queries + */ + + bool is_same_as(const Stat& rhs); + bool is_same_device_as(const Stat& rhs); + + /* + ** convenience methods + */ + + operator bool() const { return Valid; } + +protected: + + void stat(const std::string& path, bool follow_links= true); + void clear(); + +protected: + std::string Path; + bool FollowLinks; + + bool Valid; + + dev_t Device; + ino_t Inode; + mode_t Mode; + nlink_t NumLinks; + uid_t Uid; + gid_t Gid; + dev_t DeviceType; + off_t Size; + time_t Atime; + time_t Mtime; + time_t Ctime; + + bool IsLink : 1; + bool IsRegular : 1; + bool IsDirectory : 1; + bool IsCharacterDevice : 1; + bool IsBlockDevice : 1; + bool IsFifo : 1; + bool IsSocket : 1; }; // eo class Stat /* @@ -130,31 +128,29 @@ bool path_exists(const std::string& path); bool file_exists(const std::string& path); long file_size (const std::string &name); -time_t fileMTime(const std::string& path); +time_t file_mtime(const std::string& path); -bool getDir(const std::string& path, std::vector< std::string >& result, bool include_dot_names= false ); -std::vector< std::string > getDir(const std::string& path, bool include_dot_names= false ); +bool get_dir(const std::string& path, std::vector< std::string >& result, bool include_dot_names= false ); +std::vector< std::string > get_dir(const std::string& path, bool include_dot_names= false ); bool unlink(const std::string& path); bool symlink(const std::string& target, const std::string& link_name, bool force= false); -std::string readLink(const std::string& path); - - -std::string readFile(const std::string& path); +std::string read_link(const std::string& path); -bool writeFile(const std::string& path, const std::string& data); +std::string read_file(const std::string& path); +bool write_file(const std::string& path, const std::string& data); std::string basename(const std::string& path); std::string dirname(const std::string& path); -std::string normalizePath(const std::string& path); +std::string normalize_path(const std::string& path); bool chmod(const std::string& path, int mode); -bool chown(const std::string& path, const i2n::User& user, const i2n::Group& group= i2n::Group()); +bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group= I2n::Group()); bool recursive_delete(const std::string &path, std::string *error=NULL);