| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* This file is part of the CernVM File System. |
| 3 |
|
|
*/ |
| 4 |
|
|
|
| 5 |
|
|
#ifndef CVMFS_NETWORK_SINK_PATH_H_ |
| 6 |
|
|
#define CVMFS_NETWORK_SINK_PATH_H_ |
| 7 |
|
|
|
| 8 |
|
|
#include <cerrno> |
| 9 |
|
|
#include <cstdio> |
| 10 |
|
|
#include <string> |
| 11 |
|
|
|
| 12 |
|
|
#include "sink.h" |
| 13 |
|
|
#include "sink_file.h" |
| 14 |
|
|
#include "util/pointer.h" |
| 15 |
|
|
|
| 16 |
|
|
namespace cvmfs { |
| 17 |
|
|
|
| 18 |
|
|
/** |
| 19 |
|
|
* PathSink is a data sink that writes to a file given by a path. |
| 20 |
|
|
* |
| 21 |
|
|
* Internally it uses a FileSink that has ownership of the file. |
| 22 |
|
|
* (Though as PathSink is owner of the FileSink, to the outside also PathSink |
| 23 |
|
|
* is considered to be the owner.) |
| 24 |
|
|
* |
| 25 |
|
|
* Like FileSink, PathSink does not require to reserve space. |
| 26 |
|
|
* Contrary to FileSink, PathSink does not allow to adopt and write to a |
| 27 |
|
|
* different file path. |
| 28 |
|
|
*/ |
| 29 |
|
|
class PathSink : public Sink { |
| 30 |
|
|
public: |
| 31 |
|
|
explicit PathSink(const std::string &destination_path); |
| 32 |
|
|
|
| 33 |
|
368 |
virtual ~PathSink() { } // UniquePtr<FileSink> sink_ takes care of everything |
| 34 |
|
|
|
| 35 |
|
|
/** |
| 36 |
|
|
* Appends data to the sink |
| 37 |
|
|
* |
| 38 |
|
|
* @returns on success: number of bytes written (can be less than requested) |
| 39 |
|
|
* on failure: -errno. |
| 40 |
|
|
*/ |
| 41 |
|
138 |
virtual int64_t Write(const void *buf, uint64_t sz) { |
| 42 |
|
138 |
return sink_->Write(buf, sz); |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
|
/** |
| 46 |
|
|
* Truncate all written data and start over at position zero. |
| 47 |
|
|
* |
| 48 |
|
|
* @returns Success = 0 |
| 49 |
|
|
* Failure = -errno |
| 50 |
|
|
*/ |
| 51 |
|
46 |
virtual int Reset() { return sink_->Reset(); } |
| 52 |
|
|
|
| 53 |
|
|
virtual int Purge(); |
| 54 |
|
|
|
| 55 |
|
|
/** |
| 56 |
|
|
* @returns true if the object is correctly initialized. |
| 57 |
|
|
*/ |
| 58 |
|
230 |
virtual bool IsValid() { return sink_->IsValid(); } |
| 59 |
|
|
|
| 60 |
|
|
/** |
| 61 |
|
|
* Commit data to the sink |
| 62 |
|
|
* @returns success = 0 |
| 63 |
|
|
* failure = -errno |
| 64 |
|
|
*/ |
| 65 |
|
✗ |
virtual int Flush() { return sink_->Flush(); } |
| 66 |
|
|
|
| 67 |
|
|
/** |
| 68 |
|
|
* Reserves new space in sinks that require reservation (see RequiresReserve) |
| 69 |
|
|
* |
| 70 |
|
|
* Successful if the requested size is smaller than already space reserved, or |
| 71 |
|
|
* if the sink is the owner of the data and can allocate enough new space. |
| 72 |
|
|
* |
| 73 |
|
|
* @note If successful, always resets the current position to 0. |
| 74 |
|
|
* |
| 75 |
|
|
* Fails if |
| 76 |
|
|
* 1) sink is not the owner of the data and more than the current size is |
| 77 |
|
|
* requested |
| 78 |
|
|
* 2) more space is requested than allowed (max_size_) |
| 79 |
|
|
* |
| 80 |
|
|
* @returns success = true |
| 81 |
|
|
* failure = false |
| 82 |
|
|
*/ |
| 83 |
|
✗ |
virtual bool Reserve(size_t size) { return sink_->Reserve(size); } |
| 84 |
|
|
|
| 85 |
|
|
/** |
| 86 |
|
|
* Returns if the specific sink type needs reservation of (data) space |
| 87 |
|
|
* |
| 88 |
|
|
* @returns true - reservation is needed |
| 89 |
|
|
* false - no reservation is needed |
| 90 |
|
|
*/ |
| 91 |
|
✗ |
virtual bool RequiresReserve() { return sink_->RequiresReserve(); } |
| 92 |
|
|
|
| 93 |
|
|
/** |
| 94 |
|
|
* Return a string representation describing the type of sink and its status |
| 95 |
|
|
*/ |
| 96 |
|
|
virtual std::string Describe(); |
| 97 |
|
|
|
| 98 |
|
✗ |
const std::string path() { return path_; } |
| 99 |
|
|
|
| 100 |
|
|
private: |
| 101 |
|
|
FILE *file_; // owned by sink_ |
| 102 |
|
|
UniquePtr<FileSink> sink_; |
| 103 |
|
|
const std::string path_; |
| 104 |
|
|
}; |
| 105 |
|
|
|
| 106 |
|
|
} // namespace cvmfs |
| 107 |
|
|
|
| 108 |
|
|
#endif // CVMFS_NETWORK_SINK_PATH_H_ |
| 109 |
|
|
|