Skip to content

Commit

Permalink
Merge pull request #76 from erikzenker/feature/erikzenker/std-filesystem
Browse files Browse the repository at this point in the history
feat: Update to c++17 (std::filesystem)
  • Loading branch information
erikzenker committed Dec 26, 2020
2 parents 5d0d203 + bccfcbe commit f60b1ae
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 73 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ on:
jobs:
build:

runs-on: ubuntu-16.04
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- name: Install Dependencies
run: sudo apt install cmake libboost-filesystem-dev libboost-test-dev gcc
run: sudo apt install cmake libboost-all-dev libboost-test-dev gcc
- name: Print System Information
run: |
cmake --version
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ on:
jobs:
build:

runs-on: ubuntu-16.04
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- name: Install Dependencies
run: sudo apt install cmake libboost-filesystem-dev libboost-test-dev gcc
run: sudo apt install cmake libboost-all-dev gcc
- name: Print System Information
run: |
cmake --version
Expand All @@ -25,7 +25,7 @@ jobs:
export CXX=g++
export CC=gcc
mkdir build && cd build
cmake ..
cmake .. -DBUILD_TEST=OFF
cmake --build . --target inotify_example
- name: Run example
run: |
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ test/unit/testDirectory/
install_manifest.txt
.idea/
**/build/
cmake-build-debug/
cmake-build-debug/
.vscode
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ the implementation of a simple filesystem event watcher for the commandline.
```c++
#include <inotify-cpp/NotifierBuilder.h>

#include <boost/filesystem.hpp>
#include <filesystem>

#include <iostream>
#include <thread>
Expand All @@ -27,7 +27,7 @@ int main(int argc, char** argv)
}

// Parse the directory to watch
boost::filesystem::path path(argv[1]);
std::filesystem::path path(argv[1]);

// Set the event handler which will be used to process particular events
auto handleNotification = [&](Notification notification) {
Expand Down Expand Up @@ -98,7 +98,7 @@ cmake --build . --target inotify_example
## Dependencies ##
+ lib
+ boost 1.54.0
+ c++11
+ c++17
+ linux 2.6.13
+ build
+ cmake 3.8
Expand Down
6 changes: 2 additions & 4 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ endif()
###############################################################################
find_package(
Boost 1.54.0
COMPONENTS unit_test_framework system filesystem
COMPONENTS unit_test_framework system
REQUIRED
)

Expand All @@ -26,10 +26,8 @@ find_package(Threads)
# Target
###############################################################################
add_executable(inotify_example main.cpp)
target_compile_features(inotify_example PRIVATE cxx_std_17)
target_link_libraries(inotify_example
PRIVATE
inotify-cpp::inotify-cpp
Boost::unit_test_framework
Boost::system
Boost::filesystem
${CMAKE_THREAD_LIBS_INIT})
4 changes: 2 additions & 2 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <inotify-cpp/NotifierBuilder.h>

#include <boost/filesystem.hpp>
#include <filesystem>

#include <iostream>
#include <thread>
Expand All @@ -16,7 +16,7 @@ int main(int argc, char** argv)
}

// Parse the directory to watch
boost::filesystem::path path(argv[1]);
std::filesystem::path path(argv[1]);

// Set the event handler which will be used to process particular events
auto handleNotification = [&](Notification notification) {
Expand Down
4 changes: 1 addition & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ include(GNUInstallDirs)
# dependencies
find_package(
Boost 1.54.0
COMPONENTS system filesystem
REQUIRED
)

Expand All @@ -40,8 +39,7 @@ macro(configure_target target)
target_include_directories(${target} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(${target}
INTERFACE Boost::system Boost::filesystem)
target_compile_features(${target} PRIVATE cxx_std_17)
endmacro(configure_target target)

# library definition
Expand Down
2 changes: 1 addition & 1 deletion src/FileSystemEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace inotify {
FileSystemEvent::FileSystemEvent(
const int wd,
uint32_t mask,
const boost::filesystem::path& path,
const std::filesystem::path& path,
const std::chrono::steady_clock::time_point& eventTime)
: wd(wd)
, mask(mask)
Expand Down
16 changes: 9 additions & 7 deletions src/Inotify.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

#include <inotify-cpp/Inotify.h>

#include <iostream>
#include <optional>
#include <string>
#include <vector>
#include <iostream>

#include <sys/epoll.h>
#include <fcntl.h>
#include <unistd.h>

namespace fs = boost::filesystem;
namespace fs = std::filesystem;

namespace inotify {

Expand Down Expand Up @@ -97,14 +98,15 @@ Inotify::~Inotify()
*/
void Inotify::watchDirectoryRecursively(fs::path path)
{
std::vector<boost::filesystem::path> paths;
std::vector<std::filesystem::path> paths;

if (fs::exists(path)) {
paths.push_back(path);

if (fs::is_directory(path)) {
boost::system::error_code ec;
fs::recursive_directory_iterator it(path, fs::symlink_option::recurse, ec);
std::error_code ec;
fs::recursive_directory_iterator it(
path, fs::directory_options::follow_directory_symlink, ec);
fs::recursive_directory_iterator end;

for (; it != end; it.increment(ec)) {
Expand Down Expand Up @@ -234,7 +236,7 @@ void Inotify::setEventTimeout(
* @return A new FileSystemEvent
*
*/
boost::optional<FileSystemEvent> Inotify::getNextEvent()
std::optional<FileSystemEvent> Inotify::getNextEvent()
{
std::vector<FileSystemEvent> newEvents;

Expand All @@ -245,7 +247,7 @@ boost::optional<FileSystemEvent> Inotify::getNextEvent()
}

if (mStopped) {
return boost::none;
return std::nullopt;
}

auto event = mEventQueue.front();
Expand Down
2 changes: 1 addition & 1 deletion src/Notification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace inotify {

Notification::Notification(
const Event& event,
const boost::filesystem::path& path,
const std::filesystem::path& path,
std::chrono::steady_clock::time_point time)
: event(event)
, path(path)
Expand Down
10 changes: 5 additions & 5 deletions src/NotifierBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ NotifierBuilder BuildNotifier()
return {};
}

auto NotifierBuilder::watchPathRecursively(boost::filesystem::path path) -> NotifierBuilder&
auto NotifierBuilder::watchPathRecursively(std::filesystem::path path) -> NotifierBuilder&
{
mInotify->watchDirectoryRecursively(path);
return *this;
}

auto NotifierBuilder::watchFile(boost::filesystem::path file) -> NotifierBuilder&
auto NotifierBuilder::watchFile(std::filesystem::path file) -> NotifierBuilder&
{
mInotify->watchFile(file);
return *this;
}

auto NotifierBuilder::unwatchFile(boost::filesystem::path file) -> NotifierBuilder&
auto NotifierBuilder::unwatchFile(std::filesystem::path file) -> NotifierBuilder&
{
mInotify->unwatchFile(file);
return *this;
}

auto NotifierBuilder::ignoreFileOnce(boost::filesystem::path file) -> NotifierBuilder&
auto NotifierBuilder::ignoreFileOnce(std::filesystem::path file) -> NotifierBuilder&
{
mInotify->ignoreFileOnce(file.string());
return *this;
}

auto NotifierBuilder::ignoreFile(boost::filesystem::path file) -> NotifierBuilder&
auto NotifierBuilder::ignoreFile(std::filesystem::path file) -> NotifierBuilder&
{
mInotify->ignoreFile(file.string());
return *this;
Expand Down
6 changes: 3 additions & 3 deletions src/include/inotify-cpp/FileSystemEvent.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <boost/filesystem.hpp>
#include <filesystem>

#include <chrono>
#include <string>
Expand All @@ -10,15 +10,15 @@ class FileSystemEvent {
FileSystemEvent(
int wd,
uint32_t mask,
const boost::filesystem::path& path,
const std::filesystem::path& path,
const std::chrono::steady_clock::time_point& eventTime);

~FileSystemEvent();

public:
int wd;
uint32_t mask;
boost::filesystem::path path;
std::filesystem::path path;
std::chrono::steady_clock::time_point eventTime;
};
}
28 changes: 14 additions & 14 deletions src/include/inotify-cpp/Inotify.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
**/
#pragma once
#include <assert.h>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
#include <atomic>
#include <boost/bimap.hpp>
#include <chrono>
#include <errno.h>
#include <exception>
#include <filesystem>
#include <map>
#include <memory>
#include <optional>
#include <queue>
#include <sstream>
#include <string>
#include <sys/inotify.h>
#include <sys/epoll.h>
#include <sys/inotify.h>
#include <thread>
#include <time.h>
#include <vector>
#include <chrono>
#include <thread>
#include <atomic>

#include <inotify-cpp/FileSystemEvent.h>

Expand Down Expand Up @@ -79,20 +79,20 @@ class Inotify {
public:
Inotify();
~Inotify();
void watchDirectoryRecursively(boost::filesystem::path path);
void watchFile(boost::filesystem::path file);
void unwatchFile(boost::filesystem::path file);
void ignoreFileOnce(boost::filesystem::path file);
void ignoreFile(boost::filesystem::path file);
void watchDirectoryRecursively(std::filesystem::path path);
void watchFile(std::filesystem::path file);
void unwatchFile(std::filesystem::path file);
void ignoreFileOnce(std::filesystem::path file);
void ignoreFile(std::filesystem::path file);
void setEventMask(uint32_t eventMask);
uint32_t getEventMask();
void setEventTimeout(std::chrono::milliseconds eventTimeout, std::function<void(FileSystemEvent)> onEventTimeout);
boost::optional<FileSystemEvent> getNextEvent();
std::optional<FileSystemEvent> getNextEvent();
void stop();
bool hasStopped();

private:
boost::filesystem::path wdToPath(int wd);
std::filesystem::path wdToPath(int wd);
bool isIgnored(std::string file);
bool isOnTimeout(const std::chrono::steady_clock::time_point &eventTime);
void removeWatch(int wd);
Expand All @@ -110,7 +110,7 @@ class Inotify {
std::vector<std::string> mIgnoredDirectories;
std::vector<std::string> mOnceIgnoredDirectories;
std::queue<FileSystemEvent> mEventQueue;
boost::bimap<int, boost::filesystem::path> mDirectorieMap;
boost::bimap<int, std::filesystem::path> mDirectorieMap;
int mInotifyFd;
std::atomic<bool> mStopped;
int mEpollFd;
Expand Down
6 changes: 3 additions & 3 deletions src/include/inotify-cpp/Notification.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <inotify-cpp/Event.h>

#include <boost/filesystem.hpp>
#include <filesystem>

#include <chrono>

Expand All @@ -12,12 +12,12 @@ class Notification {
public:
Notification(
const Event& event,
const boost::filesystem::path& path,
const std::filesystem::path& path,
std::chrono::steady_clock::time_point time);

public:
const Event event;
const boost::filesystem::path path;
const std::filesystem::path path;
const std::chrono::steady_clock::time_point time;
};
}
12 changes: 6 additions & 6 deletions src/include/inotify-cpp/NotifierBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <inotify-cpp/Inotify.h>
#include <inotify-cpp/Notification.h>

#include <boost/filesystem.hpp>
#include <filesystem>

#include <memory>
#include <string>
Expand All @@ -19,11 +19,11 @@ class NotifierBuilder {
auto run() -> void;
auto runOnce() -> void;
auto stop() -> void;
auto watchPathRecursively(boost::filesystem::path path) -> NotifierBuilder&;
auto watchFile(boost::filesystem::path file) -> NotifierBuilder&;
auto unwatchFile(boost::filesystem::path file) -> NotifierBuilder&;
auto ignoreFileOnce(boost::filesystem::path file) -> NotifierBuilder&;
auto ignoreFile(boost::filesystem::path file) -> NotifierBuilder&;
auto watchPathRecursively(std::filesystem::path path) -> NotifierBuilder&;
auto watchFile(std::filesystem::path file) -> NotifierBuilder&;
auto unwatchFile(std::filesystem::path file) -> NotifierBuilder&;
auto ignoreFileOnce(std::filesystem::path file) -> NotifierBuilder&;
auto ignoreFile(std::filesystem::path file) -> NotifierBuilder&;
auto onEvent(Event event, EventObserver) -> NotifierBuilder&;
auto onEvents(std::vector<Event> event, EventObserver) -> NotifierBuilder&;
auto onUnexpectedEvent(EventObserver) -> NotifierBuilder&;
Expand Down
Loading

0 comments on commit f60b1ae

Please sign in to comment.