Line data Source code
1 : #pragma once 2 : 3 : #include <memory> 4 : #include <string> 5 : 6 : #include "source/common/buffer/buffer_impl.h" 7 : #include "source/extensions/common/async_files/async_file_action.h" 8 : 9 : #include "absl/status/statusor.h" 10 : 11 : namespace Envoy { 12 : namespace Extensions { 13 : namespace Common { 14 : namespace AsyncFiles { 15 : 16 : // The context for AsyncFile operations that operate on an open file. 17 : // Instantiated from an AsyncFileManager. 18 : class AsyncFileContext : public std::enable_shared_from_this<AsyncFileContext> { 19 : public: 20 : // Gets a stat struct for the file. 21 : virtual absl::StatusOr<CancelFunction> 22 : stat(std::function<void(absl::StatusOr<struct stat>)> on_complete) PURE; 23 : 24 : // Action to hard link the file that is currently open. Typically for use in tandem with 25 : // createAnonymousFile to turn that file into a named file after finishing writing its contents. 26 : // 27 : // If cancelled before the callback is called but after creating the file, unlinks the file. 28 : virtual absl::StatusOr<CancelFunction> 29 : createHardLink(absl::string_view filename, std::function<void(absl::Status)> on_complete) PURE; 30 : 31 : // Enqueues an action to close the currently open file. 32 : // It is an error to use an AsyncFileContext after calling close. 33 : // It is an error to destroy an AsyncFileHandle without calling close. 34 : // It is an error to call close twice. 35 : // Note that because an AsyncFileHandle is a shared_ptr, it is okay to 36 : // call close during the handle's owner's destructor - that will queue the close 37 : // event, which will keep the handle alive until after the close operation 38 : // is completed. (But be careful that the on_complete callback doesn't require any 39 : // context from the destroyed owner!) 40 : // close cannot be cancelled. 41 : virtual absl::Status close(std::function<void(absl::Status)> on_complete) PURE; 42 : 43 : // Enqueues an action to read from the currently open file, at position offset, up to the number 44 : // of bytes specified by length. The size of the buffer passed to on_complete informs you if less 45 : // than the requested amount was read. It is an error to read on an AsyncFileContext that does not 46 : // have a file open. There must not already be an action queued for this handle. 47 : virtual absl::StatusOr<CancelFunction> 48 : read(off_t offset, size_t length, 49 : std::function<void(absl::StatusOr<Buffer::InstancePtr>)> on_complete) PURE; 50 : 51 : // Enqueues an action to write to the currently open file, at position offset, the bytes contained 52 : // by contents. It is an error to call write on an AsyncFileContext that does not have a file 53 : // open. 54 : // 55 : // This call consumes the 'contents' buffer immediately, by move, so it is safe to discard the 56 : // buffer after the call, and is not safe to assume it still contains the same data. 57 : // 58 : // on_complete is called with the number of bytes written on success. 59 : // There must not already be an action queued for this handle. 60 : virtual absl::StatusOr<CancelFunction> 61 : write(Buffer::Instance& contents, off_t offset, 62 : std::function<void(absl::StatusOr<size_t>)> on_complete) PURE; 63 : 64 : // Creates a new AsyncFileHandle referencing the same file. 65 : // Note that a file handle duplicated in this way shares positioning and permissions 66 : // with the original. Since AsyncFileContext functions are all position-explicit, this should not 67 : // matter. 68 : virtual absl::StatusOr<CancelFunction> duplicate( 69 : std::function<void(absl::StatusOr<std::shared_ptr<AsyncFileContext>>)> on_complete) PURE; 70 : 71 : protected: 72 0 : virtual ~AsyncFileContext() = default; 73 : }; 74 : 75 : using AsyncFileHandle = std::shared_ptr<AsyncFileContext>; 76 : 77 : } // namespace AsyncFiles 78 : } // namespace Common 79 : } // namespace Extensions 80 : } // namespace Envoy