GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/sink_file.h
Date: 2026-04-26 02:35:59
Exec Total Coverage
Lines: 11 12 91.7%
Branches: 5 6 83.3%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_NETWORK_SINK_FILE_H_
6 #define CVMFS_NETWORK_SINK_FILE_H_
7
8 #include <cerrno>
9 #include <cstdio>
10 #include <string>
11
12 #include "sink.h"
13 // IWYU pragma: keep
14 #include "util/posix.h"
15
16 namespace cvmfs {
17
18 /**
19 * FileSink is a data sink that write to a given FILE.
20 * It does not require any space reservation. It can change to which file to
21 * write to using Adopt().
22 *
23 * By default FileSink is not the owner of FILE, so the outside entity has to
24 * take care of opening and closing the FILE.
25 */
26 class FileSink : public Sink {
27 public:
28 1719 explicit FileSink(FILE *destination_file)
29 1719 : Sink(false), file_(destination_file) { }
30 175 FileSink(FILE *destination_file, bool is_owner)
31 175 : Sink(is_owner), file_(destination_file) { }
32
33 4068 virtual ~FileSink() {
34
4/4
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 1719 times.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 105 times.
3788 if (is_owner_ && file_) {
35 140 (void)fclose(file_);
36 }
37 4068 }
38
39 /**
40 * Appends data to the sink
41 *
42 * @returns on success: number of bytes written (can be less than requested)
43 * on failure: -errno.
44 */
45 virtual int64_t Write(const void *buf, uint64_t sz);
46
47 /**
48 * Truncate all written data and start over at position zero.
49 *
50 * @returns Success = 0
51 * Failure = -1
52 */
53 virtual int Reset();
54
55 virtual int Purge();
56
57 /**
58 * @returns true if the object is correctly initialized.
59 */
60 2008 virtual bool IsValid() { return file_ != NULL; }
61
62 /**
63 * Commit data to the sink
64 * @returns success = 0
65 * failure = -errno
66 */
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1623 times.
1623 virtual int Flush() { return fflush(file_) == 0 ? 0 : -errno; }
68
69 /**
70 * Reserves new space in sinks that require reservation (see RequiresReserve)
71 *
72 * Successful if the requested size is smaller than already space reserved, or
73 * if the sink is the owner of the data and can allocate enough new space.
74 *
75 * @note If successful, always resets the current position to 0.
76 *
77 * Fails if
78 * 1) sink is not the owner of the data and more than the current size is
79 * requested
80 * 2) more space is requested than allowed (max_size_)
81 *
82 * @returns success = true
83 * failure = false
84 */
85 virtual bool Reserve(size_t /*size*/) { return true; }
86
87 /**
88 * Returns if the specific sink type needs reservation of (data) space
89 *
90 * @returns true - reservation is needed
91 * false - no reservation is needed
92 */
93 6468 virtual bool RequiresReserve() { return false; }
94
95 /**
96 * Return a string representation describing the type of sink and its status
97 */
98 virtual std::string Describe();
99
100 /**
101 * Allows the sink to adopt data that was initialized outside this class.
102 * The sink can become the new owner of the data, or not.
103 */
104 void Adopt(FILE *file, bool is_owner = true);
105
106 FILE *file() { return file_; }
107
108
109 private:
110 FILE *file_;
111 };
112
113 } // namespace cvmfs
114
115 #endif // CVMFS_NETWORK_SINK_FILE_H_
116