1
#pragma once
2

            
3
#include <string>
4

            
5
#include "envoy/filesystem/filesystem.h"
6

            
7
#include "source/common/common/assert.h"
8

            
9
namespace Envoy {
10
namespace Filesystem {
11

            
12
class IoFileError : public Api::IoError {
13
public:
14
22
  explicit IoFileError(int sys_errno) : errno_(sys_errno) {}
15

            
16
19
  ~IoFileError() override = default;
17

            
18
  Api::IoError::IoErrorCode getErrorCode() const override;
19
  std::string getErrorDetails() const override;
20
  int getSystemErrorCode() const override { return errno_; }
21

            
22
private:
23
  const int errno_;
24
};
25

            
26
using IoFileErrorPtr = std::unique_ptr<IoFileError, Api::IoErrorDeleterType>;
27

            
28
19
template <typename T> Api::IoCallResult<T> resultFailure(T result, int sys_errno) {
29
19
  return {result, IoFileErrorPtr(new IoFileError(sys_errno), [](Api::IoError* err) {
30
19
            ASSERT(err != nullptr);
31
19
            delete err;
32
19
          })};
33
19
}
34

            
35
104884
template <typename T> Api::IoCallResult<T> resultSuccess(T result) {
36
104884
  return {result, IoFileErrorPtr(nullptr, [](Api::IoError*) { PANIC("unimplemented"); })};
37
104884
}
38

            
39
class FileSharedImpl : public File {
40
public:
41
  FileSharedImpl(const FilePathAndType& filepath_and_type)
42
41660
      : filepath_and_type_(filepath_and_type) {}
43

            
44
41660
  ~FileSharedImpl() override = default;
45

            
46
  bool isOpen() const override;
47
  absl::string_view path() const override;
48
  DestinationType destinationType() const override;
49

            
50
protected:
51
  filesystem_os_id_t fd_{INVALID_HANDLE};
52
  const FilePathAndType filepath_and_type_;
53
  static std::string generateTmpFilePath(absl::string_view path);
54
};
55

            
56
} // namespace Filesystem
57
} // namespace Envoy