xref: /aosp_15_r20/external/puffin/src/include/puffin/file_stream.h (revision 07fb1d065b7cfb4729786fadd42a612532d2f466)
1*07fb1d06SElliott Hughes // Copyright 2017 The ChromiumOS Authors
2*07fb1d06SElliott Hughes // Use of this source code is governed by a BSD-style license that can be
3*07fb1d06SElliott Hughes // found in the LICENSE file.
4*07fb1d06SElliott Hughes 
5*07fb1d06SElliott Hughes #ifndef SRC_FILE_STREAM_H_
6*07fb1d06SElliott Hughes #define SRC_FILE_STREAM_H_
7*07fb1d06SElliott Hughes 
8*07fb1d06SElliott Hughes #include <string>
9*07fb1d06SElliott Hughes #include <utility>
10*07fb1d06SElliott Hughes 
11*07fb1d06SElliott Hughes #include "puffin/common.h"
12*07fb1d06SElliott Hughes #include "puffin/stream.h"
13*07fb1d06SElliott Hughes 
14*07fb1d06SElliott Hughes namespace puffin {
15*07fb1d06SElliott Hughes 
16*07fb1d06SElliott Hughes // A very simple class for reading and writing data into a file descriptor.
17*07fb1d06SElliott Hughes class FileStream : public StreamInterface {
18*07fb1d06SElliott Hughes  public:
FileStream(int fd)19*07fb1d06SElliott Hughes   explicit FileStream(int fd) : fd_(fd) { Seek(0); }
20*07fb1d06SElliott Hughes   ~FileStream() override = default;
21*07fb1d06SElliott Hughes 
22*07fb1d06SElliott Hughes   static UniqueStreamPtr Open(const std::string& path, bool read, bool write);
23*07fb1d06SElliott Hughes 
24*07fb1d06SElliott Hughes   bool GetSize(uint64_t* size) const override;
25*07fb1d06SElliott Hughes   bool GetOffset(uint64_t* offset) const override;
26*07fb1d06SElliott Hughes   bool Seek(uint64_t offset) override;
27*07fb1d06SElliott Hughes   bool Read(void* buffer, size_t length) override;
28*07fb1d06SElliott Hughes   bool Write(const void* buffer, size_t length) override;
29*07fb1d06SElliott Hughes   bool Close() override;
30*07fb1d06SElliott Hughes 
31*07fb1d06SElliott Hughes  protected:
32*07fb1d06SElliott Hughes   FileStream() = default;
33*07fb1d06SElliott Hughes 
34*07fb1d06SElliott Hughes  private:
35*07fb1d06SElliott Hughes   // The file descriptor.
36*07fb1d06SElliott Hughes   int fd_;
37*07fb1d06SElliott Hughes 
38*07fb1d06SElliott Hughes   DISALLOW_COPY_AND_ASSIGN(FileStream);
39*07fb1d06SElliott Hughes };
40*07fb1d06SElliott Hughes 
41*07fb1d06SElliott Hughes }  // namespace puffin
42*07fb1d06SElliott Hughes 
43*07fb1d06SElliott Hughes #endif  // SRC_FILE_STREAM_H_
44