mirror of https://github.com/vitalif/grive2
removed OpenSSL in Download.cc
parent
b34e41c3a5
commit
89e07bb257
|
@ -65,7 +65,7 @@ void Entry::FromLocal( const fs::path& path )
|
||||||
m_title = path.filename().string() ;
|
m_title = path.filename().string() ;
|
||||||
m_filename = path.filename().string() ;
|
m_filename = path.filename().string() ;
|
||||||
m_kind = fs::is_directory(path) ? "folder" : "file" ;
|
m_kind = fs::is_directory(path) ? "folder" : "file" ;
|
||||||
m_md5 = fs::is_directory(path) ? "" : crypt::MD5( path ) ;
|
m_md5 = fs::is_directory(path) ? "" : crypt::MD5::Get( path ) ;
|
||||||
m_mtime = fs::exists(path) ? os::FileCTime( path ) : DateTime() ;
|
m_mtime = fs::exists(path) ? os::FileCTime( path ) : DateTime() ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -216,7 +216,6 @@ void Resource::FromLocal( const DateTime& last_sync )
|
||||||
m_state = ( mtime > last_sync ? local_new : remote_deleted ) ;
|
m_state = ( mtime > last_sync ? local_new : remote_deleted ) ;
|
||||||
|
|
||||||
m_entry.FromLocal( path ) ;
|
m_entry.FromLocal( path ) ;
|
||||||
Trace( "file %1% read from local %2%", path, m_state ) ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert( m_state != unknown ) ;
|
assert( m_state != unknown ) ;
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
// #include "util/SignalHandler.hh"
|
// #include "util/SignalHandler.hh"
|
||||||
|
|
||||||
#include "Error.hh"
|
#include "Error.hh"
|
||||||
|
#include "util/Crypt.hh"
|
||||||
|
|
||||||
// boost headers
|
// boost headers
|
||||||
#include <boost/throw_exception.hpp>
|
#include <boost/throw_exception.hpp>
|
||||||
|
@ -32,8 +33,6 @@
|
||||||
#include <boost/exception/errinfo_file_open_mode.hpp>
|
#include <boost/exception/errinfo_file_open_mode.hpp>
|
||||||
#include <boost/exception/info.hpp>
|
#include <boost/exception/info.hpp>
|
||||||
|
|
||||||
#include <openssl/evp.h>
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <new>
|
#include <new>
|
||||||
|
|
||||||
|
@ -43,25 +42,17 @@ namespace gr { namespace http {
|
||||||
|
|
||||||
Download::Download( const std::string& filename ) :
|
Download::Download( const std::string& filename ) :
|
||||||
m_file( filename, 0600 ),
|
m_file( filename, 0600 ),
|
||||||
m_mdctx( ::EVP_MD_CTX_create() )
|
m_crypt( new crypt::MD5 )
|
||||||
{
|
{
|
||||||
if ( m_mdctx == 0 )
|
|
||||||
throw std::bad_alloc() ;
|
|
||||||
|
|
||||||
if ( ::EVP_DigestInit_ex( m_mdctx, ::EVP_md5(), 0 ) != 1 )
|
|
||||||
BOOST_THROW_EXCEPTION( Error() << expt::ErrMsg( "cannot create MD5 digest context" ) ) ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Download::Download( const std::string& filename, NoChecksum ) :
|
Download::Download( const std::string& filename, NoChecksum ) :
|
||||||
m_file( filename, 0600 ),
|
m_file( filename, 0600 )
|
||||||
m_mdctx( 0 )
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Download::~Download( )
|
Download::~Download()
|
||||||
{
|
{
|
||||||
if ( m_mdctx != 0 )
|
|
||||||
::EVP_MD_CTX_destroy( m_mdctx ) ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Download::Clear()
|
void Download::Clear()
|
||||||
|
@ -71,32 +62,15 @@ void Download::Clear()
|
||||||
|
|
||||||
std::string Download::Finish() const
|
std::string Download::Finish() const
|
||||||
{
|
{
|
||||||
// Unregister the signal
|
return m_crypt.get() != 0 ? m_crypt->Get() : "" ;
|
||||||
// SignalHandler::GetInstance().UnregisterSignal( SIGINT ) ;
|
|
||||||
|
|
||||||
std::string result ;
|
|
||||||
|
|
||||||
// get the checksum and return it ;
|
|
||||||
if ( m_mdctx != 0 )
|
|
||||||
{
|
|
||||||
unsigned int size = EVP_MAX_MD_SIZE ;
|
|
||||||
result.resize( size ) ;
|
|
||||||
|
|
||||||
if ( ::EVP_DigestFinal_ex( m_mdctx, reinterpret_cast<unsigned char*>(&result[0]), &size ) != 1 )
|
|
||||||
throw Error() << expt::ErrMsg( "cannot calculate checksum" ) ;
|
|
||||||
|
|
||||||
result.resize( size ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t Download::OnData( void *data, std::size_t count )
|
std::size_t Download::OnData( void *data, std::size_t count )
|
||||||
{
|
{
|
||||||
assert( data != 0 ) ;
|
assert( data != 0 ) ;
|
||||||
|
|
||||||
if ( m_mdctx != 0 )
|
if ( m_crypt.get() != 0 )
|
||||||
::EVP_DigestUpdate( m_mdctx, data, count ) ;
|
m_crypt->Write( data, count ) ;
|
||||||
|
|
||||||
return m_file.Write( data, count ) ;
|
return m_file.Write( data, count ) ;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,9 +24,14 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <openssl/evp.h>
|
namespace gr {
|
||||||
|
|
||||||
namespace gr { namespace http {
|
namespace crypt
|
||||||
|
{
|
||||||
|
class MD5 ;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace http {
|
||||||
|
|
||||||
class Download : public http::Receivable
|
class Download : public http::Receivable
|
||||||
{
|
{
|
||||||
|
@ -34,7 +39,7 @@ public :
|
||||||
struct NoChecksum {} ;
|
struct NoChecksum {} ;
|
||||||
Download( const std::string& filename ) ;
|
Download( const std::string& filename ) ;
|
||||||
Download( const std::string& filename, NoChecksum ) ;
|
Download( const std::string& filename, NoChecksum ) ;
|
||||||
~Download( ) ;
|
~Download() ;
|
||||||
|
|
||||||
std::string Finish() const ;
|
std::string Finish() const ;
|
||||||
|
|
||||||
|
@ -43,7 +48,7 @@ public :
|
||||||
|
|
||||||
private :
|
private :
|
||||||
StdioFile m_file ;
|
StdioFile m_file ;
|
||||||
EVP_MD_CTX *m_mdctx ;
|
std::auto_ptr<crypt::MD5> m_crypt ;
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
} } // end of namespace
|
} } // end of namespace
|
||||||
|
|
|
@ -33,12 +33,49 @@ namespace gr { namespace crypt {
|
||||||
|
|
||||||
const std::size_t read_size = 8 * 1024 ;
|
const std::size_t read_size = 8 * 1024 ;
|
||||||
|
|
||||||
std::string MD5( const fs::path& file )
|
struct MD5::Impl
|
||||||
|
{
|
||||||
|
gcry_md_hd_t hd ;
|
||||||
|
} ;
|
||||||
|
|
||||||
|
MD5::MD5() : m_impl( new Impl )
|
||||||
|
{
|
||||||
|
::gcry_error_t err = ::gcry_md_open( &m_impl->hd, GCRY_MD_MD5, 0 ) ;
|
||||||
|
if ( err != GPG_ERR_NO_ERROR )
|
||||||
|
{
|
||||||
|
BOOST_THROW_EXCEPTION( Exception() << expt::ErrMsg( ::gcry_strerror(err) ) ) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MD5::~MD5()
|
||||||
|
{
|
||||||
|
::gcry_md_close( m_impl->hd ) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MD5::Write( const void *data, std::size_t size )
|
||||||
|
{
|
||||||
|
::gcry_md_write( m_impl->hd, data, size ) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string MD5::Get() const
|
||||||
|
{
|
||||||
|
unsigned char *md5 = ::gcry_md_read( m_impl->hd, GCRY_MD_MD5 ) ;
|
||||||
|
unsigned int len = ::gcry_md_get_algo_dlen(GCRY_MD_MD5) ;
|
||||||
|
|
||||||
|
// format the MD5 string
|
||||||
|
std::ostringstream ss ;
|
||||||
|
for ( unsigned int i = 0 ; i < len ; i++ )
|
||||||
|
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(md5[i]) ;
|
||||||
|
|
||||||
|
return ss.str() ;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string MD5::Get( const fs::path& file )
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
StdioFile sfile( file ) ;
|
StdioFile sfile( file ) ;
|
||||||
return MD5( sfile ) ;
|
return Get( sfile ) ;
|
||||||
}
|
}
|
||||||
catch ( StdioFile::Error& )
|
catch ( StdioFile::Error& )
|
||||||
{
|
{
|
||||||
|
@ -46,32 +83,17 @@ std::string MD5( const fs::path& file )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string MD5( StdioFile& file )
|
std::string MD5::Get( StdioFile& file )
|
||||||
{
|
{
|
||||||
char buf[read_size] ;
|
char buf[read_size] ;
|
||||||
|
|
||||||
gcry_md_hd_t hd ;
|
MD5 crypt ;
|
||||||
gcry_error_t err = gcry_md_open( &hd, GCRY_MD_MD5, 0 ) ;
|
|
||||||
if ( err != GPG_ERR_NO_ERROR )
|
|
||||||
{
|
|
||||||
BOOST_THROW_EXCEPTION( Exception() << expt::ErrMsg( gcry_strerror(err) ) ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t count = 0 ;
|
std::size_t count = 0 ;
|
||||||
while ( (count = file.Read( buf, sizeof(buf) )) > 0 )
|
while ( (count = file.Read( buf, sizeof(buf) )) > 0 )
|
||||||
gcry_md_write( hd, buf, count ) ;
|
crypt.Write( buf, count ) ;
|
||||||
|
|
||||||
unsigned char *md5 = gcry_md_read( hd, GCRY_MD_MD5 ) ;
|
return crypt.Get() ;
|
||||||
unsigned int len = gcry_md_get_algo_dlen(GCRY_MD_MD5) ;
|
|
||||||
|
|
||||||
// format the MD5 string
|
|
||||||
std::ostringstream ss ;
|
|
||||||
for ( unsigned int i = 0 ; i < len ; i++ )
|
|
||||||
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(md5[i]) ;
|
|
||||||
|
|
||||||
gcry_md_close( hd ) ;
|
|
||||||
|
|
||||||
return ss.str() ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} } // end of namespaces
|
} } // end of namespaces
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <boost/filesystem.hpp>
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
|
@ -27,10 +28,23 @@ namespace gr {
|
||||||
|
|
||||||
class StdioFile ;
|
class StdioFile ;
|
||||||
|
|
||||||
namespace crypt
|
namespace crypt {
|
||||||
{
|
|
||||||
std::string MD5( StdioFile& file ) ;
|
|
||||||
std::string MD5( const boost::filesystem::path& file ) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // end of namespace gr
|
class MD5
|
||||||
|
{
|
||||||
|
public :
|
||||||
|
MD5() ;
|
||||||
|
~MD5() ;
|
||||||
|
|
||||||
|
static std::string Get( StdioFile& file ) ;
|
||||||
|
static std::string Get( const boost::filesystem::path& file ) ;
|
||||||
|
|
||||||
|
void Write( const void *data, std::size_t size ) ;
|
||||||
|
std::string Get() const ;
|
||||||
|
|
||||||
|
private :
|
||||||
|
struct Impl ;
|
||||||
|
std::auto_ptr<Impl> m_impl ;
|
||||||
|
} ;
|
||||||
|
|
||||||
|
} } // end of namespace gr
|
||||||
|
|
Loading…
Reference in New Issue