blob: 934b6b3fee88acd38d4108836f95675ac9fa96c6 (
plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#ifndef UTIL_STREAM_IO__
#define UTIL_STREAM_IO__
#include "util/exception.hh"
#include "util/file.hh"
namespace util {
namespace stream {
class ChainPosition;
class ReadSizeException : public util::Exception {
public:
ReadSizeException() throw();
~ReadSizeException() throw();
};
class Read {
public:
explicit Read(int fd) : file_(fd) {}
void Run(const ChainPosition &position);
private:
int file_;
};
// Like read but uses pread so that the file can be accessed from multiple threads.
class PRead {
public:
explicit PRead(int fd, bool take_own = false) : file_(fd), own_(take_own) {}
void Run(const ChainPosition &position);
private:
int file_;
bool own_;
};
class Write {
public:
explicit Write(int fd) : file_(fd) {}
void Run(const ChainPosition &position);
private:
int file_;
};
class WriteAndRecycle {
public:
explicit WriteAndRecycle(int fd) : file_(fd) {}
void Run(const ChainPosition &position);
private:
int file_;
};
// Reuse the same file over and over again to buffer output.
class FileBuffer {
public:
explicit FileBuffer(int fd) : file_(fd) {}
WriteAndRecycle Sink() const {
util::SeekOrThrow(file_.get(), 0);
return WriteAndRecycle(file_.get());
}
PRead Source() const {
return PRead(file_.get());
}
uint64_t Size() const {
return SizeOrThrow(file_.get());
}
private:
scoped_fd file_;
};
} // namespace stream
} // namespace util
#endif // UTIL_STREAM_IO__
|