filesystem_util.cpp
Go to the documentation of this file.
1 #include "filesystem_util.h"
2 
3 #include <algorithm>
4 
6 
7 
9 {
10  namespace filesystem::util
11  {
12  size_t 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  const int max_size = 256;
26  char readbuf[max_size];
27  if (fgets(readbuf, max_size, stream) != NULL)
28  {
29  return atoll(readbuf);
30  }
31  pclose(stream);
32  }
33 
34  // return error val
35  return 0;
36  }
37 
38  bool checkIfFolderInFilesystemExists(const std::filesystem::path& p)
39  {
40  return std::filesystem::exists(p) and std::filesystem::is_directory(p);
41  }
42 
43  void ensureFolderInFilesystemExists(const std::filesystem::path& p, bool createIfNotExistent)
44  {
45  if (!std::filesystem::exists(p))
46  {
47  if (createIfNotExistent)
48  {
49  std::filesystem::create_directories(p);
50  }
51  }
52  if (!std::filesystem::is_directory(p))
53  {
54  throw error::ArMemError("Could not find folder: " + p.string());
55  }
56  }
57 
58  bool checkIfFileInFilesystemExists(const std::filesystem::path& p)
59  {
60  return std::filesystem::exists(p) and std::filesystem::is_regular_file(p);
61  }
62 
63  void ensureFileInFilesystemExists(const std::filesystem::path& p)
64  {
65  if (!std::filesystem::exists(p) || !std::filesystem::is_regular_file(p))
66  {
67  // not found
68  throw error::ArMemError("Could not find file: " + p.string());
69  }
70  }
71 
72  void writeDataInFilesystemFile(const std::filesystem::path& p, const std::vector<unsigned char>& data)
73  {
74  std::ofstream dataofs;
75  dataofs.open(p);
76  if (!dataofs)
77  {
78  throw error::ArMemError("Could not write data to filesystem file '" + p.string() + "'. Skipping this file.");
79  }
80  dataofs.write(reinterpret_cast<const char*>(data.data()), data.size());
81  dataofs.close();
82  }
83 
84  std::vector<unsigned char> readDataFromFilesystemFile(const std::filesystem::path path)
85  {
86  std::ifstream dataifs(path);
87  std::vector<unsigned char> datafilecontent((std::istreambuf_iterator<char>(dataifs)), (std::istreambuf_iterator<char>()));
88  return datafilecontent;
89  }
90 
91  std::vector<std::string> getAllFilesystemDirectories(const std::filesystem::path path)
92  {
93  std::vector<std::string> ret;
94  for (const auto& subdir : std::filesystem::directory_iterator(path))
95  {
96  std::filesystem::path subdirPath = subdir.path();
97  if (std::filesystem::is_directory(subdirPath))
98  {
99  ret.push_back(subdirPath.filename());
100  }
101  }
102  std::sort(ret.begin(), ret.end());
103  return ret;
104  }
105 
106  std::vector<std::string> getAllFilesystemFiles(const std::filesystem::path path)
107  {
108  std::vector<std::string> ret;
109  for (const auto& subdir : std::filesystem::directory_iterator(path))
110  {
111  std::filesystem::path subdirPath = subdir.path();
112  if (std::filesystem::is_regular_file(subdirPath))
113  {
114  ret.push_back(subdirPath.filename());
115  }
116  }
117  std::sort(ret.begin(), ret.end());
118  return ret;
119  }
120  }
121 } // namespace armarx::armem::server::ltm::diskfile:///home/fabian/code/armarx/RobotAPI/source/RobotAPI/libraries/armem/server/ltm/legacy/util/util.h
122 
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::armem::server::ltm::disk::filesystem::util::readDataFromFilesystemFile
std::vector< unsigned char > readDataFromFilesystemFile(const std::filesystem::path path)
Definition: filesystem_util.cpp:84
armarx::armem::server::ltm::disk::filesystem::util::getAllFilesystemDirectories
std::vector< std::string > getAllFilesystemDirectories(const std::filesystem::path path)
Definition: filesystem_util.cpp:91
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:58
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:72
armarx::armem::server::ltm::disk::filesystem::util::ensureFolderInFilesystemExists
void ensureFolderInFilesystemExists(const std::filesystem::path &p, bool createIfNotExistent)
Definition: filesystem_util.cpp:43
armarx::armem::server::ltm::disk::filesystem::util::checkIfFolderInFilesystemExists
bool checkIfFolderInFilesystemExists(const std::filesystem::path &p)
Definition: filesystem_util.cpp:38
armarx::armem::server::ltm::disk::filesystem::util::getAllFilesystemFiles
std::vector< std::string > getAllFilesystemFiles(const std::filesystem::path path)
Definition: filesystem_util.cpp:106
armarx::armem::server::ltm::disk
Definition: filesystem_util.cpp:8
armarx::armem::server::ltm::disk::filesystem::util::ensureFileInFilesystemExists
void ensureFileInFilesystemExists(const std::filesystem::path &p)
Definition: filesystem_util.cpp:63