Line |
Branch |
Exec |
Source |
1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#ifndef CVMFS_NETWORK_SINK_PATH_H_ |
6 |
|
|
#define CVMFS_NETWORK_SINK_PATH_H_ |
7 |
|
|
|
8 |
|
|
#include <cerrno> |
9 |
|
|
#include <cstdio> |
10 |
|
|
#include <string> |
11 |
|
|
|
12 |
|
|
#include "sink.h" |
13 |
|
|
#include "sink_file.h" |
14 |
|
|
#include "util/pointer.h" |
15 |
|
|
|
16 |
|
|
namespace cvmfs { |
17 |
|
|
|
18 |
|
|
/** |
19 |
|
|
* PathSink is a data sink that writes to a file given by a path. |
20 |
|
|
* |
21 |
|
|
* Internally it uses a FileSink that has ownership of the file. |
22 |
|
|
* (Though as PathSink is owner of the FileSink, to the outside also PathSink |
23 |
|
|
* is considered to be the owner.) |
24 |
|
|
* |
25 |
|
|
* Like FileSink, PathSink does not require to reserve space. |
26 |
|
|
* Contrary to FileSink, PathSink does not allow to adopt and write to a |
27 |
|
|
* different file path. |
28 |
|
|
*/ |
29 |
|
|
class PathSink : public Sink { |
30 |
|
|
public: |
31 |
|
|
explicit PathSink(const std::string &destination_path); |
32 |
|
|
|
33 |
|
8 |
virtual ~PathSink() { } // UniquePtr<FileSink> sink_ takes care of everything |
34 |
|
|
|
35 |
|
|
/** |
36 |
|
|
* Appends data to the sink |
37 |
|
|
* |
38 |
|
|
* @returns on success: number of bytes written (can be less than requested) |
39 |
|
|
* on failure: -errno. |
40 |
|
|
*/ |
41 |
|
3 |
virtual int64_t Write(const void *buf, uint64_t sz) { |
42 |
|
3 |
return sink_->Write(buf, sz); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* Truncate all written data and start over at position zero. |
47 |
|
|
* |
48 |
|
|
* @returns Success = 0 |
49 |
|
|
* Failure = -errno |
50 |
|
|
*/ |
51 |
|
1 |
virtual int Reset() { |
52 |
|
1 |
return sink_->Reset(); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
virtual int Purge(); |
56 |
|
|
|
57 |
|
|
/** |
58 |
|
|
* @returns true if the object is correctly initialized. |
59 |
|
|
*/ |
60 |
|
5 |
virtual bool IsValid() { |
61 |
|
5 |
return sink_->IsValid(); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
/** |
65 |
|
|
* Commit data to the sink |
66 |
|
|
* @returns success = 0 |
67 |
|
|
* failure = -errno |
68 |
|
|
*/ |
69 |
|
✗ |
virtual int Flush() { |
70 |
|
✗ |
return sink_->Flush(); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* Reserves new space in sinks that require reservation (see RequiresReserve) |
75 |
|
|
* |
76 |
|
|
* Successful if the requested size is smaller than already space reserved, or |
77 |
|
|
* if the sink is the owner of the data and can allocate enough new space. |
78 |
|
|
* |
79 |
|
|
* @note If successful, always resets the current position to 0. |
80 |
|
|
* |
81 |
|
|
* Fails if |
82 |
|
|
* 1) sink is not the owner of the data and more than the current size is |
83 |
|
|
* requested |
84 |
|
|
* 2) more space is requested than allowed (max_size_) |
85 |
|
|
* |
86 |
|
|
* @returns success = true |
87 |
|
|
* failure = false |
88 |
|
|
*/ |
89 |
|
✗ |
virtual bool Reserve(size_t size) { |
90 |
|
✗ |
return sink_->Reserve(size); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* Returns if the specific sink type needs reservation of (data) space |
95 |
|
|
* |
96 |
|
|
* @returns true - reservation is needed |
97 |
|
|
* false - no reservation is needed |
98 |
|
|
*/ |
99 |
|
✗ |
virtual bool RequiresReserve() { |
100 |
|
✗ |
return sink_->RequiresReserve(); |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* Return a string representation describing the type of sink and its status |
105 |
|
|
*/ |
106 |
|
|
virtual std::string Describe(); |
107 |
|
|
|
108 |
|
✗ |
const std::string path() { return path_; } |
109 |
|
|
|
110 |
|
|
private: |
111 |
|
|
FILE *file_; // owned by sink_ |
112 |
|
|
UniquePtr<FileSink> sink_; |
113 |
|
|
const std::string path_; |
114 |
|
|
}; |
115 |
|
|
|
116 |
|
|
} // namespace cvmfs |
117 |
|
|
|
118 |
|
|
#endif // CVMFS_NETWORK_SINK_PATH_H_ |
119 |
|
|
|