Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
justin committed Jul 19, 2012
2 parents d5315f4 + c6a1c47 commit c12029a
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 66 deletions.
5 changes: 4 additions & 1 deletion grive/src/main.cc
Expand Up @@ -21,6 +21,8 @@


#include "drive/Drive.hh" #include "drive/Drive.hh"


#include "http/CurlAgent.hh"
#include "protocol/AuthAgent.hh"
#include "protocol/OAuth2.hh" #include "protocol/OAuth2.hh"
#include "protocol/Json.hh" #include "protocol/Json.hh"


Expand Down Expand Up @@ -172,7 +174,8 @@ int Main( int argc, char **argv )
} }


OAuth2 token( refresh_token, client_id, client_secret ) ; OAuth2 token( refresh_token, client_id, client_secret ) ;
Drive drive( token, options ) ; AuthAgent agent( token, std::auto_ptr<http::Agent>( new http::CurlAgent ) ) ;
Drive drive( &agent, options ) ;
drive.DetectChanges() ; drive.DetectChanges() ;


if ( vm.count( "dry-run" ) == 0 ) if ( vm.count( "dry-run" ) == 0 )
Expand Down
2 changes: 2 additions & 0 deletions libgrive/CMakeLists.txt
Expand Up @@ -29,6 +29,8 @@ endif ( BFD_FOUND )


if ( IBERTY_FOUND ) if ( IBERTY_FOUND )
set( OPT_LIBS ${OPT_LIBS} ${IBERTY_LIBRARY} ) set( OPT_LIBS ${OPT_LIBS} ${IBERTY_LIBRARY} )
else ( IBERTY_FOUND )
set( IBERTY_LIBRARY "" )
endif ( IBERTY_FOUND ) endif ( IBERTY_FOUND )


if ( ZLIB_FOUND ) if ( ZLIB_FOUND )
Expand Down
42 changes: 19 additions & 23 deletions libgrive/src/drive/Drive.cc
Expand Up @@ -23,11 +23,11 @@
#include "Entry.hh" #include "Entry.hh"
#include "Feed.hh" #include "Feed.hh"


#include "http/CurlAgent.hh" #include "http/Agent.hh"
#include "http/ResponseLog.hh" #include "http/ResponseLog.hh"
#include "http/XmlResponse.hh" #include "http/XmlResponse.hh"
#include "protocol/Json.hh" #include "protocol/Json.hh"
#include "protocol/OAuth2.hh" // #include "protocol/OAuth2.hh"
#include "util/Destroy.hh" #include "util/Destroy.hh"
#include "util/log/Log.hh" #include "util/log/Log.hh"
#include "xml/Node.hh" #include "xml/Node.hh"
Expand All @@ -53,12 +53,11 @@ namespace
const std::string state_file = ".grive_state" ; const std::string state_file = ".grive_state" ;
} }


Drive::Drive( OAuth2& auth, const Json& options ) : Drive::Drive( http::Agent *http, const Json& options ) :
m_auth( auth ), m_http( http ),
m_state( state_file, options ) m_state( state_file, options )
{ {
m_http_hdr.Add( "Authorization: Bearer " + m_auth.AccessToken() ) ; assert( m_http != 0 ) ;
m_http_hdr.Add( "GData-Version: 3.0" ) ;
} }


void Drive::FromRemote( const Entry& entry ) void Drive::FromRemote( const Entry& entry )
Expand Down Expand Up @@ -93,14 +92,14 @@ void Drive::SaveState()
m_state.Write( state_file ) ; m_state.Write( state_file ) ;
} }


void Drive::SyncFolders( http::Agent *http ) void Drive::SyncFolders( )
{ {
assert( http != 0 ) ; assert( m_http != 0 ) ;


Log( "Synchronizing folders", log::info ) ; Log( "Synchronizing folders", log::info ) ;


http::XmlResponse xml ; http::XmlResponse xml ;
http->Get( feed_base + "/-/folder?max-results=50&showroot=true", &xml, m_http_hdr ) ; m_http->Get( feed_base + "/-/folder?max-results=50&showroot=true", &xml, http::Header() ) ;


Feed feed( xml.Response() ) ; Feed feed( xml.Response() ) ;
do do
Expand All @@ -121,7 +120,7 @@ void Drive::SyncFolders( http::Agent *http )
m_state.FromRemote( e ) ; m_state.FromRemote( e ) ;
} }
} }
} while ( feed.GetNext( http, m_http_hdr ) ) ; } while ( feed.GetNext( m_http, http::Header() ) ) ;


