Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#ifndef CVMFS_NETWORK_SINK_H_ |
6 |
|
|
#define CVMFS_NETWORK_SINK_H_ |
7 |
|
|
|
8 |
|
|
#include <stdint.h> |
9 |
|
|
#include <string> |
10 |
|
|
|
11 |
|
|
namespace cvmfs { |
12 |
|
|
|
13 |
|
|
/** |
14 |
|
|
* A data sink that behaves like a writable file descriptor with a custom |
15 |
|
|
* implementation. |
16 |
|
|
*/ |
17 |
|
|
class Sink { |
18 |
|
|
protected: |
19 |
|
227 |
explicit Sink(bool is_owner) : is_owner_(is_owner) { } |
20 |
|
|
|
21 |
|
|
public: |
22 |
|
454 |
virtual ~Sink() { } |
23 |
|
|
/** |
24 |
|
|
* Appends data to the sink |
25 |
|
|
* |
26 |
|
|
* @returns on success: number of bytes written (can be less than requested) |
27 |
|
|
* on failure: -errno. |
28 |
|
|
*/ |
29 |
|
|
virtual int64_t Write(const void *buf, uint64_t sz) = 0; |
30 |
|
|
|
31 |
|
|
/** |
32 |
|
|
* Truncate all written data and start over at position zero. |
33 |
|
|
* |
34 |
|
|
* @returns Success = 0 |
35 |
|
|
* Failure = -errno |
36 |
|
|
*/ |
37 |
|
|
virtual int Reset() = 0; |
38 |
|
|
|
39 |
|
|
/** |
40 |
|
|
* Purges all resources leaving the sink in an invalid state. |
41 |
|
|
* More aggressive version of Reset(). |
42 |
|
|
* For some sinks and depending on owner status it might do |
43 |
|
|
* the same as Reset(). |
44 |
|
|
* |
45 |
|
|
* @returns Success = 0 |
46 |
|
|
* Failure = -errno |
47 |
|
|
*/ |
48 |
|
|
virtual int Purge() = 0; |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* @returns true if the object is correctly initialized. |
52 |
|
|
*/ |
53 |
|
|
virtual bool IsValid() = 0; |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* Release ownership of the sink's resource |
57 |
|
|
*/ |
58 |
|
74 |
void Release() { is_owner_ = false; } |
59 |
|
|
|
60 |
|
|
/** |
61 |
|
|
* Commit data to the sink |
62 |
|
|
* @returns Success = 0 |
63 |
|
|
* Failure = -errno |
64 |
|
|
*/ |
65 |
|
|
virtual int Flush() = 0; |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* Reserves new space in sinks that require reservation (see RequiresReserve) |
69 |
|
|
* |
70 |
|
|
* Successful if the requested size is smaller than already space reserved, or |
71 |
|
|
* if the sink is the owner of the data and can allocate enough new space. |
72 |
|
|
* |
73 |
|
|
* @note If successful, always resets the current position to 0. |
74 |
|
|
* |
75 |
|
|
* Fails if |
76 |
|
|
* 1) sink is not the owner of the data and more than the current size is |
77 |
|
|
* requested |
78 |
|
|
* 2) more space is requested than allowed (max_size_) |
79 |
|
|
* |
80 |
|
|
* @returns success = true |
81 |
|
|
* failure = false |
82 |
|
|
*/ |
83 |
|
|
virtual bool Reserve(size_t size) = 0; |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* Returns if the specific sink type needs reservation of (data) space |
87 |
|
|
* |
88 |
|
|
* @returns true - reservation is needed |
89 |
|
|
* false - no reservation is needed |
90 |
|
|
*/ |
91 |
|
|
virtual bool RequiresReserve() = 0; |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* Return a string representation describing the type of sink and its status |
95 |
|
|
*/ |
96 |
|
|
virtual std::string Describe() = 0; |
97 |
|
|
|
98 |
|
|
protected: |
99 |
|
|
bool is_owner_; |
100 |
|
|
}; |
101 |
|
|
|
102 |
|
|
} // namespace cvmfs |
103 |
|
|
|
104 |
|
|
#endif // CVMFS_NETWORK_SINK_H_ |
105 |
|
|
|