grive2/libgrive/src/drive/Drive.cc

201 lines
5.0 KiB
C++
Raw Normal View History

2012-04-25 20:13:17 +04:00
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
2012-04-25 20:13:17 +04:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "Drive.hh"
2012-05-06 18:27:52 +04:00
#include "CommonUri.hh"
#include "Entry.hh"
2012-06-03 21:07:14 +04:00
#include "Feed.hh"
#include "http/Agent.hh"
#include "http/ResponseLog.hh"
2012-05-06 13:13:48 +04:00
#include "http/XmlResponse.hh"
2012-05-13 21:07:23 +04:00
#include "util/Destroy.hh"
2012-06-03 14:31:02 +04:00
#include "util/log/Log.hh"
2012-05-06 13:13:48 +04:00
#include "xml/Node.hh"
#include "xml/NodeSet.hh"
2012-04-25 20:13:17 +04:00
2012-05-13 21:07:23 +04:00
#include <boost/bind.hpp>
// standard C++ library
#include <algorithm>
2012-04-26 20:55:10 +04:00
#include <cassert>
#include <cstdlib>
#include <fstream>
2012-04-26 20:55:10 +04:00
#include <map>
2012-04-29 06:22:58 +04:00
#include <sstream>
2012-04-25 20:13:17 +04:00
// for debugging only
#include <iostream>
namespace gr { namespace v1 {
2012-04-25 20:13:17 +04:00
2012-05-16 20:52:17 +04:00
namespace
{
const std::string state_file = ".grive_state" ;
}
Drive::Drive( http::Agent *agent, const Val& options ) :
m_http ( agent ),
m_root ( options["path"].Str() ),
m_state ( m_root / state_file, options ),
m_options ( options )
2012-04-25 20:13:17 +04:00
{
assert( m_http != 0 ) ;
}
void Drive::FromRemote( const Entry& entry )
{
// entries from change feed does not have the parent HREF,
// so these checkings are done in normal entries only
Resource *parent = m_state.FindByHref( entry.ParentHref() ) ;
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 if ( parent == 0 || !parent->IsInRootTree() )
Log( "file \"%1%\" parent doesn't exist, ignored", entry.Title(), log::verbose ) ;
else
m_state.FromRemote( entry ) ;
}
void Drive::FromChange( const Entry& entry )
{
if ( entry.IsRemoved() )
Log( "file \"%1%\" represents a deletion, ignored", entry.Title(), log::verbose ) ;
2012-06-14 20:55:00 +04:00
// folders go directly
2012-06-10 17:03:32 +04:00
else
2012-06-14 20:55:00 +04:00
m_state.FromRemote( entry ) ;
}
void Drive::SaveState()
{
2012-05-16 20:52:17 +04:00
m_state.Write( state_file ) ;
}
void Drive::SyncFolders( )
{
assert( m_http != 0 ) ;
2012-06-10 19:11:32 +04:00
Log( "Synchronizing folders", log::info ) ;
2012-05-09 18:25:42 +04:00
http::XmlResponse xml ;
m_http->Get( feed_base + "/-/folder?max-results=50&showroot=true", &xml, http::Header() ) ;
Feed feed( xml.Response() ) ;
do
2012-04-25 20:13:17 +04:00
{
2012-05-06 14:14:36 +04:00
// first, get all collections from the query result
for ( Feed::iterator i = feed.begin() ; i != feed.end() ; ++i )
2012-04-26 20:55:10 +04:00
{
Entry e( *i ) ;
if ( e.Kind() == "folder" )
{
2012-05-27 20:36:02 +04:00
if ( e.ParentHrefs().size() != 1 )
Log( "folder \"%1%\" has multiple parents, ignored", e.Title(), log::verbose ) ;
2012-05-27 20:36:02 +04:00
else if ( e.Title().find('/') != std::string::npos )
Log( "folder \"%1%\" contains a slash in its name, ignored", e.Title(), log::verbose ) ;
else
m_state.FromRemote( e ) ;
}
2012-04-26 20:55:10 +04:00
}
} while ( feed.GetNext( m_http ) ) ;
2012-05-09 18:25:42 +04:00
2012-05-17 20:37:11 +04:00
m_state.ResolveEntry() ;
}
2012-05-19 13:14:04 +04:00
void Drive::DetectChanges()
2012-05-13 21:07:23 +04:00
{
Log( "Reading local directories", log::info ) ;
m_state.FromLocal( m_root ) ;
long prev_stamp = m_state.ChangeStamp() ;
Trace( "previous change stamp is %1%", prev_stamp ) ;
SyncFolders( ) ;
Log( "Reading remote server file list", log::info ) ;
Feed feed ;
if ( m_options["log-xml"].Bool() )
2012-07-26 20:17:44 +04:00
feed.EnableLog( "/tmp/file", ".xml" ) ;
feed.Start( m_http, feed_base + "?showfolders=true&showroot=true" ) ;
m_resume_link = feed.Root()["link"].
Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ;
do
{
std::for_each(
feed.begin(), feed.end(),
boost::bind( &Drive::FromRemote, this, _1 ) ) ;
} while ( feed.GetNext( m_http ) ) ;
// pull the changes feed
if ( prev_stamp != -1 )
{
2012-06-25 20:13:18 +04:00
Log( "Detecting changes from last sync", log::info ) ;
Feed changes ;
if ( m_options["log-xml"].Bool() )
2012-07-26 20:17:44 +04:00
feed.EnableLog( "/tmp/changes", ".xml" ) ;
feed.Start( m_http, ChangesFeed(prev_stamp+1) ) ;
std::for_each(
changes.begin(), changes.end(),
boost::bind( &Drive::FromChange, this, _1 ) ) ;
}
}
void Drive::Update()
{
2012-06-25 20:13:18 +04:00
Log( "Synchronizing files", log::info ) ;
m_state.Sync( m_http, m_options ) ;
2012-06-25 20:13:18 +04:00
UpdateChangeStamp( ) ;
2012-05-13 21:07:23 +04:00
}
2012-06-17 12:33:38 +04:00
void Drive::DryRun()
{
Log( "Synchronizing files (dry-run)", log::info ) ;
m_state.Sync( 0, m_options ) ;
2012-06-17 12:33:38 +04:00
}
void Drive::UpdateChangeStamp( )
2012-06-25 20:13:18 +04:00
{
assert( m_http != 0 ) ;
2012-06-25 20:13:18 +04:00
// get changed feed
2012-06-27 19:18:12 +04:00
http::XmlResponse xrsp ;
m_http->Get( ChangesFeed(m_state.ChangeStamp()+1), &xrsp, http::Header() ) ;
2012-06-25 20:13:18 +04:00
// we should go through the changes to see if it was really Grive to made that change
// maybe by recording the updated timestamp and compare it?
m_state.ChangeStamp(
std::atoi(xrsp.Response()["docs:largestChangestamp"]["@value"].front().Value().c_str()) ) ;
}
} } // end of namespace gr::v1