added state

pull/40/head
Matchman Green 2012-05-16 22:35:22 +08:00
parent 2095d0f956
commit 6dd505f8a6
15 changed files with 515 additions and 4 deletions

View File

@ -0,0 +1,48 @@
# - Find gdbm
# Find the native GDBM includes and library
#
# GDBM_INCLUDE_DIR - where to find gdbm.h, etc.
# GDBM_LIBRARIES - List of libraries when using gdbm.
# GDBM_FOUND - True if gdbm found.
IF (GDBM_INCLUDE_DIR)
# Already in cache, be silent
SET(GDBM_FIND_QUIETLY TRUE)
ENDIF (GDBM_INCLUDE_DIR)
FIND_PATH(GDBM_INCLUDE_DIR gdbm.h
/usr/local/include
/usr/include
/opt/local/include
)
SET(GDBM_NAMES gdbm)
FIND_LIBRARY(GDBM_LIBRARY
NAMES ${GDBM_NAMES}
PATHS /usr/lib /usr/local/lib /opt/local/lib
)
IF (GDBM_INCLUDE_DIR AND GDBM_LIBRARY)
SET(GDBM_FOUND TRUE)
SET( GDBM_LIBRARIES ${GDBM_LIBRARY} )
ELSE (GDBM_INCLUDE_DIR AND GDBM_LIBRARY)
SET(GDBM_FOUND FALSE)
SET( GDBM_LIBRARIES )
ENDIF (GDBM_INCLUDE_DIR AND GDBM_LIBRARY)
IF (GDBM_FOUND)
IF (NOT GDBM_FIND_QUIETLY)
MESSAGE(STATUS "Found GDBM: ${GDBM_LIBRARY}")
ENDIF (NOT GDBM_FIND_QUIETLY)
ELSE (GDBM_FOUND)
IF (GDBM_FIND_REQUIRED)
MESSAGE(STATUS "Looked for gdbm libraries named ${GDBMS_NAMES}.")
MESSAGE(FATAL_ERROR "Could NOT find gdbm library")
ENDIF (GDBM_FIND_REQUIRED)
ENDIF (GDBM_FOUND)
MARK_AS_ADVANCED(
GDBM_LIBRARY
GDBM_INCLUDE_DIR
)

View File

@ -6,9 +6,10 @@ find_package(OpenSSL REQUIRED)
find_package(JSONC REQUIRED)
find_package(CURL REQUIRED)
find_package(EXPAT REQUIRED)
find_package(CppUnit)
find_package(Boost REQUIRED)
find_package(Boost COMPONENTS filesystem system REQUIRED)
find_package(GDBM REQUIRED)
find_package(BFD)
find_package(CppUnit)
IF ( CPPUNIT_FOUND )
set( OPT_INCS ${CPPUNIT_INCLUDE_DIR} )
@ -26,6 +27,7 @@ endif ( BFD_FOUND )
include_directories(
${libgrive_SOURCE_DIR}/src
${libgrive_SOURCE_DIR}/test
${GDBM_INCLUDE_DIR}
${OPT_INCS}
)
@ -65,6 +67,8 @@ target_link_libraries( grive
${CURL_LIBRARIES}
${JSONC_LIBRARY}
${OPENSSL_LIBRARIES}
${GDBM_LIBRARIES}
${Boost_LIBRARIES}
${OPT_LIBS}
expat
)

View File

