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: #96 "Sync files and folders which start with a '.'" solved properly #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion grive/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

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

#include "drive/Drive.hh"

Expand Down Expand Up @@ -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<std::string>(), "Adds the file to the list of ignored files." )
( "unignore,u", po::value<std::string>(), "Removes the file from the list of ignored files." )
;

po::variables_map vm;
Expand Down Expand Up @@ -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::string>() << "\"" << std::endl;
Log( "ignoring file %1%", vm["ignore"].as<std::string>(), log::verbose );
Ignore igno(config);
igno.Add(vm["ignore"].as<std::string>());
igno.Save();
return 0;
} else if( vm.count( "unignore" ) ) {
std::cout << "Unignoring file \"" << vm["unignore"].as<std::string>() << "\"" << std::endl;
Log( "unignoring file %1%", vm["unignore"].as<std::string>(), log::verbose );
Ignore igno(config);
igno.Remove(vm["unignore"].as<std::string>());
igno.Save();
return 0;
}

std::string refresh_token ;
Expand All @@ -184,7 +208,7 @@ 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, config.GetAll() ) ;
Drive drive( &agent, config.GetAll(), Ignore(config) ) ;
drive.DetectChanges() ;

if ( vm.count( "dry-run" ) == 0 )
Expand Down
4 changes: 2 additions & 2 deletions libgrive/src/drive/Drive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ;
Expand Down
4 changes: 2 additions & 2 deletions libgrive/src/drive/Drive.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include "http/Header.hh"
#include "protocol/Json.hh"
#include "util/Exception.hh"
#include "util/Ignore.hh"

#include <string>
#include <vector>

namespace gr {

Expand All @@ -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() ;
Expand Down
12 changes: 8 additions & 4 deletions libgrive/src/drive/State.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ;

Expand All @@ -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 )
Expand Down
12 changes: 7 additions & 5 deletions libgrive/src/drive/State.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "util/DateTime.hh"
#include "util/FileSystem.hh"
#include "util/Ignore.hh"

#include <memory>

Expand All @@ -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 ) ;
Expand All @@ -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<Entry> m_unresolved ;
Ignore m_ignore;
} ;

} // end of namespace
7 changes: 7 additions & 0 deletions libgrive/src/protocol/Json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ;
Expand Down
6 changes: 4 additions & 2 deletions libgrive/src/protocol/Json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;

Expand All @@ -93,4 +95,4 @@ public :
struct json_object *m_json ;
} ;

}
}
58 changes: 58 additions & 0 deletions libgrive/src/util/Ignore.cc
Original file line number Diff line number Diff line change
@@ -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


54 changes: 54 additions & 0 deletions libgrive/src/util/Ignore.hh
Original file line number Diff line number Diff line change
@@ -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