23 uint32_t last_crc = 0;
24 int64_t last_size = 0;
28 for (
auto record = records.begin(); record != records.end();)
30 bool is_fragment =
false;
40 "FragmentsToFilesFilter::filter: "
41 "WARNING: record %s has abnormal name. Skipping.\n",
42 record->first.c_str())
47 if (record->second.is_binary_file_type())
57 record->first.substr(record->first.find_last_of(
'.') + 1);
58 std::string base_name = record->first.substr(prefix.size() + 1);
59 base_name.erase(base_name.find_last_of(
'.'));
60 base_name.erase(base_name.find_last_of(
'.'));
61 std::size_t last_period = record->first.find_last_of(
'.');
66 filename = path +
"/" + base_name +
".";
72 if (last_file ==
"" || last_file.compare(0, last_file.size(),
73 record->first.substr(0, last_period)) != 0)
75 if (last_file_path !=
"")
78 last_file_path, last_crc,
true,
false))
82 "FragmentsToFilesFilter::filter: "
83 "SUCCESS: file %s is recreated\n",
84 last_file_path.c_str())
90 "FragmentsToFilesFilter::filter: "
91 "FAIL: file %s is incomplete\n",
92 last_file_path.c_str())
97 last_file_path = path +
"/" +
98 record->first.substr(prefix.size() + 1,
99 last_period - prefix.size() - 1 - 9);
100 last_file = prefix +
"." + base_name;
101 auto crc_record = records.find(last_file +
".crc");
102 auto size_record = records.find(last_file +
".size");
104 if (crc_record != records.end())
106 last_crc = (uint32_t)crc_record->second.to_integer();
107 str_crc = crc_record->second.to_string();
110 if (size_record != records.end())
112 last_size = size_record->second.to_integer();
118 filename +=
"." + str_crc +
".frag";
126 record->second.to_file(filename);
130 "FragmentsToFilesFilter::filter: "
131 "found fragment %s:\n"
134 " last_file_path=%s\n"
136 " last_size=%" PRId64
"\n"
138 record->first.c_str(), base_name.c_str(), last_file.c_str(),
139 last_file_path.c_str(), str_crc.c_str(), last_size,
146 "FragmentsToFilesFilter::filter: "
150 " last_file_path=%s\n"
152 " last_size=%" PRId64
"\n"
154 record->first.c_str(), base_name.c_str(), last_file.c_str(),
155 last_file_path.c_str(), str_crc.c_str(), last_size,
163 "FragmentsToFilesFilter::filter: "
164 "%s is not a fragment of %s\n",
165 record->first.c_str(), map.first.c_str())
175 "FragmentsToFilesFilter::filter: "
176 "removing variable %s\n",
177 record->first.c_str())
179 record = records.erase(record);
185 "FragmentsToFilesFilter::filter: "
186 "%s is not being deleted\n",
187 record->first.c_str())++ record;
191 if (last_file_path !=
"")
197 "FragmentsToFilesFilter::filter: "
198 "SUCCESS: file %s is recreated\n",
199 last_file_path.c_str())
205 "FragmentsToFilesFilter::filter: "
206 "FAIL: file %s is incomplete\n",
207 last_file_path.c_str())
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
std::map< std::string, std::string > map_
map of variable prefixes to directories
bool clear_fragments_
if true, clear fragments after sent to file
virtual MADARA_EXPORT void filter(knowledge::KnowledgeMap &records, const transport::TransportContext &, knowledge::Variables &)
Filters the fragments and converts them to files.
Provides an interface for external functions into the MADARA KaRL variable settings.
Provides context about the transport.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
::std::map< std::string, KnowledgeRecord > KnowledgeMap
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
MADARA_EXPORT bool file_from_fragments(const std::string &filename, uint32_t crc, bool delete_incomplete=true, bool delete_fragments=true)
Builds a file from fragments that have the format: filename.
bool recursive_mkdir(const std::string &path)
MADARA_EXPORT std::string extract_path(const std::string &name)
Extracts the path of a filename.
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
MADARA_EXPORT bool filename_has_redirect(const std::string &filename)
Checks the filename for abnormal redirects such as "..".
Copyright(c) 2020 Galois.