From 6e2ccb75d7c9e1e9102a0512449381e2fffc57a7 Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Wed, 1 Mar 2017 16:53:35 +0000 Subject: Bazel client: simplify {Read,Write}File semantics Introduce a platform-specific file handle type (HANDLE on Windows, int on Linux/Darwin/FreeBSD) so we can get rid of the read_func and write_func functions, since they are always the same everywhere. Also include file_platform.h in file.h, since they are logically the same file (file_platform.h is just the platform-specific part of file.h). -- PiperOrigin-RevId: 148892736 MOS_MIGRATED_REVID=148892736 --- src/main/cpp/util/file.cc | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) (limited to 'src/main/cpp/util/file.cc') diff --git a/src/main/cpp/util/file.cc b/src/main/cpp/util/file.cc index 97f7d3da15..44df52be12 100644 --- a/src/main/cpp/util/file.cc +++ b/src/main/cpp/util/file.cc @@ -11,8 +11,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -#include "src/main/cpp/util/file.h" - #include #include // PATH_MAX @@ -20,9 +18,9 @@ #include #include -#include "src/main/cpp/util/file_platform.h" #include "src/main/cpp/util/errors.h" #include "src/main/cpp/util/exit_code.h" +#include "src/main/cpp/util/file.h" #include "src/main/cpp/util/strings.h" namespace blaze_util { @@ -31,16 +29,15 @@ using std::pair; using std::string; using std::vector; -bool ReadFrom(const std::function &read_func, - string *content, int max_size) { +bool ReadFrom(file_handle_type handle, string *content, int max_size) { static const size_t kReadSize = 4096; // read 4K chunks content->clear(); char buf[kReadSize]; // OPT: This loop generates one spurious read on regular files. - while (int r = read_func( - buf, max_size > 0 - ? std::min(static_cast(max_size), kReadSize) - : kReadSize)) { + while (int r = ReadFromHandle( + handle, buf, + max_size > 0 ? std::min(static_cast(max_size), kReadSize) + : kReadSize)) { if (r < 0) { if (errno == EINTR || errno == EAGAIN) continue; return false; @@ -57,12 +54,11 @@ bool ReadFrom(const std::function &read_func, return true; } -bool ReadFrom(const std::function &read_func, void *data, - size_t size) { +bool ReadFrom(file_handle_type handle, void *data, size_t size) { static const size_t kReadSize = 4096; // read 4K chunks size_t offset = 0; - while (int r = read_func(reinterpret_cast(data) + offset, - std::min(kReadSize, size))) { + while (int r = ReadFromHandle(handle, reinterpret_cast(data) + offset, + std::min(kReadSize, size))) { if (r < 0) { if (errno == EINTR || errno == EAGAIN) continue; return false; @@ -77,15 +73,6 @@ bool ReadFrom(const std::function &read_func, void *data, return true; } -bool WriteTo(const std::function &write_func, - const void *data, size_t size) { - int r = write_func(data, size); - if (r == -1) { - return false; - } - return r == static_cast(size); -} - bool WriteFile(const std::string &content, const std::string &filename, unsigned int perm) { return WriteFile(content.c_str(), content.size(), filename, perm); -- cgit v1.2.3