Skip to content

Commit b3b10b2

Browse files
author
Juraj Puchky
committed
Found nice fix from User Indian4646, merging...
Merge branch 'master' of https://github.com/indian4646/grive Conflicts: grive/src/main.cc libgrive/src/drive/Drive.cc libgrive/src/drive/Resource.cc libgrive/src/drive/Resource.hh libgrive/src/drive/State.cc libgrive/src/drive/State.hh libgrive/src/http/Agent.hh libgrive/src/http/CurlAgent.cc libgrive/src/http/CurlAgent.hh libgrive/src/util/File.hh libgrive/src/util/StdioFile.cc
2 parents 27fd61b + f3e914a commit b3b10b2

File tree

16 files changed

+382
-27
lines changed

16 files changed

+382
-27
lines changed

grive/src/main.cc

+56-7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void InitGCrypt()
6666

6767
void InitLog( const po::variables_map& vm )
6868
{
69+
<<<<<<< HEAD
6970
std::auto_ptr<log::CompositeLog> comp_log(new log::CompositeLog) ;
7071
LogBase* console_log = comp_log->Add( std::auto_ptr<LogBase>( new log::DefaultLog ) ) ;
7172

@@ -85,6 +86,14 @@ void InitLog( const po::variables_map& vm )
8586

8687
comp_log->Add( file_log ) ;
8788
}
89+
=======
90+
InitGCrypt() ;
91+
92+
Config config ;
93+
94+
std::auto_ptr<gr::log::CompositeLog> comp_log(new gr::log::CompositeLog) ;
95+
LogBase* console_log = comp_log->Add( std::auto_ptr<LogBase>( new gr::log::DefaultLog ) ) ;
96+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
8897

8998
if ( vm.count( "verbose" ) )
9099
{
@@ -174,6 +183,46 @@ int Main( int argc, char **argv )
174183
config.Set( "refresh_token", Json( token.RefreshToken() ) ) ;
175184
config.Save() ;
176185
}
186+
<<<<<<< HEAD
187+
=======
188+
if ( vm.count( "log" ) )
189+
{
190+
std::auto_ptr<LogBase> file_log(new gr::log::DefaultLog( vm["log"].as<std::string>() )) ;
191+
file_log->Enable( gr::log::debug ) ;
192+
file_log->Enable( gr::log::verbose ) ;
193+
file_log->Enable( gr::log::info ) ;
194+
file_log->Enable( gr::log::warning ) ;
195+
file_log->Enable( gr::log::error ) ;
196+
file_log->Enable( gr::log::critical ) ;
197+
198+
// log grive version to log file
199+
file_log->Log( gr::log::Fmt("grive version " VERSION " " __DATE__ " " __TIME__), gr::log::verbose ) ;
200+
file_log->Log( gr::log::Fmt("current time: %1%") % DateTime::Now(), gr::log::verbose ) ;
201+
202+
comp_log->Add( file_log ) ;
203+
}
204+
if ( vm.count( "version" ) )
205+
{
206+
std::cout
207+
<< "grive version " << VERSION << ' ' << __DATE__ << ' ' << __TIME__ << std::endl ;
208+
return 0 ;
209+
}
210+
if ( vm.count( "verbose" ) )
211+
{
212+
console_log->Enable( gr::log::verbose ) ;
213+
}
214+
if ( vm.count( "debug" ) )
215+
{
216+
console_log->Enable( gr::log::verbose ) ;
217+
console_log->Enable( gr::log::debug ) ;
218+
}
219+
if ( vm.count( "force" ) )
220+
{
221+
options.Add( "force", Json(true) ) ;
222+
}
223+
224+
LogBase::Inst( std::auto_ptr<LogBase>(comp_log.release()) ) ;
225+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
177226

178227
std::string refresh_token ;
179228
try
@@ -185,7 +234,7 @@ int Main( int argc, char **argv )
185234
Log(
186235
"Please run grive with the \"-a\" option if this is the "
187236
"first time you're accessing your Google Drive!",
188-
log::critical ) ;
237+
gr::log::critical ) ;
189238

190239
return -1;
191240
}
@@ -198,14 +247,14 @@ int Main( int argc, char **argv )
198247

199248
if ( vm.count( "dry-run" ) == 0 )
200249
{
201-
drive.Update() ;
250+
drive.Update( token ) ;
202251
drive.SaveState() ;
203252
}
204253
else
205-
drive.DryRun() ;
254+
drive.DryRun( token ) ;
206255

207256
config.Save() ;
208-
Log( "Finished!", log::info ) ;
257+
Log( "Finished!", gr::log::info ) ;
209258
return 0 ;
210259
}
211260

@@ -217,15 +266,15 @@ int main( int argc, char **argv )
217266
}
218267
catch ( Exception& e )
219268
{
220-
Log( "exception: %1%", boost::diagnostic_information(e), log::critical ) ;
269+
Log( "exception: %1%", boost::diagnostic_information(e), gr::log::critical ) ;
221270
}
222271
catch ( std::exception& e )
223272
{
224-
Log( "exception: %1%", e.what(), log::critical ) ;
273+
Log( "exception: %1%", e.what(), gr::log::critical ) ;
225274
}
226275
catch ( ... )
227276
{
228-
Log( "unexpected exception", log::critical ) ;
277+
Log( "unexpected exception", gr::log::critical ) ;
229278
}
230279
return -1 ;
231280
}

