mirror of https://github.com/vitalif/grive2
minor clean up
parent
dfbe859531
commit
90bf78e716
|
@ -23,7 +23,7 @@
|
||||||
#include "Entry.hh"
|
#include "Entry.hh"
|
||||||
|
|
||||||
#include "http/Agent.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"
|
||||||
|
@ -97,7 +97,6 @@ Drive::Drive( OAuth2& auth ) :
|
||||||
if ( file.Kind() != "folder" )
|
if ( file.Kind() != "folder" )
|
||||||
{
|
{
|
||||||
Resource *p = m_state.FindFolderByHref( file.ParentHref() ) ;
|
Resource *p = m_state.FindFolderByHref( file.ParentHref() ) ;
|
||||||
Trace( "finding parent of %1%: %2%", file.Title(), (void*)p ) ;
|
|
||||||
if ( file.Filename().empty() )
|
if ( file.Filename().empty() )
|
||||||
Log( "file \"%1%\" is a google document, ignored", file.Title() ) ;
|
Log( "file \"%1%\" is a google document, ignored", file.Title() ) ;
|
||||||
|
|
||||||
|
@ -118,8 +117,8 @@ Trace( "finding parent of %1%: %2%", file.Title(), (void*)p ) ;
|
||||||
|
|
||||||
if ( has_next )
|
if ( has_next )
|
||||||
{
|
{
|
||||||
http::ResponseLog log2( "second-", ".xml", &xrsp ) ;
|
// http::ResponseLog log2( "second-", ".xml", &xrsp ) ;
|
||||||
http.Get( nss["@href"], &log2, m_http_hdr ) ;
|
http.Get( nss["@href"], &xrsp, m_http_hdr ) ;
|
||||||
resp = xrsp.Response() ;
|
resp = xrsp.Response() ;
|
||||||
}
|
}
|
||||||
} while ( has_next ) ;
|
} while ( has_next ) ;
|
||||||
|
@ -133,9 +132,9 @@ void Drive::SaveState()
|
||||||
void Drive::ConstructDirTree( http::Agent *http )
|
void Drive::ConstructDirTree( http::Agent *http )
|
||||||
{
|
{
|
||||||
http::XmlResponse xml ;
|
http::XmlResponse xml ;
|
||||||
http::ResponseLog log( "dir-", ".xml", &xml ) ;
|
// http::ResponseLog log( "dir-", ".xml", &xml ) ;
|
||||||
|
|
||||||
http->Get( feed_base + "/-/folder?max-results=10&showroot=true", &log, m_http_hdr ) ;
|
http->Get( feed_base + "/-/folder?max-results=10&showroot=true", &xml, m_http_hdr ) ;
|
||||||
|
|
||||||
xml::Node resp = xml.Response() ;
|
xml::Node resp = xml.Response() ;
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,10 @@ Resource::Resource() :
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// construct from previously serialized JSON object
|
/// Construct from previously serialized JSON object. The state of the
|
||||||
|
/// resource is treated as local_deleted by default. It is because the
|
||||||
|
/// state will be updated by scanning the local directory. If the state
|
||||||
|
/// is not updated during scanning, that means the resource is deleted.
|
||||||
Resource::Resource( const Json& json, Resource *parent ) :
|
Resource::Resource( const Json& json, Resource *parent ) :
|
||||||
m_entry (
|
m_entry (
|
||||||
json["name"].Str(),
|
json["name"].Str(),
|
||||||
|
@ -56,8 +59,11 @@ Resource::Resource( const Json& json, Resource *parent ) :
|
||||||
DateTime( json["mtime"]["sec"].Int(), json["mtime"]["nsec"].Int() ),
|
DateTime( json["mtime"]["sec"].Int(), json["mtime"]["nsec"].Int() ),
|
||||||
parent != 0 ? parent->SelfHref() : "" ),
|
parent != 0 ? parent->SelfHref() : "" ),
|
||||||
m_parent( parent ),
|
m_parent( parent ),
|
||||||
m_state( local_new )
|
m_state( local_deleted )
|
||||||
{
|
{
|
||||||
|
// if the file exists in local directory, FromLocal() will mark the
|
||||||
|
// state as local_changed
|
||||||
|
FromLocal() ;
|
||||||
}
|
}
|
||||||
|
|
||||||
Resource::Resource( const xml::Node& entry ) :
|
Resource::Resource( const xml::Node& entry ) :
|
||||||
|
@ -79,7 +85,6 @@ Resource::Resource( const fs::path& path ) :
|
||||||
m_parent( 0 ),
|
m_parent( 0 ),
|
||||||
m_state ( local_new )
|
m_state ( local_new )
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the state according to information (i.e. Entry) from remote. This function
|
/// Update the state according to information (i.e. Entry) from remote. This function
|
||||||
|
@ -94,12 +99,32 @@ void Resource::FromRemote( const Entry& remote )
|
||||||
// use mtime to check which one is more recent
|
// use mtime to check which one is more recent
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_state = ( remote.MTime() > m_entry.MTime() ? remote_changed : local_changed ) ;
|
assert( m_state == local_new || m_state == local_changed || m_state == local_deleted ) ;
|
||||||
|
|
||||||
|
m_state = ( remote.MTime() > m_entry.MTime() ? remote_changed : m_state ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_entry = remote ;
|
m_entry = remote ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Resource::FromLocal()
|
||||||
|
{
|
||||||
|
fs::path path = Path() ;
|
||||||
|
if ( !fs::exists( path ) )
|
||||||
|
m_state = local_deleted ;
|
||||||
|
|
||||||
|
// to save time, compare mtime before checksum
|
||||||
|
else if ( m_entry.MTime() > os::FileMTime( path ) )
|
||||||
|
{
|
||||||
|
if ( m_entry.MD5() == crypt::MD5( path ) )
|
||||||
|
m_state = local_new ;
|
||||||
|
else
|
||||||
|
m_state = local_changed ;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_state = local_changed ;
|
||||||
|
}
|
||||||
|
|
||||||
std::string Resource::SelfHref() const
|
std::string Resource::SelfHref() const
|
||||||
{
|
{
|
||||||
return m_entry.SelfHref() ;
|
return m_entry.SelfHref() ;
|
||||||
|
|
|
@ -71,6 +71,8 @@ public :
|
||||||
bool IsInRootTree() const ;
|
bool IsInRootTree() const ;
|
||||||
|
|
||||||
void FromRemote( const Entry& e ) ;
|
void FromRemote( const Entry& e ) ;
|
||||||
|
void FromLocal() ;
|
||||||
|
|
||||||
void Sync( http::Agent *http, const http::Headers& auth ) ;
|
void Sync( http::Agent *http, const http::Headers& auth ) ;
|
||||||
void Delete( http::Agent* http, const http::Headers& auth ) ;
|
void Delete( http::Agent* http, const http::Headers& auth ) ;
|
||||||
|
|
||||||
|
@ -96,6 +98,9 @@ private :
|
||||||
/// than remote. We should upload local copy to overwrite remote.
|
/// than remote. We should upload local copy to overwrite remote.
|
||||||
local_changed,
|
local_changed,
|
||||||
|
|
||||||
|
/// Resource deleted from local since last time grive has checked.
|
||||||
|
local_deleted,
|
||||||
|
|
||||||
/// Resource created in google drive, but not exist in local.
|
/// Resource created in google drive, but not exist in local.
|
||||||
/// We should download the file.
|
/// We should download the file.
|
||||||
remote_new,
|
remote_new,
|
||||||
|
|
|
@ -75,11 +75,7 @@ void State::FromLocal( const fs::path& p, gr::Resource* folder )
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Resource *c = folder->FindChild( fname ) ;
|
Resource *c = folder->FindChild( fname ) ;
|
||||||
if ( c != 0 )
|
if ( c == 0 )
|
||||||
{
|
|
||||||
Trace( "wow! file %1% is loaded from previous state", fname ) ;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
c = new Resource( i->path() ) ;
|
c = new Resource( i->path() ) ;
|
||||||
folder->AddChild( c ) ;
|
folder->AddChild( c ) ;
|
||||||
|
@ -133,14 +129,12 @@ bool State::Update( const Entry& e )
|
||||||
{
|
{
|
||||||
assert( !e.ParentHref().empty() ) ;
|
assert( !e.ParentHref().empty() ) ;
|
||||||
|
|
||||||
Resource *r = m_res.FindByHref( e.SelfHref() ) ;
|
if ( Resource *res = m_res.FindByHref( e.SelfHref() ) )
|
||||||
if ( r != 0 )
|
|
||||||
{
|
{
|
||||||
Trace( "wow! find %1% from state file?", r->Name() ) ;
|
m_res.Update( res, e ) ;
|
||||||
|
return true ;
|
||||||
}
|
}
|
||||||
|
else if ( Resource *parent = m_res.FindByHref( e.ParentHref() ) )
|
||||||
Resource *parent = m_res.FindByHref( e.ParentHref() ) ;
|
|
||||||
if ( parent != 0 )
|
|
||||||
{
|
{
|
||||||
assert( parent->IsFolder() ) ;
|
assert( parent->IsFolder() ) ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue