Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
file.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19/*
20 * Copyright 2020 ScyllaDB
21 */
22
23#pragma once
24
25#include "modules.hh"
26#include <seastar/core/seastar.hh>
27#include <seastar/core/future.hh>
29#include <seastar/core/sstring.hh>
30#include <seastar/core/coroutine.hh>
31#include <seastar/coroutine/as_future.hh>
32#include <seastar/util/std-compat.hh>
33#include <seastar/util/short_streams.hh>
34#include <seastar/util/modules.hh>
35
36namespace seastar {
37
51SEASTAR_MODULE_EXPORT
52future<> recursive_remove_directory(std::filesystem::path path) noexcept;
53
55
61
62namespace util {
63
66
67SEASTAR_MODULE_EXPORT_BEGIN
68template <typename Func>
69requires std::invocable<Func, input_stream<char>&>
70typename futurize<typename std::invoke_result_t<Func, input_stream<char>&>>::type with_file_input_stream(const std::filesystem::path& path, Func func, file_open_options file_opts = {}, file_input_stream_options input_stream_opts = {}) {
71 static_assert(std::is_nothrow_move_constructible_v<Func>);
72 auto f = co_await open_file_dma(path.native(), open_flags::ro, std::move(file_opts));
74 std::exception_ptr ex;
75 try {
76 in = make_file_input_stream(f, std::move(input_stream_opts));
77 } catch (...) {
78 ex = std::current_exception();
79 }
80 if (ex) {
81 co_await f.close();
82 co_await coroutine::return_exception_ptr(std::move(ex));
83 }
84
85 auto res = co_await coroutine::as_future(futurize_invoke(std::move(func), in));
86 co_await in.close();
87 co_await f.close();
88 co_return co_await std::move(res);
89}
90
97
104
105SEASTAR_MODULE_EXPORT_END
107
108} // namespace util
109
110} // namespace seastar
co_await:s a future, returning it as result.
Definition: as_future.hh:86
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
future close() noexcept
Definition: iostream.hh:353
future< std::vector< temporary_buffer< char > > > read_entire_file(std::filesystem::path path)
future< sstring > read_entire_file_contiguous(std::filesystem::path path)
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
future recursive_remove_directory(std::filesystem::path path) noexcept
input_stream< char > make_file_input_stream(file file, uint64_t offset, uint64_t len, file_input_stream_options options={})
Creates an input_stream to read a portion of a file.
Data structure describing options for opening a file input stream.
Definition: fstream.hh:60
Definition: file.hh:96
Converts a type to a future type, if it isn't already.
Definition: future.hh:1853