From f65e9386253fc3d69f0e7651e5a7d9bb0cec131f Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Tue, 28 Aug 2018 17:08:59 +0200
Subject: [PATCH] fix paths

---
 .../event_monitor_producer/src/eventmon_config.cpp   |  3 ++-
 .../event_monitor_producer/src/eventmon_config.h     |  3 ++-
 .../src/folder_event_detector.cpp                    |  2 +-
 .../src/system_folder_watch_linux.cpp                | 12 ++++++++----
 .../src/system_folder_watch_linux.h                  |  3 ++-
 .../src/system_folder_watch_windows.cpp              |  3 ++-
 .../unittests/mock_eventmon_config.cpp               |  5 +++--
 .../unittests/mock_system_folder_watch.h             |  7 ++++---
 .../unittests/test_eventmon_config.cpp               |  6 ++++--
 .../unittests/test_folder_event_detector.cpp         | 12 +++++++-----
 10 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/producer/event_monitor_producer/src/eventmon_config.cpp b/producer/event_monitor_producer/src/eventmon_config.cpp
index e4ee00fa3..0cf4549f6 100644
--- a/producer/event_monitor_producer/src/eventmon_config.cpp
+++ b/producer/event_monitor_producer/src/eventmon_config.cpp
@@ -19,8 +19,9 @@ Error EventMonConfigFactory::ParseConfigFile(std::string file_name) {
     (err = parser.GetString("BeamtimeID", &config.beamtime_id)) ||
     (err = parser.GetString("Mode", &config.mode_str)) ||
     (err = parser.GetUInt64("NThreads", &config.nthreads)) ||
+    (err = parser.GetString("RootMonitoredFolder", &config.root_monitored_folder)) ||
     (err = parser.GetString("LogLevel", &config.log_level_str)) ||
-    (err = parser.GetArrayString("MonitoredFolders", &config.monitored_folders)) ||
+    (err = parser.GetArrayString("MonitoredSubFolders", &config.monitored_subfolders)) ||
     (err = parser.GetArrayString("IgnoreExtentions", &config.ignored_extentions));
 
     return err;
diff --git a/producer/event_monitor_producer/src/eventmon_config.h b/producer/event_monitor_producer/src/eventmon_config.h
index 9066df3c7..e6f0cc0ec 100644
--- a/producer/event_monitor_producer/src/eventmon_config.h
+++ b/producer/event_monitor_producer/src/eventmon_config.h
@@ -16,7 +16,8 @@ struct EventMonConfig {
     uint64_t nthreads = 1;
     std::string beamtime_id;
     RequestHandlerType mode = RequestHandlerType::kTcp;
-    std::vector<std::string> monitored_folders;
+    std::string root_monitored_folder;
+    std::vector<std::string> monitored_subfolders;
     std::vector<std::string> ignored_extentions;
   private:
     std::string log_level_str;
diff --git a/producer/event_monitor_producer/src/folder_event_detector.cpp b/producer/event_monitor_producer/src/folder_event_detector.cpp
index d9f650f4d..c744c613e 100644
--- a/producer/event_monitor_producer/src/folder_event_detector.cpp
+++ b/producer/event_monitor_producer/src/folder_event_detector.cpp
@@ -70,7 +70,7 @@ Error FolderEventDetector::StartMonitoring() {
         return nullptr;
     }
 
-    auto err = system_folder_watch__->StartFolderMonitor(config_->monitored_folders);
+    auto err = system_folder_watch__->StartFolderMonitor(config_->root_monitored_folder, config_->monitored_subfolders);
     if (err) {
         return err;
     }
diff --git a/producer/event_monitor_producer/src/system_folder_watch_linux.cpp b/producer/event_monitor_producer/src/system_folder_watch_linux.cpp
index 05df62c32..3d83d0e73 100644
--- a/producer/event_monitor_producer/src/system_folder_watch_linux.cpp
+++ b/producer/event_monitor_producer/src/system_folder_watch_linux.cpp
@@ -22,7 +22,9 @@ Error SystemFolderWatch::AddFolderToWatch(std::string folder, bool recursive) {
     } else {
         GetDefaultEventMonLogger()->Debug("added folder to monitor: " + folder);
     }
-    watched_folders_paths_[id] = folder;
+    std::string relative_path = folder;
+    relative_path.erase(0, root_folder_.size() + 1);
+    watched_folders_paths_[id] = relative_path;
     if (recursive) {
         Error err;
         auto subdirs = io_-> GetSubDirectories(folder, &err);
@@ -41,13 +43,15 @@ Error SystemFolderWatch::AddFolderToWatch(std::string folder, bool recursive) {
 }
 
 
-Error SystemFolderWatch::StartFolderMonitor(const std::vector<std::string>& monitored_folders) {
+Error SystemFolderWatch::StartFolderMonitor(const std::string& root_folder,
+                                            const std::vector<std::string>& monitored_folders) {
     watch_fd_ = inotify_init();
     if (watch_fd_ == -1) {
         return EventMonitorErrorTemplates::kSystemError.Generate("cannot initialize inotify");
     }
+    root_folder_ = root_folder;
     for (auto& folder : monitored_folders) {
-        auto err = AddFolderToWatch(folder, true);
+        auto err = AddFolderToWatch(root_folder_ + "/" + folder, true);
         if (err) {
             return EventMonitorErrorTemplates::kSystemError.Generate("cannot initialize inotify: " + err->Explain());
         }
@@ -91,7 +95,7 @@ Error SystemFolderWatch::ProcessInotifyEvent(struct inotify_event* i, FileEvents
                         i->wd));
         }
 
-        std::string newpath = it->second + "/" + i->name;
+        std::string newpath = root_folder_ + "/" + it->second + "/" + i->name;
         auto err = AddFolderToWatch(newpath, true);
         if (err) {
             return err;
diff --git a/producer/event_monitor_producer/src/system_folder_watch_linux.h b/producer/event_monitor_producer/src/system_folder_watch_linux.h
index 69ffc51b5..b54fee763 100644
--- a/producer/event_monitor_producer/src/system_folder_watch_linux.h
+++ b/producer/event_monitor_producer/src/system_folder_watch_linux.h
@@ -18,7 +18,7 @@ namespace asapo {
 
 class SystemFolderWatch {
   public:
-    VIRTUAL Error StartFolderMonitor(const std::vector<std::string>& monitored_folders);
+    VIRTUAL Error StartFolderMonitor(const std::string& root_folder, const std::vector<std::string>& monitored_folders);
     VIRTUAL FileEvents GetFileEventList(Error* err);
   private:
     Error AddFolderToWatch(std::string folder, bool recursive);
@@ -28,6 +28,7 @@ class SystemFolderWatch {
     static const uint64_t kBufLen  = 2000 * (sizeof(struct inotify_event) + FILENAME_MAX + 1);
     std::map<int, std::string> watched_folders_paths_;
     int watch_fd_ = -1;
+    std::string root_folder_;
 };
 
 }
diff --git a/producer/event_monitor_producer/src/system_folder_watch_windows.cpp b/producer/event_monitor_producer/src/system_folder_watch_windows.cpp
index dc0dfa558..b4e699f37 100644
--- a/producer/event_monitor_producer/src/system_folder_watch_windows.cpp
+++ b/producer/event_monitor_producer/src/system_folder_watch_windows.cpp
@@ -2,7 +2,8 @@
 
 namespace asapo {
 
-Error SystemFolderWatch::StartFolderMonitor(const std::vector<std::string>& monitored_folders) {
+Error SystemFolderWatch::StartFolderMonitor(const std::string& root_folder,
+                                            const std::vector<std::string>& monitored_folders) {
     return nullptr;
 }
 
diff --git a/producer/event_monitor_producer/unittests/mock_eventmon_config.cpp b/producer/event_monitor_producer/unittests/mock_eventmon_config.cpp
index f7772c736..2ff7a98bc 100644
--- a/producer/event_monitor_producer/unittests/mock_eventmon_config.cpp
+++ b/producer/event_monitor_producer/unittests/mock_eventmon_config.cpp
@@ -49,7 +49,7 @@ Error SetFolderMonConfig (const EventMonConfig& config) {
     config_string += "," + std::string("\"NThreads\":") + std::to_string(config.nthreads);
     config_string += "," + std::string("\"LogLevel\":") + "\"" + log_level + "\"";
     std::string mon_folders;
-    for (auto folder : config.monitored_folders) {
+    for (auto folder : config.monitored_subfolders) {
         mon_folders += "\"" + folder + "\"" + ",";
     }
     if (mon_folders.size()) {
@@ -65,7 +65,8 @@ Error SetFolderMonConfig (const EventMonConfig& config) {
     }
 
 
-    config_string += "," + std::string("\"MonitoredFolders\":") + "[" + mon_folders + "]";
+    config_string += "," + std::string("\"MonitoredSubFolders\":") + "[" + mon_folders + "]";
+    config_string += "," + std::string("\"RootMonitoredFolder\":") + "\"" + config.root_monitored_folder + "\"";
     config_string += "," + std::string("\"IgnoreExtentions\":") + "[" + ignored_exts + "]";
     config_string += "," + std::string("\"Tag\":") + "\"" + config.tag + "\"";
     config_string += "," + std::string("\"AsapoEndpoint\":") + "\"" + config.asapo_endpoint + "\"";
diff --git a/producer/event_monitor_producer/unittests/mock_system_folder_watch.h b/producer/event_monitor_producer/unittests/mock_system_folder_watch.h
index 43dc6cdf9..6cf14de9a 100644
--- a/producer/event_monitor_producer/unittests/mock_system_folder_watch.h
+++ b/producer/event_monitor_producer/unittests/mock_system_folder_watch.h
@@ -11,10 +11,11 @@ namespace asapo {
 
 class MockSystemFolderWatch : public SystemFolderWatch {
   public:
-    MOCK_METHOD1(StartFolderMonitor_t, ErrorInterface * (const std::vector<std::string>& monitored_folders));
+    MOCK_METHOD2(StartFolderMonitor_t, ErrorInterface * (const std::string& root_folder,
+                 const std::vector<std::string>& monitored_folders));
 
-    Error StartFolderMonitor(const std::vector<std::string>& monitored_folders) override {
-        return Error{StartFolderMonitor_t(monitored_folders)};
+    Error StartFolderMonitor(const std::string& root_folder, const std::vector<std::string>& monitored_folders) override {
+        return Error{StartFolderMonitor_t(root_folder, monitored_folders)};
 
     }
 
diff --git a/producer/event_monitor_producer/unittests/test_eventmon_config.cpp b/producer/event_monitor_producer/unittests/test_eventmon_config.cpp
index 5e1b14f0e..fdaaa055b 100644
--- a/producer/event_monitor_producer/unittests/test_eventmon_config.cpp
+++ b/producer/event_monitor_producer/unittests/test_eventmon_config.cpp
@@ -56,7 +56,8 @@ TEST_F(ConfigTests, ReadSettingsOK) {
     test_config.beamtime_id = "test";
     test_config.asapo_endpoint = "uri:001";
     test_config.mode = asapo::RequestHandlerType::kTcp;
-    test_config.monitored_folders = {"test1", "test2"};
+    test_config.root_monitored_folder = "tmp";
+    test_config.monitored_subfolders = {"test1", "test2"};
     test_config.ignored_extentions = {"tmp", "test"};
     auto err = asapo::SetFolderMonConfig(test_config);
 
@@ -69,7 +70,8 @@ TEST_F(ConfigTests, ReadSettingsOK) {
     ASSERT_THAT(config->beamtime_id, Eq("test"));
     ASSERT_THAT(config->asapo_endpoint, Eq("uri:001"));
     ASSERT_THAT(config->mode, Eq(asapo::RequestHandlerType::kTcp));
-    ASSERT_THAT(config->monitored_folders, ElementsAre("test1", "test2"));
+    ASSERT_THAT(config->monitored_subfolders, ElementsAre("test1", "test2"));
+    ASSERT_THAT(config->root_monitored_folder, Eq("tmp"));
     ASSERT_THAT(config->ignored_extentions, ElementsAre("tmp", "test"));
 
 }
diff --git a/producer/event_monitor_producer/unittests/test_folder_event_detector.cpp b/producer/event_monitor_producer/unittests/test_folder_event_detector.cpp
index 9ad181cbb..477dd7dcd 100644
--- a/producer/event_monitor_producer/unittests/test_folder_event_detector.cpp
+++ b/producer/event_monitor_producer/unittests/test_folder_event_detector.cpp
@@ -48,6 +48,7 @@ class FolderEventDetectorTests : public testing::Test {
     ::testing::NiceMock<asapo::MockSystemFolderWatch> mock_system_folder_watch;
     asapo::EventMonConfig test_config;
     FolderEventDetector detector{&test_config};
+    std::string expected_root_folder = "/tmp";
     std::vector<std::string> expected_folders{"test1", "test2"};
     FileEvent expected_event1{EventType::closed, 10, "test1.dat"};
     FileEvent expected_event2{EventType::renamed_to, 11, "test2.dat"};
@@ -55,7 +56,8 @@ class FolderEventDetectorTests : public testing::Test {
     FileEvent expected_event4{EventType::closed, 12, "test4.tmp"};
     FileEvents expected_events{expected_event1, expected_event2, expected_event3, expected_event4};
     void SetUp() override {
-        test_config.monitored_folders = expected_folders;
+        test_config.root_monitored_folder = expected_root_folder;
+        test_config.monitored_subfolders = expected_folders;
         err = nullptr;
         detector.system_folder_watch__ = std::unique_ptr<asapo::SystemFolderWatch> {&mock_system_folder_watch};
     }
@@ -68,7 +70,7 @@ class FolderEventDetectorTests : public testing::Test {
 };
 
 void FolderEventDetectorTests::MockStartMonitoring() {
-    EXPECT_CALL(mock_system_folder_watch, StartFolderMonitor_t(expected_folders))
+    EXPECT_CALL(mock_system_folder_watch, StartFolderMonitor_t(expected_root_folder, expected_folders))
     .WillOnce(
         Return(nullptr)
     );
@@ -77,7 +79,7 @@ void FolderEventDetectorTests::MockStartMonitoring() {
 
 
 TEST_F(FolderEventDetectorTests, StartsFolderMonitoringOK) {
-    EXPECT_CALL(mock_system_folder_watch, StartFolderMonitor_t(expected_folders))
+    EXPECT_CALL(mock_system_folder_watch, StartFolderMonitor_t(expected_root_folder, expected_folders))
     .WillOnce(
         Return(nullptr)
     );
@@ -86,7 +88,7 @@ TEST_F(FolderEventDetectorTests, StartsFolderMonitoringOK) {
 }
 
 TEST_F(FolderEventDetectorTests, StartFolderMonitoringInitiatesOnlyOnce) {
-    EXPECT_CALL(mock_system_folder_watch, StartFolderMonitor_t(expected_folders))
+    EXPECT_CALL(mock_system_folder_watch, StartFolderMonitor_t(expected_root_folder, expected_folders))
     .WillOnce(
         Return(nullptr)
     );
@@ -97,7 +99,7 @@ TEST_F(FolderEventDetectorTests, StartFolderMonitoringInitiatesOnlyOnce) {
 }
 
 TEST_F(FolderEventDetectorTests, StartFolderMonitoringReturnsError) {
-    EXPECT_CALL(mock_system_folder_watch, StartFolderMonitor_t(expected_folders))
+    EXPECT_CALL(mock_system_folder_watch, StartFolderMonitor_t(expected_root_folder, expected_folders))
     .Times(2)
     .WillOnce(
         Return(asapo::ErrorTemplates::kMemoryAllocationError.Generate().release())
-- 
GitLab