Project
Loading...
Searching...
No Matches
DirectoryLoader.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
17#include <filesystem>
18#include <algorithm>
19#include <climits>
20#include <map>
21#include <iostream>
22#include <forward_list>
23#include <regex>
24#include <fairlogger/Logger.h>
25
26using namespace std;
27using namespace o2::event_visualisation;
28
29deque<string> DirectoryLoader::load(const std::string& path, const std::string& marker, const std::vector<std::string>& ext)
30{
31 deque<string> result;
32 try {
33 for (const auto& entry : std::filesystem::directory_iterator(path)) {
34 if (std::find(ext.begin(), ext.end(), entry.path().extension()) != ext.end()) {
35 result.push_back(entry.path().filename());
36 }
37 }
38 } catch (std::filesystem::filesystem_error const& ex) {
39 LOGF(error, "filesystem problem during DirectoryLoader::load: %s", ex.what());
40 return result;
41 }
42 // comparison with safety if marker not in the filename (-1+1 gives 0)
43 std::sort(result.begin(), result.end(),
44 [marker](const std::string& a, const std::string& b) {
45 return a.substr(a.find_first_of(marker) + 1) < b.substr(b.find_first_of(marker) + 1);
46 });
47
48 return result;
49}
50
51bool DirectoryLoader::canCreateNextFile(const std::vector<std::string>& paths, const std::string& marker, const std::vector<std::string>& ext, long long millisec, long capacityAllowed)
52{
53 deque<string> result;
54 std::map<std::string, std::string> fullPath;
55 for (const auto& path : paths) {
56 try {
57 for (const auto& entry : std::filesystem::directory_iterator(path)) {
58 if (std::find(ext.begin(), ext.end(), entry.path().extension()) != ext.end()) {
59 result.push_back(entry.path().filename());
60 fullPath[entry.path().filename()] = entry.path();
61 }
62 }
63 } catch (std::filesystem::filesystem_error const& ex) {
64 LOGF(error, "filesystem problem during DirectoryLoader::canCreateNextFile: %s", ex.what());
65 return false;
66 }
67 }
68
69 // comparison with safety if marker not in the filename (-1+1 gives 0)
70 std::ranges::sort(result.begin(), result.end(),
71 [marker](const std::string& a, const std::string& b) {
72 return a.substr(a.find_first_of(marker) + 1) > b.substr(b.find_first_of(marker) + 1);
73 });
74 unsigned long accumulatedSize = 0L;
75 const std::regex delimiter{"_"};
76 for (auto const& file : result) {
77 std::vector<std::string> c(std::sregex_token_iterator(file.begin(), file.end(), delimiter, -1), {});
78 if (std::stoll(c[1]) < millisec) {
79 break;
80 }
81 try {
82 accumulatedSize += filesystem::file_size(fullPath[file]);
83 } catch (std::filesystem::filesystem_error const& ex) {
84 LOGF(info, "problem scanning folder: %s", ex.what());
85 }
86 if (accumulatedSize > capacityAllowed) {
87 return false;
88 }
89 }
90 return true;
91}
92
93deque<string> DirectoryLoader::load(const std::vector<std::string>& paths, const std::string& marker, const std::vector<std::string>& ext)
94{
95 deque<string> result;
96 try {
97 for (const auto& path : paths) {
98 for (const auto& entry : std::filesystem::directory_iterator(path)) {
99 if (std::find(ext.begin(), ext.end(), entry.path().extension()) != ext.end()) {
100 result.push_back(entry.path().filename());
101 }
102 }
103 }
104 } catch (std::filesystem::filesystem_error const& ex) {
105 LOGF(error, "filesystem problem during DirectoryLoader::load: %s", ex.what());
106 return result;
107 }
108 // comparison with safety if marker not in the filename (-1+1 gives 0)
109 std::sort(result.begin(), result.end(),
110 [marker](const std::string& a, const std::string& b) {
111 return a.substr(a.find_first_of(marker) + 1) < b.substr(b.find_first_of(marker) + 1);
112 });
113
114 return result;
115}
116
117std::vector<std::string> DirectoryLoader::allFolders(const std::string& location)
118{
119 auto const pos = location.find_last_of('_');
120 std::vector<std::string> folders;
121 folders.push_back(location.substr(0, pos) + "_PHYSICS");
122 folders.push_back(location.substr(0, pos) + "_COSMICS");
123 folders.push_back(location.substr(0, pos) + "_SYNTHETIC");
124 return folders;
125}
126
127void DirectoryLoader::reduceNumberOfFiles(const std::string& path, const std::deque<std::string>& files, std::size_t filesInFolder)
128{
129 if (filesInFolder == -1) {
130 return; // do not reduce
131 }
132 const auto items = files.size() - std::min(files.size(), filesInFolder);
133 for (int i = 0; i < items; i++) {
134 std::remove((path + "/" + files[i]).c_str()); // delete file
135 }
136}
137
138template <typename TP>
139std::time_t to_time_t(TP tp)
140{
141 using namespace std::chrono;
142 auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now() + system_clock::now());
143 return system_clock::to_time_t(sctp);
144}
145
146int DirectoryLoader::getNumberOfFiles(const std::string& path, std::vector<std::string>& ext)
147{
148 int res = 0;
149 try {
150 for (const auto& entry : std::filesystem::directory_iterator(path)) {
151 if (std::find(ext.begin(), ext.end(), entry.path().extension()) != ext.end()) {
152 res++;
153 }
154 }
155 } catch (std::filesystem::filesystem_error const& ex) {
156 LOGF(error, "filesystem problem during DirectoryLoader::getNumberOfFiles: %s", ex.what());
157 }
158 return res;
159}
160std::string DirectoryLoader::getLatestFile(const std::string& path, std::vector<std::string>& ext)
161{
162 std::string oldest_file_name = "";
163 std::time_t oldest_file_time = LONG_MAX;
164 for (const auto& entry : std::filesystem::directory_iterator(path)) {
165 if (std::find(ext.begin(), ext.end(), entry.path().extension()) != ext.end()) {
166 const auto file_time = entry.last_write_time();
167 if (const std::time_t tt = to_time_t(file_time); tt < oldest_file_time) {
168 oldest_file_time = tt;
169 oldest_file_name = entry.path().filename().string();
170 }
171 }
172 }
173 return oldest_file_name;
174}
175
176void DirectoryLoader::removeOldestFiles(const std::string& path, std::vector<std::string>& ext, const int remaining)
177{
178 try {
179 while (getNumberOfFiles(path, ext) > remaining) {
180 LOGF(info, "removing oldest file in folder: %s : %s", path, getLatestFile(path, ext));
181 filesystem::remove(path + "/" + getLatestFile(path, ext));
182 }
183 } catch (std::filesystem::filesystem_error const& ex) {
184 LOGF(error, "filesystem problem during DirectoryLoader::removeOldestFiles: %s", ex.what());
185 }
186}
std::time_t to_time_t(TP tp)
Loading content of the Folder and returning sorted.
int32_t i
uint16_t pos
Definition RawData.h:3
uint32_t res
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
static void removeOldestFiles(const std::string &path, std::vector< std::string > &ext, int remaining)
static bool canCreateNextFile(const std::vector< std::string > &paths, const std::string &marker, const std::vector< std::string > &ext, long long millisec, long capacityAllowed)
static void reduceNumberOfFiles(const std::string &path, const std::deque< std::string > &files, std::size_t filesInFolder)
static std::vector< std::string > allFolders(const std::string &location)
static std::deque< std::string > load(const std::string &path, const std::string &marker, const std::vector< std::string > &ext)
GLuint64EXT * result
Definition glcorearb.h:5662
GLuint entry
Definition glcorearb.h:5735
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei const GLuint * paths
Definition glcorearb.h:5475
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
Defining DataPointCompositeObject explicitly as copiable.