GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/sink_path.h
Date: 2026-08-30 02:40:36
Exec Total Coverage
Lines: 6 10 60.0%
Branches: 0 0 -%

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 <memory>
11 #include <string>
12
13 #include "sink.h"
14 #include "sink_file.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 304 virtual ~PathSink() {
34 304 } // std::unique_ptr<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 114 virtual int64_t Write(const void *buf, uint64_t sz) {
43 114 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 38 virtual int Reset() { return sink_->Reset(); }
53
54 virtual int Purge();
55
56 /**
57 * @returns true if the object is correctly initialized.
58 */
59 190 virtual bool IsValid() { return sink_->IsValid(); }
60
61 /**
62 * Commit data to the sink
63 * @returns success = 0
64 * failure = -errno
65 */
66 virtual int Flush() { return sink_->Flush(); }
67
68 /**
69 * Reserves new space in sinks that require reservation (see RequiresReserve)
70 *
71 * Successful if the requested size is smaller than already space reserved, or
72 * if the sink is the owner of the data and can allocate enough new space.
73 *
74 * @note If successful, always resets the current position to 0.
75 *
76 * Fails if
77 * 1) sink is not the owner of the data and more than the current size is
78 * requested
79 * 2) more space is requested than allowed (max_size_)
80 *
81 * @returns success = true
82 * failure = false
83 */
84 virtual bool Reserve(size_t size) { return sink_->Reserve(size); }
85
86 /**
87 * Returns if the specific sink type needs reservation of (data) space
88 *
89 * @returns true - reservation is needed
90 * false - no reservation is needed
91 */
92 virtual bool RequiresReserve() { return sink_->RequiresReserve(); }
93
94 /**
95 * Return a string representation describing the type of sink and its status
96 */
97 virtual std::string Describe();
98
99 const std::string path() { return path_; }
100
101 private:
102 FILE *file_; // owned by sink_
103 std::unique_ptr<FileSink> sink_;
104 const std::string path_;
105 };
106
107 } // namespace cvmfs
108
109 #endif // CVMFS_NETWORK_SINK_PATH_H_
110