diff --git a/CMakeLists.txt b/CMakeLists.txt index 32978da..7310308 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/grive/CMakeLists.txt b/grive/CMakeLists.txt new file mode 100644 index 0000000..48243b8 --- /dev/null +++ b/grive/CMakeLists.txt @@ -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) diff --git a/src/main.cc b/grive/src/main.cc similarity index 100% rename from src/main.cc rename to grive/src/main.cc diff --git a/libgrive/CMakeLists.txt b/libgrive/CMakeLists.txt new file mode 100644 index 0000000..6f4be84 --- /dev/null +++ b/libgrive/CMakeLists.txt @@ -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 ) diff --git a/src/drive/Collection.cc b/libgrive/src/drive/Collection.cc similarity index 100% rename from src/drive/Collection.cc rename to libgrive/src/drive/Collection.cc diff --git a/src/drive/Collection.hh b/libgrive/src/drive/Collection.hh similarity index 100% rename from src/drive/Collection.hh rename to libgrive/src/drive/Collection.hh diff --git a/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc similarity index 100% rename from src/drive/Drive.cc rename to libgrive/src/drive/Drive.cc diff --git a/src/drive/Drive.hh b/libgrive/src/drive/Drive.hh similarity index 100% rename from src/drive/Drive.hh rename to libgrive/src/drive/Drive.hh diff --git a/src/drive/File.cc b/libgrive/src/drive/File.cc similarity index 100% rename from src/drive/File.cc rename to libgrive/src/drive/File.cc diff --git a/src/drive/File.hh b/libgrive/src/drive/File.hh similarity index 100% rename from src/drive/File.hh rename to libgrive/src/drive/File.hh diff --git a/libgrive/src/lib/CMakeLists.txt b/libgrive/src/lib/CMakeLists.txt new file mode 100644 index 0000000..6e444c2 --- /dev/null +++ b/libgrive/src/lib/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/src/protocol/Download.cc b/libgrive/src/protocol/Download.cc similarity index 94% rename from src/protocol/Download.cc rename to libgrive/src/protocol/Download.cc index 1959dd3..6313e74 100644 --- a/src/protocol/Download.cc +++ b/libgrive/src/protocol/Download.cc @@ -18,6 +18,7 @@ */ #include "Download.hh" +#include "util/SignalHandler.hh" #include @@ -25,6 +26,8 @@ #include #include +#include + 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 ; diff --git a/src/protocol/Download.hh b/libgrive/src/protocol/Download.hh similarity index 100% rename from src/protocol/Download.hh rename to libgrive/src/protocol/Download.hh diff --git a/src/protocol/HTTP.cc b/libgrive/src/protocol/HTTP.cc similarity index 96% rename from src/protocol/HTTP.cc rename to libgrive/src/protocol/HTTP.cc index be3ea7d..6523448 100644 --- a/src/protocol/HTTP.cc +++ b/libgrive/src/protocol/HTTP.cc @@ -20,6 +20,7 @@ #include "HTTP.hh" #include "Download.hh" +#include "util/SignalHandler.hh" // dependent libraries #include @@ -31,6 +32,8 @@ #include #include +#include + 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 ) ; diff --git a/src/protocol/HTTP.hh b/libgrive/src/protocol/HTTP.hh similarity index 100% rename from src/protocol/HTTP.hh rename to libgrive/src/protocol/HTTP.hh diff --git a/src/protocol/Json.cc b/libgrive/src/protocol/Json.cc similarity index 100% rename from src/protocol/Json.cc rename to libgrive/src/protocol/Json.cc diff --git a/src/protocol/Json.hh b/libgrive/src/protocol/Json.hh similarity index 100% rename from src/protocol/Json.hh rename to libgrive/src/protocol/Json.hh diff --git a/src/protocol/OAuth2.cc b/libgrive/src/protocol/OAuth2.cc similarity index 100% rename from src/protocol/OAuth2.cc rename to libgrive/src/protocol/OAuth2.cc diff --git a/src/protocol/OAuth2.hh b/libgrive/src/protocol/OAuth2.hh similarity index 100% rename from src/protocol/OAuth2.hh rename to libgrive/src/protocol/OAuth2.hh diff --git a/src/util/Crypt.cc b/libgrive/src/util/Crypt.cc similarity index 100% rename from src/util/Crypt.cc rename to libgrive/src/util/Crypt.cc diff --git a/src/util/Crypt.hh b/libgrive/src/util/Crypt.hh similarity index 100% rename from src/util/Crypt.hh rename to libgrive/src/util/Crypt.hh diff --git a/src/util/DateTime.cc b/libgrive/src/util/DateTime.cc similarity index 100% rename from src/util/DateTime.cc rename to libgrive/src/util/DateTime.cc diff --git a/src/util/DateTime.hh b/libgrive/src/util/DateTime.hh similarity index 100% rename from src/util/DateTime.hh rename to libgrive/src/util/DateTime.hh diff --git a/src/util/Function.hh b/libgrive/src/util/Function.hh similarity index 100% rename from src/util/Function.hh rename to libgrive/src/util/Function.hh diff --git a/src/util/OS.cc b/libgrive/src/util/OS.cc similarity index 100% rename from src/util/OS.cc rename to libgrive/src/util/OS.cc diff --git a/src/util/OS.hh b/libgrive/src/util/OS.hh similarity index 100% rename from src/util/OS.hh rename to libgrive/src/util/OS.hh diff --git a/src/util/Path.cc b/libgrive/src/util/Path.cc similarity index 100% rename from src/util/Path.cc rename to libgrive/src/util/Path.cc diff --git a/src/util/Path.hh b/libgrive/src/util/Path.hh similarity index 100% rename from src/util/Path.hh rename to libgrive/src/util/Path.hh diff --git a/libgrive/src/util/SignalHandler.cc b/libgrive/src/util/SignalHandler.cc new file mode 100644 index 0000000..43328ee --- /dev/null +++ b/libgrive/src/util/SignalHandler.cc @@ -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 +#include +#include +#include +#include + +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! " ) ; + } +} + +} + + + diff --git a/libgrive/src/util/SignalHandler.hh b/libgrive/src/util/SignalHandler.hh new file mode 100644 index 0000000..2ef0867 --- /dev/null +++ b/libgrive/src/util/SignalHandler.hh @@ -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 +#include +#include + +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 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; +}; + +} diff --git a/libgrive/src/xml/Node.hh b/libgrive/src/xml/Node.hh new file mode 100644 index 0000000..e69de29 diff --git a/test/UnitTest.cc b/libgrive/test/UnitTest.cc similarity index 93% rename from test/UnitTest.cc rename to libgrive/test/UnitTest.cc index 3343354..4670ced 100644 --- a/test/UnitTest.cc +++ b/libgrive/test/UnitTest.cc @@ -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 ; diff --git a/test/util/DateTimeTest.cc b/libgrive/test/util/DateTimeTest.cc similarity index 100% rename from test/util/DateTimeTest.cc rename to libgrive/test/util/DateTimeTest.cc diff --git a/test/util/DateTimeTest.hh b/libgrive/test/util/DateTimeTest.hh similarity index 100% rename from test/util/DateTimeTest.hh rename to libgrive/test/util/DateTimeTest.hh diff --git a/test/util/FunctionTest.cc b/libgrive/test/util/FunctionTest.cc similarity index 100% rename from test/util/FunctionTest.cc rename to libgrive/test/util/FunctionTest.cc diff --git a/test/util/FunctionTest.hh b/libgrive/test/util/FunctionTest.hh similarity index 100% rename from test/util/FunctionTest.hh rename to libgrive/test/util/FunctionTest.hh diff --git a/test/util/PathTest.cc b/libgrive/test/util/PathTest.cc similarity index 100% rename from test/util/PathTest.cc rename to libgrive/test/util/PathTest.cc diff --git a/test/util/PathTest.hh b/libgrive/test/util/PathTest.hh similarity index 100% rename from test/util/PathTest.hh rename to libgrive/test/util/PathTest.hh diff --git a/libgrive/test/util/SignalHandlerTest.cc b/libgrive/test/util/SignalHandlerTest.cc new file mode 100644 index 0000000..c7110d7 --- /dev/null +++ b/libgrive/test/util/SignalHandlerTest.cc @@ -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 + +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 )); +} + +} diff --git a/libgrive/test/util/SignalHandlerTest.hh b/libgrive/test/util/SignalHandlerTest.hh new file mode 100644 index 0000000..81d6232 --- /dev/null +++ b/libgrive/test/util/SignalHandlerTest.hh @@ -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 +#include + +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