CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sink_file.cc
Go to the documentation of this file.
1 
5 #include <cerrno>
6 #include <cstdio>
7 #include <string>
8 
9 #include "sink_file.h"
10 
11 namespace cvmfs {
12 
19 int64_t FileSink::Write(const void *buf, uint64_t sz) {
20  size_t ret = fwrite(buf, 1ul, sz, file_);
21 
22  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  return static_cast<int64_t>(ret);
30 }
31 
39  return ((fflush(file_) == 0) &&
40  (ftruncate(fileno(file_), 0) == 0) &&
41  (freopen(NULL, "w", file_) == file_)) ? 0 : -errno;
42 }
43 
54  if (is_owner_ && file_) {
55  int ret = fclose(file_);
56  file_ = NULL;
57  if (ret != 0) {
58  return -errno;
59  }
60 
61  return 0;
62  } else {
63  return Reset();
64  }
65 }
66 
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
virtual int Purge()
Definition: sink_file.cc:53
virtual std::string Describe()
Definition: sink_file.cc:70
virtual bool IsValid()
Definition: sink_file.h:55
void Adopt(FILE *file, bool is_owner=true)
Definition: sink_file.cc:75
FILE * file()
Definition: sink_file.h:109
virtual int64_t Write(const void *buf, uint64_t sz)
Definition: sink_file.cc:19
virtual int Reset()
Definition: sink_file.cc:38
bool is_owner_
Definition: sink.h:99