filesystem_util.cpp
Go to the documentation of this file.
1 #include "filesystem_util.h"
2 
3 #include <algorithm>
4 
6 
8 {
9  namespace filesystem::util
10  {
11  size_t
12  getSizeOfDirectory(const std::filesystem::path& p)
13  {
14  if (!std::filesystem::exists(p) || !std::filesystem::is_directory(p))
15  {
16  return 0;
17  }
18 
19  // command to be executed. Taken from https://stackoverflow.com/questions/15495756/how-can-i-find-the-size-of-all-files-located-inside-a-folder/15501719
20  std::string cmd = "du -sb " + p.string() + " | cut -f1 2>&1";
21 
22  // execute above command and get the output
23  FILE* stream = popen(cmd.c_str(), "r");
24  if (stream)
25  {
26  const int max_size = 256;
27  char readbuf[max_size];
28  if (fgets(readbuf, max_size, stream) != NULL)
29  {
30  return atoll(readbuf);
31  }
32  pclose(stream);
33  }
34 
35  // return error val
36  return 0;
37  }
38 
39  bool
40  checkIfFolderInFilesystemExists(const std::filesystem::path& p)
41  {
42  return std::filesystem::exists(p) and std::filesystem::is_directory(p);
43  }
44 
45  void
46  ensureFolderInFilesystemExists(const std::filesystem::path& p, bool createIfNotExistent)
47  {
48  if (!std::filesystem::exists(p))
49  {
50  if (createIfNotExistent)
51  {
52  std::filesystem::create_directories(p);
53  }
54  }
55  if (!std::filesystem::is_directory(p))
56  {
57  throw error::ArMemError("Could not find folder: " + p.string());
58  }
59  }
60 
61  bool
62  checkIfFileInFilesystemExists(const std::filesystem::path& p)
63  {
64  return std::filesystem::exists(p) and std::filesystem::is_regular_file(p);
65  }
66 
67  void
68  ensureFileInFilesystemExists(const std::filesystem::path& p)
69  {
70  if (!std::filesystem::exists(p) || !std::filesystem::is_regular_file(p))
71  {
72  // not found
73  throw error::ArMemError("Could not find file: " + p.string());
74  }
75  }
76 
77  void
78  writeDataInFilesystemFile(const std::filesystem::path& p,
79  const std::vector<unsigned char>& data)
80  {
81  std::ofstream dataofs;
82  dataofs.open(p);
83  if (!dataofs)
84  {
85  throw error::ArMemError("Could not write data to filesystem file '" + p.string() +
86  "'. Skipping this file.");
87  }
88  dataofs.write(reinterpret_cast<const char*>(data.data()), data.size());
89  dataofs.close();
90  }
91 
92  std::vector<unsigned char>
93  readDataFromFilesystemFile(const std::filesystem::path path)
94  {
95  std::ifstream dataifs(path);
96  std::vector<unsigned char> datafilecontent((std::istreambuf_iterator<char>(dataifs)),
97  (std::istreambuf_iterator<char>()));
98  return datafilecontent;
99  }
100 
101  std::vector<std::string>
102  getAllFilesystemDirectories(const std::filesystem::path path)
103  {
104  std::vector<std::string> ret;
105  for (const auto& subdir : std::filesystem::directory_iterator(path))
106  {
107  std::filesystem::path subdirPath = subdir.path();
108  if (std::filesystem::is_directory(subdirPath))
109  {
110  ret.push_back(subdirPath.filename());
111  }
112  }
113  std::sort(ret.begin(), ret.end());
114  return ret;
115  }
116 
117  std::vector<std::string>
118  getAllFilesystemFiles(const std::filesystem::path path)
119  {
120  std::vector<std::string> ret;
121  for (const auto& subdir : std::filesystem::directory_iterator(path))
122  {
123  std::filesystem::path subdirPath = subdir.path();
124  if (std::filesystem::is_regular_file(subdirPath))
125  {
126  ret.push_back(subdirPath.filename());
127  }
128  }
129  std::sort(ret.begin(), ret.end());
130  return ret;
131  }
132  } // namespace filesystem::util
133 } // namespace armarx::armem::server::ltm::disk
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:13
armarx::armem::server::ltm::disk::filesystem::util::readDataFromFilesystemFile
std::vector< unsigned char > readDataFromFilesystemFile(const std::filesystem::path path)
Definition: filesystem_util.cpp:93
armarx::armem::server::ltm::disk::filesystem::util::getAllFilesystemDirectories
std::vector< std::string > getAllFilesystemDirectories(const std::filesystem::path path)
Definition: filesystem_util.cpp:102
armarx::armem::error::ArMemError
Base class for all exceptions thrown by the armem library.
Definition: ArMemError.h:18
filesystem_util.h
armarx::armem::server::ltm::disk::filesystem::util::checkIfFileInFilesystemExists
bool checkIfFileInFilesystemExists(const std::filesystem::path &p)
Definition: filesystem_util.cpp:62
ArMemError.h
armarx::armem::server::ltm::disk::filesystem::util::getSizeOfDirectory
size_t getSizeOfDirectory(const std::filesystem::path &p)
Definition: filesystem_util.cpp:12
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::armem::server::ltm::disk::filesystem::util::writeDataInFilesystemFile
void writeDataInFilesystemFile(const std::filesystem::path &p, const std::vector< unsigned char > &data)
Definition: filesystem_util.cpp:78
armarx::armem::server::ltm::disk::filesystem::util::ensureFolderInFilesystemExists
void ensureFolderInFilesystemExists(const std::filesystem::path &p, bool createIfNotExistent)
Definition: filesystem_util.cpp:46
armarx::armem::server::ltm::disk::filesystem::util::checkIfFolderInFilesystemExists
bool checkIfFolderInFilesystemExists(const std::filesystem::path &p)
Definition: filesystem_util.cpp:40
armarx::armem::server::ltm::disk::filesystem::util::getAllFilesystemFiles
std::vector< std::string > getAllFilesystemFiles(const std::filesystem::path path)
Definition: filesystem_util.cpp:118
armarx::armem::server::ltm::disk
Definition: filesystem_util.cpp:7
armarx::armem::server::ltm::disk::filesystem::util::ensureFileInFilesystemExists
void ensureFileInFilesystemExists(const std::filesystem::path &p)
Definition: filesystem_util.cpp:68