aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util/file.cc
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2017-03-01 16:53:35 +0000
committerGravatar Yue Gan <yueg@google.com>2017-03-01 17:20:57 +0000
commit6e2ccb75d7c9e1e9102a0512449381e2fffc57a7 (patch)
treeed1d6e22971d4312f2370211638aeb2381374c2e /src/main/cpp/util/file.cc
parent0dff43a2e3c566766f64fed597d143885d5d8368 (diff)
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
Diffstat (limited to 'src/main/cpp/util/file.cc')
-rw-r--r--src/main/cpp/util/file.cc31
1 files changed, 9 insertions, 22 deletions
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 <errno.h>
#include <limits.h> // PATH_MAX
@@ -20,9 +18,9 @@
#include <cstdlib>
#include <vector>
-#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<int(void *, size_t)> &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<size_t>(max_size), kReadSize)
- : kReadSize)) {
+ while (int r = ReadFromHandle(
+ handle, buf,
+ max_size > 0 ? std::min(static_cast<size_t>(max_size), kReadSize)
+ : kReadSize)) {
if (r < 0) {
if (errno == EINTR || errno == EAGAIN) continue;
return false;
@@ -57,12 +54,11 @@ bool ReadFrom(const std::function<int(void *, size_t)> &read_func,
return true;
}
-bool ReadFrom(const std::function<int(void *, size_t)> &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<char *>(data) + offset,
- std::min(kReadSize, size))) {
+ while (int r = ReadFromHandle(handle, reinterpret_cast<char *>(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<int(void *, size_t)> &read_func, void *data,
return true;
}
-bool WriteTo(const std::function<int(const void *, size_t)> &write_func,
- const void *data, size_t size) {
- int r = write_func(data, size);
- if (r == -1) {
- return false;
- }
- return r == static_cast<int>(size);
-}
-
bool WriteFile(const std::string &content, const std::string &filename,
unsigned int perm) {
return WriteFile(content.c_str(), content.size(), filename, perm);