MADARA  3.1.8
Files.cpp
Go to the documentation of this file.
1 #ifndef _MADARA_KNOWLEDGE_BASE_FILES_CPP_
2 #define _MADARA_KNOWLEDGE_BASE_FILES_CPP_
3 
4 #include "Files.h"
5 #include <sstream>
6 #include "ace/OS_NS_fcntl.h"
7 
8 const std::string
10 const std::string
12 
15  map_(map)
16 {
17 }
18 
20 int
22  const std::string &, const std::string &,
23  const std::string &)
24 {
25 
26 
27  return 0;
28 }
29 
31 int
32 madara::knowledge::Files::read_file (const char * filename,
33  ACE_Mem_Map & mem_map)
34 {
35  // load the source file into a mapped file of the OS's
36  // virtual memory system
37  ACE_Mem_Map mapped_file (filename);
38  return mem_map.map (filename);
39 }
40 
42 int
44  const std::string & knowledge_key, const std::string & filename)
45 {
46  // return value for function
47  int ret = 0;
48 
49  // Stringstreams for building the name of the files
50  std::stringstream source_filename;
51  std::stringstream dest_filename;
52 
53  // build source file name
54  source_filename << filename;
55 
56  // build dest file name
57  dest_filename << files_folder_;
58  dest_filename << "/";
59  dest_filename << knowledge_key;
60 
62  "Files::read_file : %s->%s\n",
63  source_filename.str ().c_str (), dest_filename.str ().c_str ());
64 
65  // load the source file into a mapped file of the OS's
66  // virtual memory system
67  ACE_Mem_Map mapped_file;
68  read_file (source_filename.str ().c_str (), mapped_file);
69  void * file_contents = mapped_file.addr ();
70  size_t size = mapped_file.size ();
71 
73  "Files::read_file : file size is %d, loaded at 0x%x\n",
74  size, file_contents);
75 
76  if (size > 0)
77  {
78  // obtain the address of the mapped file and write the mapped file
79  // to the destination
80  size_t actual = write_file (dest_filename.str ().c_str (),
81  file_contents, size);
82 
83  // if we actually wrote something to the file, update the specified
84  // knowledge key
85  if (actual > 0)
86  {
87  map_.set (knowledge_key, (madara::knowledge::KnowledgeRecord::Integer) actual);
88 
90  "Files::read_file : file imported with %" PRIu64 " bytes\n", actual);
91  }
92  else ret = -2;
93  }
94  else ret = -1;
95 
96  // unmap the memory mapped file
97  mapped_file.close_filemapping_handle ();
98  mapped_file.close ();
99 
100  if (ret < 0)
101  {
103  "Files::read_file : read file failed with %d\n", ret);
104  }
105 
106  return ret;
107 }
108 
110 ssize_t
112  const std::string & knowledge_key, const std::string & filename)
113 {
114  // return value for function
115  ssize_t ret = 0;
116 
117  // Stringstreams for building the name of the files
118  std::stringstream source_filename;
119  std::stringstream dest_filename;
120 
121  // build source file name
122  source_filename << files_folder_;
123  source_filename << "/";
124  source_filename << knowledge_key;
125 
126  // build dest file name
127  dest_filename << filename;
128 
130  "Files::write_file : %s->%s\n",
131  source_filename.str ().c_str (), dest_filename.str ().c_str ());
132 
133  // load the source file into a mapped file of the OS's
134  // virtual memory system
135  ACE_Mem_Map mapped_file;
136  read_file (source_filename.str ().c_str (), mapped_file);
137  void * file_contents = mapped_file.addr ();
138  size_t size = mapped_file.size ();
139 
141  "Files::write_file : file size is %d, loaded at 0x%x\n",
142  size, file_contents);
143 
144  if (size > 0)
145  {
146  // obtain the address of the mapped file and write the mapped file
147  // to the destination
148  size_t actual = write_file (dest_filename.str ().c_str (),
149  file_contents, size);
150 
151  // if we actually wrote something to the file, update the specified
152  // knowledge key
153  if (actual > 0)
154  {
155  map_.set (knowledge_key, (madara::knowledge::KnowledgeRecord::Integer) actual);
156 
158  "Files::write_file : file saved with %" PRIu64 " bytes\n", actual);
159  }
160  else ret = -2;
161  }
162  else ret = -1;
163 
164  // unmap the memory mapped file
165  mapped_file.close_filemapping_handle ();
166  mapped_file.close ();
167 
168  if (ret < 0)
169  {
171  "Files::write_file : write file failed with %d\n", ret);
172  }
173 
174  return ret;
175 }
176 
178 ssize_t
179 madara::knowledge::Files::write_file (const char * filename,
180  void *& buffer, size_t & size)
181 {
182  // error is -1
183  ssize_t actual = -1;
184 
185  // using ACE for writing to the destination file
186  ACE_HANDLE file_handle = ACE_OS::open (filename,
187  O_RDWR | O_CREAT | O_TRUNC,
188  ACE_DEFAULT_FILE_PERMS);
189 
191  "Files::write_file : beginning write of %" PRIu64 " bytes\n", size);
192 
193  if (file_handle != ACE_INVALID_HANDLE)
194  {
195  // write to the file, save actual bytes read, and close the file handle
196  actual = ACE_OS::write (file_handle, buffer, size);
197  ACE_OS::close (file_handle);
198  }
199 
200  // return the actual bytes written. -1 if error
201  return actual;
202 }
203 
204 
206 int
208  const std::string & policy_key, const std::string & policy_file)
209 {
210 
211  // return value for function
212  int ret = 0;
213 
214  // Stringstreams for building the name of the files
215  std::stringstream source_filename;
216  std::stringstream dest_filename;
217 
218  // build source file name
219  source_filename << policy_file;
220 
221  // build dest file name
222  dest_filename << policies_folder_;
223  dest_filename << "/";
224  dest_filename << policy_key;
225 
227  "Files::read_policy : %s->%s\n",
228  source_filename.str ().c_str (), dest_filename.str ().c_str ());
229 
230  // load the source file into a mapped file of the OS's
231  // virtual memory system
232  ACE_Mem_Map mapped_file;
233  read_file (source_filename.str ().c_str (), mapped_file);
234  void * file_contents = mapped_file.addr ();
235  size_t size = mapped_file.size ();
236 
238  "Files::read_policy : file size is %d, loaded at 0x%x\n",
239  size, file_contents);
240 
241  if (size > 0)
242  {
243  // obtain the address of the mapped file and write the mapped file
244  // to the destination
245  size_t actual = write_file (dest_filename.str ().c_str (),
246  file_contents, size);
247 
248  // if we actually wrote something to the file, update the specified
249  // knowledge key
250  if (actual > 0)
251  {
253 
255  "Files::read_policy : file imported with %zu bytes\n", actual);
256  }
257  else ret = -2;
258  }
259  else ret = -1;
260 
261  // unmap the memory mapped file
262  mapped_file.close_filemapping_handle ();
263  mapped_file.close ();
264 
265  if (ret < 0)
266  {
268  "Files::read_policy : read file failed with %d\n", ret);
269  }
270 
271  return ret;
272 }
273 
274 #endif
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
ThreadSafeContext & map_
Definition: Files.h:94
static const std::string files_folder_
Definition: Files.h:88
MADARA_Export int read_policy(const std::string &policy_key, const std::string &policy_file)
Read a policy into the knowledge base.
Definition: Files.cpp:207
static const std::string policies_folder_
Definition: Files.h:91
MADARA_Export int shape_file(const std::string &source_key, const std::string &target_key, const std::string &policy_key)
Shape a file according to a shaping policy.
Definition: Files.cpp:21
MADARA_Export int read_file(const std::string &knowledge_key, const std::string &filename)
Read a file into the knowledge base.
Definition: Files.cpp:43
This class stores variables and their values for use by any entity needing state information in a thr...
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
Files(ThreadSafeContext &map)
Constructor.
Definition: Files.cpp:14
MADARA_Export ssize_t write_file(const std::string &knowledge_key, const std::string &filename)
Write a file from the knowledge base to a specified location.
Definition: Files.cpp:111
static constexpr struct madara::knowledge::tags::string_t string
logger::Logger & get_logger(void) const
Gets the logger used for information printing.