File-inl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2014-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FOLLY_GEN_FILE_H_
  17. #error This file may only be included from folly/gen/File.h
  18. #endif
  19. #include <system_error>
  20. #include <folly/gen/String.h>
  21. namespace folly {
  22. namespace gen {
  23. namespace detail {
  24. class FileReader : public GenImpl<ByteRange, FileReader> {
  25. public:
  26. FileReader(File file, std::unique_ptr<IOBuf> buffer)
  27. : file_(std::move(file)), buffer_(std::move(buffer)) {
  28. buffer_->clear();
  29. }
  30. template <class Body>
  31. bool apply(Body&& body) const {
  32. for (;;) {
  33. ssize_t n;
  34. do {
  35. n = ::read(file_.fd(), buffer_->writableTail(), buffer_->capacity());
  36. } while (n == -1 && errno == EINTR);
  37. if (n == -1) {
  38. throw std::system_error(errno, std::system_category(), "read failed");
  39. }
  40. if (n == 0) {
  41. return true;
  42. }
  43. if (!body(ByteRange(buffer_->tail(), size_t(n)))) {
  44. return false;
  45. }
  46. }
  47. }
  48. // Technically, there could be infinite files (e.g. /dev/random), but people
  49. // who open those can do so at their own risk.
  50. static constexpr bool infinite = false;
  51. private:
  52. File file_;
  53. std::unique_ptr<IOBuf> buffer_;
  54. };
  55. class FileWriter : public Operator<FileWriter> {
  56. public:
  57. FileWriter(File file, std::unique_ptr<IOBuf> buffer)
  58. : file_(std::move(file)), buffer_(std::move(buffer)) {
  59. if (buffer_) {
  60. buffer_->clear();
  61. }
  62. }
  63. template <class Source, class Value>
  64. void compose(const GenImpl<Value, Source>& source) const {
  65. auto fn = [&](ByteRange v) {
  66. if (!this->buffer_ || v.size() >= this->buffer_->capacity()) {
  67. this->flushBuffer();
  68. this->write(v);
  69. } else {
  70. if (v.size() > this->buffer_->tailroom()) {
  71. this->flushBuffer();
  72. }
  73. memcpy(this->buffer_->writableTail(), v.data(), v.size());
  74. this->buffer_->append(v.size());
  75. }
  76. };
  77. // Iterate
  78. source.foreach(std::move(fn));
  79. flushBuffer();
  80. file_.close();
  81. }
  82. private:
  83. void write(ByteRange v) const {
  84. ssize_t n;
  85. while (!v.empty()) {
  86. do {
  87. n = ::write(file_.fd(), v.data(), v.size());
  88. } while (n == -1 && errno == EINTR);
  89. if (n == -1) {
  90. throw std::system_error(
  91. errno, std::system_category(), "write() failed");
  92. }
  93. v.advance(size_t(n));
  94. }
  95. }
  96. void flushBuffer() const {
  97. if (buffer_ && buffer_->length() != 0) {
  98. write(ByteRange(buffer_->data(), buffer_->length()));
  99. buffer_->clear();
  100. }
  101. }
  102. mutable File file_;
  103. std::unique_ptr<IOBuf> buffer_;
  104. };
  105. inline auto byLineImpl(File file, char delim, bool keepDelimiter) {
  106. // clang-format off
  107. return fromFile(std::move(file))
  108. | eachAs<StringPiece>()
  109. | resplit(delim, keepDelimiter);
  110. // clang-format on
  111. }
  112. } // namespace detail
  113. /**
  114. * Generator which reads lines from a file.
  115. * Note: This produces StringPieces which reference temporary strings which are
  116. * only valid during iteration.
  117. */
  118. inline auto byLineFull(File file, char delim = '\n') {
  119. return detail::byLineImpl(std::move(file), delim, true);
  120. }
  121. inline auto byLineFull(int fd, char delim = '\n') {
  122. return byLineFull(File(fd), delim);
  123. }
  124. inline auto byLineFull(const char* f, char delim = '\n') {
  125. return byLineFull(File(f), delim);
  126. }
  127. inline auto byLine(File file, char delim = '\n') {
  128. return detail::byLineImpl(std::move(file), delim, false);
  129. }
  130. inline auto byLine(int fd, char delim = '\n') {
  131. return byLine(File(fd), delim);
  132. }
  133. inline auto byLine(const char* f, char delim = '\n') {
  134. return byLine(File(f), delim);
  135. }
  136. } // namespace gen
  137. } // namespace folly