-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathFileWatcher.cxx
More file actions
166 lines (144 loc) · 4.38 KB
/
FileWatcher.cxx
File metadata and controls
166 lines (144 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file FileWatcher.h
/// \brief Observing folder for created and removed files - preserving current
/// \author julian.myrcha@cern.ch
#include "EventVisualisationBase/FileWatcher.h"
#include "FairLogger.h"
#include <list>
#include <filesystem>
#include <algorithm>
#include <sys/stat.h>
using namespace std;
namespace o2
{
namespace event_visualisation
{
const char* FileWatcher::mLowGuard = " 0"; /// start guard
const char* FileWatcher::mEndGard = "~0"; /// stop guard
deque<string> FileWatcher::load(string path)
{
LOG(INFO) << "FileWatcher::load(" << path << ")";
deque<string> result;
for (const auto& entry : std::filesystem::directory_iterator(path)) {
if (entry.path().extension() == ".json") {
result.push_back(entry.path().filename());
}
}
LOG(INFO) << result.size();
return result;
}
FileWatcher::FileWatcher(const string& path)
{
LOG(INFO) << "FileWatcher::FileWatcher(" << path << ")";
this->mDataFolder = path;
this->mCurrentFile = mLowGuard;
this->mFiles.clear();
this->mFiles.push_front(mLowGuard);
this->mFiles.push_back(mEndGard);
LOG(INFO) << "FileWatcher" << this->getSize();
}
string FileWatcher::nextItem(const string& item) const
{
if (item == mEndGard) {
return mEndGard;
}
return *(std::find(this->mFiles.begin(), this->mFiles.end(), item) + 1);
}
string FileWatcher::prevItem(const string& item) const
{
if (item == mLowGuard) {
return mLowGuard;
}
return *(std::find(mFiles.begin(), mFiles.end(), item) - 1);
}
string FileWatcher::currentItem() const
{
if (this->mFiles.size() == 2) { // only guards on the list
return "";
}
if (this->mCurrentFile == mLowGuard) {
return *(this->mFiles.begin() + 1);
}
if (this->mCurrentFile == mEndGard) {
return *(this->mFiles.end() - 2);
}
return this->mCurrentFile;
}
void FileWatcher::setFirst()
{
this->mCurrentFile = mLowGuard;
}
void FileWatcher::setLast()
{
this->mCurrentFile = mEndGard;
}
void FileWatcher::setNext()
{
this->mCurrentFile = nextItem(this->mCurrentFile);
}
void FileWatcher::setPrev()
{
this->mCurrentFile = prevItem(this->mCurrentFile);
}
int FileWatcher::getSize() const
{
return this->mFiles.size(); // guards
}
int FileWatcher::getPos() const
{
return std::distance(mFiles.begin(), std::find(mFiles.begin(), mFiles.end(), this->mCurrentFile));
}
bool FileWatcher::refresh()
{
string previous = this->currentItem();
LOG(INFO) << "previous:" << previous;
LOG(INFO) << "currentFile:" << this->mCurrentFile;
this->mFiles = load(this->mDataFolder);
std::sort(this->mFiles.begin(), this->mFiles.end());
if (this->mCurrentFile != mEndGard) {
if (this->mFiles.empty()) {
this->mCurrentFile = mEndGard; // list empty - stick to last element
} else if (this->mCurrentFile < *(this->mFiles.begin())) {
this->mCurrentFile = mLowGuard; // lower then first => go to first
} else {
auto it = std::find(mFiles.begin(), mFiles.end(), this->mCurrentFile);
if (it == this->mFiles.end()) {
this->mCurrentFile = mEndGard; // not on the list -> go to last element
}
}
}
for (auto it = this->mFiles.begin(); it != this->mFiles.end(); ++it) {
LOG(INFO) << *it;
}
this->mFiles.push_front(mLowGuard);
this->mFiles.push_back(mEndGard);
LOG(INFO) << "this->mFiles.size() = " << this->mFiles.size();
LOG(INFO) << "this->mCurrentFile = " << this->mCurrentFile;
LOG(INFO) << "current:" << this->currentItem();
return previous != this->currentItem();
}
void FileWatcher::setCurrentItem(int no)
{
this->mCurrentFile = this->mFiles[no];
LOG(INFO) << "this->setCurrentItem(" << no << ")";
LOG(INFO) << "this->mCurrentFile = " << this->mCurrentFile;
}
std::string FileWatcher::currentFilePath() const
{
return this->mDataFolder + "/" + this->currentItem();
}
bool FileWatcher::currentFileExist()
{
struct stat buffer;
return (stat(this->currentFilePath().c_str(), &buffer) == 0);
}
} // namespace event_visualisation
} // namespace o2