Project
Loading...
Searching...
No Matches
FileWatcher.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
18#include <fairlogger/Logger.h>
19
20#include <list>
21#include <filesystem>
22#include <algorithm>
23#include <sys/stat.h>
24
25using namespace std;
26using namespace o2::event_visualisation;
27
28const char* FileWatcher::mLowGuard = " 0";
29const char* FileWatcher::mEndGuard = "~0";
30
31FileWatcher::FileWatcher(const std::vector<string>& path, const std::vector<std::string>& ext) : mExt(ext)
32{
33 //LOG(info) << "FileWatcher::FileWatcher(" << path << ")";
34 this->mDataFolders = path;
35 this->mCurrentFile = mEndGuard;
36 this->mFiles.clear();
37 this->mFiles.push_front(mLowGuard);
38 this->mFiles.push_back(mEndGuard);
39}
40
42{
43 if (this->mDataFolders.size() == 1 && this->mDataFolders[0] == path) {
44 return; // the same folder - no action
45 }
46 this->mDataFolders.clear();
47 this->mDataFolders.push_back(path);
48 this->mCurrentFile = mEndGuard;
49 this->mFiles.clear();
50 this->mFiles.push_front(mLowGuard);
51 this->mFiles.push_back(mEndGuard);
52 this->refresh();
53 // LOG(info) << "FileWatcher" << this->getSize();
54}
55
56void FileWatcher::changeFolder(const std::vector<string>& paths)
57{
58 if (this->mDataFolders == paths) {
59 return; // the same folders - no action
60 }
61 this->mDataFolders.clear();
62 this->mDataFolders = paths;
63 this->mCurrentFile = mEndGuard;
64 this->mFiles.clear();
65 this->mFiles.push_front(mLowGuard);
66 this->mFiles.push_back(mEndGuard);
67 this->refresh();
68 //LOG(info) << "FileWatcher" << this->getSize();
69}
70
71string FileWatcher::nextItem(const string& item) const
72{
73 if (item == mEndGuard) {
74 return mEndGuard;
75 }
76 return *(std::find(this->mFiles.begin(), this->mFiles.end(), item) + 1);
77}
78
79string FileWatcher::prevItem(const string& item) const
80{
81 if (item == mLowGuard) {
82 return mLowGuard;
83 }
84 return *(std::find(mFiles.begin(), mFiles.end(), item) - 1);
85}
86
88{
89 if (this->mFiles.size() == 2) { // only guards on the list
90 return "";
91 }
92 if (this->mCurrentFile == mLowGuard) {
93 return *(this->mFiles.begin() + 1);
94 }
95 if (this->mCurrentFile == mEndGuard) {
96 return *(this->mFiles.end() - 2);
97 }
98 return this->mCurrentFile;
99}
100
102{
103 if (this->mFiles.size() == 2) { // only guards on the list
104 return; // nothing to do
105 }
106 this->setNext();
107 if (this->mCurrentFile == mEndGuard) {
108 this->setFirst();
109 this->setNext();
110 }
111}
112
114{
115 this->mCurrentFile = mLowGuard;
116}
117
119{
120 this->mCurrentFile = mEndGuard;
121}
122
124{
125 this->mCurrentFile = nextItem(this->mCurrentFile);
126}
127
129{
130 this->mCurrentFile = prevItem(this->mCurrentFile);
131}
132
134{
135 return this->mFiles.size(); // guards
136}
137
139{
140 return std::distance(mFiles.begin(), std::find(mFiles.begin(), mFiles.end(), this->mCurrentFile));
141}
142
144{
145 string previous = this->currentItem();
146 LOGF(info, "previous:", previous);
147 LOGF(info, "currentFile:", this->mCurrentFile);
148
149 this->mFiles = DirectoryLoader::load(this->mDataFolders, "_", this->mExt); // already sorted according part staring with marker
150 if (this->mCurrentFile != mEndGuard) {
151 if (this->mFiles.empty()) {
152 this->mCurrentFile = mEndGuard; // list empty - stick to last element
153 } else if (this->mCurrentFile < *(this->mFiles.begin())) {
154 this->mCurrentFile = mLowGuard; // lower then first => go to first
155 } else {
156 auto it = std::find(mFiles.begin(), mFiles.end(), this->mCurrentFile);
157 if (it == this->mFiles.end()) {
158 this->mCurrentFile = mEndGuard; // not on the list -> go to last element
159 }
160 }
161 }
162 //for (auto it = this->mFiles.begin(); it != this->mFiles.end(); ++it) {
163 // LOG(info) << *it;
164 //}
165 this->mFiles.push_front(mLowGuard);
166 this->mFiles.push_back(mEndGuard);
167
168 LOGF(info, "this->mFiles.size() = ", this->mFiles.size());
169 LOGF(info, "this->mCurrentFile = ", this->mCurrentFile);
170 LOGF(info, "current:", this->currentItem());
171 return previous != this->currentItem();
172}
173
175{
176 this->mCurrentFile = this->mFiles[no];
177 LOGF(info, "this->setCurrentItem(", no, ")");
178 LOGF(info, "this->mCurrentFile = ", this->mCurrentFile);
179}
180
182{
183 if (this->mDataFolders.size() > 1) {
184 for (std::string dataFolder : mDataFolders) {
185 struct stat buffer;
186 std::string path = dataFolder + "/" + this->currentItem();
187 if (stat(path.c_str(), &buffer) == 0) {
188 return dataFolder + "/" + this->currentItem();
189 }
190 }
191 }
192 return this->mDataFolders[0] + "/" + this->currentItem();
193}
194
195bool FileWatcher::currentFileExist()
196{
197 struct stat buffer;
198 return (stat(this->currentFilePath().c_str(), &buffer) == 0);
199}
200
201void FileWatcher::saveCurrentFileToFolder(const string& destinationFolder)
202{
203 if (!std::filesystem::exists(destinationFolder)) {
204 return; // do not specified, where to save
205 }
206 for (const auto& folder : this->mDataFolders) {
207 if (folder == destinationFolder) {
208 return; // could not save to yourself
209 }
210 }
211 if (this->currentFileExist()) {
212 std::filesystem::path source = this->currentFilePath();
213 std::filesystem::path destination = destinationFolder;
214 destination /= source.filename();
215 std::filesystem::copy_file(source, destination);
216 }
217}
Loading content of the Folder and returning sorted.
Observing folder for created and removed files - preserving current.
static std::deque< std::string > load(const std::string &path, const std::string &marker, const std::vector< std::string > &ext)
std::string currentFilePath() const
name of the file (with path) but guards replaced with file names
void saveCurrentFileToFolder(const std::string &destinationFolder)
copies
FileWatcher(const std::vector< std::string > &path, const std::vector< std::string > &ext)
stop guard
int getPos() const
include guards -> 0 points to mLowGuard
void setCurrentItem(int no)
sets using index
void rollToNext()
round robin next item
std::string currentItem() const
name of the file (without path) but guards replaced with file names
bool refresh()
reads folder content, updates current if points to not existing file
int getSize() const
include guards (so >=2 )
void changeFolder(const std::string &path)
switch to observe other folder
GLuint buffer
Definition glcorearb.h:655
GLsizei GLsizei GLchar * source
Definition glcorearb.h:798
GLsizei const GLuint * paths
Definition glcorearb.h:5475
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
Defining DataPointCompositeObject explicitly as copiable.