Aktualizr
C++ SOTA Client
filelist.cc
1 #include "filelist.h"
2 
3 #include <algorithm>
4 #include <boost/filesystem.hpp>
5 #include <boost/functional/hash.hpp>
6 #include <cstring>
7 
8 namespace fs = boost::filesystem;
9 
10 namespace opcuabridge {
11 
12 FileSetEntryHash::result_type FileSetEntryHash::operator()(FileSetEntryHash::argument_type const& e) const {
13  result_type seed = 0;
14  argument_type x = e;
15  for (; *x != '\0'; ++x) {
16  boost::hash_combine(seed, *x);
17  }
18  return seed;
19 }
20 
21 bool FileSetEntryEqual::operator()(const FileSetEntryEqual::argument_type& lhs,
22  const FileSetEntryEqual::argument_type& rhs) const {
23  return (0 == strcmp(reinterpret_cast<const char*>(lhs), reinterpret_cast<const char*>(rhs)));
24 }
25 
26 std::size_t UpdateFileList(FileList* filelist, const fs::path& repo_dir_path) {
27  FileList::block_type& block = filelist->getBlock();
28  std::size_t file_count = 0;
29  fs::recursive_directory_iterator repo_dir_it_end, repo_dir_it(repo_dir_path);
30  for (; repo_dir_it != repo_dir_it_end; ++repo_dir_it) {
31  const fs::path& ent_path = repo_dir_it->path();
32  if (fs::is_regular_file(ent_path)) {
33  fs::path rel_path = fs::relative(ent_path, repo_dir_path);
34  std::copy(rel_path.native().begin(), rel_path.native().end(), std::back_inserter(block));
35  block.push_back('\0');
36  ++file_count;
37  }
38  }
39  return file_count;
40 }
41 
42 void UpdateFileUnorderedSet(FileUnorderedSet* file_unordered_set, const FileList& file_list) {
43  if (!file_list.getBlock().empty()) {
44  auto block_rev_it = file_list.getBlock().rbegin(), p = block_rev_it, block_rev_end_it = file_list.getBlock().rend();
45  for (; block_rev_it != block_rev_end_it; p = block_rev_it) {
46  if ((block_rev_end_it == ++block_rev_it) || ('\0' == *(block_rev_it))) {
47  file_unordered_set->insert(&*p);
48  }
49  }
50  }
51 }
52 
53 } // namespace opcuabridge