-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathFileWatcher.cxx
More file actions
217 lines (192 loc) · 6.1 KB
/
FileWatcher.cxx
File metadata and controls
217 lines (192 loc) · 6.1 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// 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 "EventVisualisationBase/DirectoryLoader.h"
#include <fairlogger/Logger.h>
#include <list>
#include <filesystem>
#include <algorithm>
#include <sys/stat.h>
using namespace std;
using namespace o2::event_visualisation;
const char* FileWatcher::mLowGuard = " 0"; /// start guard
const char* FileWatcher::mEndGuard = "~0"; /// stop guard
FileWatcher::FileWatcher(const std::vector<string>& path, const std::vector<std::string>& ext) : mExt(ext)
{
//LOG(info) << "FileWatcher::FileWatcher(" << path << ")";
this->mDataFolders = path;
this->mCurrentFile = mEndGuard;
this->mFiles.clear();
this->mFiles.push_front(mLowGuard);
this->mFiles.push_back(mEndGuard);
}
void FileWatcher::changeFolder(const string& path)
{
if (this->mDataFolders.size() == 1 && this->mDataFolders[0] == path) {
return; // the same folder - no action
}
this->mDataFolders.clear();
this->mDataFolders.push_back(path);
this->mCurrentFile = mEndGuard;
this->mFiles.clear();
this->mFiles.push_front(mLowGuard);
this->mFiles.push_back(mEndGuard);
this->refresh();
// LOG(info) << "FileWatcher" << this->getSize();
}
void FileWatcher::changeFolder(const std::vector<string>& paths)
{
if (this->mDataFolders == paths) {
return; // the same folders - no action
}
this->mDataFolders.clear();
this->mDataFolders = paths;
this->mCurrentFile = mEndGuard;
this->mFiles.clear();
this->mFiles.push_front(mLowGuard);
this->mFiles.push_back(mEndGuard);
this->refresh();
//LOG(info) << "FileWatcher" << this->getSize();
}
string FileWatcher::nextItem(const string& item) const
{
if (item == mEndGuard) {
return mEndGuard;
}
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 == mEndGuard) {
return *(this->mFiles.end() - 2);
}
return this->mCurrentFile;
}
void FileWatcher::rollToNext()
{
if (this->mFiles.size() == 2) { // only guards on the list
return; // nothing to do
}
this->setNext();
if (this->mCurrentFile == mEndGuard) {
this->setFirst();
this->setNext();
}
}
void FileWatcher::setFirst()
{
this->mCurrentFile = mLowGuard;
}
void FileWatcher::setLast()
{
this->mCurrentFile = mEndGuard;
}
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();
LOGF(info, "previous:", previous);
LOGF(info, "currentFile:", this->mCurrentFile);
this->mFiles = DirectoryLoader::load(this->mDataFolders, "_", this->mExt); // already sorted according part staring with marker
if (this->mCurrentFile != mEndGuard) {
if (this->mFiles.empty()) {
this->mCurrentFile = mEndGuard; // 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 = mEndGuard; // 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(mEndGuard);
LOGF(info, "this->mFiles.size() = ", this->mFiles.size());
LOGF(info, "this->mCurrentFile = ", this->mCurrentFile);
LOGF(info, "current:", this->currentItem());
return previous != this->currentItem();
}
void FileWatcher::setCurrentItem(int no)
{
this->mCurrentFile = this->mFiles[no];
LOGF(info, "this->setCurrentItem(", no, ")");
LOGF(info, "this->mCurrentFile = ", this->mCurrentFile);
}
std::string FileWatcher::currentFilePath() const
{
if (this->mDataFolders.size() > 1) {
for (std::string dataFolder : mDataFolders) {
struct stat buffer;
std::string path = dataFolder + "/" + this->currentItem();
if (stat(path.c_str(), &buffer) == 0) {
return dataFolder + "/" + this->currentItem();
}
}
}
return this->mDataFolders[0] + "/" + this->currentItem();
}
bool FileWatcher::currentFileExist()
{
struct stat buffer;
return (stat(this->currentFilePath().c_str(), &buffer) == 0);
}
void FileWatcher::saveCurrentFileToFolder(const string& destinationFolder)
{
if (!std::filesystem::exists(destinationFolder)) {
return; // do not specified, where to save
}
for (const auto& folder : this->mDataFolders) {
if (folder == destinationFolder) {
return; // could not save to yourself
}
}
if (this->currentFileExist()) {
std::filesystem::path source = this->currentFilePath();
std::filesystem::path destination = destinationFolder;
destination /= source.filename();
std::filesystem::copy_file(source, destination);
}
}