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
|
// Copyright 2013 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 "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/threading/worker_pool.h"
#include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
#include "chrome/browser/extensions/api/image_writer_private/operation.h"
#include "chrome/browser/extensions/api/image_writer_private/operation_manager.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/zlib/google/zip.h"
namespace extensions {
namespace image_writer {
using content::BrowserThread;
const int kBurningBlockSize = 8 * 1024; // 8 KiB
void Operation::Write(const base::Closure& continuation) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (IsCancelled()) {
return;
}
SetStage(image_writer_api::STAGE_WRITE);
// TODO (haven): Unmount partitions before writing. http://crbug.com/284834
base::PlatformFileError result;
image_file_ = base::CreatePlatformFile(
image_path_,
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
NULL,
&result);
if (result != base::PLATFORM_FILE_OK) {
Error(error::kImageOpenError);
return;
}
device_file_ = base::CreatePlatformFile(
device_path_,
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE,
NULL,
&result);
if (result != base::PLATFORM_FILE_OK) {
Error(error::kDeviceOpenError);
base::ClosePlatformFile(image_file_);
return;
}
int64 total_size;
base::GetFileSize(image_path_, &total_size);
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&Operation::WriteChunk, this, 0, total_size, continuation));
}
void Operation::WriteChunk(const int64& bytes_written,
const int64& total_size,
const base::Closure& continuation) {
if (!IsCancelled()) {
scoped_ptr<char[]> buffer(new char[kBurningBlockSize]);
int64 len = base::ReadPlatformFile(
image_file_, bytes_written, buffer.get(), kBurningBlockSize);
if (len > 0) {
if (base::WritePlatformFile(
device_file_, bytes_written, buffer.get(), len) == len) {
int percent_curr =
kProgressComplete * (bytes_written + len) / total_size;
SetProgress(percent_curr);
BrowserThread::PostTask(BrowserThread::FILE,
FROM_HERE,
base::Bind(&Operation::WriteChunk,
this,
bytes_written + len,
total_size,
continuation));
return;
} else {
Error(error::kDeviceWriteError);
}
} else if (len == 0) {
WriteComplete(continuation);
} else { // len < 0
Error(error::kImageReadError);
}
}
base::ClosePlatformFile(image_file_);
base::ClosePlatformFile(device_file_);
}
void Operation::WriteComplete(const base::Closure& continuation) {
SetProgress(kProgressComplete);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation);
}
void Operation::VerifyWrite(const base::Closure& continuation) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (IsCancelled()) {
return;
}
SetStage(image_writer_api::STAGE_VERIFYWRITE);
base::PlatformFileError result;
image_file_ = base::CreatePlatformFile(
image_path_,
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
NULL,
&result);
if (result != base::PLATFORM_FILE_OK) {
Error(error::kImageOpenError);
return;
}
device_file_ = base::CreatePlatformFile(
device_path_,
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
NULL,
&result);
if (result != base::PLATFORM_FILE_OK) {
Error(error::kDeviceOpenError);
base::ClosePlatformFile(image_file_);
return;
}
int64 total_size;
base::GetFileSize(image_path_, &total_size);
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(
&Operation::VerifyWriteChunk, this, 0, total_size, continuation));
}
void Operation::VerifyWriteChunk(const int64& bytes_processed,
const int64& total_size,
const base::Closure& continuation) {
if (!IsCancelled()) {
scoped_ptr<char[]> source_buffer(new char[kBurningBlockSize]);
scoped_ptr<char[]> target_buffer(new char[kBurningBlockSize]);
int64 image_bytes_read = base::ReadPlatformFile(
image_file_, bytes_processed, source_buffer.get(), kBurningBlockSize);
if (image_bytes_read > 0) {
int64 device_bytes_read = base::ReadPlatformFile(
device_file_, bytes_processed, target_buffer.get(), image_bytes_read);
if (image_bytes_read == device_bytes_read &&
memcmp(source_buffer.get(), target_buffer.get(), image_bytes_read) ==
0) {
int percent_curr = kProgressComplete *
(bytes_processed + image_bytes_read) / total_size;
SetProgress(percent_curr);
BrowserThread::PostTask(BrowserThread::FILE,
FROM_HERE,
base::Bind(&Operation::VerifyWriteChunk,
this,
bytes_processed + image_bytes_read,
total_size,
continuation));
return;
} else {
Error(error::kVerificationFailed);
}
} else if (image_bytes_read == 0) {
VerifyWriteComplete(continuation);
} else { // len < 0
Error(error::kImageReadError);
}
}
base::ClosePlatformFile(image_file_);
base::ClosePlatformFile(device_file_);
}
void Operation::VerifyWriteComplete(const base::Closure& continuation) {
SetProgress(kProgressComplete);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation);
}
} // namespace image_writer
} // namespace extensions
|