Merge branch 'v0.0.2'

pull/40/head
Matchman Green 2012-04-28 01:54:31 +08:00
commit cfe319cf01
17 changed files with 427 additions and 16 deletions

View File

@ -4,16 +4,34 @@ cmake_minimum_required(VERSION 2.8)
include(FindOpenSSL)
include_directories(
${grive_SOURCE_DIR}/src
)
add_executable( grive
src/main.cc
src/OAuth2.cc
src/Drive.cc
src/Collection.cc
src/drive/Collection.cc
src/drive/Drive.cc
src/protocol/Download.cc
src/protocol/HTTP.cc
src/protocol/Json.cc
src/protocol/Download.cc )
src/protocol/OAuth2.cc
src/util/DateTime.cc
src/util/OS.cc
)
target_link_libraries( grive
curl
json
${OPENSSL_LIBRARIES} )
${OPENSSL_LIBRARIES}
)
add_executable( unittest
test/UnitTest.cc
src/util/DateTime.cc
test/util/DateTimeTest.cc
)
target_link_libraries( unittest
cppunit
)

View File

@ -20,13 +20,10 @@
#include "Collection.hh"
#include "protocol/Json.hh"
#include "util/OS.hh"
#include <cassert>
// OS specific library
#include <sys/stat.h>
#include <sys/types.h>
// for debugging
#include <iostream>
@ -104,7 +101,8 @@ void Collection::Swap( Collection& coll )
void Collection::CreateSubDir( const std::string& prefix )
{
std::string dir = prefix + m_title ;
mkdir( dir.c_str(), 0700 ) ;
// mkdir( dir.c_str(), 0700 ) ;
os::MakeDir( dir ) ;
for ( std::vector<Collection*>::iterator i = m_child.begin() ; i != m_child.end() ; ++i )
{

View File

@ -21,7 +21,9 @@
#include "protocol/HTTP.hh"
#include "protocol/Json.hh"
#include "OAuth2.hh"
#include "protocol/OAuth2.hh"
#include "util/DateTime.hh"
#include "util/OS.hh"
// dependent libraries
#include <openssl/evp.h>
@ -180,10 +182,12 @@ void Drive::UpdateFile( const Json& entry )
if ( entry.Has( "docs$filename" ) )
{
// use title as the filename
std::string filename = entry["docs$filename"]["$t"].As<std::string>() ;
std::string url = entry["content"]["src"].As<std::string>() ;
std::string filename = entry["docs$filename"]["$t"].As() ;
std::string url = entry["content"]["src"].As() ;
std::string parent_href = Parent( entry ) ;
DateTime remote( entry["updated"]["$t"].As<std::string>() ) ;
bool changed = true ;
std::string path = "./" + filename ;
@ -194,11 +198,16 @@ void Drive::UpdateFile( const Json& entry )
if ( pit != m_coll.end() )
path = pit->Path() + "/" + filename ;
}
DateTime local = os::FileMTime( path ) ;
std::cout << "file time: " << entry["updated"]["$t"].As<std::string>() << " " << remote << " " << local << std::endl ;
// compare checksum first if file exists
std::ifstream ifile( path.c_str(), std::ios::binary | std::ios::out ) ;
if ( ifile && entry.Has("docs$md5Checksum") )
{
os::SetFileTime( path, remote ) ;
std::string remote_md5 = entry["docs$md5Checksum"]["$t"].As<std::string>() ;
std::string local_md5 = MD5( ifile.rdbuf() ) ;

View File

@ -17,8 +17,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "OAuth2.hh"
#include "Drive.hh"
#include "drive/Drive.hh"
#include "protocol/OAuth2.hh"
#include "protocol/Json.hh"
#include <cassert>

View File

@ -138,6 +138,11 @@ void Json::Add( const std::string& key, const Json& json )
::json_object_object_add( m_json, key.c_str(), json.m_json ) ;
}
Json::Proxy Json::As() const
{
return Proxy( *this ) ;
}
template <>
std::string Json::As<std::string>() const
{
@ -243,5 +248,4 @@ bool Json::FindInArray( const std::string& key, const std::string& value, Json&
}
}
}

View File

@ -52,6 +52,14 @@ public :
template <typename T>
T As() const ;
struct Proxy
{
const Json& referring ;
explicit Proxy( const Json& j ) : referring( j ) { }
template <typename T> operator T() const { return referring.As<T>() ; }
} ;
Proxy As() const ;
template <typename T>
bool Is() const ;

95
src/util/DateTime.cc Normal file
View File

@ -0,0 +1,95 @@
/*
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 "DateTime.hh"
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <time.h>
namespace gr {
DateTime::DateTime( ) :
m_sec ( 0 ),
m_nsec ( 0 )
{
}
DateTime::DateTime( const std::string& iso ) :
m_sec ( 0 ),
m_nsec ( 0 )
{
struct tm tp = {} ;
const char *r = ::strptime( iso.c_str(), "%Y-%m-%dT%H:%M:%S", &tp ) ;
// should be '.' followed by 3 digits and 'Z' (e.g. .123Z)
if ( r != 0 && r - iso.c_str() == 19 )
{
m_sec = ::timegm( &tp ) ;
// at least 3 digits is OK. we don't care the Z
if ( *r == '.' && ::strlen( r+1 ) >= 3 )
m_nsec = std::atoi( r+1 ) * 1000 * 1000 ;
}
}
DateTime::DateTime( std::time_t sec, unsigned long nsec ) :
m_sec ( sec ),
m_nsec ( nsec )
{
}
struct tm DateTime::Tm() const
{
struct tm tp ;
gmtime_r( &m_sec, &tp ) ;
return tp ;
}
std::time_t DateTime::Sec( ) const
{
return m_sec ;
}
unsigned long DateTime::NanoSec( ) const
{
return m_nsec ;
}
std::ostream& operator<<( std::ostream& os, const DateTime& dt )
{
struct tm tp = dt.Tm() ;
char buf[40] ;
strftime( buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tp ) ;
return os << buf << '.' << std::setw( 3 ) << std::setfill('0') << dt.NanoSec()/1000000 << 'Z' ;
}
struct timeval DateTime::Tv() const
{
timeval result ;
result.tv_sec = m_sec ;
result.tv_usec = m_nsec / 1000 ;
return result ;
}
} // end of namespace

48
src/util/DateTime.hh Normal file
View File

@ -0,0 +1,48 @@
/*
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 <ctime>
#include <string>
#include <iosfwd>
namespace gr {
class DateTime
{
public :
DateTime( ) ;
explicit DateTime( const std::string& iso ) ;
explicit DateTime( std::time_t sec, unsigned long nsec = 0 ) ;
std::time_t Sec( ) const ;
unsigned long NanoSec( ) const ;
struct tm Tm() const ;
struct timeval Tv() const ;
private :
std::time_t m_sec ;
unsigned long m_nsec ;
} ;
std::ostream& operator<<( std::ostream& os, const DateTime& dt ) ;
} // end of namespace

53
src/util/OS.cc Normal file
View File

@ -0,0 +1,53 @@
/*
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 "OS.hh"
#include "DateTime.hh"
#include <stdexcept>
// OS specific headers
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
namespace gr { namespace os {
void MakeDir( const std::string& dir )
{
mkdir( dir.c_str(), 0700 ) ;
}
DateTime FileMTime( const std::string& filename )
{
struct stat s = {} ;
if ( ::stat( filename.c_str(), &s ) != 0 )
throw std::runtime_error( "cannot get file attribute of " + filename ) ;
return DateTime( s.st_mtim.tv_sec, s.st_mtim.tv_nsec ) ;
}
void SetFileTime( const std::string& filename, const DateTime& t )
{
struct timeval tvp[2] = { t.Tv(), t.Tv() } ;
if ( ::utimes( filename.c_str(), tvp ) != 0 )
throw std::runtime_error( "cannot set file time" ) ;
}
} } // end of namespaces

35
src/util/OS.hh Normal file
View File

@ -0,0 +1,35 @@
/*
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 <string>
namespace gr {
class DateTime ;
namespace os
{
void MakeDir( const std::string& dir ) ;
DateTime FileMTime( const std::string& filename ) ;
void SetFileTime( const std::string& filename, const DateTime& t ) ;
}
} // end of namespaces

33
test/UnitTest.cc Normal file
View File

@ -0,0 +1,33 @@
/*
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 <cppunit/ui/text/TestRunner.h>
#include "util/DateTimeTest.hh"
int main( int argc, char **argv )
{
using namespace grut ;
CppUnit::TextUi::TestRunner runner;
runner.addTest( DateTimeTest::suite( ) ) ;
runner.run();
return 0 ;
}

63
test/util/DateTimeTest.cc Normal file
View File

@ -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 "DateTimeTest.hh"
#include "util/DateTime.hh"
#include <cstring>
namespace grut {
using namespace gr ;
DateTimeTest::DateTimeTest( )
{
}
void DateTimeTest::TestParseIso( )
{
DateTime subject( "2009-07-29T20:31:39.804Z" ) ;
struct tm tp = subject.Tm() ;
CPPUNIT_ASSERT( tp.tm_year == 109 ) ;
CPPUNIT_ASSERT( tp.tm_sec == 39 ) ;
CPPUNIT_ASSERT_EQUAL( 804000000UL, subject.NanoSec() ) ;
}
void DateTimeTest::TestParseNoMillisec( )
{
DateTime subject( "2009-07-29T20:31:39Z" ) ;
CPPUNIT_ASSERT_EQUAL( 0UL, subject.NanoSec() ) ;
}
void DateTimeTest::TestParseInvalid( )
{
DateTime subject( "abcdefg" ) ;
CPPUNIT_ASSERT_EQUAL( static_cast<time_t>(0), subject.Sec() ) ;
CPPUNIT_ASSERT_EQUAL( 0UL, subject.NanoSec() ) ;
}
void DateTimeTest::TestOffByOne( )
{
DateTime subject( "2008-12-21T02:48:53.940Z" ) ;
struct tm tp = subject.Tm() ;
CPPUNIT_ASSERT_EQUAL( 21, tp.tm_mday ) ;
}
} // end of namespace grut

47
test/util/DateTimeTest.hh Normal file
View File

@ -0,0 +1,47 @@
/*
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 <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
namespace grut {
class DateTimeTest : public CppUnit::TestFixture
{
public :
DateTimeTest( ) ;
// declare suit function
CPPUNIT_TEST_SUITE( DateTimeTest ) ;
CPPUNIT_TEST( TestParseIso ) ;
CPPUNIT_TEST( TestParseNoMillisec ) ;
CPPUNIT_TEST( TestOffByOne ) ;
CPPUNIT_TEST( TestParseInvalid ) ;
CPPUNIT_TEST_SUITE_END();
private :
void TestParseIso( ) ;
void TestParseNoMillisec( ) ;
void TestOffByOne( ) ;
void TestParseInvalid( ) ;
} ;
} // end of namespace