Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 86 partial sync #109

Merged
merged 2 commits into from Jul 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 60 additions & 33 deletions grive/src/main.cc
Expand Up @@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "Config.hh"
#include "util/Config.hh"

#include "drive/Drive.hh"

Expand Down Expand Up @@ -46,6 +46,9 @@

const std::string client_id = "22314510474.apps.googleusercontent.com" ;
const std::string client_secret = "bl4ufi89h-9MkFlypcI7R785" ;
const std::string defaultRootFolder = ".";
const std::string defaultConfigFileName = ".grive";
const char *configFileEnvironmentVariable = "GR_CONFIG";

using namespace gr ;

Expand All @@ -66,8 +69,8 @@ int Main( int argc, char **argv )
{
InitGCrypt() ;

Config config ;
std::string rootFolder = defaultRootFolder;

std::auto_ptr<log::CompositeLog> comp_log(new log::CompositeLog) ;
LogBase* console_log = comp_log->Add( std::auto_ptr<LogBase>( new log::DefaultLog ) ) ;

Expand All @@ -81,6 +84,7 @@ int Main( int argc, char **argv )
( "help,h", "Produce help message" )
( "version,v", "Display Grive version" )
( "auth,a", "Request authorization token" )
( "path,p", po::value<std::string>(), "Path to sync")
( "verbose,V", "Verbose mode. Enable more messages than normal.")
( "log-xml", "Log more HTTP responses as XML for debugging.")
( "new-rev", "Create new revisions in server for updated files.")
Expand All @@ -101,27 +105,9 @@ int Main( int argc, char **argv )
std::cout << desc << std::endl ;
return 0 ;
}
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
config.Get().Add( "refresh_token", Json( token.RefreshToken() ) ) ;
config.Save() ;
}

boost::shared_ptr<Config> config ;

if ( vm.count( "log" ) )
{
std::auto_ptr<LogBase> file_log(new log::DefaultLog( vm["log"].as<std::string>() )) ;
Expand All @@ -138,12 +124,6 @@ int Main( int argc, char **argv )

comp_log->Add( file_log ) ;
}
if ( vm.count( "version" ) )
{
std::cout
<< "grive version " << VERSION << ' ' << __DATE__ << ' ' << __TIME__ << std::endl ;
return 0 ;
}
if ( vm.count( "verbose" ) )
{
console_log->Enable( log::verbose ) ;
Expand All @@ -157,6 +137,52 @@ int Main( int argc, char **argv )
console_log->Enable( log::verbose ) ;
console_log->Enable( log::debug ) ;
}

// config file will be (in order of preference)
// value specified in environment string
// value specified in defaultConfigFileName in path from commandline --path
// value specified in defaultConfigFileName in current directory
const char *envConfigFileName = ::getenv( configFileEnvironmentVariable ) ;
if (envConfigFileName) {
config.reset(new Config(envConfigFileName));

} else if ( vm.count( "path" ) ) {
rootFolder = vm["path"].as<std::string>();
config.reset(new Config( fs::path(rootFolder) / fs::path(defaultConfigFileName) ));

} else {
config.reset(new Config( defaultConfigFileName) );
}

Log( "config file name %1%", config->ConfigFile().string(), log::verbose );

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
config->Get().Add( "refresh_token", Json( token.RefreshToken() ) ) ;
config->Save() ;
}
if ( vm.count( "version" ) )
{
std::cout
<< "grive version " << VERSION << ' ' << __DATE__ << ' ' << __TIME__ << std::endl ;
return 0 ;
}
if ( vm.count( "force" ) )
{
options.Add( "force", Json(true) ) ;
Expand All @@ -167,7 +193,7 @@ int Main( int argc, char **argv )
std::string refresh_token ;
try
{
refresh_token = config.Get()["refresh_token"].Str() ;
refresh_token = config->Get()["refresh_token"].Str() ;
}
catch ( Exception& e )
{
Expand All @@ -181,7 +207,8 @@ int Main( int argc, char **argv )

OAuth2 token( refresh_token, client_id, client_secret ) ;
AuthAgent agent( token, std::auto_ptr<http::Agent>( new http::CurlAgent ) ) ;
Drive drive( &agent, options ) ;

Drive drive( &agent, options, rootFolder ) ;
drive.DetectChanges() ;

if ( vm.count( "dry-run" ) == 0 )
Expand All @@ -192,7 +219,7 @@ int Main( int argc, char **argv )
else
drive.DryRun() ;

config.Save() ;
config->Save() ;
Log( "Finished!", log::info ) ;
return 0 ;
}
Expand Down
9 changes: 5 additions & 4 deletions libgrive/src/drive/Drive.cc
Expand Up @@ -51,9 +51,10 @@ namespace
const std::string state_file = ".grive_state" ;
}

