blob: 616a8825b745d296eb1ff9d016da9c89e3008725 [file] [log] [blame]
Patrick Venturea78e39f2018-11-06 18:37:06 -08001#include "static_handler.hpp"
2
Patrick Venture400c6362018-11-06 20:06:11 -08003#include <cstdint>
Patrick Venturea78e39f2018-11-06 18:37:06 -08004#include <memory>
5#include <string>
Patrick Venture400c6362018-11-06 20:06:11 -08006#include <vector>
Patrick Venturea78e39f2018-11-06 18:37:06 -08007
8namespace blobs
9{
10
11bool StaticLayoutHandler::open(const std::string& path)
12{
13 this->path = path;
Patrick Venturea17cf442018-11-15 09:31:51 -080014
15 if (stagedOutput.is_open())
16 {
17 /* This wasn't properly closed somehow.
18 * TODO: Throw an error or just reset the state?
19 */
20 return false;
21 }
22
23 /* using ofstream no need to set out */
24 stagedOutput.open(stagedFilename, std::ios::binary);
25 if (stagedOutput.bad())
26 {
27 /* TODO: Oh no! Care about this. */
28 return false;
29 }
30
31 /* We were able to open the file for staging.
32 * TODO: We'll need to do other stuff to eventually.
33 */
34 return true;
Patrick Venturea78e39f2018-11-06 18:37:06 -080035}
36
Patrick Venture68bb1432018-11-09 20:08:48 -080037void StaticLayoutHandler::close()
38{
Patrick Venturea17cf442018-11-15 09:31:51 -080039 if (stagedOutput.is_open())
40 {
41 stagedOutput.close();
42 }
Patrick Venture68bb1432018-11-09 20:08:48 -080043 return;
44}
45
Patrick Venture400c6362018-11-06 20:06:11 -080046bool StaticLayoutHandler::write(std::uint32_t offset,
47 const std::vector<std::uint8_t>& data)
48{
Patrick Venturea17cf442018-11-15 09:31:51 -080049 if (!stagedOutput.is_open())
50 {
51 return false;
52 }
53
54 /* We could track this, but if they write in a scattered method, this is
55 * easier.
56 */
57 stagedOutput.seekp(offset, std::ios_base::beg);
58 if (!stagedOutput.good())
59 {
60 /* the documentation wasn't super clear on fail vs bad in these cases,
61 * so let's only be happy with goodness.
62 */
63 return false;
64 }
65
66 stagedOutput.write(reinterpret_cast<const char*>(data.data()), data.size());
67 if (!stagedOutput.good())
68 {
69 return false;
70 }
71
72 return true;
Patrick Venture400c6362018-11-06 20:06:11 -080073}
74
Patrick Venturea78e39f2018-11-06 18:37:06 -080075} // namespace blobs