Track both server-side and local sync times - fixes #6

pull/40/head
Vitaliy Filippov 2015-10-08 13:30:50 +03:00
parent d9300c953e
commit 1cca10272d
6 changed files with 44 additions and 37 deletions

View File

@ -71,7 +71,7 @@ void Resource::SetState( State new_state )
boost::bind( &Resource::SetState, _1, new_state ) ) ;
}
void Resource::FromRemoteFolder( const Entry& remote, const DateTime& last_sync )
void Resource::FromRemoteFolder( const Entry& remote, const DateTime& last_change )
{
fs::path path = Path() ;
@ -86,7 +86,7 @@ void Resource::FromRemoteFolder( const Entry& remote, const DateTime& last_sync
}
// remote file created after last sync, so remote is newer
else if ( remote.MTime() > last_sync )
else if ( remote.MTime() > last_change )
{
if ( fs::exists( path ) )
{
@ -120,13 +120,13 @@ void Resource::FromRemoteFolder( const Entry& remote, const DateTime& last_sync
/// Update the state according to information (i.e. Entry) from remote. This function
/// compares the modification time and checksum of both copies and determine which
/// one is newer.
void Resource::FromRemote( const Entry& remote, const DateTime& last_sync )
void Resource::FromRemote( const Entry& remote, const DateTime& last_change )
{
// sync folder
if ( remote.IsDir() && IsFolder() )
FromRemoteFolder( remote, last_sync ) ;
FromRemoteFolder( remote, last_change ) ;
else
FromRemoteFile( remote, last_sync ) ;
FromRemoteFile( remote, last_change ) ;
AssignIDs( remote ) ;
@ -152,7 +152,7 @@ void Resource::AssignIDs( const Entry& remote )
}
}
void Resource::FromRemoteFile( const Entry& remote, const DateTime& last_sync )
void Resource::FromRemoteFile( const Entry& remote, const DateTime& last_change )
{
assert( m_parent != 0 ) ;
@ -175,7 +175,7 @@ void Resource::FromRemoteFile( const Entry& remote, const DateTime& last_sync )
{
Trace( "file %1% change stamp = %2%", Path(), remote.ChangeStamp() ) ;
if ( remote.MTime() > last_sync || remote.ChangeStamp() > 0 )
if ( remote.MTime() > last_change || remote.ChangeStamp() > 0 )
{
Log( "file %1% is created in remote (change %2%)", path,
remote.ChangeStamp(), log::verbose ) ;

View File

@ -106,7 +106,7 @@ public :
bool HasID() const ;
std::string MD5() const ;
void FromRemote( const Entry& remote, const DateTime& last_sync ) ;
void FromRemote( const Entry& remote, const DateTime& last_change ) ;
void FromLocal( const DateTime& last_sync ) ;
void Sync( Syncer* syncer, DateTime& sync_time, const Val& options ) ;
@ -128,8 +128,8 @@ private :
private :
void SetState( State new_state ) ;
void FromRemoteFolder( const Entry& remote, const DateTime& last_sync ) ;
void FromRemoteFile( const Entry& remote, const DateTime& last_sync ) ;
void FromRemoteFolder( const Entry& remote, const DateTime& last_change ) ;
void FromRemoteFile( const Entry& remote, const DateTime& last_change ) ;
void DeleteLocal() ;

View File

@ -123,11 +123,11 @@ void ResourceTree::Erase( Resource *coll )
s.erase( s.find( coll ) ) ;
}
void ResourceTree::Update( Resource *coll, const Entry& e, const DateTime& last_sync )
void ResourceTree::Update( Resource *coll, const Entry& e, const DateTime& last_change )
{
assert( coll != 0 ) ;
coll->FromRemote( e, last_sync ) ;
coll->FromRemote( e, last_change ) ;
ReInsert( coll ) ;
}

View File

@ -75,7 +75,7 @@ public :
void Insert( Resource *coll ) ;
void Erase( Resource *coll ) ;
void Update( Resource *coll, const Entry& e, const DateTime& last_sync ) ;
void Update( Resource *coll, const Entry& e, const DateTime& last_change ) ;
Resource* Root() ;
const Resource* Root() const ;

View File

@ -44,9 +44,12 @@ State::State( const fs::path& filename, const Val& options ) :
// the "-f" option will make grive always think remote is newer
Val force ;
if ( options.Get("force", force) && force.Bool() )
m_last_sync = DateTime() ;
{
m_last_change = DateTime() ;
m_last_sync = DateTime::Now() ;
}
Log( "last sync time: %1%", m_last_sync, log::verbose ) ;
Log( "last server change time: %1%; last sync time: %2%", m_last_change, m_last_sync, log::verbose ) ;
}
State::~State()
@ -69,7 +72,7 @@ void State::FromLocal( const fs::path& p, Resource* folder )
{
assert( folder != 0 ) ;
assert( folder->IsFolder() ) ;
// sync the folder itself
folder->FromLocal( m_last_sync ) ;
@ -188,7 +191,7 @@ bool State::Update( const Entry& e )
Log( "%1% is ignored by grive", path, log::verbose ) ;
return true;
}
m_res.Update( res, e, m_last_sync ) ;
m_res.Update( res, e, m_last_change ) ;
}
else if ( Resource *parent = m_res.FindByHref( e.ParentHref() ) )
{
@ -207,7 +210,7 @@ bool State::Update( const Entry& e )
if ( child != 0 )
{
// since we are updating the ID and Href, we need to remove it and re-add it.
m_res.Update( child, e, m_last_sync ) ;
m_res.Update( child, e, m_last_change ) ;
}
// folder entry exist in google drive, but not local. we should create
@ -220,7 +223,7 @@ bool State::Update( const Entry& e )
m_res.Insert( child ) ;
// update the state of the resource
m_res.Update( child, e, m_last_sync ) ;
m_res.Update( child, e, m_last_change ) ;
}
return true ;
@ -246,22 +249,22 @@ State::iterator State::end()
void State::Read( const fs::path& filename )
{
m_last_sync.Assign( 0 ) ;
m_last_change.Assign( 0 ) ;
try
{
File file( filename ) ;
Val json = ParseJson( file );
Val last_sync = json["last_sync"] ;
m_last_sync.Assign(
last_sync["sec"].Int(),
last_sync["nsec"].Int() ) ;
Val last_change = json.Has( "last_change" ) ? json["last_change"] : json["last_sync"] ;
m_last_sync.Assign( last_sync["sec"].Int(), last_sync["nsec"].Int() ) ;
m_last_change.Assign( last_change["sec"].Int(), last_change["nsec"].Int() ) ;
m_cstamp = json["change_stamp"].Int() ;
}
catch ( Exception& )
{
m_last_sync.Assign(0) ;
}
}
@ -271,8 +274,13 @@ void State::Write( const fs::path& filename ) const
last_sync.Add( "sec", Val( (int)m_last_sync.Sec() ) );
last_sync.Add( "nsec", Val( (unsigned)m_last_sync.NanoSec() ) );
Val last_change ;
last_change.Add( "sec", Val( (int)m_last_change.Sec() ) );
last_change.Add( "nsec", Val( (unsigned)m_last_change.NanoSec() ) );
Val result ;
result.Add( "last_sync", last_sync ) ;
result.Add( "last_change", last_change ) ;
result.Add( "change_stamp", Val(m_cstamp) ) ;
std::ofstream fs( filename.string().c_str() ) ;
@ -288,19 +296,17 @@ void State::Sync( Syncer *syncer, const Val& options )
// the last sync time would always be a server time rather than a client time
// TODO - WARNING - do we use the last sync time to compare to client file times
// need to check if this introduces a new problem
DateTime last_sync_time = m_last_sync;
m_res.Root()->Sync( syncer, last_sync_time, options ) ;
DateTime last_change_time = m_last_change;
m_res.Root()->Sync( syncer, last_change_time, options ) ;
if ( last_sync_time == m_last_sync )
{
Trace( "nothing changed? %1%", m_last_sync ) ;
m_last_sync = DateTime::Now();
}
else
{
Trace( "updating last sync? %1%", last_sync_time ) ;
m_last_sync = last_sync_time;
}
if ( last_change_time == m_last_change )
Trace( "nothing changed at the server side since %1%", m_last_change ) ;
else
{
Trace( "updating last server-side change time to %1%", last_change_time ) ;
m_last_change = last_change_time;
}
m_last_sync = DateTime::Now();
}
long State::ChangeStamp() const

View File

@ -75,6 +75,7 @@ private :
private :
ResourceTree m_res ;
DateTime m_last_sync ;
DateTime m_last_change ;
int m_cstamp ;
std::string m_dir ;
boost::regex m_ign ;