GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/sink_file.h
Date: 2024-04-28 02:33:07
Exec Total Coverage
Lines: 11 13 84.6%
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 73 explicit FileSink(FILE *destination_file) : Sink(false),
28 73 file_(destination_file) { }
29 5 FileSink(FILE *destination_file, bool is_owner) : Sink(is_owner),
30 5 file_(destination_file) { }
31
32
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
164 virtual ~FileSink() { if (is_owner_ && file_) { (void) fclose(file_); } }
33
34 /**
35 * Appends data to the sink
36 *
37 * @returns on success: number of bytes written (can be less than requested)
38 * on failure: -errno.
39 */
40 virtual int64_t Write(const void *buf, uint64_t sz);
41
42 /**
43 * Truncate all written data and start over at position zero.
44 *
45 * @returns Success = 0
46 * Failure = -1
47 */
48 virtual int Reset();
49
50 virtual int Purge();
51
52 /**
53 * @returns true if the object is correctly initialized.
54 */
55 81 virtual bool IsValid() {
56 81 return file_ != NULL;
57 }
58
59 /**
60 * Commit data to the sink
61 * @returns success = 0
62 * failure = -errno
63 */
64 70 virtual int Flush() {
65
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
70 return fflush(file_) == 0 ? 0 : -errno;
66 }
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*/) {
85 return true;
86 }
87
88 /**
89 * Returns if the specific sink type needs reservation of (data) space
90 *
91 * @returns true - reservation is needed
92 * false - no reservation is needed
93 */
94 274 virtual bool RequiresReserve() {
95 274 return false;
96 }
97
98 /**
99 * Return a string representation describing the type of sink and its status
100 */
101 virtual std::string Describe();
102
103 /**
104 * Allows the sink to adopt data that was initialized outside this class.
105 * The sink can become the new owner of the data, or not.
106 */
107 void Adopt(FILE *file, bool is_owner = true);
108
109 FILE* file() { return file_; }
110
111
112
113 private:
114 FILE *file_;
115 };
116
117 } // namespace cvmfs
118
119 #endif // CVMFS_NETWORK_SINK_FILE_H_
120