grive2/libgrive/src/drive/Drive.cc

200 lines
5.4 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"
2012-05-09 20:22:27 +04:00
#include "http/Agent.hh"
#include "http/ResponseLog.hh"
2012-05-06 13:13:48 +04:00
#include "http/XmlResponse.hh"
#include "protocol/Json.hh"
#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"
#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 {
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 ) :
m_auth( auth ),
2012-05-31 19:37:47 +04:00
m_state( state_file, options )
2012-04-25 20:13:17 +04:00
{
m_http_hdr.Add( "Authorization: Bearer " + m_auth.AccessToken() ) ;
m_http_hdr.Add( "GData-Version: 3.0" ) ;
2012-05-31 19:48:30 +04:00
Log( "Reading local directories", log::info ) ;
2012-05-19 21:44:46 +04:00
m_state.FromLocal( "." ) ;
http::Agent http ;
2012-06-03 19:58:28 +04:00
long prev_stamp = m_state.ChangeStamp() ;
// get metadata
http::XmlResponse xrsp ;
// http::ResponseLog log( "meta-", ".xml", &xrsp ) ;
http.Get( "https://docs.google.com/feeds/metadata/default", &xrsp, m_http_hdr ) ;
2012-06-03 19:58:28 +04:00
Trace( "return %1%", xrsp.Response()["docs:largestChangestamp"] ) ;
m_state.ChangeStamp(
std::atoi(xrsp.Response()["docs:largestChangestamp"]["@value"].front().Value().c_str()) ) ;
SyncFolders( &http ) ;
2012-05-31 19:48:30 +04:00
Log( "Reading remote server file list", log::info ) ;
http.Get( feed_base + "?showfolders=true&showroot=true", &xrsp, m_http_hdr ) ;
xml::Node resp = xrsp.Response() ;
2012-05-06 14:14:36 +04:00
m_resume_link = resp["link"].
Find( "@rel", "http://schemas.google.com/g/2005#resumable-create-media" )["@href"] ;
2012-05-17 20:37:11 +04:00
2012-06-03 21:07:14 +04:00
Feed feed( resp ) ;
2012-05-06 14:14:36 +04:00
do
{
std::for_each( feed.begin(), feed.end(), boost::bind( &Drive::FromRemote, this, _1 ) ) ;
2012-06-03 21:07:14 +04:00
} while ( feed.GetNext( &http, m_http_hdr ) ) ;
2012-06-03 19:58:28 +04:00
// pull the changes feed
2012-06-04 20:59:14 +04:00
if ( prev_stamp != -1 )
{
boost::format changes_uri( "https://docs.google.com/feeds/default/private/changes?start-index=%1%" ) ;
2012-06-05 19:29:37 +04:00
// http::ResponseLog log2( "changes-", ".xml", &xrsp ) ;
http.Get( (changes_uri%(prev_stamp+1)).str(), &xrsp, m_http_hdr ) ;
2012-06-04 20:59:14 +04:00
Feed changes( xrsp.Response() ) ;
std::for_each( changes.begin(), changes.end(), boost::bind( &Drive::FromChange, this, _1 ) ) ;
}
}
void Drive::FromRemote( const Entry& entry )
{
if ( entry.Kind() != "folder" && !entry.ContentSrc().empty() )
{
Resource *parent = m_state.FindByHref( entry.ParentHref() ) ;
std::string fn = entry.Filename() ;
if ( fn.empty() )
Log( "file \"%1%\" is a google document, ignored", entry.Title(), log::verbose ) ;
else if ( fn.find('/') != fn.npos )
Log( "file \"%1%\" contains a slash in its name, ignored", entry.Title(), log::verbose ) ;
else if ( parent == 0 || !parent->IsInRootTree() )
Log( "file \"%1%\" parent doesn't exist, ignored", entry.Title(), log::verbose ) ;
else 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
m_state.FromRemote( entry ) ;
}
}
void Drive::FromChange( const Entry& entry )
{
std::string fn = entry.Filename() ;
if ( entry.IsRemoved() )
Log( "file \"%1%\" represents a deletion, ignored", entry.Title(), log::verbose ) ;
else if ( fn.empty() )
Log( "file \"%1%\" is a google document, ignored", entry.Title(), log::verbose ) ;
else if ( !entry.ContentSrc().empty() )
m_state.FromChange( entry ) ;
}
void Drive::SaveState()
{
2012-05-16 20:52:17 +04:00
m_state.Write( state_file ) ;
}
void Drive::SyncFolders( http::Agent *http )
{
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::ResponseLog log( "dir-", ".xml", &xml ) ;
http->Get( feed_base + "/-/folder?max-results=50&showroot=true", &xml, m_http_hdr ) ;
xml::Node resp = xml.Response() ;
2012-05-09 18:25:42 +04:00
2012-05-06 14:14:36 +04:00
while ( true )
2012-04-25 20:13:17 +04:00
{
xml::NodeSet entries = resp["entry"] ;
2012-05-06 14:14:36 +04:00
// first, get all collections from the query result
for ( xml::NodeSet::iterator i = entries.begin() ; i != entries.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
}
2012-05-06 14:14:36 +04:00
xml::NodeSet next = resp["link"].Find( "@rel", "next" ) ;
if ( next.empty() )
2012-05-06 14:14:36 +04:00
break ;
http->Get( next["@href"], &xml, m_http_hdr ) ;
resp = xml.Response() ;
}
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
2012-05-13 21:07:23 +04:00
void Drive::Update()
{
Log( "Synchronizing files", log::info ) ;
2012-05-13 21:07:23 +04:00
http::Agent http ;
m_state.Sync( &http, m_http_hdr ) ;
2012-05-13 21:07:23 +04:00
}
2012-04-25 20:13:17 +04:00
} // end of namespace