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.
|
|
|
|
*/
|
|
|
|
|
2012-07-29 21:17:10 +04:00
|
|
|
#include "util/Config.hh"
|
2012-05-15 19:07:03 +04:00
|
|
|
|
2012-04-27 06:49:33 +04:00
|
|
|
#include "drive/Drive.hh"
|
2012-05-13 11:27:58 +04:00
|
|
|
|
2012-07-19 20:39:36 +04:00
|
|
|
#include "http/CurlAgent.hh"
|
|
|
|
#include "protocol/AuthAgent.hh"
|
2012-04-27 06:49:33 +04:00
|
|
|
#include "protocol/OAuth2.hh"
|
2012-04-26 08:21:40 +04:00
|
|
|
#include "protocol/Json.hh"
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-05-13 11:27:58 +04:00
|
|
|
#include "bfd/Backtrace.hh"
|
|
|
|
#include "util/Exception.hh"
|
2012-06-03 14:31:02 +04:00
|
|
|
#include "util/log/Log.hh"
|
|
|
|
#include "util/log/CompositeLog.hh"
|
|
|
|
#include "util/log/DefaultLog.hh"
|
2012-05-13 11:27:58 +04:00
|
|
|
|
2012-06-14 20:04:17 +04:00
|
|
|
// boost header
|
2012-05-13 11:27:58 +04:00
|
|
|
#include <boost/exception/all.hpp>
|
2012-06-14 20:04:17 +04:00
|
|
|
#include <boost/program_options.hpp>
|
2012-05-13 11:27:58 +04:00
|
|
|
|
2012-06-14 20:04:17 +04:00
|
|
|
// initializing libgcrypt, must be done in executable
|
2012-06-05 18:51:54 +04:00
|
|
|
#include <gcrypt.h>
|
|
|
|
|
2012-04-26 05:20:41 +04:00
|
|
|
#include <cassert>
|
2012-04-25 20:13:17 +04:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
2012-04-29 19:40:13 +04:00
|
|
|
#include <unistd.h>
|
2012-04-29 02:45:33 +04:00
|
|
|
|
2012-04-26 08:21:40 +04:00
|
|
|
const std::string client_id = "22314510474.apps.googleusercontent.com" ;
|
|
|
|
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
|
|
|
|
|
2012-06-06 19:21:04 +04:00
|
|
|
using namespace gr ;
|
2012-07-30 11:24:47 +04:00
|
|
|
namespace po = boost::program_options;
|
2012-06-06 19:21:04 +04:00
|
|
|
|
2012-06-05 18:51:54 +04:00
|
|
|
// libgcrypt insist this to be done in application, not library
|
|
|
|
void InitGCrypt()
|
|
|
|
{
|
|
|
|
if ( !gcry_check_version(GCRYPT_VERSION) )
|
2012-06-06 19:21:04 +04:00
|
|
|
throw Exception() << expt::ErrMsg( "libgcrypt version mismatch" ) ;
|
2012-06-05 18:51:54 +04:00
|
|
|
|
|
|
|
// disable secure memory
|
|
|
|
gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
|
|
|
|
|
|
|
|
// tell Libgcrypt that initialization has completed
|
|
|
|
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
|
|
|
|
}
|
|
|
|
|
2012-07-30 11:24:47 +04:00
|
|
|
void InitLog( const po::variables_map& vm )
|
2012-04-25 20:13:17 +04:00
|
|
|
{
|
2012-06-03 14:31:02 +04:00
|
|
|
std::auto_ptr<log::CompositeLog> comp_log(new log::CompositeLog) ;
|
|
|
|
LogBase* console_log = comp_log->Add( std::auto_ptr<LogBase>( new log::DefaultLog ) ) ;
|
2012-07-30 11:24:47 +04:00
|
|
|
|
|
|
|
if ( vm.count( "log" ) )
|
|
|
|
{
|
|
|
|
std::auto_ptr<LogBase> file_log(new log::DefaultLog( vm["log"].as<std::string>() )) ;
|
|
|
|
file_log->Enable( log::debug ) ;
|
|
|
|
file_log->Enable( log::verbose ) ;
|
|
|
|
file_log->Enable( log::info ) ;
|
|
|
|
file_log->Enable( log::warning ) ;
|
|
|
|
file_log->Enable( log::error ) ;
|
|
|
|
file_log->Enable( log::critical ) ;
|
|
|
|
|
|
|
|
// log grive version to log file
|
|
|
|
file_log->Log( log::Fmt("grive version " VERSION " " __DATE__ " " __TIME__), log::verbose ) ;
|
|
|
|
file_log->Log( log::Fmt("current time: %1%") % DateTime::Now(), log::verbose ) ;
|
|
|
|
|
|
|
|
comp_log->Add( file_log ) ;
|
|
|
|
}
|
2012-05-13 13:27:40 +04:00
|
|
|
|
2012-07-30 11:24:47 +04:00
|
|
|
if ( vm.count( "verbose" ) )
|
|
|
|
{
|
|
|
|
console_log->Enable( log::verbose ) ;
|
|
|
|
}
|
2012-05-31 19:37:47 +04:00
|
|
|
|
2012-07-30 11:24:47 +04:00
|
|
|
if ( vm.count( "debug" ) )
|
|
|
|
{
|
|
|
|
console_log->Enable( log::verbose ) ;
|
|
|
|
console_log->Enable( log::debug ) ;
|
|
|
|
}
|
|
|
|
LogBase::Inst( std::auto_ptr<LogBase>(comp_log.release()) ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
InitGCrypt() ;
|
|
|
|
|
2012-06-14 20:04:17 +04:00
|
|
|
// construct the program options
|
|
|
|
po::options_description desc( "Grive options" );
|
|
|
|
desc.add_options()
|
|
|
|
( "help,h", "Produce help message" )
|
|
|
|
( "version,v", "Display Grive version" )
|
|
|
|
( "auth,a", "Request authorization token" )
|
2012-07-29 21:17:10 +04:00
|
|
|
( "path,p", po::value<std::string>(), "Path to sync")
|
2012-06-14 20:04:17 +04:00
|
|
|
( "verbose,V", "Verbose mode. Enable more messages than normal.")
|
2012-07-26 20:17:44 +04:00
|
|
|
( "log-xml", "Log more HTTP responses as XML for debugging.")
|
2012-07-26 20:45:53 +04:00
|
|
|
( "new-rev", "Create new revisions in server for updated files.")
|
2012-06-14 20:04:17 +04:00
|
|
|
( "debug,d", "Enable debug level messages. Implies -v.")
|
|
|
|
( "log,l", po::value<std::string>(), "Set log output filename." )
|
|
|
|
( "force,f", "Force grive to always download a file from Google Drive "
|
|
|
|
"instead of uploading it." )
|
2012-06-17 11:57:47 +04:00
|
|
|
( "dry-run", "Only detect which files need to be uploaded/downloaded, "
|
|
|
|
"without actually performing them." )
|
2012-06-14 20:04:17 +04:00
|
|
|
;
|
|
|
|
|
|
|
|
po::variables_map vm;
|
|
|
|
po::store(po::parse_command_line( argc, argv, desc), vm );
|
|
|
|
po::notify(vm);
|
|
|
|
|
2012-07-30 12:52:59 +04:00
|
|
|
// simple commands that doesn't require log or config
|
2012-06-14 20:04:17 +04:00
|
|
|
if ( vm.count("help") )
|
|
|
|
{
|
|
|
|
std::cout << desc << std::endl ;
|
|
|
|
return 0 ;
|
|
|
|
}
|
2012-07-30 11:24:47 +04:00
|
|
|
else if ( vm.count( "version" ) )
|
2012-06-14 20:04:17 +04:00
|
|
|
{
|
2012-07-30 11:24:47 +04:00
|
|
|
std::cout
|
|
|
|
<< "grive version " << VERSION << ' ' << __DATE__ << ' ' << __TIME__ << std::endl ;
|
|
|
|
return 0 ;
|
2012-06-14 20:04:17 +04:00
|
|
|
}
|
2012-07-30 11:24:47 +04:00
|
|
|
|
|
|
|
// initialize logging
|
|
|
|
InitLog(vm) ;
|
2012-07-26 20:17:44 +04:00
|
|
|
|
2012-07-30 12:52:59 +04:00
|
|
|
Config config(vm) ;
|
2012-07-30 11:24:47 +04:00
|
|
|
|
2012-07-30 12:52:59 +04:00
|
|
|
Log( "config file name %1%", config.Filename(), log::verbose );
|
2012-07-29 21:17:10 +04:00
|
|
|
|
|
|
|
if ( vm.count( "auth" ) )
|
|
|
|
{
|
|
|
|
std::cout
|
|
|
|
<< "-----------------------\n"
|
|
|
|
<< "Please go to this URL and get an authentication code:\n\n"
|
|
|
|
<< OAuth2::MakeAuthURL( client_id )
|
|
|
|
<< std::endl ;
|
|
|
|
|
|
|
|
std::cout
|
|
|
|
<< "\n-----------------------\n"
|
|
|
|
<< "Please input the authentication code here: " << std::endl ;
|
|
|
|
std::string code ;
|
|
|
|
std::cin >> code ;
|
|
|
|
|
|
|
|
OAuth2 token( client_id, client_secret ) ;
|
|
|
|
token.Auth( code ) ;
|
|
|
|
|
|
|
|
// save to config
|
2012-07-30 12:52:59 +04:00
|
|
|
config.Set( "refresh_token", Json( token.RefreshToken() ) ) ;
|
|
|
|
config.Save() ;
|
2012-07-29 21:17:10 +04:00
|
|
|
}
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-04-29 07:17:15 +04:00
|
|
|
std::string refresh_token ;
|
2012-04-29 02:45:33 +04:00
|
|
|
try
|
|
|
|
{
|
2012-07-30 12:52:59 +04:00
|
|
|
refresh_token = config.Get("refresh_token").Str() ;
|
2012-04-29 02:45:33 +04:00
|
|
|
}
|
2012-05-19 05:03:34 +04:00
|
|
|
catch ( Exception& e )
|
2012-04-29 02:45:33 +04:00
|
|
|
{
|
2012-05-13 12:45:27 +04:00
|
|
|
Log(
|
2012-05-13 12:10:18 +04:00
|
|
|
"Please run grive with the \"-a\" option if this is the "
|
|
|
|
"first time you're accessing your Google Drive!",
|
2012-05-13 12:45:27 +04:00
|
|
|
log::critical ) ;
|
2012-05-13 12:10:18 +04:00
|
|
|
|
2012-04-29 02:45:33 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-04-25 20:13:17 +04:00
|
|
|
|
2012-06-06 19:21:04 +04:00
|
|
|
OAuth2 token( refresh_token, client_id, client_secret ) ;
|
2012-07-19 20:39:36 +04:00
|
|
|
AuthAgent agent( token, std::auto_ptr<http::Agent>( new http::CurlAgent ) ) ;
|
2012-07-29 21:17:10 +04:00
|
|
|
|
2012-07-30 12:52:59 +04:00
|
|
|
Drive drive( &agent, config.GetAll() ) ;
|
2012-06-23 14:23:43 +04:00
|
|
|
drive.DetectChanges() ;
|
2012-06-06 19:21:04 +04:00
|
|
|
|
2012-06-17 11:57:47 +04:00
|
|
|
if ( vm.count( "dry-run" ) == 0 )
|
|
|
|
{
|
|
|
|
drive.Update() ;
|
|
|
|
drive.SaveState() ;
|
|
|
|
}
|
2012-06-17 12:33:38 +04:00
|
|
|
else
|
|
|
|
drive.DryRun() ;
|
2012-06-06 19:21:04 +04:00
|
|
|
|
2012-07-30 12:52:59 +04:00
|
|
|
config.Save() ;
|
2012-06-14 20:55:00 +04:00
|
|
|
Log( "Finished!", log::info ) ;
|
2012-06-06 19:21:04 +04:00
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
2012-05-13 11:27:58 +04:00
|
|
|
try
|
|
|
|
{
|
2012-06-06 19:21:04 +04:00
|
|
|
return Main( argc, argv ) ;
|
2012-05-13 11:27:58 +04:00
|
|
|
}
|
2012-06-06 19:21:04 +04:00
|
|
|
catch ( Exception& e )
|
2012-05-13 11:27:58 +04:00
|
|
|
{
|
2012-05-13 12:45:27 +04:00
|
|
|
Log( "exception: %1%", boost::diagnostic_information(e), log::critical ) ;
|
2012-06-14 20:55:00 +04:00
|
|
|
}
|
|
|
|
catch ( std::exception& e )
|
|
|
|
{
|
|
|
|
Log( "exception: %1%", e.what(), log::critical ) ;
|
2012-05-13 11:27:58 +04:00
|
|
|
}
|
2012-06-07 14:26:57 +04:00
|
|
|
catch ( ... )
|
|
|
|
{
|
2012-06-14 20:55:00 +04:00
|
|
|
Log( "unexpected exception", log::critical ) ;
|
2012-06-07 14:26:57 +04:00
|
|
|
}
|
2012-06-14 20:55:00 +04:00
|
|
|
return -1 ;
|
2012-04-25 20:13:17 +04:00
|
|
|
}
|