mirror of https://github.com/vitalif/grive2
commit
d4a8d9fb65
|
@ -1,87 +1,4 @@
|
|||
project(grive)
|
||||
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
|
||||
|
||||
find_package(OpenSSL REQUIRED)
|
||||
find_package(JSONC REQUIRED)
|
||||
find_package(CURL REQUIRED)
|
||||
find_package(CppUnit)
|
||||
|
||||
IF ( CPPUNIT_FOUND )
|
||||
set( OPT_INCS ${CPPUNIT_INCLUDE_DIR} )
|
||||
ENDIF ( CPPUNIT_FOUND )
|
||||
|
||||
include_directories(
|
||||
${grive_SOURCE_DIR}/src
|
||||
${OPT_INCS}
|
||||
)
|
||||
|
||||
file(GLOB DRIVE_HEADERS
|
||||
${grive_SOURCE_DIR}/src/drive/*.hh
|
||||
)
|
||||
|
||||
file (GLOB PROTOCOL_HEADERS
|
||||
${grive_SOURCE_DIR}/src/protocol/*.hh
|
||||
)
|
||||
|
||||
file (GLOB UTIL_HEADERS
|
||||
${grive_SOURCE_DIR}/src/util/*.hh
|
||||
)
|
||||
|
||||
add_library( fgrive SHARED
|
||||
src/drive/Collection.cc
|
||||
src/drive/Drive.cc
|
||||
src/drive/File.cc
|
||||
src/protocol/Download.cc
|
||||
src/protocol/HTTP.cc
|
||||
src/protocol/Json.cc
|
||||
src/protocol/OAuth2.cc
|
||||
src/util/Crypt.cc
|
||||
src/util/DateTime.cc
|
||||
src/util/OS.cc
|
||||
src/util/Path.cc
|
||||
)
|
||||
|
||||
add_executable( grive
|
||||
src/main.cc
|
||||
)
|
||||
|
||||
target_link_libraries( fgrive
|
||||
${CURL_LIBRARIES}
|
||||
${JSONC_LIBRARY}
|
||||
${OPENSSL_LIBRARIES}
|
||||
)
|
||||
|
||||
target_link_libraries( grive
|
||||
fgrive
|
||||
)
|
||||
|
||||
set_target_properties(fgrive PROPERTIES
|
||||
SOVERSION 0 VERSION 0.0.1
|
||||
)
|
||||
|
||||
IF ( CPPUNIT_FOUND )
|
||||
add_executable( unittest
|
||||
test/UnitTest.cc
|
||||
test/util/DateTimeTest.cc
|
||||
test/util/FunctionTest.cc
|
||||
test/util/PathTest.cc
|
||||
)
|
||||
|
||||
target_link_libraries( unittest
|
||||
fgrive
|
||||
${CPPUNIT_LIBRARY}
|
||||
)
|
||||
else ( CPPUNIT_FOUND )
|
||||
message( STATUS "skip building unittest" )
|
||||
endif ( CPPUNIT_FOUND )
|
||||
|
||||
|
||||
## Install targets
|
||||
install(TARGETS fgrive LIBRARY DESTINATION lib)
|
||||
install(TARGETS grive RUNTIME DESTINATION bin)
|
||||
install(FILES ${DRIVE_HEADERS} DESTINATION include/grive/drive)
|
||||
install(FILES ${PROTOCOL_HEADERS} DESTINATION include/grive/protocol)
|
||||
install(FILES ${UTIL_HEADERS} DESTINATION include/grive/util)
|
||||
add_subdirectory( libgrive )
|
||||
add_subdirectory( grive )
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
project( grive )
|
||||
|
||||
include_directories(
|
||||
${grive_SOURCE_DIR}/../libgrive/src
|
||||
${OPT_INCS}
|
||||
)
|
||||
|
||||
add_executable( grive_executable
|
||||
src/main.cc
|
||||
)
|
||||
|
||||
target_link_libraries( grive_executable
|
||||
grive
|
||||
)
|
||||
|
||||
set_target_properties( grive_executable
|
||||
PROPERTIES OUTPUT_NAME grive
|
||||
)
|
||||
|
||||
install(TARGETS grive_executable RUNTIME DESTINATION bin)
|
|
@ -0,0 +1,72 @@
|
|||
project(libgrive)
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
|
||||
|
||||
find_package(OpenSSL REQUIRED)
|
||||
find_package(JSONC REQUIRED)
|
||||
find_package(CURL REQUIRED)
|
||||
find_package(CppUnit)
|
||||
|
||||
IF ( CPPUNIT_FOUND )
|
||||
set( OPT_INCS ${CPPUNIT_INCLUDE_DIR} )
|
||||
ENDIF ( CPPUNIT_FOUND )
|
||||
|
||||
include_directories(
|
||||
${libgrive_SOURCE_DIR}/src
|
||||
${OPT_INCS}
|
||||
)
|
||||
|
||||
file(GLOB DRIVE_HEADERS
|
||||
${libgrive_SOURCE_DIR}/src/drive/*.hh
|
||||
)
|
||||
|
||||
file (GLOB PROTOCOL_HEADERS
|
||||
${libgrive_SOURCE_DIR}/src/protocol/*.hh
|
||||
)
|
||||
|
||||
file (GLOB UTIL_HEADERS
|
||||
${libgrive_SOURCE_DIR}/src/util/*.hh
|
||||
)
|
||||
|
||||
file (GLOB LIBGRIVE_SRC
|
||||
src/drive/*.cc
|
||||
src/protocol/*.cc
|
||||
src/util/*.cc
|
||||
)
|
||||
|
||||
add_library( grive SHARED ${LIBGRIVE_SRC} )
|
||||
|
||||
target_link_libraries( grive
|
||||
${CURL_LIBRARIES}
|
||||
${JSONC_LIBRARY}
|
||||
${OPENSSL_LIBRARIES}
|
||||
)
|
||||
|
||||
set_target_properties(grive PROPERTIES
|
||||
SOVERSION 0 VERSION 0.0.3
|
||||
)
|
||||
|
||||
install(TARGETS grive LIBRARY DESTINATION lib)
|
||||
install(FILES ${DRIVE_HEADERS} DESTINATION include/grive/drive)
|
||||
install(FILES ${PROTOCOL_HEADERS} DESTINATION include/grive/protocol)
|
||||
|
||||
IF ( CPPUNIT_FOUND )
|
||||
message("-- Building unitary tests along with the library and the binary")
|
||||
set( OPT_INCS ${CPPUNIT_INCLUDE_DIR} )
|
||||
|
||||
# list of test source files here
|
||||
file(GLOB TEST_SRC
|
||||
test/util/*.cc
|
||||
)
|
||||
|
||||
add_executable( unittest
|
||||
test/UnitTest.cc
|
||||
${TEST_SRC}
|
||||
)
|
||||
|
||||
target_link_libraries( unittest
|
||||
grive
|
||||
${CPPUNIT_LIBRARY}
|
||||
)
|
||||
|
||||
ENDIF ( CPPUNIT_FOUND )
|
|
@ -0,0 +1,33 @@
|
|||
# lib subproject
|
||||
|
||||
file(GLOB DRIVE_SOURCES
|
||||
drive/*.cc
|
||||
)
|
||||
|
||||
file(GLOB PROTOCOL_SOURCES
|
||||
protocol/*.cc
|
||||
)
|
||||
|
||||
file(GLOB UTIL_SOURCES
|
||||
util/*.cc
|
||||
)
|
||||
|
||||
file(GLOB DRIVE_HEADERS
|
||||
drive/*.hh
|
||||
)
|
||||
|
||||
file (GLOB PROTOCOL_HEADERS
|
||||
protocol/*.hh
|
||||
)
|
||||
|
||||
file (GLOB UTIL_HEADERS
|
||||
util/*.hh
|
||||
)
|
||||
|
||||
add_library( grive SHARED
|
||||
${DRIVE_SOURCES}
|
||||
${PROTOCOL_SOURCES}
|
||||
${UTIL_SOURCES}
|
||||
)
|
||||
|
||||
nstall(FILES ${UTIL_HEADERS} DESTINATION include/grive/util)
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
|
||||
#include "Download.hh"
|
||||
#include "util/SignalHandler.hh"
|
||||
|
||||
#include <openssl/evp.h>
|
||||
|
||||
|
@ -25,6 +26,8 @@
|
|||
#include <new>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
namespace gr {
|
||||
|
||||
Download::Download( const std::string& filename ) :
|
||||
|
@ -57,6 +60,9 @@ Download::~Download( )
|
|||
|
||||
std::string Download::Finish() const
|
||||
{
|
||||
// Unregister the signal
|
||||
SignalHandler::GetInstance().UnregisterSignal( SIGINT ) ;
|
||||
|
||||
std::string result ;
|
||||
|
||||
// get the checksum and return it ;
|
|
@ -20,6 +20,7 @@
|
|||
#include "HTTP.hh"
|
||||
|
||||
#include "Download.hh"
|
||||
#include "util/SignalHandler.hh"
|
||||
|
||||
// dependent libraries
|
||||
#include <curl/curl.h>
|
||||
|
@ -31,6 +32,8 @@
|
|||
#include <sstream>
|
||||
#include <streambuf>
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace gr::http ;
|
||||
|
@ -109,6 +112,14 @@ void DoCurl( CURL *curl )
|
|||
}
|
||||
}
|
||||
|
||||
// Callback for SIGINT
|
||||
void CallbackInt( int )
|
||||
{
|
||||
// TODO: instead of just disabling the signal, clean up the environment
|
||||
// and exit gracefully
|
||||
std::cout << " Signal disabled while downloading file..\n";
|
||||
}
|
||||
|
||||
} // end of local namespace
|
||||
|
||||
namespace gr { namespace http {
|
||||
|
@ -139,6 +150,9 @@ void GetFile(
|
|||
const std::string& filename,
|
||||
const Headers& hdr )
|
||||
{
|
||||
// Register the callback
|
||||
SignalHandler::GetInstance().RegisterSignal( SIGINT, &CallbackInt ) ;
|
||||
|
||||
Download dl( filename, Download::NoChecksum() ) ;
|
||||
|
||||
CURL *curl = InitCurl( url, 0, hdr ) ;
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
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 "SignalHandler.hh"
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <signal.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace gr {
|
||||
|
||||
SignalError::SignalError( const std::string& message ) :
|
||||
std::runtime_error( message )
|
||||
{
|
||||
}
|
||||
|
||||
SignalError::~SignalError() throw ()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SignalHandler::SignalHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SignalHandler::SignalHandler( const SignalHandler& right )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SignalHandler& SignalHandler::operator ==( const SignalHandler& right )
|
||||
{
|
||||
return (*this);
|
||||
}
|
||||
|
||||
SignalHandler::~SignalHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SignalHandler& SignalHandler::GetInstance()
|
||||
{
|
||||
static SignalHandler _instance;
|
||||
return _instance;
|
||||
}
|
||||
|
||||
void SignalHandler::UnregisterSignal( unsigned int signumber )
|
||||
{
|
||||
m_signals[signumber] = 0 ;
|
||||
|
||||
// Restore the old signal
|
||||
signal( ( int ) signumber, m_signalsOld[signumber] );
|
||||
}
|
||||
|
||||
void SignalHandler::RegisterSignal( unsigned int signumber, Callback callback )
|
||||
{
|
||||
signals_t::const_iterator anIterator ;
|
||||
for (anIterator = m_signals.begin(); anIterator != m_signals.end(); ++anIterator)
|
||||
{
|
||||
if (anIterator->first == signumber)
|
||||
{
|
||||
if (anIterator->second != 0)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Signal " << signumber << " already has a callback!";
|
||||
throw SignalError( oss.str() ); ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_signals[signumber] = callback ;
|
||||
|
||||
if ( ( m_signalsOld[signumber] = signal( ( int ) signumber, m_signals[signumber] ) ) == SIG_ERR ) {
|
||||
throw SignalError( " Error while registering the signal! " ) ;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
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 <stdexcept>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
namespace gr {
|
||||
|
||||
class SignalError : public std::runtime_error
|
||||
{
|
||||
public :
|
||||
SignalError( const std::string& message ) ;
|
||||
virtual ~SignalError() throw () ;
|
||||
};
|
||||
|
||||
class SignalFunctor
|
||||
{
|
||||
public :
|
||||
SignalFunctor() ;
|
||||
virtual ~SignalFunctor() ;
|
||||
static void Callback( int signumber ) ;
|
||||
};
|
||||
|
||||
class SignalHandler
|
||||
{
|
||||
typedef void (*Callback)(int);
|
||||
typedef std::map<unsigned int, Callback> signals_t ;
|
||||
|
||||
public :
|
||||
virtual ~SignalHandler() ;
|
||||
void RegisterSignal ( unsigned int signumber, Callback callback ) ;
|
||||
void UnregisterSignal( unsigned int signumber );
|
||||
static SignalHandler& GetInstance() ;
|
||||
private :
|
||||
SignalHandler() ;
|
||||
SignalHandler( const SignalHandler& right ) ;
|
||||
SignalHandler& operator==( const SignalHandler& right ) ;
|
||||
|
||||
signals_t m_signals;
|
||||
signals_t m_signalsOld;
|
||||
};
|
||||
|
||||
}
|
|
@ -22,6 +22,7 @@
|
|||
#include "util/DateTimeTest.hh"
|
||||
#include "util/FunctionTest.hh"
|
||||
#include "util/PathTest.hh"
|
||||
#include "util/SignalHandlerTest.hh"
|
||||
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
|
@ -31,6 +32,7 @@ int main( int argc, char **argv )
|
|||
runner.addTest( DateTimeTest::suite( ) ) ;
|
||||
runner.addTest( FunctionTest::suite( ) ) ;
|
||||
runner.addTest( PathTest::suite( ) ) ;
|
||||
runner.addTest( SignalHandlerTest::suite( ) ) ;
|
||||
runner.run();
|
||||
|
||||
return 0 ;
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
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 "SignalHandlerTest.hh"
|
||||
|
||||
#include "util/SignalHandler.hh"
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
void test_callback( int )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
namespace grut {
|
||||
|
||||
using namespace gr ;
|
||||
|
||||
SignalHandlerTest::SignalHandlerTest( )
|
||||
{
|
||||
}
|
||||
|
||||
void SignalHandlerTest::TestMultipleSignals( )
|
||||
{
|
||||
SignalHandler::GetInstance().RegisterSignal( SIGINT, &test_callback );
|
||||
CPPUNIT_ASSERT_THROW(
|
||||
SignalHandler::GetInstance().RegisterSignal( SIGINT, &test_callback ),
|
||||
SignalError);
|
||||
|
||||
SignalHandler::GetInstance().UnregisterSignal( SIGINT );
|
||||
CPPUNIT_ASSERT_NO_THROW(
|
||||
SignalHandler::GetInstance().RegisterSignal( SIGINT, &test_callback ));
|
||||
}
|
||||
|
||||
}
|
|
@ -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 SignalHandlerTest : public CppUnit::TestFixture
|
||||
{
|
||||
public :
|
||||
SignalHandlerTest( ) ;
|
||||
|
||||
// declare suit function
|
||||
CPPUNIT_TEST_SUITE( SignalHandlerTest ) ;
|
||||
CPPUNIT_TEST( TestMultipleSignals ) ;
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
private :
|
||||
void TestMultipleSignals( ) ;
|
||||
} ;
|
||||
|
||||
} // end of namespace
|
Loading…
Reference in New Issue