diff --git a/grive/src/main.cc b/grive/src/main.cc index cf3a3d1a..9c9c299d 100644 --- a/grive/src/main.cc +++ b/grive/src/main.cc @@ -18,6 +18,7 @@ */ #include "util/Config.hh" +#include "util/Ignore.hh" #include "drive/Drive.hh" @@ -118,6 +119,8 @@ int Main( int argc, char **argv ) "instead of uploading it." ) ( "dry-run", "Only detect which files need to be uploaded/downloaded, " "without actually performing them." ) + ( "ignore,i", po::value(), "Adds the file to the list of ignored files." ) + ( "unignore,u", po::value(), "Removes the file from the list of ignored files." ) ; po::variables_map vm; @@ -164,6 +167,27 @@ int Main( int argc, char **argv ) // save to config config.Set( "refresh_token", Json( token.RefreshToken() ) ) ; config.Save() ; + + // storing standard ignore files + Ignore igno(config); + igno.Add(".grive"); + igno.Add(".grive_state"); + igno.Add(".trash"); + igno.Save(); + } else if( vm.count( "ignore" ) ) { + std::cout << "Ignoring file \"" << vm["ignore"].as() << "\"" << std::endl; + Log( "ignoring file %1%", vm["ignore"].as(), log::verbose ); + Ignore igno(config); + igno.Add(vm["ignore"].as()); + igno.Save(); + return 0; + } else if( vm.count( "unignore" ) ) { + std::cout << "Unignoring file \"" << vm["unignore"].as() << "\"" << std::endl; + Log( "unignoring file %1%", vm["unignore"].as(), log::verbose ); + Ignore igno(config); + igno.Remove(vm["unignore"].as()); + igno.Save(); + return 0; } std::string refresh_token ; @@ -184,7 +208,7 @@ int Main( int argc, char **argv ) OAuth2 token( refresh_token, client_id, client_secret ) ; AuthAgent agent( token, std::auto_ptr( new http::CurlAgent ) ) ; - Drive drive( &agent, config.GetAll() ) ; + Drive drive( &agent, config.GetAll(), Ignore(config) ) ; drive.DetectChanges() ; if ( vm.count( "dry-run" ) == 0 ) diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 82cfc749..e33cd869 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -51,10 +51,10 @@ namespace const std::string state_file = ".grive_state" ; } -Drive::Drive( http::Agent *http, const Json& options ) : +Drive::Drive( http::Agent *http, const Json& options, const Ignore& igno ) : m_http ( http ), m_root ( options["path"].Str() ), - m_state ( m_root / state_file, options ), + m_state ( m_root / state_file, options, igno ), m_options ( options ) { assert( m_http != 0 ) ; diff --git a/libgrive/src/drive/Drive.hh b/libgrive/src/drive/Drive.hh index 5424f44f..556862bd 100644 --- a/libgrive/src/drive/Drive.hh +++ b/libgrive/src/drive/Drive.hh @@ -24,9 +24,9 @@ #include "http/Header.hh" #include "protocol/Json.hh" #include "util/Exception.hh" +#include "util/Ignore.hh" #include -#include namespace gr { @@ -40,7 +40,7 @@ class Entry ; class Drive { public : - Drive( http::Agent *http, const Json& options ) ; + Drive( http::Agent *http, const Json& options, const Ignore& igno ) ; void DetectChanges() ; void Update() ; diff --git a/libgrive/src/drive/State.cc b/libgrive/src/drive/State.cc index 43e63679..9b6b6fdd 100644 --- a/libgrive/src/drive/State.cc +++ b/libgrive/src/drive/State.cc @@ -32,9 +32,11 @@ namespace gr { -State::State( const fs::path& filename, const Json& options ) : - m_res ( options["path"].Str() ), - m_cstamp ( -1 ) + +State::State( const fs::path& filename, const Json& options, const Ignore& igo ) : + m_res ( options["path"].Str() ), + m_cstamp ( -1 ), + m_ignore ( igo ) { Read( filename ) ; @@ -59,7 +61,9 @@ void State::FromLocal( const fs::path& p ) bool State::IsIgnore( const std::string& filename ) { - return filename[0] == '.' ; + return m_ignore.Contains(filename); + //filename == ".grive" || filename == ".grive_state" || filename == ".trash"; + //return filename[0] == '.' ; } void State::FromLocal( const fs::path& p, gr::Resource* folder ) diff --git a/libgrive/src/drive/State.hh b/libgrive/src/drive/State.hh index a0e12132..9d7d306b 100644 --- a/libgrive/src/drive/State.hh +++ b/libgrive/src/drive/State.hh @@ -23,6 +23,7 @@ #include "util/DateTime.hh" #include "util/FileSystem.hh" +#include "util/Ignore.hh" #include @@ -43,7 +44,7 @@ public : typedef ResourceTree::iterator iterator ; public : - explicit State( const fs::path& filename, const Json& options ) ; + explicit State( const fs::path& filename, const Json& options, const Ignore& igo ) ; ~State() ; void FromLocal( const fs::path& p ) ; @@ -70,14 +71,15 @@ private : bool Update( const Entry& e ) ; std::size_t TryResolveEntry() ; - static bool IsIgnore( const std::string& filename ) ; - + bool IsIgnore( const std::string& filename ) ; + private : ResourceTree m_res ; - DateTime m_last_sync ; - long m_cstamp ; + DateTime m_last_sync ; + long m_cstamp ; std::vector m_unresolved ; + Ignore m_ignore; } ; } // end of namespace diff --git a/libgrive/src/protocol/Json.cc b/libgrive/src/protocol/Json.cc index 7012db77..9c64427c 100644 --- a/libgrive/src/protocol/Json.cc +++ b/libgrive/src/protocol/Json.cc @@ -214,6 +214,13 @@ void Json::Add( const std::string& key, const Json& json ) ::json_object_object_add( m_json, key.c_str(), json.m_json ) ; } +void Json::Remove( const std::string& key ) +{ + assert( m_json != 0 ) ; + + ::json_object_object_del( m_json, key.c_str() ) ; +} + void Json::Add( const Json& json ) { assert( m_json != 0 ) ; diff --git a/libgrive/src/protocol/Json.hh b/libgrive/src/protocol/Json.hh index 034a4fc7..3b947ea9 100644 --- a/libgrive/src/protocol/Json.hh +++ b/libgrive/src/protocol/Json.hh @@ -74,7 +74,9 @@ public : void Add( const Json& json ) ; Json FindInArray( const std::string& key, const std::string& value ) const ; bool FindInArray( const std::string& key, const std::string& value, Json& result ) const ; - + + void Remove( const std::string& key ) ; + friend std::ostream& operator<<( std::ostream& os, const Json& json ) ; void Write( StdioFile& file ) const ; @@ -93,4 +95,4 @@ public : struct json_object *m_json ; } ; -} \ No newline at end of file +} diff --git a/libgrive/src/util/Ignore.cc b/libgrive/src/util/Ignore.cc new file mode 100644 index 00000000..59fbf226 --- /dev/null +++ b/libgrive/src/util/Ignore.cc @@ -0,0 +1,58 @@ +/* + 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. + + 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 "Ignore.hh" + +namespace po = boost::program_options; + +namespace gr { + +Ignore::Ignore( const Config& cfg ) : + m_cfg ( cfg ) +{ + try { + ignoreList = m_cfg.Get( "ignore" ); + } catch( Exception e) { // ignore not in config + ignoreList = Json(); + } +} + +void Ignore::Add( const std::string& filename ) { + ignoreList.Add( filename, Json::Parse("1") ); +} + +void Ignore::Remove( const std::string& filename ) { + ignoreList.Remove( filename ); +} + +bool Ignore::Contains( const std::string& filename ) { + return ignoreList.Has(filename); +} + +void Ignore::Save( ) +{ + m_cfg.Set( "ignore", ignoreList ); + m_cfg.Save(); +} + + + +} // end of namespace + + diff --git a/libgrive/src/util/Ignore.hh b/libgrive/src/util/Ignore.hh new file mode 100644 index 00000000..634474de --- /dev/null +++ b/libgrive/src/util/Ignore.hh @@ -0,0 +1,54 @@ +/* + 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. + + 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. +*/ + +#pragma once + +#include "Config.hh" + +#include "protocol/Json.hh" + + +namespace boost +{ + namespace program_options + { + class variables_map ; + } +} + +namespace gr { + +class Ignore +{ +public : + Ignore( const Config& cfg ) ; + + void Add( const std::string& filename ); + void Remove( const std::string& filename ); + + bool Contains( const std::string& filename ); + + void Save( ) ; + +private : + Config m_cfg; + Json ignoreList; +} ; + +} // end of namespace