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
8#include <filesystem>
9#include <fstream>
10
11namespace asynchost
12{
14 {
15 const std::filesystem::path root_dir = ".index";
16
18
20 {
21 if (std::filesystem::is_directory(root_dir))
22 {
23 LOG_INFO_FMT("Clearing contents from existing directory {}", root_dir);
24 std::filesystem::remove_all(root_dir);
25 }
26
27 if (!std::filesystem::create_directory(root_dir))
28 {
29 throw std::logic_error(
30 fmt::format("Could not create directory: {}", root_dir));
31 }
32 }
33
35 {
37 disp,
38 ccf::indexing::LFSMsg::store,
39 [&](const uint8_t* data, size_t size) {
40 auto [key, encrypted] =
41 ringbuffer::read_message<ccf::indexing::LFSMsg::store>(data, size);
42
43 const auto target_path = root_dir / key;
44 std::ofstream f(target_path, std::ios::trunc | std::ios::binary);
46 "Writing {} byte file to {}", encrypted.size(), target_path);
47 f.write((char const*)encrypted.data(), encrypted.size());
48 f.close();
49 });
50
52 disp,
53 ccf::indexing::LFSMsg::get,
54 [&](const uint8_t* data, size_t size) {
55 auto [key] =
56 ringbuffer::read_message<ccf::indexing::LFSMsg::get>(data, size);
57
58 const auto target_path = root_dir / key;
59 if (std::filesystem::is_regular_file(target_path))
60 {
61 std::ifstream f(target_path, std::ios::binary);
62 f.seekg(0, f.end);
63 const auto file_size = f.tellg();
65 "Reading {} byte file from {}",
66 static_cast<size_t>(file_size),
67 target_path);
68 f.seekg(0, f.beg);
69
71 f.read((char*)blob.data(), blob.size());
72 f.close();
74 ccf::indexing::LFSMsg::response, writer, key, blob);
75 }
76 else
77 {
78 LOG_TRACE_FMT("File {} not found", target_path);
80 ccf::indexing::LFSMsg::not_found, writer, key);
81 }
82 });
83 }
84 };
85}
#define LOG_INFO_FMT
Definition logger.h:362
#define LOG_TRACE_FMT
Definition logger.h:356
#define DISPATCHER_SET_MESSAGE_HANDLER(DISP, MSG,...)
Definition messaging.h:316
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:150
#define RINGBUFFER_WRITE_MESSAGE(MSG,...)
Definition ring_buffer_types.h:255
Definition lfs_file_handler.h:14
LFSFileHandler(ringbuffer::WriterPtr &&w)
Definition lfs_file_handler.h:19
const std::filesystem::path root_dir
Definition lfs_file_handler.h:15
void register_message_handlers(messaging::RingbufferDispatcher &disp)
Definition lfs_file_handler.h:34
ringbuffer::WriterPtr writer
Definition lfs_file_handler.h:17