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