Drive::Drive( http::Agent *http, const Json& options ) :
m_http ( http ),
m_state ( state_file, options ),
Drive::Drive( http::Agent *http, const Json& options, const std::string &rootFolder) :
m_http( http ),
m_state( fs::path(rootFolder), fs::path(rootFolder) / fs::path(state_file), options ),
m_rootFolder(rootFolder),
m_options ( options )
{
assert( m_http != 0 ) ;
Expand Down Expand Up @@ -127,7 +128,7 @@ void Drive::SyncFolders( )
void Drive::DetectChanges()
{
Log( "Reading local directories", log::info ) ;
m_state.FromLocal( "." ) ;
m_state.FromLocal( m_rootFolder ) ;

long prev_stamp = m_state.ChangeStamp() ;
Trace( "previous change stamp is %1%", prev_stamp ) ;
Expand Down
4 changes: 2 additions & 2 deletions libgrive/src/drive/Drive.hh
Expand Up @@ -40,7 +40,7 @@ class Entry ;
class Drive
{
public :
Drive( http::Agent *http, const Json& options ) ;
Drive( http::Agent *http, const Json& options, const std::string &rootFolder ) ;

void DetectChanges() ;
void Update() ;
Expand All @@ -60,7 +60,7 @@ private :
http::Agent *m_http ;
std::string m_resume_link ;
State m_state ;
fs::path m_rootFolder;
Json m_options ;
} ;

Expand Down
4 changes: 2 additions & 2 deletions libgrive/src/drive/Resource.cc
Expand Up @@ -58,8 +58,8 @@ const std::string xml_meta =


/// default constructor creates the root folder
Resource::Resource() :
m_name ( "." ),
Resource::Resource(const fs::path& rootFolder) :
m_name ( rootFolder.string() ),
m_kind ( "folder" ),
m_id ( "folder:root" ),
m_href ( root_href ),
Expand Down
2 changes: 1 addition & 1 deletion libgrive/src/drive/Resource.hh
Expand Up @@ -51,7 +51,7 @@ public :
typedef Children::const_iterator iterator ;

public :
Resource() ;
Resource(const fs::path& rootFolder) ;
Resource( const std::string& name, const std::string& kind ) ;

// default copy ctor & op= are fine
Expand Down
27 changes: 2 additions & 25 deletions libgrive/src/drive/ResourceTree.cc
Expand Up @@ -31,8 +31,8 @@ namespace gr {

using namespace details ;

ResourceTree::ResourceTree( ) :
m_root( new Resource )
ResourceTree::ResourceTree( const fs::path& rootFolder ) :
m_root(new Resource(rootFolder))
{
m_set.insert( m_root ) ;
}
Expand Down Expand Up @@ -111,29 +111,6 @@ const Resource* ResourceTree::FindByHref( const std::string& href ) const
return i != map.end() ? *i : 0 ;
}

/// Unlike other search functions, this one does not depend on the multi-index
/// container. It traverses the tree instead.
Resource* ResourceTree::FindByPath( const fs::path& path )
{
Resource *current = m_root ;
for ( fs::path::iterator i = path.begin() ; i != path.end() && current != 0 ; ++i )
{
Trace( "path it = %1%", *i ) ;

// current directory
if ( *i == "." )
continue ;

else if ( *i == ".." )
current = current->Parent() ;

else
current = current->FindChild( Path2Str(*i) ) ;
}

return current ;
}

/// Reinsert should be called when the ID/HREF were updated
bool ResourceTree::ReInsert( Resource *coll )
{
Expand Down
3 changes: 1 addition & 2 deletions libgrive/src/drive/ResourceTree.hh
Expand Up @@ -64,7 +64,7 @@ public :
typedef details::Set::iterator iterator ;

public :
ResourceTree( ) ;
ResourceTree( const fs::path& rootFolder ) ;
ResourceTree( const ResourceTree& fs ) ;
~ResourceTree( ) ;

Expand All @@ -74,7 +74,6 @@ public :
Resource* FindByHref( const std::string& href ) ;
const Resource* FindByHref( const std::string& href ) const ;

Resource* FindByPath( const fs::path& path ) ;
Resource* FindByID( const std::string& id ) ;

bool ReInsert( Resource *coll ) ;
Expand Down
10 changes: 3 additions & 7 deletions libgrive/src/drive/State.cc
Expand Up @@ -32,8 +32,9 @@

namespace gr {

State::State( const fs::path& filename, const Json& options ) :
m_cstamp( -1 )
State::State( const fs::path& rootFolder, const fs::path& filename, const Json& options ) :
m_cstamp( -1 ),
m_res(rootFolder)
{
Read( filename ) ;

Expand Down Expand Up @@ -213,11 +214,6 @@ Resource* State::FindByHref( const std::string& href )
return m_res.FindByHref( href ) ;
}

Resource* State::Find( const fs::path& path )
{
return m_res.FindByPath( path ) ;
}

State::iterator State::begin()
{
return m_res.begin() ;
Expand Down
3 changes: 1 addition & 2 deletions libgrive/src/drive/State.hh
Expand Up @@ -43,7 +43,7 @@ public :
typedef ResourceTree::iterator iterator ;

public :
explicit State( const fs::path& filename, const Json& options ) ;
explicit State( const fs::path& rootFolder, const fs::path& filename, const Json& options ) ;
~State() ;

void FromLocal( const fs::path& p ) ;
Expand All @@ -55,7 +55,6 @@ public :

Resource* FindByHref( const std::string& href ) ;
Resource* FindByID( const std::string& id ) ;
Resource* Find( const fs::path& path ) ;

void Sync( http::Agent *http, const Json& options ) ;

Expand Down
25 changes: 13 additions & 12 deletions grive/src/Config.cc → libgrive/src/util/Config.cc
Expand Up @@ -21,26 +21,28 @@

#include "util/StdioFile.hh"

#include <iostream>
#include <iterator>

namespace gr {
using namespace gr;

const std::string& Config::Filename()
Config::Config(const fs::path& configFile)
: m_configFile(configFile)
, m_cfg( Read() )
{
static const char *env_cfg = ::getenv( "GR_CONFIG" ) ;
static const std::string filename = (env_cfg != 0) ? env_cfg : ".grive" ;

return filename ;
if (configFile.empty()) {
throw Error() << expt::ErrMsg("Config cannot be initalised with an empty string.");
}
}

Config::Config() :
m_cfg( Read( Filename() ) )
const fs::path& Config::ConfigFile() const
{
return m_configFile ;
}

void Config::Save( )
{
StdioFile file( Filename(), 0600 ) ;
StdioFile file( m_configFile.string(), 0600 ) ;
m_cfg.Write( file ) ;
}

Expand All @@ -49,16 +51,15 @@ Json& Config::Get()
return m_cfg ;
}

Json Config::Read( const std::string& filename )
Json Config::Read()
{
try
{
return Json::ParseFile( filename ) ;
return Json::ParseFile( m_configFile.string() ) ;
}
catch ( Exception& e )
{
return Json() ;
}
}

} // end of namespace