From 7830136518eb27c547018c51fa3a8fb44d835ec0 Mon Sep 17 00:00:00 2001 From: Matchman Green Date: Thu, 31 May 2012 19:22:18 +0800 Subject: [PATCH] fixed the log messages and some state update --- libgrive/src/drive/Drive.cc | 8 ++- libgrive/src/drive/Entry.cc | 12 +++-- libgrive/src/drive/Entry.hh | 2 +- libgrive/src/drive/Resource.cc | 93 +++++++++++++++++----------------- libgrive/src/drive/Resource.hh | 2 +- libgrive/src/drive/State.cc | 7 ++- libgrive/src/util/Log.hh | 24 ++++++++- 7 files changed, 91 insertions(+), 57 deletions(-) diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 913d855..68bfc7f 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -97,7 +97,7 @@ Drive::Drive( OAuth2& auth ) : else if ( parent != 0 && !parent->IsFolder() ) Log( "warning: entry %1% has parent %2% which is not a folder, ignored", - entry.Title(), parent->Name(), log::warning ) ; + entry.Title(), parent->Name(), log::verbose ) ; else m_state.FromRemote( entry ) ; @@ -122,6 +122,8 @@ void Drive::SaveState() void Drive::SyncFolders( http::Agent *http ) { + Log( "Synchronizing folders", log::info ) ; + http::XmlResponse xml ; http->Get( feed_base + "/-/folder?max-results=50&showroot=true", &xml, m_http_hdr ) ; @@ -138,7 +140,7 @@ void Drive::SyncFolders( http::Agent *http ) if ( e.Kind() == "folder" ) { if ( e.ParentHrefs().size() != 1 ) - Log( "folder \"%1%\" has multiple parents, ignored", e.Title(), log::warning ) ; + Log( "folder \"%1%\" has multiple parents, ignored", e.Title(), log::verbose ) ; else if ( e.Title().find('/') != std::string::npos ) Log( "folder \"%1%\" contains a slash in its name, ignored", e.Title(), log::verbose ) ; @@ -161,6 +163,8 @@ void Drive::SyncFolders( http::Agent *http ) void Drive::Update() { + Log( "Synchronizing files", log::info ) ; + http::Agent http ; m_state.Sync( &http, m_http_hdr ) ; } diff --git a/libgrive/src/drive/Entry.cc b/libgrive/src/drive/Entry.cc index 5c64689..2f39e0c 100644 --- a/libgrive/src/drive/Entry.cc +++ b/libgrive/src/drive/Entry.cc @@ -48,12 +48,17 @@ Entry::Entry( const xml::Node& n ) } /// construct an entry from a file or folder in local directory -Entry::Entry( const fs::path& path ) : +Entry::Entry( const fs::path& path, const std::string& kind ) : m_title ( path.filename().string() ), m_filename ( path.filename().string() ), - m_kind ( fs::is_directory(path) ? "folder" : "file" ), + m_kind + ( + fs::exists(path) + ? (fs::is_directory(path) ? "folder" : "file" ) + : kind + ), m_md5 ( fs::is_directory(path) ? "" : crypt::MD5( path ) ), - m_mtime ( os::FileMTime( path ) ) + m_mtime ( fs::exists(path) ? os::FileMTime( path ) : DateTime() ) { } @@ -109,6 +114,7 @@ const std::vector& Entry::ParentHrefs() const void Entry::AssignID( const Entry& entry ) { m_self_href = entry.m_self_href ; + m_content_src = entry.m_content_src ; m_resource_id = entry.m_resource_id ; m_parent_hrefs = entry.m_parent_hrefs ; m_edit_link = entry.m_edit_link ; diff --git a/libgrive/src/drive/Entry.hh b/libgrive/src/drive/Entry.hh index d868d81..5dac22e 100644 --- a/libgrive/src/drive/Entry.hh +++ b/libgrive/src/drive/Entry.hh @@ -43,7 +43,7 @@ class Entry { public : Entry( ) ; - explicit Entry( const fs::path& path ) ; + explicit Entry( const fs::path& path, const std::string& kind = "" ) ; explicit Entry( const xml::Node& n ) ; explicit Entry( const std::string& name, diff --git a/libgrive/src/drive/Resource.cc b/libgrive/src/drive/Resource.cc index 143aea2..b74c845 100644 --- a/libgrive/src/drive/Resource.cc +++ b/libgrive/src/drive/Resource.cc @@ -56,28 +56,7 @@ Resource::Resource() : m_state ( sync ) { } -/* -/// 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 ) : - m_entry ( - json["name"].Str(), - json["id"].Str(), - json["href"].Str(), - json["md5"].Str(), - json["kind"].Str(), - DateTime( json["mtime"]["sec"].Int(), json["mtime"]["nsec"].Int() ), - parent != 0 ? parent->SelfHref() : "" ), - m_parent( parent ), - 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 ) : m_entry ( entry ), m_parent( 0 ), @@ -92,8 +71,8 @@ Resource::Resource( const Entry& entry, Resource *parent ) : { } -Resource::Resource( const fs::path& path ) : - m_entry ( path ), +Resource::Resource( const fs::path& path, const std::string& kind ) : + m_entry ( path, kind ), m_parent( 0 ), m_state ( local_new ) { @@ -119,39 +98,57 @@ void Resource::FromRemote( const Entry& remote, const DateTime& last_sync ) // remote file created after last sync, so remote is newer else if ( remote.MTime() > last_sync ) { - Log( "creating %1% directory", path, log::info ) ; - fs::create_directories( path ) ; - m_state = sync ; + if ( fs::exists( path ) ) + { + // TODO: handle type change + Log( "%1% changed from folder to file", path, log::verbose ) ; + m_state = sync ; + } + else + { + Log( "folder %1% is created in local", path, log::verbose ) ; + fs::create_directories( path ) ; + m_state = sync ; + } } else { - Trace( "should I delete the local directory %1%?", path ) ; - m_state = local_deleted ; + if ( fs::exists( path ) ) + { + // TODO: handle type chage + Log( "%1% changed from file to folder", path, log::verbose ) ; + m_state = sync ; + } + else + { + Log( "folder %1% is deleted in local", path, log::verbose ) ; + m_state = local_deleted ; + } } } - // if checksum is equal, no need to compare the mtime - else if ( remote.MD5() == m_entry.MD5() ) - { - Log( "MD5 matches: %1% is already in sync", Path(), log::verbose ) ; - m_state = sync ; - } - // local not exists else if ( !fs::exists( path ) ) { if ( remote.MTime() > last_sync ) { - Trace( "new file found in remote", path ) ; + Log( "file %1% is created in remote", path, log::verbose ) ; m_state = remote_new ; } else { - Trace( "should I delete the local file %1%?", path ) ; + Log( "file %1% should be erased in remote", path, log::verbose ) ; m_state = local_deleted ; } } + // if checksum is equal, no need to compare the mtime + else if ( remote.MD5() == m_entry.MD5() ) + { + Log( "file %1% is already in sync", Path(), log::verbose ) ; + m_state = sync ; + } + // use mtime to check which one is more recent else { @@ -159,13 +156,19 @@ void Resource::FromRemote( const Entry& remote, const DateTime& last_sync ) // if remote is modified if ( remote.MTime() > m_entry.MTime() ) + { + Log( "file %1% is changed in remote", path, log::verbose ) ; m_state = remote_changed ; + } // remote also has the file, so it's not new in local else if ( m_state == local_new || m_state == remote_deleted ) + { + Log( "file %1% is changed in local", path, log::verbose ) ; m_state = local_changed ; + } - Log( "%1% state is %2%", Name(), m_state, log::verbose ) ; + Trace( "%1% state is %2%", Name(), m_state ) ; } m_entry.AssignID( remote ) ; @@ -190,7 +193,7 @@ void Resource::FromLocal( const DateTime& last_sync ) DateTime mtime = os::FileMTime( path ) ; m_state = ( mtime > last_sync ? local_new : remote_deleted ) ; - Log( "%1% found on disk: %2%", Name(), m_state ) ; + Trace( "%1% found on disk: %2%", Name(), m_state ) ; } } @@ -287,8 +290,7 @@ void Resource::Sync( http::Agent *http, const http::Headers& auth ) switch ( m_state ) { case local_new : - Log( "sync %1% doesn't exist in server. upload \"%2%\"?", - Path(), m_parent->m_entry.CreateLink(), log::verbose ) ; + Log( "sync %1% doesn't exist in server, uploading", Path(), log::verbose ) ; if ( Create( http, auth ) ) m_state = sync ; @@ -299,14 +301,14 @@ void Resource::Sync( http::Agent *http, const http::Headers& auth ) break ; case local_changed : - Log( "sync %1% changed in local", Path(), log::verbose ) ; + Log( "sync %1% changed in local. uploading", Path(), log::verbose ) ; if ( EditContent( http, auth ) ) m_state = sync ; break ; case remote_new : case remote_changed : - Log( "sync %1% changed in remote. download?", Path(), log::verbose ) ; + Log( "sync %1% changed in remote. downloading", Path(), log::verbose ) ; Download( http, Path(), auth ) ; m_state = sync ; break ; @@ -336,7 +338,6 @@ void Resource::Delete( http::Agent *http, const http::Headers& auth ) void Resource::Download( http::Agent* http, const fs::path& file, const http::Headers& auth ) const { - Log( "Downloading %1%", file ) ; http::Download dl( file.string(), http::Download::NoChecksum() ) ; long r = http->Get( m_entry.ContentSrc(), &dl, auth ) ; if ( r <= 400 ) @@ -401,8 +402,6 @@ bool Resource::Create( http::Agent* http, const http::Headers& auth ) bool Resource::Upload( http::Agent* http, const std::string& link, const http::Headers& auth, bool post ) { - Log( "Uploading %1%", m_entry.Title() ) ; - StdioFile file( Path() ) ; // TODO: upload in chunks diff --git a/libgrive/src/drive/Resource.hh b/libgrive/src/drive/Resource.hh index 25b2299..4b41bb5 100644 --- a/libgrive/src/drive/Resource.hh +++ b/libgrive/src/drive/Resource.hh @@ -49,7 +49,7 @@ public : Resource() ; explicit Resource( const xml::Node& entry ) ; explicit Resource( const Entry& entry, Resource *parent = 0 ) ; - explicit Resource( const fs::path& path ) ; + explicit Resource( const fs::path& path, const std::string& kind = "" ) ; // explicit Resource( const Json& json, Resource *parent = 0 ) ; void Swap( Resource& coll ) ; diff --git a/libgrive/src/drive/State.cc b/libgrive/src/drive/State.cc index 3a8234e..f2b866d 100644 --- a/libgrive/src/drive/State.cc +++ b/libgrive/src/drive/State.cc @@ -63,7 +63,7 @@ void State::FromLocal( const fs::path& p, gr::Resource* folder ) std::string fname = i->path().filename().string() ; if ( fname[0] == '.' ) - Log( "file %1% is ignored by grive", fname, log::info ) ; + Log( "file %1% is ignored by grive", fname, log::verbose ) ; else { @@ -148,9 +148,12 @@ bool State::Update( const Entry& e ) // the directory else if ( e.Kind() == "folder" || !e.Filename().empty() ) { - child = new Resource( e ) ; + // first create a dummy resource and update it later + child = new Resource( parent->Path() / e.Filename(), e.Kind() ) ; parent->AddChild( child ) ; m_res.Insert( child ) ; + + // update the state of the resource m_res.Update( child, e, m_last_sync ) ; } else diff --git a/libgrive/src/util/Log.hh b/libgrive/src/util/Log.hh index 8514b73..45360e0 100644 --- a/libgrive/src/util/Log.hh +++ b/libgrive/src/util/Log.hh @@ -25,7 +25,29 @@ namespace gr { namespace log { - enum Serverity { debug, verbose, info, warning, error, critical } ; + /// defines the types of the log message + enum Serverity + { + /// user unfriendly messages. only meant for developers. + debug, + + /// enabled only if -V is specified. grive tries to tell you every + /// single thing it tries to do. + verbose, + + /// notification messages indicates nothing is going wrong + info, + + /// potential error messages + warning, + + /// an error has occurs but grive doesn't need to quit + error, + + /// grive cannot proceed + critical + } ; + typedef boost::format Fmt ; }