1
#pragma once
2

            
3
#include <memory>
4
#include <string>
5

            
6
#include "envoy/event/dispatcher.h"
7

            
8
#include "source/common/buffer/buffer_impl.h"
9
#include "source/extensions/common/async_files/async_file_action.h"
10

            
11
#include "absl/status/statusor.h"
12

            
13
namespace Envoy {
14
namespace Extensions {
15
namespace Common {
16
namespace AsyncFiles {
17

            
18
// The context for AsyncFile operations that operate on an open file.
19
// Instantiated from an AsyncFileManager.
20
class AsyncFileContext : public std::enable_shared_from_this<AsyncFileContext> {
21
public:
22
  // Gets a stat struct for the file.
23
  virtual absl::StatusOr<CancelFunction>
24
  stat(Event::Dispatcher* dispatcher,
25
       absl::AnyInvocable<void(absl::StatusOr<struct stat>)> on_complete) PURE;
26

            
27
  // Action to hard link the file that is currently open. Typically for use in tandem with
28
  // createAnonymousFile to turn that file into a named file after finishing writing its contents.
29
  //
30
  // If cancelled before the callback is called but after creating the file, unlinks the file.
31
  virtual absl::StatusOr<CancelFunction>
32
  createHardLink(Event::Dispatcher* dispatcher, absl::string_view filename,
33
                 absl::AnyInvocable<void(absl::Status)> on_complete) PURE;
34

            
35
  // Enqueues an action to close the currently open file.
36
  // It is an error to use an AsyncFileContext after calling close.
37
  // It is an error to destroy an AsyncFileHandle without calling close.
38
  // It is an error to call close twice.
39
  // Note that because an AsyncFileHandle is a shared_ptr, it is okay to
40
  // call close during the handle's owner's destructor - that will queue the close
41
  // event, which will keep the handle alive until after the close operation
42
  // is completed.
43
  // Cancelling close can abort the callback, but the close action will always complete.
44
  virtual absl::StatusOr<CancelFunction>
45
  close(Event::Dispatcher* dispatcher, absl::AnyInvocable<void(absl::Status)> on_complete) PURE;
46

            
47
  // Enqueues an action to read from the currently open file, at position offset, up to the number
48
  // of bytes specified by length. The size of the buffer passed to on_complete informs you if less
49
  // than the requested amount was read. It is an error to read on an AsyncFileContext that does not
50
  // have a file open. There must not already be an action queued for this handle.
51
  virtual absl::StatusOr<CancelFunction>
52
  read(Event::Dispatcher* dispatcher, off_t offset, size_t length,
53
       absl::AnyInvocable<void(absl::StatusOr<Buffer::InstancePtr>)> on_complete) PURE;
54

            
55
  // Enqueues an action to write to the currently open file, at position offset, the bytes contained
56
  // by contents. It is an error to call write on an AsyncFileContext that does not have a file
57
  // open.
58
  //
59
  // This call consumes the 'contents' buffer immediately, by move, so it is safe to discard the
60
  // buffer after the call, and is not safe to assume it still contains the same data.
61
  //
62
  // on_complete is called with the number of bytes written on success.
63
  // There must not already be an action queued for this handle.
64
  virtual absl::StatusOr<CancelFunction>
65
  write(Event::Dispatcher* dispatcher, Buffer::Instance& contents, off_t offset,
66
        absl::AnyInvocable<void(absl::StatusOr<size_t>)> on_complete) PURE;
67

            
68
  // Creates a new AsyncFileHandle referencing the same file.
69
  // Note that a file handle duplicated in this way shares positioning and permissions
70
  // with the original. Since AsyncFileContext functions are all position-explicit, this should not
71
  // matter.
72
  virtual absl::StatusOr<CancelFunction> duplicate(
73
      Event::Dispatcher* dispatcher,
74
      absl::AnyInvocable<void(absl::StatusOr<std::shared_ptr<AsyncFileContext>>)> on_complete) PURE;
75

            
76
  virtual absl::StatusOr<CancelFunction>
77
  truncate(Event::Dispatcher* dispatcher, size_t length,
78
           absl::AnyInvocable<void(absl::Status)> on_complete) PURE;
79

            
80
protected:
81
238
  virtual ~AsyncFileContext() = default;
82
};
83

            
84
using AsyncFileHandle = std::shared_ptr<AsyncFileContext>;
85

            
86
} // namespace AsyncFiles
87
} // namespace Common
88
} // namespace Extensions
89
} // namespace Envoy