diff options
author | 2017-03-01 16:53:35 +0000 | |
---|---|---|
committer | 2017-03-01 17:20:57 +0000 | |
commit | 6e2ccb75d7c9e1e9102a0512449381e2fffc57a7 (patch) | |
tree | ed1d6e22971d4312f2370211638aeb2381374c2e /src/main/cpp/util/file_windows.cc | |
parent | 0dff43a2e3c566766f64fed597d143885d5d8368 (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_windows.cc')
-rw-r--r-- | src/main/cpp/util/file_windows.cc | 39 |
1 files changed, 12 insertions, 27 deletions
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc index f4e4e9239f..3b99efef65 100644 --- a/src/main/cpp/util/file_windows.cc +++ b/src/main/cpp/util/file_windows.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_platform.h" - #include <ctype.h> // isalpha #include <wchar.h> // wcslen #include <wctype.h> // iswalpha @@ -548,6 +546,12 @@ static bool OpenFileForReading(const string& filename, HANDLE* result) { return true; } +int ReadFromHandle(file_handle_type handle, void* data, size_t size) { + DWORD actually_read = 0; + return ::ReadFile(handle, data, size, &actually_read, NULL) ? actually_read + : -1; +} + bool ReadFile(const string& filename, string* content, int max_size) { HANDLE handle; if (!OpenFileForReading(filename, &handle)) { @@ -559,13 +563,7 @@ bool ReadFile(const string& filename, string* content, int max_size) { return false; } content->clear(); - return ReadFrom( - [handle](void* buf, int len) { - DWORD actually_read = 0; - ::ReadFile(handle, buf, len, &actually_read, NULL); - return actually_read; - }, - content, max_size); + return ReadFrom(handle, content, max_size); } bool ReadFile(const string& filename, void* data, size_t size) { @@ -578,13 +576,7 @@ bool ReadFile(const string& filename, void* data, size_t size) { if (!autohandle.IsValid()) { return false; } - return ReadFrom( - [handle](void* buf, int len) { - DWORD actually_read = 0; - ::ReadFile(handle, buf, len, &actually_read, NULL); - return actually_read; - }, - data, size); + return ReadFrom(handle, data, size); } bool WriteFile(const void* data, size_t size, const string& filename, @@ -610,17 +602,10 @@ bool WriteFile(const void* data, size_t size, const string& filename, return false; } - // TODO(laszlocsomor): respect `perm` and set the file permissions accoridngly - - HANDLE h = handle; - bool result = WriteTo( - [h](const void* buf, size_t bufsize) { - DWORD actually_written = 0; - ::WriteFile(h, buf, bufsize, &actually_written, NULL); - return actually_written; - }, - data, size); - return result; + // TODO(laszlocsomor): respect `perm` and set the file permissions accordingly + DWORD actually_written = 0; + ::WriteFile(handle, data, size, &actually_written, NULL); + return actually_written == size; } int RenameDirectory(const std::string& old_name, const std::string& new_name) { |