Directory: | cvmfs/ |
---|---|
File: | cvmfs/network/sink_file.cc |
Date: | 2025-04-20 02:34:28 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 15 | 26 | 57.7% |
Branches: | 8 | 26 | 30.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | */ | ||
4 | |||
5 | #include <cerrno> | ||
6 | #include <cstdio> | ||
7 | #include <string> | ||
8 | |||
9 | #include "sink_file.h" | ||
10 | |||
11 | namespace cvmfs { | ||
12 | |||
13 | /** | ||
14 | * Appends data to the sink | ||
15 | * | ||
16 | * @returns on success: number of bytes written | ||
17 | * on failure: -errno. | ||
18 | */ | ||
19 | 149 | int64_t FileSink::Write(const void *buf, uint64_t sz) { | |
20 | 149 | size_t ret = fwrite(buf, 1ul, sz, file_); | |
21 | |||
22 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 149 times.
|
149 | if (ferror(file_) != 0) { |
23 | // ferror does not tell us what exactly the error is | ||
24 | // and errno is also not set | ||
25 | // so just return generic I/O error flag | ||
26 | ✗ | return -EIO; | |
27 | } | ||
28 | |||
29 | 149 | return static_cast<int64_t>(ret); | |
30 | } | ||
31 | |||
32 | /** | ||
33 | * Truncate all written data and start over at position zero. | ||
34 | * | ||
35 | * @returns Success = 0 | ||
36 | * Failure = -1 | ||
37 | */ | ||
38 | 14 | int FileSink::Reset() { | |
39 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
28 | return ((fflush(file_) == 0) && |
40 | 14 | (ftruncate(fileno(file_), 0) == 0) && | |
41 |
2/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
|
28 | (freopen(NULL, "w", file_) == file_)) ? 0 : -errno; |
42 | } | ||
43 | |||
44 | /** | ||
45 | * Purges all resources leaving the sink in an invalid state. | ||
46 | * More aggressive version of Reset(). | ||
47 | * For some sinks and depending on owner status it might do | ||
48 | * the same as Reset(). | ||
49 | * | ||
50 | * @returns Success = 0 | ||
51 | * Failure = -errno | ||
52 | */ | ||
53 | 9 | int FileSink::Purge() { | |
54 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
9 | if (is_owner_ && file_) { |
55 | 2 | int ret = fclose(file_); | |
56 | 2 | file_ = NULL; | |
57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret != 0) { |
58 | ✗ | return -errno; | |
59 | } | ||
60 | |||
61 | 2 | return 0; | |
62 | } else { | ||
63 | 7 | return Reset(); | |
64 | } | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * Return a string representation describing the type of sink and its status | ||
69 | */ | ||
70 | ✗ | std::string FileSink::Describe() { | |
71 | ✗ | std::string result = "File sink with "; | |
72 | ✗ | result += IsValid() ? " valid file pointer" : " invalid file pointer"; | |
73 | ✗ | return result; | |
74 | } | ||
75 | ✗ | void FileSink::Adopt(FILE *file, bool is_owner) { | |
76 | ✗ | if (is_owner_ && file_) { | |
77 | ✗ | (void) fclose(file_); | |
78 | } | ||
79 | |||
80 | ✗ | is_owner_ = is_owner; | |
81 | ✗ | file_ = file; | |
82 | } | ||
83 | } // namespace cvmfs | ||
84 |