m_state.ResolveEntry() ; m_state.ResolveEntry() ;
} }
Expand All @@ -131,17 +130,15 @@ void Drive::DetectChanges()
Log( "Reading local directories", log::info ) ; Log( "Reading local directories", log::info ) ;
m_state.FromLocal( "." ) ; m_state.FromLocal( "." ) ;


http::CurlAgent http ;

long prev_stamp = m_state.ChangeStamp() ; long prev_stamp = m_state.ChangeStamp() ;
Trace( "previous change stamp is %1%", prev_stamp ) ; Trace( "previous change stamp is %1%", prev_stamp ) ;


SyncFolders( &http ) ; SyncFolders( ) ;


Log( "Reading remote server file list", log::info ) ; Log( "Reading remote server file list", log::info ) ;
Feed feed ; Feed feed ;
// feed.EnableLog( "/tmp/file", ".xml" ) ; // feed.EnableLog( "/tmp/file", ".xml" ) ;
feed.Start( &http, m_http_hdr, feed_base + "?showfolders=true&showroot=true" ) ; feed.Start( m_http, http::Header(), feed_base + "?showfolders=true&showroot=true" ) ;


m_resume_link = feed.Root()["link"]. m_resume_link = feed.Root()["link"].
Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ; Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ;
Expand All @@ -152,15 +149,15 @@ void Drive::DetectChanges()
feed.begin(), feed.end(), feed.begin(), feed.end(),
boost::bind( &Drive::FromRemote, this, _1 ) ) ; boost::bind( &Drive::FromRemote, this, _1 ) ) ;


} while ( feed.GetNext( &http, m_http_hdr ) ) ; } while ( feed.GetNext( m_http, http::Header() ) ) ;


