diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 258ce94..f0d454f 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -139,28 +139,32 @@ void Drive::DetectChanges() SyncFolders( &http ) ; Log( "Reading remote server file list", log::info ) ; - http::XmlResponse xrsp ; - http.Get( feed_base + "?showfolders=true&showroot=true", &xrsp, m_http_hdr ) ; - xml::Node resp = xrsp.Response() ; - - m_resume_link = resp["link"]. + Feed feed ; +// feed.EnableLog( "/tmp/file", ".xml" ) ; + feed.Start( &http, m_http_hdr, feed_base + "?showfolders=true&showroot=true" ) ; + + m_resume_link = feed.Root()["link"]. Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ; - - Feed feed( resp ) ; + do { - std::for_each( feed.begin(), feed.end(), boost::bind( &Drive::FromRemote, this, _1 ) ) ; + std::for_each( + feed.begin(), feed.end(), + boost::bind( &Drive::FromRemote, this, _1 ) ) ; + } while ( feed.GetNext( &http, m_http_hdr ) ) ; // pull the changes feed if ( prev_stamp != -1 ) { - http::ResponseLog log( "/tmp/changes-", ".xml", &xrsp ) ; Log( "Detecting changes from last sync", log::info ) ; - http.Get( ChangesFeed(prev_stamp+1), &log, m_http_hdr ) ; + Feed changes ; +// feed.EnableLog( "/tmp/changes", ".xml" ) ; + feed.Start( &http, m_http_hdr, ChangesFeed(prev_stamp+1) ) ; - Feed changes( xrsp.Response() ) ; - std::for_each( changes.begin(), changes.end(), boost::bind( &Drive::FromChange, this, _1 ) ) ; + std::for_each( + changes.begin(), changes.end(), + boost::bind( &Drive::FromChange, this, _1 ) ) ; } } diff --git a/libgrive/src/drive/Feed.cc b/libgrive/src/drive/Feed.cc index bbb1b7f..72bfa9a 100644 --- a/libgrive/src/drive/Feed.cc +++ b/libgrive/src/drive/Feed.cc @@ -20,13 +20,20 @@ #include "Feed.hh" #include "http/Agent.hh" +#include "http/ResponseLog.hh" #include "http/XmlResponse.hh" #include "xml/NodeSet.hh" +#include + #include namespace gr { +Feed::Feed( ) +{ +} + Feed::Feed( const xml::Node& root ) : m_root ( root ), m_entries ( m_root["entry"] ) @@ -55,6 +62,22 @@ std::string Feed::Next() const return nss.empty() ? "" : std::string(nss["@href"]) ; } +void Feed::Start( http::Agent *http, const http::Header& auth, const std::string& url ) +{ + http::XmlResponse xrsp ; + http::ResponseLog log( &xrsp ) ; + + if ( m_log.get() != 0 ) + log.Reset( + (boost::format( "%1%-%2%." ) % m_log->prefix % m_log->sequence++).str(), + m_log->suffix, &xrsp ) ; + + http->Get( url, &log, auth ) ; + + m_root = xrsp.Response() ; + m_entries = m_root["entry"] ; +} + bool Feed::GetNext( http::Agent *http, const http::Header& auth ) { assert( http != 0 ) ; @@ -62,12 +85,7 @@ bool Feed::GetNext( http::Agent *http, const http::Header& auth ) xml::NodeSet nss = m_root["link"].Find( "@rel", "next" ) ; if ( !nss.empty() ) { - http::XmlResponse xrsp ; - http->Get( nss["@href"], &xrsp, auth ) ; - - m_root = xrsp.Response() ; - m_entries = m_root["entry"] ; - + Start( http, auth, nss["@href"] ) ; return true ; } else @@ -90,4 +108,17 @@ Feed::iterator::reference Feed::iterator::dereference() const return Entry( *base_reference() ) ; } +void Feed::EnableLog( const std::string& prefix, const std::string& suffix ) +{ + m_log.reset( new LogInfo ) ; + m_log->prefix = prefix ; + m_log->suffix = suffix ; + m_log->sequence = 0 ; +} + +const xml::Node& Feed::Root() const +{ + return m_root ; +} + } // end of namespace diff --git a/libgrive/src/drive/Feed.hh b/libgrive/src/drive/Feed.hh index 321498c..f41fb7d 100644 --- a/libgrive/src/drive/Feed.hh +++ b/libgrive/src/drive/Feed.hh @@ -43,15 +43,29 @@ public : public : explicit Feed( const xml::Node& root ) ; + Feed( ) ; + void Start( http::Agent *http, const http::Header& auth, const std::string& url ) ; + void Assign( const xml::Node& root ) ; + const xml::Node& Root() const ; iterator begin() const ; iterator end() const ; std::string Next() const ; bool GetNext( http::Agent *http, const http::Header& auth ) ; - + + void EnableLog( const std::string& prefix, const std::string& suffix ) ; + private : + struct LogInfo + { + std::string prefix ; + std::string suffix ; + std::size_t sequence ; + } ; + std::auto_ptr m_log ; + xml::Node m_root ; xml::NodeSet m_entries ; } ; diff --git a/libgrive/src/drive/Resource.cc b/libgrive/src/drive/Resource.cc index a4e8501..4c7e813 100644 --- a/libgrive/src/drive/Resource.cc +++ b/libgrive/src/drive/Resource.cc @@ -259,7 +259,10 @@ void Resource::FromLocal( const DateTime& last_sync ) // remote_deleted first, it will be updated to sync/remote_changed // in FromRemote() else + { +// Trace( "file %1% mtime %2%, last_sync %3%", path, m_mtime, last_sync ) ; m_state = ( m_mtime > last_sync ? local_new : remote_deleted ) ; + } m_name = Path2Str( path.filename() ) ; m_kind = fs::is_directory(path) ? "folder" : "file" ; diff --git a/libgrive/src/drive/State.cc b/libgrive/src/drive/State.cc index dddb012..373e1b3 100644 --- a/libgrive/src/drive/State.cc +++ b/libgrive/src/drive/State.cc @@ -73,7 +73,6 @@ void State::FromLocal( const fs::path& p, gr::Resource* folder ) for ( fs::directory_iterator i( p ) ; i != fs::directory_iterator() ; ++i ) { std::string fname = Path2Str(i->path().filename()) ; -// Trace( "found file %1%", i->path() ) ; if ( IsIgnore(fname) ) Log( "file %1% is ignored by grive", fname, log::verbose ) ; diff --git a/libgrive/src/http/ResponseLog.cc b/libgrive/src/http/ResponseLog.cc index 0e7e631..8de81fe 100644 --- a/libgrive/src/http/ResponseLog.cc +++ b/libgrive/src/http/ResponseLog.cc @@ -29,14 +29,28 @@ ResponseLog::ResponseLog( const std::string& prefix, const std::string& suffix, Receivable *next ) : - m_log( Filename(prefix, suffix).c_str() ), - m_next( next ) + m_enabled ( true ), + m_log ( Filename(prefix, suffix).c_str() ), + m_next ( next ) { + assert( m_next != 0 ) ; +} + +ResponseLog::ResponseLog( Receivable *next ) : + m_enabled ( false ), + m_next ( next ) +{ + assert( m_next != 0 ) ; } std::size_t ResponseLog::OnData( void *data, std::size_t count ) { - m_log.rdbuf()->sputn( reinterpret_cast(data), count ) ; + if ( m_enabled ) + { + assert( m_log.rdbuf() != 0 ) ; + m_log.rdbuf()->sputn( reinterpret_cast(data), count ) ; + } + return m_next->OnData( data, count ) ; } @@ -51,4 +65,21 @@ std::string ResponseLog::Filename( const std::string& prefix, const std::string& return prefix + DateTime::Now().Format( "%H%M%S" ) + suffix ; } +void ResponseLog::Enable( bool enable ) +{ + m_enabled = enable ; +} + +void ResponseLog::Reset( const std::string& prefix, const std::string& suffix, Receivable *next ) +{ + assert( next != 0 ) ; + + if ( m_log.is_open() ) + m_log.close() ; + + m_log.open( Filename(prefix, suffix).c_str() ) ; + m_next = next ; + m_enabled = true ; +} + }} // end of namespace diff --git a/libgrive/src/http/ResponseLog.hh b/libgrive/src/http/ResponseLog.hh index a427324..9b7de65 100644 --- a/libgrive/src/http/ResponseLog.hh +++ b/libgrive/src/http/ResponseLog.hh @@ -33,14 +33,19 @@ public : const std::string& prefix, const std::string& suffix, Receivable *next ) ; - + ResponseLog( Receivable *next ) ; + std::size_t OnData( void *data, std::size_t count ) ; void Clear() ; + void Enable( bool enable = true ) ; + void Reset( const std::string& prefix, const std::string& suffix, Receivable *next ) ; + private : static std::string Filename( const std::string& prefix, const std::string& suffix ) ; private : + bool m_enabled ; std::ofstream m_log ; Receivable *m_next ; } ;