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
|
2012-04-26 19:40:38 +04:00
|
|
|
as published by the Free Software Foundation version 2
|
2012-04-26 08:21:40 +04:00
|
|
|
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"
|
2012-05-09 19:09:19 +04:00
|
|
|
#include "Entry.hh"
|
2012-06-03 21:07:14 +04:00
|
|
|
#include "Feed.hh"
|
2012-05-01 19:24:40 +04:00
|
|
|
|
2012-06-10 19:11:32 +04:00
|
|
|
#include "http/CurlAgent.hh"
|
2012-06-03 12:42:51 +04:00
|
|
|
#include "http/ResponseLog.hh"
|
2012-05-06 13:13:48 +04:00
|
|
|
#include "http/XmlResponse.hh"
|
2012-05-13 21:41:27 +04:00
|
|
|
#include "protocol/Json.hh"
|
2012-04-27 06:49:33 +04:00
|
|
|
#include "protocol/OAuth2.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"
|
2012-05-12 13:38:41 +04:00
|
|
|
#include "xml/NodeSet.hh"
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-05-13 21:07:23 +04:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2012-04-26 19:40:38 +04:00
|
|
|
// standard C++ library
|
2012-04-26 08:03:30 +04:00
|
|
|
#include <algorithm>
|
2012-04-26 20:55:10 +04:00
|
|
|
#include <cassert>
|
2012-05-13 21:41:27 +04:00
|
|
|
#include <cstdlib>
|
2012-04-26 08:03:30 +04:00
|
|
|
#include <fstream>
|
2012-04-26 20:55:10 +04:00
|
|
|
#include <map>
|
2012-04-29 06:22:58 +04:00
|
|
|
#include <sstream>
|
2012-04-26 08:03:30 +04:00
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
// for debugging only
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace gr {
|
|
|
|
|
2012-05-16 20:52:17 +04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
const std::string state_file = ".grive_state" ;
|
|
|
|
}
|
|
|
|
|
2012-05-31 19:37:47 +04:00
|
|
|
Drive::Drive( OAuth2& auth, const Json& options ) :
|
2012-05-15 20:54:58 +04:00
|
|
|
m_auth( auth ),
|
2012-05-31 19:37:47 +04:00
|
|
|
m_state( state_file, options )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
2012-06-03 12:42:51 +04:00
|
|
|
m_http_hdr.Add( "Authorization: Bearer " + m_auth.AccessToken() ) ;
|
|
|
|
m_http_hdr.Add( "GData-Version: 3.0" ) ;
|
2012-06-04 20:32:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Drive::FromRemote( const Entry& entry )
|
|
|
|
{
|
2012-06-10 20:52:04 +04:00
|
|
|
// 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 ) ;
|
2012-06-04 20:32:12 +04:00
|
|
|
|
2012-06-10 20:52:04 +04:00
|
|
|
else
|
|
|
|
m_state.FromRemote( entry ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Drive::FromChange( const Entry& entry )
|
|
|
|
{
|
2012-06-04 20:32:12 +04:00
|
|
|
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 ) ;
|
2012-05-20 13:12:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Drive::SaveState()
|
|
|
|
{
|
2012-05-16 20:52:17 +04:00
|
|
|
m_state.Write( state_file ) ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
|
|
|
|
2012-05-29 21:30:06 +04:00
|
|
|
void Drive::SyncFolders( http::Agent *http )
|
2012-04-26 19:40:38 +04:00
|
|
|
{
|
2012-06-10 19:11:32 +04:00
|
|
|
assert( http != 0 ) ;
|
|
|
|
|
2012-05-31 15:22:18 +04:00
|
|
|
Log( "Synchronizing folders", log::info ) ;
|
|
|
|
|
2012-05-09 18:25:42 +04:00
|
|
|
http::XmlResponse xml ;
|
2012-06-03 13:31:16 +04:00
|
|
|
http->Get( feed_base + "/-/folder?max-results=50&showroot=true", &xml, m_http_hdr ) ;
|
2012-06-10 20:52:04 +04:00
|
|
|
|
|
|
|
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
|
2012-06-10 20:52:04 +04:00
|
|
|
for ( Feed::iterator i = feed.begin() ; i != feed.end() ; ++i )
|
2012-04-26 20:55:10 +04:00
|
|
|
{
|
2012-05-12 21:57:15 +04:00
|
|
|
Entry e( *i ) ;
|
|
|
|
if ( e.Kind() == "folder" )
|
|
|
|
{
|
2012-05-27 20:36:02 +04:00
|
|
|
if ( e.ParentHrefs().size() != 1 )
|
2012-05-31 15:22:18 +04:00
|
|
|
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-05-12 21:57:15 +04:00
|
|
|
}
|
2012-04-26 20:55:10 +04:00
|
|
|
}
|
2012-06-10 20:52:04 +04:00
|
|
|
} while ( feed.GetNext( http, m_http_hdr ) ) ;
|
2012-05-09 18:25:42 +04:00
|
|
|
|
2012-05-17 20:37:11 +04:00
|
|
|
m_state.ResolveEntry() ;
|
2012-04-26 19:40:38 +04:00
|
|
|
}
|
2012-05-19 13:14:04 +04:00
|
|
|
|
2012-06-23 14:23:43 +04:00
|
|
|
void Drive::DetectChanges()
|
2012-05-13 21:07:23 +04:00
|
|
|
{
|
2012-06-22 03:47:01 +04:00
|
|
|
Log( "Reading local directories", log::info ) ;
|
|
|
|
m_state.FromLocal( "." ) ;
|
2012-05-31 15:22:18 +04:00
|
|
|
|
2012-06-10 19:11:32 +04:00
|
|
|
http::CurlAgent http ;
|
2012-06-22 03:47:01 +04:00
|
|
|
|
|
|
|
long prev_stamp = m_state.ChangeStamp() ;
|
|
|
|
Trace( "previous change stamp is %1%", prev_stamp ) ;
|
|
|
|
|
|
|
|
SyncFolders( &http ) ;
|
|
|
|
|
|
|
|
Log( "Reading remote server file list", log::info ) ;
|
2012-06-25 20:13:18 +04:00
|
|
|
http::XmlResponse xrsp ;
|
2012-06-22 03:47:01 +04:00
|
|
|
http.Get( feed_base + "?showfolders=true&showroot=true", &xrsp, m_http_hdr ) ;
|
|
|
|
xml::Node resp = xrsp.Response() ;
|
|
|
|
|
|
|
|
m_resume_link = resp["link"].
|
|
|
|
Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ;
|
|
|
|
|
|
|
|
Feed feed( resp ) ;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
std::for_each( feed.begin(), feed.end(), boost::bind( &Drive::FromRemote, this, _1 ) ) ;
|
|
|
|
} while ( feed.GetNext( &http, m_http_hdr ) ) ;
|
|
|
|
|
|
|
|
// pull the changes feed
|
|
|
|
if ( prev_stamp != -1 )
|
|
|
|
{
|
2012-06-27 19:18:12 +04:00
|
|
|
http::ResponseLog log( "/tmp/changes-", ".xml", &xrsp ) ;
|
2012-06-25 20:13:18 +04:00
|
|
|
Log( "Detecting changes from last sync", log::info ) ;
|
2012-06-27 19:18:12 +04:00
|
|
|
http.Get( ChangesFeed(prev_stamp+1), &log, m_http_hdr ) ;
|
2012-06-22 03:47:01 +04:00
|
|
|
|
|
|
|
Feed changes( xrsp.Response() ) ;
|
|
|
|
std::for_each( changes.begin(), changes.end(), boost::bind( &Drive::FromChange, this, _1 ) ) ;
|
|
|
|
}
|
2012-06-23 14:23:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Drive::Update()
|
|
|
|
{
|
2012-06-25 20:13:18 +04:00
|
|
|
Log( "Synchronizing files", log::info ) ;
|
2012-06-23 14:23:43 +04:00
|
|
|
http::CurlAgent http ;
|
|
|
|
m_state.Sync( &http, m_http_hdr ) ;
|
2012-06-25 20:13:18 +04:00
|
|
|
|
|
|
|
UpdateChangeStamp( &http ) ;
|
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_http_hdr ) ;
|
|
|
|
}
|
|
|
|
|
2012-06-25 20:13:18 +04:00
|
|
|
void Drive::UpdateChangeStamp( http::Agent *http )
|
|
|
|
{
|
|
|
|
assert( http != 0 ) ;
|
|
|
|
|
|
|
|
// get changed feed
|
2012-06-27 19:18:12 +04:00
|
|
|
http::XmlResponse xrsp ;
|
|
|
|
http->Get( ChangesFeed(m_state.ChangeStamp()+1), &xrsp, m_http_hdr ) ;
|
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()) ) ;
|
|
|
|
}
|
|
|
|
|
2012-04-25 20:13:17 +04:00
|
|
|
} // end of namespace
|