diff options
author | 2017-02-27 12:25:11 +0000 | |
---|---|---|
committer | 2017-02-27 15:10:04 +0000 | |
commit | 5f27712d09a501c8d5da20b43484490076b749f2 (patch) | |
tree | 6d1e263f5e3ace226ed3f8cc1d9548c1162e9476 /src/main/cpp/util/file_windows.cc | |
parent | abc933266ebdebe0bd8fc8cc78a8aed3f4fc87fb (diff) |
Bazel client: add new ReadFile/WriteFile variants
Instead of writing from / reading to a string,
these variants take a buffer and a size.
These methods will be used from ijar.
See https://github.com/bazelbuild/bazel/issues/2157
--
PiperOrigin-RevId: 148635487
MOS_MIGRATED_REVID=148635487
Diffstat (limited to 'src/main/cpp/util/file_windows.cc')
-rw-r--r-- | src/main/cpp/util/file_windows.cc | 51 |
1 files changed, 40 insertions, 11 deletions
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc index 26cc47df65..f4e4e9239f 100644 --- a/src/main/cpp/util/file_windows.cc +++ b/src/main/cpp/util/file_windows.cc @@ -526,42 +526,69 @@ bool AsShortWindowsPath(const string& path, string* result) { return true; } -bool ReadFile(const string& filename, string* content, int max_size) { +static bool OpenFileForReading(const string& filename, HANDLE* result) { if (filename.empty()) { return false; } if (IsDevNull(filename)) { - content->clear(); return true; } wstring wfilename; if (!AsWindowsPathWithUncPrefix(filename, &wfilename)) { return false; } - windows_util::AutoHandle handle(::CreateFileW( + *result = ::CreateFileW( /* lpFileName */ wfilename.c_str(), /* dwDesiredAccess */ GENERIC_READ, /* dwShareMode */ FILE_SHARE_READ, /* lpSecurityAttributes */ NULL, /* dwCreationDisposition */ OPEN_EXISTING, /* dwFlagsAndAttributes */ FILE_ATTRIBUTE_NORMAL, - /* hTemplateFile */ NULL)); - if (!handle.IsValid()) { + /* hTemplateFile */ NULL); + return true; +} + +bool ReadFile(const string& filename, string* content, int max_size) { + HANDLE handle; + if (!OpenFileForReading(filename, &handle)) { return false; } - HANDLE h = handle; - bool result = ReadFrom( - [h](void* buf, int len) { + windows_util::AutoHandle autohandle(handle); + if (!autohandle.IsValid()) { + return false; + } + content->clear(); + return ReadFrom( + [handle](void* buf, int len) { DWORD actually_read = 0; - ::ReadFile(h, buf, len, &actually_read, NULL); + ::ReadFile(handle, buf, len, &actually_read, NULL); return actually_read; }, content, max_size); - return result; } -bool WriteFile(const void* data, size_t size, const string& filename) { +bool ReadFile(const string& filename, void* data, size_t size) { + HANDLE handle; + if (!OpenFileForReading(filename, &handle)) { + return false; + } + + windows_util::AutoHandle autohandle(handle); + 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); +} + +bool WriteFile(const void* data, size_t size, const string& filename, + unsigned int perm) { if (IsDevNull(filename)) { return true; // mimic write(2) behavior with /dev/null } @@ -583,6 +610,8 @@ 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) { |