From 23b56fad3873b03a4b1f04e299104a7b52c62391 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 13 Feb 2016 20:56:05 +0100 Subject: Add filesystem exception handling where required --- src/tracking/change_tracker.cc | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/tracking/change_tracker.cc b/src/tracking/change_tracker.cc index 74e656f..0feae34 100644 --- a/src/tracking/change_tracker.cc +++ b/src/tracking/change_tracker.cc @@ -1,6 +1,6 @@ #include "change_tracker.h" -#include +#include #include namespace { @@ -68,6 +68,11 @@ ChangeTracker::~ChangeTracker() { } } +// Both `is_tracked` and `create_child` may throw filesystem exceptions +// if the given path doesn't exist. We are able to disregard this as +// both functions are only and should only be called when existance is +// guaranteed. If this is not the case we want the library to fail visibly. + bool ChangeTracker::is_tracked(const std::string& file_path) const { return this->children_.find( boost::filesystem::canonical(file_path).string() @@ -102,13 +107,20 @@ bool ChangeTracker::track(const std::string& file_path) { auto result = this->create_child(file_path); if ( std::get(result) ) { - boost::filesystem::ifstream file( - std::get(*result.first) - ); - - if ( file.is_open() ) { - *std::get(*result.first) << file.rdbuf(); - std::get(*result.first)->sync(); + try { + boost::filesystem::ifstream file( + std::get(*result.first) + ); + + if ( file.is_open() ) { + *std::get(*result.first) << file.rdbuf(); + std::get(*result.first)->sync(); + } + } catch ( boost::filesystem::filesystem_error& ) { + // we catch this exception in case the parent process is + // performing system calls that are allowed to fail - e.g. + // in this instance writing to files that we are not allowed + // to read. } return true; -- cgit v1.2.3