Index: util/file/file_writer_win.cc |
diff --git a/util/file/file_writer_win.cc b/util/file/file_writer_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c9a2fac3bc4399a11d83af88f2d08ed807029af9 |
--- /dev/null |
+++ b/util/file/file_writer_win.cc |
@@ -0,0 +1,108 @@ |
+// Copyright 2014 The Crashpad Authors. All rights reserved. |
+// |
+// Licensed under the Apache License, Version 2.0 (the "License"); |
+// you may not use this file except in compliance with the License. |
+// You may obtain a copy of the License at |
+// |
+// http://www.apache.org/licenses/LICENSE-2.0 |
+// |
+// Unless required by applicable law or agreed to in writing, software |
+// distributed under the License is distributed on an "AS IS" BASIS, |
+// 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 "util/file/file_writer.h" |
+ |
+#include <algorithm> |
+ |
+#include <limits.h> |
+ |
+#include "base/logging.h" |
+#include "util/file/file_io.h" |
+ |
+namespace crashpad { |
+ |
+bool FileWriter::Open(const base::FilePath& path, int oflag, uint32_t mode) { |
+ CHECK(!file_.is_valid()); |
+ DCHECK((oflag & O_WRONLY) || (oflag & O_RDWR)); |
+ |
+ DWORD file_access = 0; |
+ switch (oflag & (O_RDONLY | O_WRONLY | O_RDWR)) { |
+ case O_WRONLY: |
+ file_access = GENERIC_WRITE; |
+ break; |
+ case O_RDWR: |
+ file_access = GENERIC_READ | GENERIC_WRITE; |
+ break; |
+ default: |
+ CHECK(false) << "Invalid open flag"; |
+ } |
+ |
+ DWORD file_create = 0; |
+ switch (oflag & (O_CREAT | O_EXCL | O_TRUNC)) { |
+ case 0: |
+ case O_EXCL: // Ignore EXCL w/o CREAT. |
+ file_create = OPEN_EXISTING; |
+ break; |
+ case O_CREAT: |
+ file_create = OPEN_ALWAYS; |
+ break; |
+ case O_CREAT | O_EXCL: |
+ case O_CREAT | O_TRUNC | O_EXCL: |
+ file_create = CREATE_NEW; |
+ break; |
+ case O_TRUNC: |
+ case O_TRUNC | O_EXCL: // Ignore EXCL w/o CREAT. |
+ file_create = TRUNCATE_EXISTING; |
+ break; |
+ case O_CREAT | O_TRUNC: |
+ file_create = CREATE_ALWAYS; |
+ break; |
+ default: |
+ CHECK(false) << "Invalid open flag"; |
+ } |
+ |
+ DCHECK_EQ(oflag & ~(O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_TRUNC), 0) |
+ << "Unhandled oflag value"; |
+ |
+ file_.reset(CreateFile(path.value().c_str(), file_access, 0, nullptr, |
+ file_create, FILE_ATTRIBUTE_NORMAL, nullptr)); |
+ if (!file_.is_valid()) { |
+ PLOG(ERROR) << "CreateFile " << path.value().c_str(); |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+FileOffset FileWriter::Seek(FileOffset offset, int whence) { |
+ DCHECK(file_.is_valid()); |
+ |
+ DWORD method = 0; |
+ switch (whence) { |
+ case SEEK_SET: |
+ method = FILE_BEGIN; |
+ break; |
+ case SEEK_CUR: |
+ method = FILE_CURRENT; |
+ break; |
+ case SEEK_END: |
+ method = FILE_END; |
+ break; |
+ default: |
+ CHECK(false) << "Invalid whence"; |
+ } |
+ |
+ LARGE_INTEGER li; |
+ li.QuadPart = offset; |
+ li.LowPart = SetFilePointer(file_.get(), li.LowPart, &li.HighPart, method); |
+ if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { |
+ PLOG(ERROR) << "SetFilePointer"; |
+ return -1; |
+ } |
+ |
+ return li.QuadPart; |
+} |
+ |
+} // namespace crashpad |