1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/file_stream.h"
#include <windows.h>
#include "base/file_path.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/threading/thread_restrictions.h"
#include "net/base/file_stream_metrics.h"
#include "net/base/net_errors.h"
namespace net {
// Ensure that we can just use our Whence values directly.
COMPILE_ASSERT(FROM_BEGIN == FILE_BEGIN, bad_whence_begin);
COMPILE_ASSERT(FROM_CURRENT == FILE_CURRENT, bad_whence_current);
COMPILE_ASSERT(FROM_END == FILE_END, bad_whence_end);
static void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) {
overlapped->Offset = offset.LowPart;
overlapped->OffsetHigh = offset.HighPart;
}
static void IncrementOffset(OVERLAPPED* overlapped, DWORD count) {
LARGE_INTEGER offset;
offset.LowPart = overlapped->Offset;
offset.HighPart = overlapped->OffsetHigh;
offset.QuadPart += static_cast<LONGLONG>(count);
SetOffset(overlapped, offset);
}
namespace {
int RecordAndMapError(int error, FileErrorSource source, bool record_uma) {
RecordFileError(error, source, record_uma);
return MapSystemError(error);
}
} // namespace
// FileStream::AsyncContext ----------------------------------------------
class FileStream::AsyncContext : public MessageLoopForIO::IOHandler {
public:
AsyncContext(FileStream* owner)
: owner_(owner), context_(), callback_(NULL), is_closing_(false),
record_uma_(false), error_source_(FILE_ERROR_SOURCE_COUNT) {
context_.handler = this;
}
~AsyncContext();
void IOCompletionIsPending(CompletionCallback* callback);
OVERLAPPED* overlapped() { return &context_.overlapped; }
CompletionCallback* callback() const { return callback_; }
void set_error_source(FileErrorSource source) { error_source_ = source; }
void EnableErrorStatistics() {
record_uma_ = true;
}
private:
virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
DWORD bytes_read, DWORD error);
FileStream* owner_;
MessageLoopForIO::IOContext context_;
CompletionCallback* callback_;
bool is_closing_;
bool record_uma_;
FileErrorSource error_source_;
};
FileStream::AsyncContext::~AsyncContext() {
is_closing_ = true;
bool waited = false;
base::TimeTicks start = base::TimeTicks::Now();
while (callback_) {
waited = true;
MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this);
}
if (waited) {
// We want to see if we block the message loop for too long.
UMA_HISTOGRAM_TIMES("AsyncIO.FileStreamClose",
base::TimeTicks::Now() - start);
}
}
void FileStream::AsyncContext::IOCompletionIsPending(
CompletionCallback* callback) {
DCHECK(!callback_);
callback_ = callback;
}
void FileStream::AsyncContext::OnIOCompleted(
MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) {
DCHECK_EQ(&context_, context);
DCHECK(callback_);
if (is_closing_) {
callback_ = NULL;
return;
}
int result = static_cast<int>(bytes_read);
if (error && error != ERROR_HANDLE_EOF)
result = RecordAndMapError(error, error_source_, record_uma_);
if (bytes_read)
IncrementOffset(&context->overlapped, bytes_read);
CompletionCallback* temp = NULL;
std::swap(temp, callback_);
temp->Run(result);
}
// FileStream ------------------------------------------------------------
FileStream::FileStream()
: file_(INVALID_HANDLE_VALUE),
open_flags_(0),
auto_closed_(true),
record_uma_(false) {
}
FileStream::FileStream(base::PlatformFile file, int flags)
: file_(file),
open_flags_(flags),
auto_closed_(false),
record_uma_(false) {
// If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to
// make sure we will perform asynchronous File IO to it.
if (flags & base::PLATFORM_FILE_ASYNC) {
async_context_.reset(new AsyncContext(this));
MessageLoopForIO::current()->RegisterIOHandler(file_,
async_context_.get());
}
}
FileStream::~FileStream() {
if (auto_closed_)
Close();
}
void FileStream::Close() {
if (file_ != INVALID_HANDLE_VALUE)
CancelIo(file_);
async_context_.reset();
if (file_ != INVALID_HANDLE_VALUE) {
CloseHandle(file_);
file_ = INVALID_HANDLE_VALUE;
}
}
int FileStream::Open(const FilePath& path, int open_flags) {
if (IsOpen()) {
DLOG(FATAL) << "File is already open!";
return ERR_UNEXPECTED;
}
open_flags_ = open_flags;
file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL);
if (file_ == INVALID_HANDLE_VALUE) {
DWORD error = GetLastError();
LOG(WARNING) << "Failed to open file: " << error;
return RecordAndMapError(error, FILE_ERROR_SOURCE_OPEN, record_uma_);
}
if (open_flags_ & base::PLATFORM_FILE_ASYNC) {
async_context_.reset(new AsyncContext(this));
if (record_uma_)
async_context_->EnableErrorStatistics();
MessageLoopForIO::current()->RegisterIOHandler(file_,
async_context_.get());
}
return OK;
}
bool FileStream::IsOpen() const {
return file_ != INVALID_HANDLE_VALUE;
}
int64 FileStream::Seek(Whence whence, int64 offset) {
if (!IsOpen())
return ERR_UNEXPECTED;
DCHECK(!async_context_.get() || !async_context_->callback());
LARGE_INTEGER distance, result;
distance.QuadPart = offset;
DWORD move_method = static_cast<DWORD>(whence);
if (!SetFilePointerEx(file_, distance, &result, move_method)) {
DWORD error = GetLastError();
LOG(WARNING) << "SetFilePointerEx failed: " << error;
return RecordAndMapError(error, FILE_ERROR_SOURCE_SEEK, record_uma_);
}
if (async_context_.get()) {
async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK);
SetOffset(async_context_->overlapped(), result);
}
return result.QuadPart;
}
int64 FileStream::Available() {
base::ThreadRestrictions::AssertIOAllowed();
if (!IsOpen())
return ERR_UNEXPECTED;
int64 cur_pos = Seek(FROM_CURRENT, 0);
if (cur_pos < 0)
return cur_pos;
LARGE_INTEGER file_size;
if (!GetFileSizeEx(file_, &file_size)) {
DWORD error = GetLastError();
LOG(WARNING) << "GetFileSizeEx failed: " << error;
return RecordAndMapError(error, FILE_ERROR_SOURCE_GET_SIZE, record_uma_);
}
return file_size.QuadPart - cur_pos;
}
int FileStream::Read(
char* buf, int buf_len, CompletionCallback* callback) {
if (!IsOpen())
return ERR_UNEXPECTED;
DCHECK(open_flags_ & base::PLATFORM_FILE_READ);
OVERLAPPED* overlapped = NULL;
if (async_context_.get()) {
DCHECK(callback);
DCHECK(!async_context_->callback());
overlapped = async_context_->overlapped();
async_context_->set_error_source(FILE_ERROR_SOURCE_READ);
} else {
DCHECK(!callback);
base::ThreadRestrictions::AssertIOAllowed();
}
int rv;
DWORD bytes_read;
if (!ReadFile(file_, buf, buf_len, &bytes_read, overlapped)) {
DWORD error = GetLastError();
if (async_context_.get() && error == ERROR_IO_PENDING) {
async_context_->IOCompletionIsPending(callback);
rv = ERR_IO_PENDING;
} else if (error == ERROR_HANDLE_EOF) {
rv = 0; // Report EOF by returning 0 bytes read.
} else {
LOG(WARNING) << "ReadFile failed: " << error;
rv = RecordAndMapError(error, FILE_ERROR_SOURCE_READ, record_uma_);
}
} else if (overlapped) {
async_context_->IOCompletionIsPending(callback);
rv = ERR_IO_PENDING;
} else {
rv = static_cast<int>(bytes_read);
}
return rv;
}
int FileStream::ReadUntilComplete(char *buf, int buf_len) {
int to_read = buf_len;
int bytes_total = 0;
do {
int bytes_read = Read(buf, to_read, NULL);
if (bytes_read <= 0) {
if (bytes_total == 0)
return bytes_read;
return bytes_total;
}
bytes_total += bytes_read;
buf += bytes_read;
to_read -= bytes_read;
} while (bytes_total < buf_len);
return bytes_total;
}
int FileStream::Write(
const char* buf, int buf_len, CompletionCallback* callback) {
if (!IsOpen())
return ERR_UNEXPECTED;
DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
OVERLAPPED* overlapped = NULL;
if (async_context_.get()) {
DCHECK(callback);
DCHECK(!async_context_->callback());
overlapped = async_context_->overlapped();
async_context_->set_error_source(FILE_ERROR_SOURCE_WRITE);
} else {
DCHECK(!callback);
base::ThreadRestrictions::AssertIOAllowed();
}
int rv;
DWORD bytes_written;
if (!WriteFile(file_, buf, buf_len, &bytes_written, overlapped)) {
DWORD error = GetLastError();
if (async_context_.get() && error == ERROR_IO_PENDING) {
async_context_->IOCompletionIsPending(callback);
rv = ERR_IO_PENDING;
} else {
LOG(WARNING) << "WriteFile failed: " << error;
rv = RecordAndMapError(error, FILE_ERROR_SOURCE_WRITE, record_uma_);
}
} else if (overlapped) {
async_context_->IOCompletionIsPending(callback);
rv = ERR_IO_PENDING;
} else {
rv = static_cast<int>(bytes_written);
}
return rv;
}
int FileStream::Flush() {
base::ThreadRestrictions::AssertIOAllowed();
if (!IsOpen())
return ERR_UNEXPECTED;
DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
if (FlushFileBuffers(file_)) {
return OK;
}
return RecordAndMapError(GetLastError(),
FILE_ERROR_SOURCE_FLUSH,
record_uma_);
}
int64 FileStream::Truncate(int64 bytes) {
base::ThreadRestrictions::AssertIOAllowed();
if (!IsOpen())
return ERR_UNEXPECTED;
// We better be open for reading.
DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
// Seek to the position to truncate from.
int64 seek_position = Seek(FROM_BEGIN, bytes);
if (seek_position != bytes)
return ERR_UNEXPECTED;
// And truncate the file.
BOOL result = SetEndOfFile(file_);
if (!result) {
DWORD error = GetLastError();
LOG(WARNING) << "SetEndOfFile failed: " << error;
return RecordAndMapError(error, FILE_ERROR_SOURCE_SET_EOF, record_uma_);
}
// Success.
return seek_position;
}
void FileStream::EnableErrorStatistics() {
record_uma_ = true;
if (async_context_.get())
async_context_->EnableErrorStatistics();
}
} // namespace net
|