From e09bdc5a8a92d9f19d290084ae3d1274c62511c5 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Sun, 27 May 2012 14:55:26 +0800 Subject: [PATCH] trying to add fdopen --- grive/src/Config.cc | 3 +- libgrive/src/drive/Resource.cc | 2 +- libgrive/src/http/Download.cc | 4 +- libgrive/src/protocol/Json.cc | 2 +- libgrive/src/util/Crypt.cc | 2 +- libgrive/src/util/StdioFile.cc | 93 ++++++++++++++++++++-------------- libgrive/src/util/StdioFile.hh | 17 ++++--- 7 files changed, 71 insertions(+), 52 deletions(-) diff --git a/grive/src/Config.cc b/grive/src/Config.cc index cc3341d..6a66d56 100644 --- a/grive/src/Config.cc +++ b/grive/src/Config.cc @@ -40,8 +40,7 @@ Config::Config() : void Config::Save( ) { - StdioFile file( Filename(), "w" ) ; - file.Chmod( 0600 ) ; + StdioFile file( Filename(), 0600 ) ; m_cfg.Write( file ) ; } diff --git a/libgrive/src/drive/Resource.cc b/libgrive/src/drive/Resource.cc index 517afbc..a22ead2 100644 --- a/libgrive/src/drive/Resource.cc +++ b/libgrive/src/drive/Resource.cc @@ -310,7 +310,7 @@ bool Resource::Upload( http::Agent* http, const std::string& link, const http::H "" + m_entry.Filename() + "" "" ; - StdioFile file( Path(), "rb" ) ; + StdioFile file( Path() ) ; // TODO: upload in chunks std::string data ; diff --git a/libgrive/src/http/Download.cc b/libgrive/src/http/Download.cc index 1545f0a..6895a98 100644 --- a/libgrive/src/http/Download.cc +++ b/libgrive/src/http/Download.cc @@ -42,7 +42,7 @@ namespace gr { namespace http { Download::Download( const std::string& filename ) : - m_file( filename, "wb" ), + m_file( filename, 0600 ), m_mdctx( ::EVP_MD_CTX_create() ) { if ( m_mdctx == 0 ) @@ -53,7 +53,7 @@ Download::Download( const std::string& filename ) : } Download::Download( const std::string& filename, NoChecksum ) : - m_file( filename, "wb" ), + m_file( filename, 0600 ), m_mdctx( 0 ) { } diff --git a/libgrive/src/protocol/Json.cc b/libgrive/src/protocol/Json.cc index 20c1cc7..8a00379 100644 --- a/libgrive/src/protocol/Json.cc +++ b/libgrive/src/protocol/Json.cc @@ -104,7 +104,7 @@ Json Json::Parse( const std::string& str ) Json Json::ParseFile( const std::string& filename ) { - StdioFile file( filename, "r" ) ; + StdioFile file( filename ) ; struct json_tokener *tok = ::json_tokener_new() ; struct json_object *json = 0 ; diff --git a/libgrive/src/util/Crypt.cc b/libgrive/src/util/Crypt.cc index 1279d5d..4567776 100644 --- a/libgrive/src/util/Crypt.cc +++ b/libgrive/src/util/Crypt.cc @@ -33,7 +33,7 @@ std::string MD5( const fs::path& file ) { try { - StdioFile sfile( file, "rb" ) ; + StdioFile sfile( file ) ; return MD5( sfile ) ; } catch ( StdioFile::Error& ) diff --git a/libgrive/src/util/StdioFile.cc b/libgrive/src/util/StdioFile.cc index a15bb91..ae0d41b 100644 --- a/libgrive/src/util/StdioFile.cc +++ b/libgrive/src/util/StdioFile.cc @@ -31,17 +31,23 @@ #include #include +#include +#include namespace gr { -StdioFile::StdioFile( const std::string& filename, const char *mode ) : m_file( 0 ) +StdioFile::StdioFile( ) : m_fd( -1 ) { - Open( filename, mode ) ; } -StdioFile::StdioFile( const fs::path& path, const char *mode ) : m_file( 0 ) +StdioFile::StdioFile( const fs::path& path ) : m_fd( -1 ) { - Open( path, mode ) ; + OpenForRead( path ) ; +} + +StdioFile::StdioFile( const fs::path& path, int mode ) : m_fd( -1 ) +{ + OpenForWrite( path, mode ) ; } StdioFile::~StdioFile( ) @@ -49,84 +55,95 @@ StdioFile::~StdioFile( ) Close() ; } -void StdioFile::Open( const std::string& filename, const char *mode ) +void StdioFile::Open( const fs::path& path, int flags, int mode ) { if ( IsOpened() ) Close() ; - assert( m_file == 0 ) ; - m_file = std::fopen( filename.c_str(), mode ) ; - - if ( m_file == 0 ) + assert( m_fd == -1 ) ; + m_fd = ::open( path.string().c_str(), flags, mode ) ; + if ( m_fd == -1 ) { BOOST_THROW_EXCEPTION( Error() - << boost::errinfo_api_function("fopen") + << boost::errinfo_api_function("open") << boost::errinfo_errno(errno) - << boost::errinfo_file_name(filename) - << boost::errinfo_file_open_mode(mode) + << boost::errinfo_file_name(path.string()) ) ; } } -void StdioFile::Open( const fs::path& path, const char *mode ) +void StdioFile::OpenForRead( const fs::path& path ) { - Open( path.string(), mode ) ; + Open( path, O_RDONLY, 0 ) ; +} + +void StdioFile::OpenForWrite( const fs::path& path, int mode ) +{ + Open( path, O_CREAT|O_RDWR|O_TRUNC, mode ) ; } void StdioFile::Close() { if ( IsOpened() ) { - std::fclose( m_file ) ; - m_file = 0 ; + close( m_fd ) ; + m_fd = -1 ; } } bool StdioFile::IsOpened() const { - return m_file != 0 ; + return m_fd != -1 ; } std::size_t StdioFile::Read( void *ptr, std::size_t size ) { - assert( m_file != 0 ) ; - return std::fread( ptr, 1, size, m_file ) ; + assert( IsOpened() ) ; + ssize_t count = ::read( m_fd, ptr, size ) ; + if ( count == -1 ) + { + BOOST_THROW_EXCEPTION( + Error() + << boost::errinfo_api_function("read") + << boost::errinfo_errno(errno) + ) ; + } + return count ; } std::size_t StdioFile::Write( const void *ptr, std::size_t size ) { - assert( m_file != 0 ) ; - return std::fwrite( ptr, 1, size, m_file ) ; + assert( IsOpened() ) ; + ssize_t count = ::write( m_fd, ptr, size ) ; + if ( count == -1 ) + { + BOOST_THROW_EXCEPTION( + Error() + << boost::errinfo_api_function("read") + << boost::errinfo_errno(errno) + ) ; + } + return count ; } -int StdioFile::Seek( long offset, int whence ) +long StdioFile::Seek( long offset, int whence ) { - assert( m_file != 0 ) ; - return std::fseek( m_file, offset, whence ) ; + assert( IsOpened() ) ; + return ::lseek( m_fd, offset, whence ) ; } long StdioFile::Tell() const { - assert( m_file != 0 ) ; - return std::ftell( m_file ) ; + assert( IsOpened() ) ; + return ::lseek( m_fd, 0, SEEK_CUR ) ; } void StdioFile::Chmod( int mode ) { - assert( m_file != 0 ) ; + assert( IsOpened() ) ; - int fd = ::fileno(m_file) ; - if ( fd == -1 ) - { - BOOST_THROW_EXCEPTION( - Error() - << boost::errinfo_api_function("fileno") - << boost::errinfo_errno(errno) - ) ; - } - - if ( ::fchmod( fd, mode ) != 0 ) + if ( ::fchmod( m_fd, mode ) != 0 ) { BOOST_THROW_EXCEPTION( Error() diff --git a/libgrive/src/util/StdioFile.hh b/libgrive/src/util/StdioFile.hh index 002fd53..30de2fb 100644 --- a/libgrive/src/util/StdioFile.hh +++ b/libgrive/src/util/StdioFile.hh @@ -22,7 +22,6 @@ #include "Exception.hh" #include "FileSystem.hh" -#include #include namespace gr { @@ -33,25 +32,29 @@ public : struct Error : virtual Exception {} ; public : - StdioFile( const std::string& filename, const char *mode ) ; - StdioFile( const fs::path& path, const char *mode ) ; + StdioFile() ; + StdioFile( const fs::path& path ) ; + StdioFile( const fs::path& path, int mode ) ; ~StdioFile( ) ; - void Open( const std::string& filename, const char *mode ) ; - void Open( const fs::path& path, const char* mode ) ; + void OpenForRead( const fs::path& path ) ; + void OpenForWrite( const fs::path& path, int mode = 0600 ) ; void Close() ; bool IsOpened() const ; std::size_t Read( void *ptr, std::size_t size ) ; std::size_t Write( const void *ptr, std::size_t size ) ; - int Seek( long offset, int whence ) ; + long Seek( long offset, int whence ) ; long Tell() const ; void Chmod( int mode ) ; + +private : + void Open( const fs::path& path, int flags, int mode ) ; private : - std::FILE *m_file ; + int m_fd ; } ; } // end of namespace