adding change stamp

pull/40/head
Nestal Wan 2012-06-03 23:58:28 +08:00
parent 28b32c6f43
commit 8466095e7b
7 changed files with 101 additions and 26 deletions

View File

@ -143,10 +143,10 @@ int main( int argc, char **argv )
OAuth2 token( refresh_token, client_id, client_secret ) ;
Drive drive( token, options ) ;
drive.Update() ;
drive.SaveState() ;
config.Save() ;
// drive.Update() ;
// drive.SaveState() ;
//
// config.Save() ;
}
catch ( gr::Exception& e )
{

View File

@ -63,10 +63,20 @@ Drive::Drive( OAuth2& auth, const Json& options ) :
m_state.FromLocal( "." ) ;
http::Agent http ;
long prev_stamp = m_state.ChangeStamp() ;
// get metadata
http::XmlResponse xrsp ;
http::ResponseLog log( "meta-", ".xml", &xrsp ) ;
http.Get( "https://docs.google.com/feeds/metadata/default", &log, m_http_hdr ) ;
Trace( "return %1%", xrsp.Response()["docs:largestChangestamp"] ) ;
m_state.ChangeStamp(
std::atoi(xrsp.Response()["docs:largestChangestamp"]["@value"].front().Value().c_str()) ) ;
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() ;
@ -115,6 +125,41 @@ Drive::Drive( OAuth2& auth, const Json& options ) :
resp = xrsp.Response() ;
}
} while ( has_next ) ;
// pull the changes feed
boost::format changes_uri( "https://docs.google.com/feeds/default/private/changes?start-index=%1%" ) ;
http::ResponseLog log2( "changes-", ".xml", &xrsp ) ;
http.Get( (changes_uri%(prev_stamp+1)).str(), &log2, m_http_hdr ) ;
xml::NodeSet centries = xrsp.Response()["entry"] ;
for ( xml::NodeSet::iterator i = centries.begin() ; i != centries.end() ; ++i )
{
if ( (*i)["content"] == "" )
continue ;
Entry entry( *i ) ;
if ( entry.Kind() != "folder" )
{
Resource *parent = m_state.FindByHref( entry.ParentHref() ) ;
std::string fn = entry.Filename() ;
if ( fn.empty() )
Log( "file \"%1%\" is a google document, ignored", entry.Title(), log::verbose ) ;
else if ( fn.find('/') != fn.npos )
Log( "file \"%1%\" contains a slash in its name, ignored", entry.Title(), log::verbose ) ;
else if ( parent == 0 || !parent->IsInRootTree() )
Log( "file \"%1%\" parent doesn't exist, ignored", entry.Title(), log::verbose ) ;
else if ( parent != 0 && !parent->IsFolder() )
Log( "warning: entry %1% has parent %2% which is not a folder, ignored",
entry.Title(), parent->Name(), log::verbose ) ;
else
m_state.FromRemote( entry ) ;
}
}
}
void Drive::SaveState()

View File

@ -37,12 +37,14 @@ Entry::Entry( ) :
m_kind ( "folder" ),
m_resource_id ( "folder:root" ),
m_self_href ( root_href ),
m_create_link ( root_create )
m_create_link ( root_create ),
m_change_stamp ( -1 )
{
}
/// construct an entry for remote
Entry::Entry( const xml::Node& n )
Entry::Entry( const xml::Node& n ) :
m_change_stamp( -1 )
{
Update( n ) ;
}
@ -51,7 +53,8 @@ Entry::Entry( const xml::Node& n )
Entry::Entry( const std::string& name, const std::string& kind ) :
m_title ( name ),
m_filename ( name ),
m_kind ( kind )
m_kind ( kind ),
m_change_stamp( -1 )
{
}
@ -79,6 +82,9 @@ void Entry::Update( const xml::Node& n )
m_edit_link = n["link"].Find( "@rel", "http://schemas.google.com/g/2005#resumable-edit-media")["@href"] ;
m_create_link = n["link"].Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media")["@href"] ;
xml::NodeSet cs = n["docs:changestamp"]["@value"] ;
m_change_stamp = cs.empty() ? -1 : std::atoi( cs.front().Value().c_str() ) ;
m_parent_hrefs.clear( ) ;
xml::NodeSet parents = n["link"].Find( "@rel", "http://schemas.google.com/docs/2007#parent" ) ;
for ( xml::NodeSet::iterator i = parents.begin() ; i != parents.end() ; ++i )
@ -188,6 +194,13 @@ void Entry::Swap( Entry& e )
m_create_link.swap( e.m_create_link ) ;
m_mtime.Swap( e.m_mtime ) ;
std::swap( m_change_stamp, e.m_change_stamp ) ;
}
long Entry::ChangeStamp() const
{
return m_change_stamp ;
}
} // end of namespace

