GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/sink_file.h
Date: 2025-06-22 02:36:02
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 #include "util/posix.h"
14
15 namespace cvmfs {
16
17 /**
18 * FileSink is a data sink that write to a given FILE.
19 * It does not require any space reservation. It can change to which file to
20 * write to using Adopt().
21 *
22 * By default FileSink is not the owner of FILE, so the outside entity has to
23 * take care of opening and closing the FILE.
24 */
25 class FileSink : public Sink {
26 public:
27 3007 explicit FileSink(FILE *destination_file)
28 3007 : Sink(false), file_(destination_file) { }
29 240 FileSink(FILE *destination_file, bool is_owner)
30 240 : Sink(is_owner), file_(destination_file) { }
31
32 6878 virtual ~FileSink() {
33
4/4
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 3007 times.
✓ Branch 2 taken 96 times.
✓ Branch 3 taken 144 times.
6494 if (is_owner_ && file_) {
34 192 (void)fclose(file_);
35 }
36 6878 }
37
38 /**
39 * Appends data to the sink
40 *
41 * @returns on success: number of bytes written (can be less than requested)
42 * on failure: -errno.
43 */
44 virtual int64_t Write(const void *buf, uint64_t sz);
45
46 /**
47 * Truncate all written data and start over at position zero.
48 *
49 * @returns Success = 0
50 * Failure = -1
51 */
52 virtual int Reset();
53
54 virtual int Purge();
55
56 /**
57 * @returns true if the object is correctly initialized.
58 */
59 3392 virtual bool IsValid() { return file_ != NULL; }
60
61 /**
62 * Commit data to the sink
63 * @returns success = 0
64 * failure = -errno
65 */
66
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2864 times.
2864 virtual int Flush() { return fflush(file_) == 0 ? 0 : -errno; }
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 true; }
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 11264 virtual bool RequiresReserve() { return false; }
93
94 /**
95 * Return a string representation describing the type of sink and its status
96 */
97 virtual std::string Describe();
98
99 /**
100 * Allows the sink to adopt data that was initialized outside this class.
101 * The sink can become the new owner of the data, or not.
102 */
103 void Adopt(FILE *file, bool is_owner = true);
104
105 FILE *file() { return file_; }
106
107
108 private:
109 FILE *file_;
110 };
111
112 } // namespace cvmfs
113
114 #endif // CVMFS_NETWORK_SINK_FILE_H_
115