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