View File

@ -71,6 +71,8 @@ public :
void Update( const xml::Node& entry ) ;
void Update( const std::string& md5, const DateTime& mtime ) ;
long ChangeStamp() const ;
private :
std::string m_title ;
std::string m_filename ;
@ -86,6 +88,8 @@ private :
std::string m_edit_link ;
std::string m_create_link ;
long m_change_stamp ;
DateTime m_mtime ;
} ;

View File

@ -152,7 +152,9 @@ void Resource::FromRemoteFile( const Entry& remote, const DateTime& last_sync )
// local not exists
else if ( !fs::exists( path ) )
{
if ( remote.MTime() > last_sync )
Trace( "file %1% change stamp = %2%", Path(), remote.ChangeStamp() ) ;
if ( remote.MTime() > last_sync || remote.ChangeStamp() > 0 )
{
Log( "file %1% is created in remote", path, log::verbose ) ;
m_state = remote_new ;
@ -211,6 +213,7 @@ void Resource::FromLocal( const DateTime& last_sync )
m_state = ( mtime > last_sync ? local_new : remote_deleted ) ;
m_entry.FromLocal( path ) ;
Log( "file %1% read from local %2%", path, m_state, log::verbose ) ;
}
assert( m_state != unknown ) ;
@ -384,26 +387,17 @@ void Resource::DeleteLocal()
void Resource::DeleteRemote( http::Agent *http, const http::Header& auth )
{
http::Header hdr( auth ) ;
hdr.Add( "If-Match: " + m_entry.ETag() ) ;
http::StringResponse str ;
try
{
try
{
http::XmlResponse xml ;
http->Get( m_entry.SelfHref(), &xml, hdr ) ;
m_entry.Update( xml.Response() ) ;
}
catch ( Exception& e1 )
{
// don't rethrow here. there are some cases that I don't know why
// the delete will fail.
Trace( "Get Exception %1% %2%",
boost::diagnostic_information(e1),
str.Response() ) ;
}
http::Header hdr( auth ) ;
hdr.Add( "If-Match: " + m_entry.ETag() ) ;
// doesn't know why, but an update before deleting seems to work always
http::XmlResponse xml ;
http->Get( m_entry.SelfHref(), &xml, hdr ) ;
m_entry.Update( xml.Response() ) ;
http->Custom( "DELETE", m_entry.SelfHref(), &str, hdr ) ;
}

View File

@ -47,6 +47,8 @@ State::State( const fs::path& filename, const Json& options )
Json force ;
if ( options.Get("force", force) && force.Bool() )
m_last_sync = DateTime() ;
Log( "last sync time: %1%", m_last_sync, log::info ) ;
}
/// Synchronize local directory. Build up the resource tree from files and folders
@ -200,6 +202,8 @@ void State::Read( const fs::path& filename )
m_last_sync.Assign(
last_sync["sec"].Int(),
last_sync["nsec"].Int() ) ;
m_cstamp = json["change_stamp"].Int() ;
}
catch ( Exception& )
{
@ -215,6 +219,7 @@ void State::Write( const fs::path& filename ) const
Json result ;
result.Add( "last_sync", last_sync ) ;
result.Add( "change_stamp", Json(m_cstamp) ) ;
std::ofstream fs( filename.string().c_str() ) ;
fs << result ;
@ -228,4 +233,14 @@ void State::Sync( http::Agent *http, const http::Header& auth )
m_last_sync = DateTime::Now() ;
}
long State::ChangeStamp() const
{
return m_cstamp ;
}
void State::ChangeStamp( long cstamp )
{
m_cstamp = cstamp ;
}
} // end of namespace

View File

@ -62,6 +62,9 @@ public :
iterator begin() ;
iterator end() ;
long ChangeStamp() const ;
void ChangeStamp( long cstamp ) ;
private :
void FromLocal( const fs::path& p, Resource *folder ) ;
bool Update( const Entry& e ) ;
@ -70,6 +73,7 @@ private :
private :
ResourceTree m_res ;
DateTime m_last_sync ;
long m_cstamp ;
std::vector<Entry> m_unresolved ;
} ;