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

Added ability to move and rename files #36

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
18c4931
Adding methods to support renaming
dylanwulf Sep 27, 2015
8e98249
Now prefers title over original filename to support renaming
dylanwulf Sep 27, 2015
7e222b9
Renaming no longer changes modified date on server
dylanwulf Sep 27, 2015
bdc5afd
Merge branch 'master' of https://github.com/vitalif/grive2
dylanwulf Oct 10, 2015
4409ad7
Changed rename to move
dylanwulf Oct 11, 2015
fe3c597
Added command line option and fixed errors
dylanwulf Oct 12, 2015
a11d8c7
Merge branch 'master' of https://github.com/vitalif/grive2
dylanwulf Oct 12, 2015
a1dc212
Changed success/failure message on file move
dylanwulf Oct 12, 2015
be8aa02
Added some logic for moving files
dylanwulf Oct 12, 2015
3c699d7
Just added some newlines, nothing to see here.
dylanwulf Oct 12, 2015
4a8cbc0
Changed Drive.Move() to accept paths as arguments, removed string_lis…
dylanwulf Oct 12, 2015
27ce2df
Fixed indents
dylanwulf Oct 12, 2015
db9d11c
Fixed more indents
dylanwulf Oct 12, 2015
0f4b1d5
Couple more slight indent fixes
dylanwulf Oct 12, 2015
3a85c76
Fixed funky indents in Syncer2::Move()
dylanwulf Oct 13, 2015
540a825
Made move option compatible with -p option
dylanwulf Oct 13, 2015
8cbc57b
Now always syncs before moving files.
dylanwulf Oct 13, 2015
c41072c
Extra logic to prevent crashing
dylanwulf Oct 13, 2015
a592721
Converts paths to canonical paths, which makes it work with relative …
dylanwulf Oct 14, 2015
83b730b
Added case when source and destination are equal
dylanwulf Oct 14, 2015
93d44c5
Stopped fs::canonical() from complaining about new_p when it doesn't …
dylanwulf Oct 21, 2015
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
44 changes: 26 additions & 18 deletions grive/src/main.cc
Expand Up @@ -78,19 +78,19 @@ void InitLog( const po::variables_map& vm )
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 ) ;
}

if ( vm.count( "verbose" ) )
{
console_log->Enable( log::verbose ) ;
}

