CCF
Loading...
Searching...
No Matches
lfs_file_handler.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ds/messaging.h"
7#include "time_bound_logger.h"
8
9#include <filesystem>
10#include <fstream>
11
12namespace asynchost
13{
15 {
16 const std::filesystem::path root_dir = ".index";
17
19
21 {
22 if (std::filesystem::is_directory(root_dir))
23 {
24 LOG_INFO_FMT("Clearing contents from existing directory {}", root_dir);
25 TimeBoundLogger log_if_slow(fmt::format(
26 "Clearing LFS index directory - remove_all({})", root_dir));
27 std::filesystem::remove_all(root_dir);
28 }
29
30 {
31 TimeBoundLogger log_if_slow(fmt::format(
32 "Creating LFS index directory - create_directory({})", root_dir));
33 if (!std::filesystem::create_directory(root_dir))
34 {
35 throw std::logic_error(
36 fmt::format("Could not create directory: {}", root_dir));
37 }
38 }
39 }
40
42 {
44 disp,
45 ccf::indexing::LFSMsg::store,
46 [&](const uint8_t* data, size_t size) {
47 auto [key, encrypted] =
48 ringbuffer::read_message<ccf::indexing::LFSMsg::store>(data, size);
49
50 const auto target_path = root_dir / key;
51 {
52 TimeBoundLogger log_if_slow(fmt::format(
53 "Writing LFS file ({} bytes) - ofstream({})",
54 encrypted.size(),
55 target_path));
56 std::ofstream f(target_path, std::ios::trunc | std::ios::binary);
58 "Writing {} byte file to {}", encrypted.size(), target_path);
59 f.write(
60 reinterpret_cast<char const*>(encrypted.data()),
61 encrypted.size());
62 f.close();
63 }
64 });
65
67 disp,
68 ccf::indexing::LFSMsg::get,
69 [&](const uint8_t* data, size_t size) {
70 auto [key] =
71 ringbuffer::read_message<ccf::indexing::LFSMsg::get>(data, size);
72
73 const auto target_path = root_dir / key;
74 if (std::filesystem::is_regular_file(target_path))
75 {
76 TimeBoundLogger log_if_slow(
77 fmt::format("Reading LFS file - ifstream({})", target_path));
78 std::ifstream f(target_path, std::ios::binary);
79 f.seekg(0, f.end);
80 const auto file_size = f.tellg();
82 "Reading {} byte file from {}",
83 static_cast<size_t>(file_size),
84 target_path);
85 f.seekg(0, f.beg);
86
88 f.read(reinterpret_cast<char*>(blob.data()), blob.size());
89 f.close();
91 ccf::indexing::LFSMsg::response, writer, key, blob);
92 }
93 else
94 {
95 LOG_TRACE_FMT("File {} not found", target_path);
97 ccf::indexing::LFSMsg::not_found, writer, key);
98 }
99 });
100 }
101 };
102}
#define LOG_INFO_FMT
Definition internal_logger.h:15
#define LOG_TRACE_FMT
Definition internal_logger.h:13
#define DISPATCHER_SET_MESSAGE_HANDLER(DISP, MSG,...)
Definition messaging.h:292
Definition after_io.h:8
std::vector< uint8_t > LFSEncryptedContents
Definition lfs_interface.h:18
std::shared_ptr< AbstractWriter > WriterPtr
Definition ring_buffer_types.h:154
#define RINGBUFFER_WRITE_MESSAGE(MSG,...)
Definition ring_buffer_types.h:259
Definition lfs_file_handler.h:15
LFSFileHandler(ringbuffer::WriterPtr &&w)
Definition lfs_file_handler.h:20
const std::filesystem::path root_dir
Definition lfs_file_handler.h:16
void register_message_handlers(messaging::RingbufferDispatcher &disp)
Definition lfs_file_handler.h:41
ringbuffer::WriterPtr writer
Definition lfs_file_handler.h:18
Definition time_bound_logger.h:14