libgrive/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
project(libgrive)
22

33
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
4-
4+
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64 -D_LARGE_FILES)
55
find_package(LibGcrypt REQUIRED)
66
find_package(JSONC REQUIRED)
77
find_package(CURL REQUIRED)

libgrive/src/drive/Drive.cc

+11-2
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,27 @@ void Drive::DetectChanges()
169169
}
170170
}
171171

172-
void Drive::Update()
172+
void Drive::Update( OAuth2& auth )
173173
{
174174
Log( "Synchronizing files", log::info ) ;
175+
<<<<<<< HEAD
175176
m_state.Sync( m_http, m_options ) ;
177+
=======
178+
http::CurlAgent http ;
179+
m_state.Sync( &http, m_http_hdr, auth) ;
180+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
176181

177182
UpdateChangeStamp( ) ;
178183
}
179184

180-
void Drive::DryRun()
185+
void Drive::DryRun( OAuth2& auth )
181186
{
182187
Log( "Synchronizing files (dry-run)", log::info ) ;
188+
<<<<<<< HEAD
183189
m_state.Sync( 0, m_options ) ;
190+
=======
191+
m_state.Sync( 0, m_http_hdr, auth ) ;
192+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
184193
}
185194

186195
void Drive::UpdateChangeStamp( )

libgrive/src/drive/Drive.hh

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public :
4545
Drive( http::Agent *agent, const Json& options ) ;
4646

4747
void DetectChanges() ;
48-
void Update() ;
49-
void DryRun() ;
48+
void Update( OAuth2& auth ) ;
49+
void DryRun( OAuth2& auth ) ;
5050
void SaveState() ;
5151

5252
struct Error : virtual Exception {} ;

libgrive/src/drive/Resource.cc

+62-3
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@
3636
#include "xml/Node.hh"
3737
#include "xml/NodeSet.hh"
3838
#include "xml/String.hh"
39+
#include "protocol/OAuth2.hh"
3940

4041
#include <boost/bind.hpp>
4142
#include <boost/exception/all.hpp>
4243

4344
#include <cassert>
44-
45+
#include <time.h>
4546
// for debugging
4647
#include <iostream>
4748

@@ -366,21 +367,45 @@ Resource* Resource::FindChild( const std::string& name )
366367
}
367368

368369
// try to change the state to "sync"
370+
<<<<<<< HEAD
369371
void Resource::Sync( http::Agent *http, DateTime& sync_time, const Json& options )
372+
=======
373+
void Resource::Sync( http::Agent *http, const http::Header& auth, OAuth2& oauth )
374+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
370375
{
371376
assert( m_state != unknown ) ;
372377
assert( !IsRoot() || m_state == sync ) ; // root folder is already synced
378+
std::vector<std::string> m_auth;
379+
//m_auth.assign(auth.begin(), auth.end());
380+
time_t t;
381+
time(&t);
382+
std::cout << auth << std::endl;
383+
if(t-oauth.Time() > oauth.ExpiresIn()-100){
384+
oauth.Refresh();
385+
m_auth.clear();
386+
oauth.m_time = t;
387+
}
388+
m_auth.push_back( "Authorization: Bearer " + oauth.AccessToken() ) ;
389+
m_auth.push_back( "GData-Version: 3.0" ) ;
373390

391+
<<<<<<< HEAD
374392
SyncSelf( http, options ) ;
375393

376394
// we want the server sync time, so we will take the server time of the last file uploaded to store as the sync time
377395
// m_mtime is updated to server modified time when the file is uploaded
378396
sync_time = std::max(sync_time, m_mtime);
397+
=======
398+
SyncSelf( http, (const http::Header&)m_auth ) ;
399+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
379400

380401
// if myself is deleted, no need to do the childrens
381402
if ( m_state != local_deleted && m_state != remote_deleted )
382403
std::for_each( m_child.begin(), m_child.end(),
404+
<<<<<<< HEAD
383405
boost::bind( &Resource::Sync, _1, http, boost::ref(sync_time), options ) ) ;
406+
=======
407+
boost::bind( &Resource::Sync, _1, http, auth, oauth ) ) ;
408+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
384409
}
385410

386411
void Resource::SyncSelf( http::Agent* http, const Json& options )
@@ -577,18 +602,45 @@ bool Resource::Create( http::Agent* http )
577602
}
578603
}
579604