if ( vm.count( "debug" ) )
{
console_log->Enable( log::verbose ) ;
Expand All @@ -102,7 +102,7 @@ void InitLog( const po::variables_map& vm )
int Main( int argc, char **argv )
{
InitGCrypt() ;

// construct the program options
po::options_description desc( "Grive options" );
desc.add_options()
Expand All @@ -121,12 +121,13 @@ int Main( int argc, char **argv )
( "dry-run", "Only detect which files need to be uploaded/downloaded, "
"without actually performing them." )
( "ignore", po::value<std::string>(), "Ignore files relative paths of which match this Perl RegExp." )
( "move,m", po::value<std::vector<std::string> >()->multitoken(), "Moves or renames a file without reuploading or redownloading." )
;

po::variables_map vm;
po::store(po::parse_command_line( argc, argv, desc), vm );
po::notify(vm);

// simple commands that doesn't require log or config
if ( vm.count("help") )
{
Expand All @@ -142,9 +143,9 @@ int Main( int argc, char **argv )

// initialize logging
InitLog(vm) ;

Config config(vm) ;

Log( "config file name %1%", config.Filename(), log::verbose );

std::auto_ptr<http::Agent> http( new http::CurlAgent );
Expand All @@ -154,26 +155,26 @@ int Main( int argc, char **argv )
if ( vm.count( "auth" ) )
{
OAuth2 token( http.get(), client_id, client_secret ) ;

std::cout
<< "-----------------------\n"
<< "Please go to this URL and get an authentication code:\n\n"
<< token.MakeAuthURL()
<< std::endl ;

std::cout
<< "\n-----------------------\n"
<< "Please input the authentication code here: " << std::endl ;
std::string code ;
std::cin >> code ;

token.Auth( code ) ;

// save to config
config.Set( "refresh_token", Val( token.RefreshToken() ) ) ;
config.Save() ;
}

std::string refresh_token ;
try
{
Expand All @@ -185,25 +186,32 @@ int Main( int argc, char **argv )
"Please run grive with the \"-a\" option if this is the "
"first time you're accessing your Google Drive!",
log::critical ) ;

return -1;
}

OAuth2 token( http.get(), refresh_token, client_id, client_secret ) ;
AuthAgent agent( token, http.get() ) ;
v2::Syncer2 syncer( &agent );

Drive drive( &syncer, config.GetAll() ) ;
drive.DetectChanges() ;

if ( vm.count( "dry-run" ) == 0 )
if ( vm.count( "dry-run" ) == 0 && vm.count( "move" ) == 0 )
{
drive.Update() ;
drive.SaveState() ;
}
else if( vm.count( "move" ) > 0 ){
bool success = drive.Move();
if (success)
Log( "Move successful!", log::info );
else
Log( "Move failed.", log::error);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use a consistent indenting style? :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, been trying out a new editor and I'm still figuring out how it handles indents. I'll fix this.

else
drive.DryRun() ;

config.Save() ;
Log( "Finished!", log::info ) ;
return 0 ;
Expand Down
29 changes: 19 additions & 10 deletions libgrive/src/base/Drive.cc
Expand Up @@ -61,14 +61,14 @@ void Drive::FromRemote( const Entry& entry )
// entries from change feed does not have the parent HREF,
// so these checkings are done in normal entries only
Resource *parent = m_state.FindByHref( entry.ParentHref() ) ;

if ( parent != 0 && !parent->IsFolder() )
Log( "warning: entry %1% has parent %2% which is not a folder, ignored",
entry.Title(), parent->Name(), log::verbose ) ;

else if ( parent == 0 || !parent->IsInRootTree() )
Log( "file \"%1%\" parent doesn't exist, ignored", entry.Title(), log::verbose ) ;

else
m_state.FromRemote( entry ) ;
}
Expand All @@ -77,7 +77,7 @@ void Drive::FromChange( const Entry& entry )
{
if ( entry.IsRemoved() )
Log( "file \"%1%\" represents a deletion, ignored", entry.Title(), log::verbose ) ;

// folders go directly
else
m_state.FromRemote( entry ) ;
Expand All @@ -103,10 +103,10 @@ void Drive::SyncFolders( )
{
if ( e.ParentHrefs().size() != 1 )
Log( "folder \"%1%\" has multiple parents, ignored", e.Title(), log::verbose ) ;

else if ( e.Title().find('/') != std::string::npos )
Log( "folder \"%1%\" contains a slash in its name, ignored", e.Title(), log::verbose ) ;

else
m_state.FromRemote( e ) ;
}
Expand All @@ -120,10 +120,10 @@ void Drive::DetectChanges()
{
Log( "Reading local directories", log::info ) ;
m_state.FromLocal( m_root ) ;

long prev_stamp = m_state.ChangeStamp() ;
Trace( "previous change stamp is %1%", prev_stamp ) ;

SyncFolders( ) ;

Log( "Reading remote server file list", log::info ) ;
Expand All @@ -135,7 +135,7 @@ void Drive::DetectChanges()
feed->begin(), feed->end(),
boost::bind( &Drive::FromRemote, this, _1 ) ) ;
}

// pull the changes feed
if ( prev_stamp != -1 )
{
Expand All @@ -150,11 +150,20 @@ void Drive::DetectChanges()
}
}

bool Drive::Move()
{
if (m_options["move"].As<std::vector<std::string> >().size() < 2){
Log("Not enough arguments for move!", log::info);
return false;
}
return m_state.Move( m_syncer, m_options["move"].As<std::vector<std::string> >()[0], m_options["move"].As<std::vector<std::string> >()[1] );
}

void Drive::Update()
{
Log( "Synchronizing files", log::info ) ;
m_state.Sync( m_syncer, m_options ) ;

UpdateChangeStamp( ) ;
}

Expand Down
7 changes: 4 additions & 3 deletions libgrive/src/base/Drive.hh
Expand Up @@ -41,18 +41,19 @@ public :
Drive( Syncer *syncer, const Val& options ) ;

void DetectChanges() ;
bool Move();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here I think Drive::Move() should take file arguments instead of taking them from Config object...

void Update() ;
void DryRun() ;
void SaveState() ;

struct Error : virtual Exception {} ;

private :
void SyncFolders( ) ;
void FromRemote( const Entry& entry ) ;
void FromChange( const Entry& entry ) ;
void UpdateChangeStamp( ) ;

private :
Syncer *m_syncer ;
fs::path m_root ;
Expand Down
2 changes: 1 addition & 1 deletion libgrive/src/base/Entry.cc
Expand Up @@ -117,7 +117,7 @@ bool Entry::IsRemoved() const

std::string Entry::Name() const
{
return !m_filename.empty() ? m_filename : m_title ;
return !m_title.empty() ? m_title : m_filename;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a pointless change since recently because m_filename is also copied from title field of json... see #32

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I did this before you changed it and I just didn't bother to update it after you changed it since it should get the correct result anyway

}

} // end of namespace gr