From bcdb2fe1e735d66e6d9f019a968f770b02fdfd7a Mon Sep 17 00:00:00 2001 From: Fabian Date: Wed, 22 Aug 2012 17:04:53 +0200 Subject: [PATCH 1/2] Ignore List added --- grive/src/main.cc | 26 +++++++++++++- libgrive/src/drive/Drive.cc | 4 +-- libgrive/src/drive/Drive.hh | 3 +- libgrive/src/drive/State.cc | 12 ++++--- libgrive/src/drive/State.hh | 13 ++++--- libgrive/src/protocol/Json.cc | 7 ++++ libgrive/src/protocol/Json.hh | 6 ++-- libgrive/src/util/Ignore.cc | 63 +++++++++++++++++++++++++++++++++ libgrive/src/util/Ignore.hh | 65 +++++++++++++++++++++++++++++++++++ 9 files changed, 184 insertions(+), 15 deletions(-) create mode 100644 libgrive/src/util/Ignore.cc create mode 100644 libgrive/src/util/Ignore.hh 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..e661492b 100644 --- a/libgrive/src/drive/Drive.hh +++ b/libgrive/src/drive/Drive.hh @@ -24,6 +24,7 @@ #include "http/Header.hh" #include "protocol/Json.hh" #include "util/Exception.hh" +#include "util/Ignore.hh" #include #include @@ -40,7 +41,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..462fc878 100644 --- a/libgrive/src/drive/State.hh +++ b/libgrive/src/drive/State.hh @@ -23,8 +23,10 @@ #include "util/DateTime.hh" #include "util/FileSystem.hh" +#include "util/Ignore.hh" #include +#include namespace gr { @@ -43,7 +45,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 +72,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..b8cddb72 --- /dev/null +++ b/libgrive/src/util/Ignore.cc @@ -0,0 +1,63 @@ +/* + 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" + +#include "util/Config.hh" +#include "util/StdioFile.hh" + +#include "protocol/Json.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..367eb54a --- /dev/null +++ b/libgrive/src/util/Ignore.hh @@ -0,0 +1,65 @@ +/* + 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 "Exception.hh" +#include "FileSystem.hh" +#include "Config.hh" + +#include "protocol/Json.hh" + +#include "Config.hh" + + +namespace boost +{ + namespace program_options + { + class variables_map ; + } +} + +namespace gr { + +class Ignore +{ +public : + //struct Error : virtual Exception {} ; + //typedef boost::error_info File ; + + 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 : + //Json Read( ) ; + //static fs::path GetPath( const fs::path& root_path ) ; + +private : + Config m_cfg; + Json ignoreList; +} ; + +} // end of namespace From 306c0a368cd6666791f9dc7585184285fb55cca7 Mon Sep 17 00:00:00 2001 From: Fabian Date: Wed, 22 Aug 2012 17:18:01 +0200 Subject: [PATCH 2/2] Unnecessary includes removed --- libgrive/src/drive/Drive.hh | 1 - libgrive/src/drive/State.hh | 1 - libgrive/src/util/Ignore.cc | 5 ----- libgrive/src/util/Ignore.hh | 11 ----------- 4 files changed, 18 deletions(-) diff --git a/libgrive/src/drive/Drive.hh b/libgrive/src/drive/Drive.hh index e661492b..556862bd 100644 --- a/libgrive/src/drive/Drive.hh +++ b/libgrive/src/drive/Drive.hh @@ -27,7 +27,6 @@ #include "util/Ignore.hh" #include -#include namespace gr { diff --git a/libgrive/src/drive/State.hh b/libgrive/src/drive/State.hh index 462fc878..9d7d306b 100644 --- a/libgrive/src/drive/State.hh +++ b/libgrive/src/drive/State.hh @@ -26,7 +26,6 @@ #include "util/Ignore.hh" #include -#include namespace gr { diff --git a/libgrive/src/util/Ignore.cc b/libgrive/src/util/Ignore.cc index b8cddb72..59fbf226 100644 --- a/libgrive/src/util/Ignore.cc +++ b/libgrive/src/util/Ignore.cc @@ -19,11 +19,6 @@ #include "Ignore.hh" -#include "util/Config.hh" -#include "util/StdioFile.hh" - -#include "protocol/Json.hh" - namespace po = boost::program_options; namespace gr { diff --git a/libgrive/src/util/Ignore.hh b/libgrive/src/util/Ignore.hh index 367eb54a..634474de 100644 --- a/libgrive/src/util/Ignore.hh +++ b/libgrive/src/util/Ignore.hh @@ -19,14 +19,10 @@ #pragma once -#include "Exception.hh" -#include "FileSystem.hh" #include "Config.hh" #include "protocol/Json.hh" -#include "Config.hh" - namespace boost { @@ -41,9 +37,6 @@ namespace gr { class Ignore { public : - //struct Error : virtual Exception {} ; - //typedef boost::error_info File ; - Ignore( const Config& cfg ) ; void Add( const std::string& filename ); @@ -53,10 +46,6 @@ public : void Save( ) ; -private : - //Json Read( ) ; - //static fs::path GetPath( const fs::path& root_path ) ; - private : Config m_cfg; Json ignoreList;