@ -71,8 +71,6 @@ Drive::Drive( OAuth2& auth, const Json& state ) :
ConstructDirTree( &http ) ;
// http::ResponseLog log( "first-", ".xml", &xrsp ) ;
std::string uri = feed_base + "?showfolders=true&showroot=true" ;
/* if ( !change_stamp.empty() )
{

View File

@ -0,0 +1,68 @@
/*
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 "State.hh"
#include "util/Crypt.hh"
#include "util/Log.hh"
#include "protocol/Json.hh"
namespace gr {
namespace fs = boost::filesystem ;
State::State( const std::string& filename ) :
m_db( filename )
{
}
void State::Sync( const fs::path& p )
{
for ( fs::directory_iterator i( p ) ; i != fs::directory_iterator() ; ++i )
{
Trace( "file found = %1%", i->path() ) ;
std::string json = m_db.Get( i->path().string() ) ;
if ( json.empty() )
{
Trace( "create new entry" ) ;
json = FileInfo(p).Str() ;
}
else
{
Json j = Json::Parse( json ) ;
if ( j["mtime"].Int() != fs::last_write_time(p) )
{
Trace( "file changed" ) ;
json = FileInfo(p).Str() ;
}
}
m_db.Set( i->path().string(), json ) ;
}
}
Json State::FileInfo( const boost::filesystem::path& p )
{
Json info ;
info.Add( "mtime", Json( fs::last_write_time(p) ) ) ;
return info ;
}
} // end of namespace

View File

@ -0,0 +1,44 @@
/*
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 "util/Gdbm.hh"
#include <boost/filesystem.hpp>
namespace gr {
class Json ;
class State
{
public :
explicit State( const std::string& filename ) ;
void Sync( const boost::filesystem::path& p ) ;
private :
static Json FileInfo( const boost::filesystem::path& p ) ;
private :
Gdbm m_db ;
} ;
} // end of namespace

View File

@ -48,6 +48,22 @@ Json::Json( const std::string& str ) :
assert( ::json_object_get_string( m_json ) == str ) ;
}
template <>
Json::Json( const int& l ) :
m_json( ::json_object_new_int( l ) )
{
if ( m_json == 0 )
throw Error() << expt::ErrMsg( "cannot create json int" ) ;
}
template <>
Json::Json( const long& l ) :
m_json( ::json_object_new_int( static_cast<int>(l) ) )
{
if ( m_json == 0 )
throw Error() << expt::ErrMsg( "cannot create json int" ) ;
}
Json Json::Parse( const std::string& str )
{
struct json_object *json = ::json_tokener_parse( str.c_str() ) ;

View File

@ -21,12 +21,19 @@
#include <iomanip>
#include <sstream>
#include <fstream>
// dependent libraries
#include <openssl/evp.h>
namespace gr { namespace crypt {
std::string MD5( const boost::filesystem::path& file )
{
std::ifstream ifile( file.string().c_str(), std::ios::binary | std::ios::in ) ;
return MD5( ifile.rdbuf() ) ;
}
std::string MD5( std::streambuf *file )
{
char buf[64 * 1024] ;

View File

@ -22,11 +22,14 @@
#include <string>
#include <iosfwd>
#include <boost/filesystem.hpp>
namespace gr {
namespace crypt
{
std::string MD5( std::streambuf *file ) ;
std::string MD5( const boost::filesystem::path& file ) ;
}
} // end of namespace gr

83
libgrive/src/util/Gdbm.cc Normal file
View File

@ -0,0 +1,83 @@
/*
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 "Gdbm.hh"
#include <boost/exception/all.hpp>
#include <gdbm.h>
#include <cassert>
namespace gr {
struct Gdbm::Impl
{
GDBM_FILE db ;
} ;
Gdbm::Gdbm( const std::string& filename ) :
m_impl( new Impl )
{
std::string str( filename ) ;
m_impl->db = ::gdbm_open( &str[0], 4096, GDBM_WRCREAT, 0600, &Gdbm::OnError ) ;
if ( m_impl->db == 0 )
{
char *msg = gdbm_strerror(gdbm_errno) ;
BOOST_THROW_EXCEPTION( Error() << expt::ErrMsg( msg ) ) ;
}
}
Gdbm::~Gdbm()
{
}
std::string Gdbm::Get( const std::string& key ) const
{
std::string ckey = key ;
datum dkey = { &ckey[0], ckey.size() } ;
datum val = gdbm_fetch( m_impl->db, dkey ) ;
std::string result ;
if ( val.dptr != 0 )
{
result.append( val.dptr, val.dsize ) ;
::free( val.dptr ) ;
}
return result ;
}
void Gdbm::Set( const std::string& key, const std::string& val )
{
assert( m_impl->db != 0 ) ;
std::string ckey = key, cval = val ;
datum dkey = { &ckey[0], ckey.size() } ;
datum dval = { &cval[0], cval.size() } ;
gdbm_store( m_impl->db, dkey, dval, GDBM_REPLACE ) ;
}
void Gdbm::OnError( )
{
BOOST_THROW_EXCEPTION( Error() << expt::ErrMsg( gdbm_strerror(gdbm_errno) ) ) ;
}
} // end of namespace

49
libgrive/src/util/Gdbm.hh Normal file
View File

@ -0,0 +1,49 @@
/*
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 <memory>
#include <vector>
namespace gr {
class Gdbm
{
public :
struct Error : virtual Exception {} ;
public :
explicit Gdbm( const std::string& filename ) ;
~Gdbm() ;
std::string Get( const std::string& key ) const ;
void Set( const std::string& key, const std::string& val ) ;
private :
static void OnError( ) ;
private :
struct Impl ;
std::auto_ptr<Impl> m_impl ;
} ;
} // end of namespace

View File

@ -19,9 +19,13 @@
#include <cppunit/ui/text/TestRunner.h>
#include "util/DefaultLog.hh"
#include "drive/EntryTest.hh"
#include "drive/StateTest.hh"
#include "util/DateTimeTest.hh"
#include "util/FunctionTest.hh"
#include "util/GdbmTest.hh"
#include "util/PathTest.hh"
#include "util/SignalHandlerTest.hh"
#include "xml/NodeTest.hh"
@ -29,13 +33,19 @@
int main( int argc, char **argv )
{
using namespace grut ;
gr::DefaultLog nofile_log ;
nofile_log.Enable( gr::log::debug, true ) ;
gr::LogBase::Inst( &nofile_log ) ;
CppUnit::TextUi::TestRunner runner;
runner.addTest( EntryTest::suite( ) ) ;
runner.addTest( StateTest::suite( ) ) ;
runner.addTest( DateTimeTest::suite( ) ) ;
runner.addTest( FunctionTest::suite( ) ) ;
runner.addTest( PathTest::suite( ) ) ;
runner.addTest( SignalHandlerTest::suite( ) ) ;
runner.addTest( GdbmTest::suite( ) ) ;
runner.addTest( NodeTest::suite( ) ) ;
runner.run();

View File

@ -0,0 +1,42 @@
/*
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 "StateTest.hh"
#include "Assert.hh"
#include "drive/State.hh"
#include <iostream>
namespace grut {
using namespace gr ;
StateTest::StateTest( )
{
}
void StateTest::TestSync( )
{
State s( ".grive_state" ) ;
s.Sync( TEST_DATA ) ;
}
} // end of namespace grut

View File

@ -0,0 +1,41 @@
/*
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 StateTest : public CppUnit::TestFixture
{
public :
StateTest( ) ;
// declare suit function
CPPUNIT_TEST_SUITE( StateTest ) ;
CPPUNIT_TEST( TestSync ) ;
CPPUNIT_TEST_SUITE_END();
private :
void TestSync( ) ;
} ;
} // end of namespace

View File

@ -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.
*/
#include "GdbmTest.hh"
#include "Assert.hh"
#include "util/Gdbm.hh"
#include <unistd.h>
namespace grut {
using namespace gr ;
GdbmTest::GdbmTest( )
{
}
void GdbmTest::Test( )
{
{
Gdbm db( "test.db" ) ;
db.Set( "key", "value" ) ;
GRUT_ASSERT_EQUAL( db.Get("key"), "value" ) ;
}
// re-open and verify
Gdbm db( "test.db" ) ;
GRUT_ASSERT_EQUAL( db.Get("key"), "value" ) ;
}
void GdbmTest::tearDown()
{
unlink( "test.db" ) ;
}
} // end of namespace grut

View File

@ -0,0 +1,44 @@
/*
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 GdbmTest : public CppUnit::TestFixture
{
public :
GdbmTest( ) ;
// declare suit function
CPPUNIT_TEST_SUITE( GdbmTest ) ;
CPPUNIT_TEST( Test ) ;
CPPUNIT_TEST_SUITE_END();
public :
void tearDown() ;
private :
void Test( ) ;
} ;
} // end of namespace