| 1 | /* |
| 2 | The software in this package is distributed under the GNU General |
| 3 | Public License version 2 (with a special exception described below). |
| 4 | |
| 5 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 6 | in the file COPYING.GPL. |
| 7 | |
| 8 | As a special exception, if other files instantiate templates or use macros |
| 9 | or inline functions from this file, or you compile this file and link it |
| 10 | with other works to produce a work based on this file, this file |
| 11 | does not by itself cause the resulting work to be covered |
| 12 | by the GNU General Public License. |
| 13 | |
| 14 | However the source code for this file must still be made available |
| 15 | in accordance with section (3) of the GNU General Public License. |
| 16 | |
| 17 | This exception does not invalidate any other reasons why a work based |
| 18 | on this file might be covered by the GNU General Public License. |
| 19 | */ |
| 20 | /** |
| 21 | * @file |
| 22 | * @brief some functions to wrap some system functions |
| 23 | * |
| 24 | * @author Reinhard Pfau \<Reinhard.Pfau@gmx.de\> |
| 25 | * @copyright © Copyright 2009 by Intra2net AG |
| 26 | */ |
| 27 | |
| 28 | #include "asyncio_system_tools.hpp" |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <unistd.h> |
| 33 | #include <netinet/in.h> |
| 34 | |
| 35 | |
| 36 | namespace AsyncIo |
| 37 | { |
| 38 | namespace Utils |
| 39 | { |
| 40 | |
| 41 | /* |
| 42 | * implementation of FileStat |
| 43 | */ |
| 44 | |
| 45 | FileStat::FileStat( const std::string& path, bool follow_symlinks) |
| 46 | : m_path( path ) |
| 47 | , m_is_valid( false ) |
| 48 | , m_follow_symlinks( follow_symlinks ) |
| 49 | { |
| 50 | refresh(); |
| 51 | }// end of FileStat::FileStat() |
| 52 | |
| 53 | |
| 54 | FileStat::~FileStat() |
| 55 | { |
| 56 | } // end of FileStat::~FileStat() |
| 57 | |
| 58 | |
| 59 | void FileStat::refresh() |
| 60 | { |
| 61 | struct stat file_stat[1]; |
| 62 | |
| 63 | int res; |
| 64 | |
| 65 | res= (m_follow_symlinks ? ::stat : ::lstat)( m_path.c_str(), file_stat ); |
| 66 | |
| 67 | if (res) |
| 68 | { |
| 69 | m_is_valid= false; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | m_mode= file_stat->st_mode; |
| 74 | |
| 75 | m_is_regular_file= S_ISREG(file_stat->st_mode); |
| 76 | m_is_directory= S_ISDIR(file_stat->st_mode); |
| 77 | m_is_character_device= S_ISCHR(file_stat->st_mode); |
| 78 | m_is_block_device= S_ISBLK(file_stat->st_mode); |
| 79 | m_is_fifo= S_ISFIFO(file_stat->st_mode); |
| 80 | m_is_symbolic_link= S_ISLNK(file_stat->st_mode); |
| 81 | m_is_socket= S_ISSOCK(file_stat->st_mode); |
| 82 | m_is_exec_file = m_is_regular_file && (file_stat->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)); |
| 83 | // TODO: implement more! |
| 84 | } // end of FileStat::refresh() |
| 85 | |
| 86 | |
| 87 | |
| 88 | /* |
| 89 | * funcs |
| 90 | */ |
| 91 | |
| 92 | bool unlink( const std::string& path ) |
| 93 | { |
| 94 | return ::unlink( path.c_str() ) == 0; |
| 95 | } // end of unlink(const std::string&) |
| 96 | |
| 97 | |
| 98 | |
| 99 | /* |
| 100 | * IPv4 |
| 101 | */ |
| 102 | |
| 103 | IPv4Address::IPv4Address( ) |
| 104 | : m_address( 0u ) |
| 105 | , m_valid( false ) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | |
| 110 | IPv4Address::IPv4Address(uint32_t _address) |
| 111 | : m_address( _address ) |
| 112 | , m_valid( true ) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | |
| 117 | uint32_t IPv4Address::get_address_nbo() const |
| 118 | { |
| 119 | return htonl( m_address ); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | } // end of namespace Utils |
| 125 | }// end of namespace AsyncIo |