Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: util/file/file_io_win.cc

Issue 1001673002: Add Locking calls to file_io.h plus implementations and test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: release load Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/file/file_io_test.cc ('k') | util/test/errors.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 DWORD size_dword = base::checked_cast<DWORD>(size); 73 DWORD size_dword = base::checked_cast<DWORD>(size);
74 DWORD bytes_written; 74 DWORD bytes_written;
75 BOOL rv = ::WriteFile(file, buffer, size_dword, &bytes_written, nullptr); 75 BOOL rv = ::WriteFile(file, buffer, size_dword, &bytes_written, nullptr);
76 if (!rv) 76 if (!rv)
77 return -1; 77 return -1;
78 CHECK_EQ(bytes_written, size_dword); 78 CHECK_EQ(bytes_written, size_dword);
79 return bytes_written; 79 return bytes_written;
80 } 80 }
81 81
82 FileHandle LoggingOpenFileForRead(const base::FilePath& path) { 82 FileHandle LoggingOpenFileForRead(const base::FilePath& path) {
83 HANDLE file = CreateFile(path.value().c_str(), GENERIC_READ, FILE_SHARE_READ, 83 HANDLE file = CreateFile(path.value().c_str(),
84 nullptr, OPEN_EXISTING, 0, nullptr); 84 GENERIC_READ,
85 FILE_SHARE_READ | FILE_SHARE_WRITE,
86 nullptr,
87 OPEN_EXISTING,
88 0,
89 nullptr);
85 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile " 90 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile "
86 << path.value().c_str(); 91 << path.value().c_str();
87 return file; 92 return file;
88 } 93 }
89 94
90 FileHandle LoggingOpenFileForWrite(const base::FilePath& path, 95 FileHandle LoggingOpenFileForWrite(const base::FilePath& path,
91 FileWriteMode mode, 96 FileWriteMode mode,
92 FilePermissions permissions) { 97 FilePermissions permissions) {
93 DWORD disposition = 0; 98 DWORD disposition = 0;
94 switch (mode) { 99 switch (mode) {
95 case FileWriteMode::kReuseOrCreate: 100 case FileWriteMode::kReuseOrCreate:
96 disposition = OPEN_ALWAYS; 101 disposition = OPEN_ALWAYS;
97 break; 102 break;
98 case FileWriteMode::kTruncateOrCreate: 103 case FileWriteMode::kTruncateOrCreate:
99 disposition = CREATE_ALWAYS; 104 disposition = CREATE_ALWAYS;
100 break; 105 break;
101 case FileWriteMode::kCreateOrFail: 106 case FileWriteMode::kCreateOrFail:
102 disposition = CREATE_NEW; 107 disposition = CREATE_NEW;
103 break; 108 break;
104 } 109 }
105 HANDLE file = CreateFile(path.value().c_str(), GENERIC_WRITE, 0, nullptr, 110 HANDLE file = CreateFile(path.value().c_str(),
106 disposition, FILE_ATTRIBUTE_NORMAL, nullptr); 111 GENERIC_WRITE,
112 FILE_SHARE_READ | FILE_SHARE_WRITE,
113 nullptr,
114 disposition,
115 FILE_ATTRIBUTE_NORMAL,
116 nullptr);
107 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile " 117 PLOG_IF(ERROR, file == INVALID_HANDLE_VALUE) << "CreateFile "
108 << path.value().c_str(); 118 << path.value().c_str();
109 return file; 119 return file;
110 } 120 }
111 121
122 bool LoggingLockFile(FileHandle file, FileLocking locking) {
123 DWORD flags =
124 (locking == FileLocking::kExclusive) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
125
126 // Note that the `Offset` fields of overlapped indicate the start location for
127 // locking (beginning of file in this case), and `hEvent` must be also be set
128 // to 0.
129 OVERLAPPED overlapped = {0};
130 if (!LockFileEx(file, flags, 0, MAXDWORD, MAXDWORD, &overlapped)) {
131 PLOG(ERROR) << "LockFileEx";
132 return false;
133 }
134 return true;
135 }
136
137 bool LoggingUnlockFile(FileHandle file) {
138 // Note that the `Offset` fields of overlapped indicate the start location for
139 // locking (beginning of file in this case), and `hEvent` must be also be set
140 // to 0.
141 OVERLAPPED overlapped = {0};
142 if (!UnlockFileEx(file, 0, MAXDWORD, MAXDWORD, &overlapped)) {
143 PLOG(ERROR) << "UnlockFileEx";
144 return false;
145 }
146 return true;
147 }
148
112 FileOffset LoggingSeekFile(FileHandle file, FileOffset offset, int whence) { 149 FileOffset LoggingSeekFile(FileHandle file, FileOffset offset, int whence) {
113 DWORD method = 0; 150 DWORD method = 0;
114 switch (whence) { 151 switch (whence) {
115 case SEEK_SET: 152 case SEEK_SET:
116 method = FILE_BEGIN; 153 method = FILE_BEGIN;
117 break; 154 break;
118 case SEEK_CUR: 155 case SEEK_CUR:
119 method = FILE_CURRENT; 156 method = FILE_CURRENT;
120 break; 157 break;
121 case SEEK_END: 158 case SEEK_END:
(...skipping 15 matching lines...) Expand all
137 return new_offset.QuadPart; 174 return new_offset.QuadPart;
138 } 175 }
139 176
140 bool LoggingCloseFile(FileHandle file) { 177 bool LoggingCloseFile(FileHandle file) {
141 BOOL rv = CloseHandle(file); 178 BOOL rv = CloseHandle(file);
142 PLOG_IF(ERROR, !rv) << "CloseHandle"; 179 PLOG_IF(ERROR, !rv) << "CloseHandle";
143 return rv; 180 return rv;
144 } 181 }
145 182
146 } // namespace crashpad 183 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/file/file_io_test.cc ('k') | util/test/errors.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698