OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/file/file_writer.h" |
| 16 |
| 17 #include <algorithm> |
| 18 |
| 19 #include <limits.h> |
| 20 |
| 21 #include "base/logging.h" |
| 22 #include "util/file/file_io.h" |
| 23 |
| 24 namespace crashpad { |
| 25 |
| 26 bool FileWriter::Open(const base::FilePath& path, int oflag, uint32_t mode) { |
| 27 CHECK(!file_.is_valid()); |
| 28 DCHECK((oflag & O_WRONLY) || (oflag & O_RDWR)); |
| 29 |
| 30 DWORD file_access = 0; |
| 31 switch (oflag & (O_RDONLY | O_WRONLY | O_RDWR)) { |
| 32 case O_WRONLY: |
| 33 file_access = GENERIC_WRITE; |
| 34 break; |
| 35 case O_RDWR: |
| 36 file_access = GENERIC_READ | GENERIC_WRITE; |
| 37 break; |
| 38 default: |
| 39 CHECK(false) << "Invalid open flag"; |
| 40 } |
| 41 |
| 42 DWORD file_create = 0; |
| 43 switch (oflag & (O_CREAT | O_EXCL | O_TRUNC)) { |
| 44 case 0: |
| 45 case O_EXCL: // Ignore EXCL w/o CREAT. |
| 46 file_create = OPEN_EXISTING; |
| 47 break; |
| 48 case O_CREAT: |
| 49 file_create = OPEN_ALWAYS; |
| 50 break; |
| 51 case O_CREAT | O_EXCL: |
| 52 case O_CREAT | O_TRUNC | O_EXCL: |
| 53 file_create = CREATE_NEW; |
| 54 break; |
| 55 case O_TRUNC: |
| 56 case O_TRUNC | O_EXCL: // Ignore EXCL w/o CREAT. |
| 57 file_create = TRUNCATE_EXISTING; |
| 58 break; |
| 59 case O_CREAT | O_TRUNC: |
| 60 file_create = CREATE_ALWAYS; |
| 61 break; |
| 62 default: |
| 63 CHECK(false) << "Invalid open flag"; |
| 64 } |
| 65 |
| 66 DCHECK_EQ(oflag & ~(O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_TRUNC), 0) |
| 67 << "Unhandled oflag value"; |
| 68 |
| 69 file_.reset(CreateFile(path.value().c_str(), file_access, 0, nullptr, |
| 70 file_create, FILE_ATTRIBUTE_NORMAL, nullptr)); |
| 71 if (!file_.is_valid()) { |
| 72 PLOG(ERROR) << "CreateFile " << path.value().c_str(); |
| 73 return false; |
| 74 } |
| 75 |
| 76 return true; |
| 77 } |
| 78 |
| 79 FileOffset FileWriter::Seek(FileOffset offset, int whence) { |
| 80 DCHECK(file_.is_valid()); |
| 81 |
| 82 DWORD method = 0; |
| 83 switch (whence) { |
| 84 case SEEK_SET: |
| 85 method = FILE_BEGIN; |
| 86 break; |
| 87 case SEEK_CUR: |
| 88 method = FILE_CURRENT; |
| 89 break; |
| 90 case SEEK_END: |
| 91 method = FILE_END; |
| 92 break; |
| 93 default: |
| 94 CHECK(false) << "Invalid whence"; |
| 95 } |
| 96 |
| 97 LARGE_INTEGER li; |
| 98 li.QuadPart = offset; |
| 99 li.LowPart = SetFilePointer(file_.get(), li.LowPart, &li.HighPart, method); |
| 100 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { |
| 101 PLOG(ERROR) << "SetFilePointer"; |
| 102 return -1; |
| 103 } |
| 104 |
| 105 return li.QuadPart; |
| 106 } |
| 107 |
| 108 } // namespace crashpad |
OLD | NEW |