605+
<<<<<<< HEAD
580606
bool Resource::Upload(
581607
http::Agent* http,
582608
const std::string& link,
583609
bool post)
610+
=======
611+
struct WriteThis {
612+
const char *readptr;
613+
off_t sizeleft;
614+
FILE* file;
615+
bool post;
616+
};
617+
bool Resource::Upload( http::Agent* http, const std::string& link, const http::Header& auth, bool post )
618+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
584619
{
585620
assert( http != 0 ) ;
621+
const fs::path& path = Path();
622+
struct WriteThis pooh;
623+
struct WriteThis meta_a;
624+
// TODO: upload in chunks
586625

626+
<<<<<<< HEAD
587627
File file( Path() ) ;
588628
std::ostringstream xcontent_len ;
589629
xcontent_len << "X-Upload-Content-Length: " << file.Size() ;
590630

591631
http::Header hdr ;
632+
=======
633+
pooh.file = fopen64(path.string().c_str(),"r");
634+
fseeko(pooh.file, 0, SEEK_END);
635+
pooh.sizeleft = ftello(pooh.file);
636+
rewind (pooh.file);
637+
638+
pooh.post = 1;
639+
640+
std::ostringstream xcontent_len ;
641+
xcontent_len << "X-Upload-Content-Length: " << pooh.sizeleft;
642+
http::Header hdr( auth ) ;
643+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
592644
hdr.Add( "Content-Type: application/atom+xml" ) ;
593645
hdr.Add( "X-Upload-Content-Type: application/octet-stream" ) ;
594646
hdr.Add( xcontent_len.str() ) ;
@@ -599,12 +651,14 @@ bool Resource::Upload(
599651
% m_kind
600652
% xml::Escape(m_name)
601653
).str() ;
602-
654+
meta_a.readptr=meta.c_str();
655+
meta_a.sizeleft=meta.size();
656+
meta_a.post=post;
603657
http::StringResponse str ;
604658
if ( post )
605659
http->Post( link, meta, &str, hdr ) ;
606660
else
607-
http->Put( link, meta, &str, hdr ) ;
661+
http->Put( link, &meta_a, &str, hdr ) ;
608662

609663
http::Header uphdr ;
610664
uphdr.Add( "Expect:" ) ;
@@ -614,7 +668,12 @@ bool Resource::Upload(
614668
std::string uplink = http->RedirLocation() ;
615669
http::XmlResponse xml ;
616670

671+
<<<<<<< HEAD
617672
http->Put( uplink, &file, &xml, uphdr ) ;
673+
=======
674+
http->Put( uplink, &pooh, &xml, uphdr ) ;
675+
fclose(pooh.file);
676+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
618677
AssignIDs( Entry( xml.Response() ) ) ;
619678
m_mtime = Entry(xml.Response()).MTime();
620679

libgrive/src/drive/Resource.hh

+10
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@ namespace http
3434
class Agent ;
3535
}
3636

37+
<<<<<<< HEAD
3738
class Json ;
3839

3940
namespace v1 {
41+
=======
42+
43+
44+
class OAuth2;
45+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
4046

4147
class Entry ;
4248

@@ -80,7 +86,11 @@ public :
8086
void FromRemote( const Entry& remote, const DateTime& last_sync ) ;
8187
void FromLocal( const DateTime& last_sync ) ;
8288

89+
<<<<<<< HEAD
8390
void Sync( http::Agent* http, DateTime& sync_time, const Json& options ) ;
91+
=======
92+
void Sync( http::Agent* http, const http::Header& auth, OAuth2& oauth ) ;
93+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
8494

8595
// children access
8696
iterator begin() const ;

libgrive/src/drive/State.cc

+7
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ void State::Write( const fs::path& filename ) const
260260
fs << result ;
261261
}
262262

263+
<<<<<<< HEAD
263264
void State::Sync( http::Agent *http, const Json& options )
264265
{
265266
// set the last sync time from the time returned by the server for the last file synced
@@ -282,6 +283,12 @@ void State::Sync( http::Agent *http, const Json& options )
282283
Trace( "updating last sync? %1%", last_sync_time ) ;
283284
m_last_sync = last_sync_time;
284285
}
286+
=======
287+
void State::Sync( http::Agent *http, const http::Header& auth, OAuth2::OAuth2& oauth)
288+
{
289+
m_res.Root()->Sync( http, auth ,oauth) ;
290+
m_last_sync = DateTime::Now() ;
291+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
285292
}
286293

287294
long State::ChangeStamp() const

libgrive/src/drive/State.hh

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "util/DateTime.hh"
2525
#include "util/FileSystem.hh"
26+
#include "protocol/OAuth2.hh"
2627

2728
#include <memory>
2829

@@ -59,7 +60,11 @@ public :
5960
Resource* FindByHref( const std::string& href ) ;
6061
Resource* FindByID( const std::string& id ) ;
6162

63+
<<<<<<< HEAD
6264
void Sync( http::Agent *http, const Json& options ) ;
65+
=======
66+
void Sync( http::Agent *http, const http::Header& auth, OAuth2::OAuth2& oauth ) ;
67+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
6368

6469
iterator begin() ;
6570
iterator end() ;

libgrive/src/http/Agent.hh

+5
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ public :
3737

3838
virtual long Put(
3939
const std::string& url,
40+
<<<<<<< HEAD
4041
const std::string& data,
4142
DataStream *dest,
43+
=======
44+
void* data,
45+
Receivable *dest,
46+
>>>>>>> f3e914a0ba807a1ebccf5d80d508c20920a7c215
4247
const Header& hdr ) = 0 ;
4348

4449
virtual long Put(

0 commit comments

Comments
 (0)