// pull the changes feed // pull the changes feed
if ( prev_stamp != -1 ) if ( prev_stamp != -1 )
{ {
Log( "Detecting changes from last sync", log::info ) ; Log( "Detecting changes from last sync", log::info ) ;
Feed changes ; Feed changes ;
// feed.EnableLog( "/tmp/changes", ".xml" ) ; // feed.EnableLog( "/tmp/changes", ".xml" ) ;
feed.Start( &http, m_http_hdr, ChangesFeed(prev_stamp+1) ) ; feed.Start( m_http, http::Header(), ChangesFeed(prev_stamp+1) ) ;


std::for_each( std::for_each(
changes.begin(), changes.end(), changes.begin(), changes.end(),
Expand All @@ -171,25 +168,24 @@ void Drive::DetectChanges()
void Drive::Update() void Drive::Update()
{ {
Log( "Synchronizing files", log::info ) ; Log( "Synchronizing files", log::info ) ;
http::CurlAgent http ; m_state.Sync( m_http ) ;
m_state.Sync( &http, m_http_hdr ) ;


UpdateChangeStamp( &http ) ; UpdateChangeStamp( ) ;
} }


void Drive::DryRun() void Drive::DryRun()
{ {
Log( "Synchronizing files (dry-run)", log::info ) ; Log( "Synchronizing files (dry-run)", log::info ) ;
m_state.Sync( 0, m_http_hdr ) ; m_state.Sync( 0 ) ;
} }


void Drive::UpdateChangeStamp( http::Agent *http ) void Drive::UpdateChangeStamp( )
{ {
assert( http != 0 ) ; assert( m_http != 0 ) ;


// get changed feed // get changed feed
http::XmlResponse xrsp ; http::XmlResponse xrsp ;
http->Get( ChangesFeed(m_state.ChangeStamp()+1), &xrsp, m_http_hdr ) ; m_http->Get( ChangesFeed(m_state.ChangeStamp()+1), &xrsp, http::Header() ) ;


// we should go through the changes to see if it was really Grive to made that change // we should go through the changes to see if it was really Grive to made that change
// maybe by recording the updated timestamp and compare it? // maybe by recording the updated timestamp and compare it?
Expand Down
12 changes: 6 additions & 6 deletions libgrive/src/drive/Drive.hh
Expand Up @@ -35,13 +35,12 @@ namespace http
} }


class Entry ; class Entry ;
class OAuth2 ;
class Json ; class Json ;


class Drive class Drive
{ {
public : public :
Drive( OAuth2& auth, const Json& options ) ; Drive( http::Agent *http, const Json& options ) ;


void DetectChanges() ; void DetectChanges() ;
void Update() ; void Update() ;
Expand All @@ -51,15 +50,16 @@ public :
struct Error : virtual Exception {} ; struct Error : virtual Exception {} ;


private : private :
void SyncFolders( http::Agent *http ) ; void SyncFolders( ) ;
void file(); void file();
void FromRemote( const Entry& entry ) ; void FromRemote( const Entry& entry ) ;
void FromChange( const Entry& entry ) ; void FromChange( const Entry& entry ) ;
void UpdateChangeStamp( http::Agent *http ) ; void UpdateChangeStamp( ) ;


private : private :
OAuth2& m_auth ; http::Agent *m_http ;
http::Header m_http_hdr ; // OAuth2& m_auth ;
// http::Header m_http_hdr ;


std::string m_resume_link ; std::string m_resume_link ;
State m_state ; State m_state ;
Expand Down
39 changes: 19 additions & 20 deletions libgrive/src/drive/Resource.cc
Expand Up @@ -365,20 +365,20 @@ Resource* Resource::FindChild( const std::string& name )
} }


// try to change the state to "sync" // try to change the state to "sync"
void Resource::Sync( http::Agent *http, const http::Header& auth, DateTime& sync_time ) void Resource::Sync( http::Agent *http, DateTime& sync_time )
{ {
assert( m_state != unknown ) ; assert( m_state != unknown ) ;
assert( !IsRoot() || m_state == sync ) ; // root folder is already synced assert( !IsRoot() || m_state == sync ) ; // root folder is already synced


SyncSelf( http, auth, sync_time ) ; SyncSelf( http, sync_time ) ;


// if myself is deleted, no need to do the childrens // if myself is deleted, no need to do the childrens
if ( m_state != local_deleted && m_state != remote_deleted ) if ( m_state != local_deleted && m_state != remote_deleted )
std::for_each( m_child.begin(), m_child.end(), std::for_each( m_child.begin(), m_child.end(),
boost::bind( &Resource::Sync, _1, http, auth, boost::ref(sync_time) ) ) ; boost::bind( &Resource::Sync, _1, http, boost::ref(sync_time) ) ) ;
} }


void Resource::SyncSelf( http::Agent* http, const http::Header& auth, DateTime& sync_time ) void Resource::SyncSelf( http::Agent* http, DateTime& sync_time )
{ {
assert( !IsRoot() || m_state == sync ) ; // root is always sync assert( !IsRoot() || m_state == sync ) ; // root is always sync
assert( IsRoot() || http == 0 || fs::is_directory( m_parent->Path() ) ) ; assert( IsRoot() || http == 0 || fs::is_directory( m_parent->Path() ) ) ;
Expand All @@ -392,19 +392,19 @@ void Resource::SyncSelf( http::Agent* http, const http::Header& auth, DateTime&
case local_new : case local_new :
Log( "sync %1% doesn't exist in server, uploading", path, log::info ) ; Log( "sync %1% doesn't exist in server, uploading", path, log::info ) ;


if ( http != 0 && Create( http, auth, sync_time ) ) if ( http != 0 && Create( http, sync_time ) )
m_state = sync ; m_state = sync ;
break ; break ;


case local_deleted : case local_deleted :
Log( "sync %1% deleted in local. deleting remote", path, log::info ) ; Log( "sync %1% deleted in local. deleting remote", path, log::info ) ;
if ( http != 0 ) if ( http != 0 )
DeleteRemote( http, auth ) ; DeleteRemote( http ) ;
break ; break ;


case local_changed : case local_changed :
Log( "sync %1% changed in local. uploading", path, log::info ) ; Log( "sync %1% changed in local. uploading", path, log::info ) ;
if ( http != 0 && EditContent( http, auth, sync_time ) ) if ( http != 0 && EditContent( http, sync_time ) )
m_state = sync ; m_state = sync ;
break ; break ;


Expand All @@ -415,7 +415,7 @@ void Resource::SyncSelf( http::Agent* http, const http::Header& auth, DateTime&
if ( IsFolder() ) if ( IsFolder() )
fs::create_directories( path ) ; fs::create_directories( path ) ;
else else
Download( http, path, auth ) ; Download( http, path ) ;


m_state = sync ; m_state = sync ;
} }
Expand All @@ -426,7 +426,7 @@ void Resource::SyncSelf( http::Agent* http, const http::Header& auth, DateTime&
Log( "sync %1% changed in remote. downloading", path, log::info ) ; Log( "sync %1% changed in remote. downloading", path, log::info ) ;
if ( http != 0 ) if ( http != 0 )
{ {
Download( http, path, auth ) ; Download( http, path ) ;
m_state = sync ; m_state = sync ;
} }
break ; break ;
Expand Down Expand Up @@ -474,14 +474,14 @@ void Resource::DeleteLocal()
} }
} }


void Resource::DeleteRemote( http::Agent *http, const http::Header& auth ) void Resource::DeleteRemote( http::Agent *http )
{ {
assert( http != 0 ) ; assert( http != 0 ) ;
http::StringResponse str ; http::StringResponse str ;


try try
{ {
http::Header hdr( auth ) ; http::Header hdr ;
hdr.Add( "If-Match: " + m_etag ) ; hdr.Add( "If-Match: " + m_etag ) ;


// doesn't know why, but an update before deleting seems to work always // doesn't know why, but an update before deleting seems to work always
Expand All @@ -502,12 +502,12 @@ void Resource::DeleteRemote( http::Agent *http, const http::Header& auth )
} }




void Resource::Download( http::Agent* http, const fs::path& file, const http::Header& auth ) const void Resource::Download( http::Agent* http, const fs::path& file ) const
{ {
assert( http != 0 ) ; assert( http != 0 ) ;


http::Download dl( file.string(), http::Download::NoChecksum() ) ; http::Download dl( file.string(), http::Download::NoChecksum() ) ;
long r = http->Get( m_content, &dl, auth ) ; long r = http->Get( m_content, &dl, http::Header() ) ;
if ( r <= 400 ) if ( r <= 400 )
{ {
if ( m_mtime != DateTime() ) if ( m_mtime != DateTime() )
Expand All @@ -517,7 +517,7 @@ void Resource::Download( http::Agent* http, const fs::path& file, const http::He
} }
} }


bool Resource::EditContent( http::Agent* http, const http::Header& auth, DateTime& sync_time ) bool Resource::EditContent( http::Agent* http, DateTime& sync_time )
{ {
assert( http != 0 ) ; assert( http != 0 ) ;
assert( m_parent != 0 ) ; assert( m_parent != 0 ) ;
Expand All @@ -530,10 +530,10 @@ bool Resource::EditContent( http::Agent* http, const http::Header& auth, DateTim
return false ; return false ;
} }


return Upload( http, m_edit, auth, false, sync_time ) ; return Upload( http, m_edit, false, sync_time ) ;
} }


bool Resource::Create( http::Agent* http, const http::Header& auth, DateTime& sync_time ) bool Resource::Create( http::Agent* http, DateTime& sync_time )
{ {
assert( http != 0 ) ; assert( http != 0 ) ;
assert( m_parent != 0 ) ; assert( m_parent != 0 ) ;
Expand All @@ -551,7 +551,7 @@ bool Resource::Create( http::Agent* http, const http::Header& auth, DateTime& sy
% xml::Escape(m_name) % xml::Escape(m_name)
).str() ; ).str() ;


http::Header hdr( auth ) ; http::Header hdr ;
hdr.Add( "Content-Type: application/atom+xml" ) ; hdr.Add( "Content-Type: application/atom+xml" ) ;


http::XmlResponse xml ; http::XmlResponse xml ;
Expand All @@ -563,7 +563,7 @@ bool Resource::Create( http::Agent* http, const http::Header& auth, DateTime& sy
} }
else if ( !m_parent->m_create.empty() ) else if ( !m_parent->m_create.empty() )
{ {
return Upload( http, m_parent->m_create + "?convert=false", auth, true, sync_time ) ; return Upload( http, m_parent->m_create + "?convert=false", true, sync_time ) ;
} }
else else
{ {
Expand All @@ -575,7 +575,6 @@ bool Resource::Create( http::Agent* http, const http::Header& auth, DateTime& sy
bool Resource::Upload( bool Resource::Upload(
http::Agent* http, http::Agent* http,
const std::string& link, const std::string& link,
const http::Header& auth,
bool post, bool post,
DateTime& sync_time ) DateTime& sync_time )
{ {
Expand All @@ -585,7 +584,7 @@ bool Resource::Upload(
std::ostringstream xcontent_len ; std::ostringstream xcontent_len ;
xcontent_len << "X-Upload-Content-Length: " << file.Size() ; xcontent_len << "X-Upload-Content-Length: " << file.Size() ;


http::Header hdr( auth ) ; http::Header hdr ;
hdr.Add( "Content-Type: application/atom+xml" ) ; hdr.Add( "Content-Type: application/atom+xml" ) ;
hdr.Add( "X-Upload-Content-Type: application/octet-stream" ) ; hdr.Add( "X-Upload-Content-Type: application/octet-stream" ) ;
hdr.Add( xcontent_len.str() ) ; hdr.Add( xcontent_len.str() ) ;
Expand Down
15 changes: 7 additions & 8 deletions libgrive/src/drive/Resource.hh
Expand Up @@ -32,7 +32,6 @@ namespace gr {
namespace http namespace http
{ {
class Agent ; class Agent ;
class Header ;
} }


class Entry ; class Entry ;
Expand Down Expand Up @@ -77,7 +76,7 @@ public :
void FromRemote( const Entry& remote, const DateTime& last_sync ) ; void FromRemote( const Entry& remote, const DateTime& last_sync ) ;
void FromLocal( const DateTime& last_sync ) ; void FromLocal( const DateTime& last_sync ) ;


void Sync( http::Agent* http, const http::Header& auth, DateTime& sync_time ) ; void Sync( http::Agent* http, DateTime& sync_time ) ;


// children access // children access
iterator begin() const ; iterator begin() const ;
Expand Down Expand Up @@ -124,19 +123,19 @@ private :
private : private :
void SetState( State new_state ) ; void SetState( State new_state ) ;


void Download( http::Agent* http, const fs::path& file, const http::Header& auth ) const ; void Download( http::Agent* http, const fs::path& file ) const ;
bool EditContent( http::Agent* http, const http::Header& auth, DateTime& sync_time ) ; bool EditContent( http::Agent* http, DateTime& sync_time ) ;
bool Create( http::Agent* http, const http::Header& auth, DateTime& sync_time ) ; bool Create( http::Agent* http, DateTime& sync_time ) ;
bool Upload( http::Agent* http, const std::string& link, const http::Header& auth, bool post, DateTime& sync_time ) ; bool Upload( http::Agent* http, const std::string& link, bool post, DateTime& sync_time ) ;


void FromRemoteFolder( const Entry& remote, const DateTime& last_sync ) ; void FromRemoteFolder( const Entry& remote, const DateTime& last_sync ) ;
void FromRemoteFile( const Entry& remote, const DateTime& last_sync ) ; void FromRemoteFile( const Entry& remote, const DateTime& last_sync ) ;


void DeleteLocal() ; void DeleteLocal() ;
void DeleteRemote( http::Agent* http, const http::Header& auth ) ; void DeleteRemote( http::Agent* http ) ;


void AssignIDs( const Entry& remote ) ; void AssignIDs( const Entry& remote ) ;
void SyncSelf( http::Agent* http, const http::Header& auth, DateTime& sync_time ) ; void SyncSelf( http::Agent* http, DateTime& sync_time ) ;


private : private :
std::string m_name ; std::string m_name ;
Expand Down

0 comments on commit c12029a

Please sign in to comment.