trying to add fdopen

pull/40/head
Matchman Green 2012-05-27 14:55:26 +08:00
parent d78728938f
commit e09bdc5a8a
7 changed files with 71 additions and 52 deletions

View File

@ -40,8 +40,7 @@ Config::Config() :
void Config::Save( )
{
StdioFile file( Filename(), "w" ) ;
file.Chmod( 0600 ) ;
StdioFile file( Filename(), 0600 ) ;
m_cfg.Write( file ) ;
}

View File

@ -310,7 +310,7 @@ bool Resource::Upload( http::Agent* http, const std::string& link, const http::H
"<title>" + m_entry.Filename() + "</title>"
"</entry>" ;
StdioFile file( Path(), "rb" ) ;
StdioFile file( Path() ) ;
// TODO: upload in chunks
std::string data ;

View File

@ -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 )
{
}

View File

@ -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 ;

View File

@ -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& )

View File

@ -31,17 +31,23 @@
#include <boost/exception/info.hpp>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
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()

View File

@ -22,7 +22,6 @@
#include "Exception.hh"
#include "FileSystem.hh"
#include <cstdio>
#include <string>
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