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