OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file_enumerator.h" | 6 #include "base/files/file_enumerator.h" |
7 #include "base/threading/worker_pool.h" | 7 #include "base/threading/worker_pool.h" |
8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h " | 10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h " |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 #include "third_party/zlib/google/zip.h" | 12 #include "third_party/zlib/google/zip.h" |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 namespace image_writer { | 15 namespace image_writer { |
16 | 16 |
17 using content::BrowserThread; | 17 using content::BrowserThread; |
18 | 18 |
19 const int kBurningBlockSize = 8 * 1024; // 8 KiB | 19 const int kBurningBlockSize = 8 * 1024; // 8 KiB |
20 | 20 |
21 void Operation::WriteStart() { | 21 void Operation::Write(const base::Closure& continuation) { |
22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
23 if (IsCancelled()) { | 23 if (IsCancelled()) { |
24 return; | 24 return; |
25 } | 25 } |
26 | 26 |
27 if (image_path_.empty()) { | |
28 Error(error::kImageNotFound); | |
29 return; | |
30 } | |
31 | |
32 DVLOG(1) << "Starting write of " << image_path_.value() | |
33 << " to " << storage_unit_id_; | |
34 | |
35 SetStage(image_writer_api::STAGE_WRITE); | 27 SetStage(image_writer_api::STAGE_WRITE); |
36 | 28 |
37 // TODO (haven): Unmount partitions before writing. http://crbug.com/284834 | 29 // TODO (haven): Unmount partitions before writing. http://crbug.com/284834 |
38 | 30 |
39 scoped_ptr<image_writer_utils::ImageReader> reader( | 31 base::PlatformFileError result; |
40 new image_writer_utils::ImageReader()); | 32 image_file_ = base::CreatePlatformFile( |
41 scoped_ptr<image_writer_utils::ImageWriter> writer( | 33 image_path_, |
42 new image_writer_utils::ImageWriter()); | 34 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
43 base::FilePath storage_path(storage_unit_id_); | 35 NULL, |
44 | 36 &result); |
45 if (reader->Open(image_path_)) { | 37 if (result != base::PLATFORM_FILE_OK) { |
46 if (!writer->Open(storage_path)) { | |
47 reader->Close(); | |
48 Error(error::kDeviceOpenError); | |
49 return; | |
50 } | |
51 } else { | |
52 Error(error::kImageOpenError); | 38 Error(error::kImageOpenError); |
53 return; | 39 return; |
54 } | 40 } |
55 | 41 |
56 BrowserThread::PostTask( | 42 device_file_ = base::CreatePlatformFile( |
57 BrowserThread::FILE, | 43 device_path_, |
58 FROM_HERE, | 44 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
59 base::Bind(&Operation::WriteChunk, | 45 NULL, |
60 this, | 46 &result); |
61 base::Passed(&reader), | 47 if (result != base::PLATFORM_FILE_OK) { |
62 base::Passed(&writer), | 48 Error(error::kDeviceOpenError); |
63 0)); | 49 base::ClosePlatformFile(image_file_); |
64 } | |
65 | |
66 void Operation::WriteChunk( | |
67 scoped_ptr<image_writer_utils::ImageReader> reader, | |
68 scoped_ptr<image_writer_utils::ImageWriter> writer, | |
69 int64 bytes_written) { | |
70 if (IsCancelled()) { | |
71 WriteCleanUp(reader.Pass(), writer.Pass()); | |
72 return; | 50 return; |
73 } | 51 } |
74 | 52 |
75 char buffer[kBurningBlockSize]; | 53 int64 total_size; |
76 int64 image_size = reader->GetSize(); | 54 base::GetFileSize(image_path_, &total_size); |
77 int len = reader->Read(buffer, kBurningBlockSize); | |
78 | 55 |
79 if (len > 0) { | 56 BrowserThread::PostTask( |
80 if (writer->Write(buffer, len) == len) { | 57 BrowserThread::FILE, |
81 int percent_prev = kProgressComplete * bytes_written / image_size; | 58 FROM_HERE, |
82 int percent_curr = kProgressComplete * (bytes_written + len) / image_size; | 59 base::Bind(&Operation::WriteChunk, this, 0, total_size, continuation)); |
60 } | |
83 | 61 |
84 if (percent_curr > percent_prev) { | 62 void Operation::WriteChunk(const int64& bytes_written, |
63 const int64& total_size, | |
64 const base::Closure& continuation) { | |
65 if (!IsCancelled()) { | |
66 scoped_ptr<char[]> buffer(new char[kBurningBlockSize]); | |
67 int64 len = base::ReadPlatformFile( | |
68 image_file_, bytes_written, buffer.get(), kBurningBlockSize); | |
69 | |
70 if (len > 0) { | |
71 if (base::WritePlatformFile( | |
72 device_file_, bytes_written, buffer.get(), len) == len) { | |
73 int percent_curr = | |
74 kProgressComplete * (bytes_written + len) / total_size; | |
75 | |
85 SetProgress(percent_curr); | 76 SetProgress(percent_curr); |
77 | |
78 BrowserThread::PostTask(BrowserThread::FILE, | |
79 FROM_HERE, | |
80 base::Bind(&Operation::WriteChunk, | |
81 this, | |
82 bytes_written + len, | |
83 total_size, | |
84 continuation)); | |
85 return; | |
86 } else { | |
87 Error(error::kDeviceWriteError); | |
86 } | 88 } |
87 | 89 } else if (len == 0) { |
88 BrowserThread::PostTask( | 90 WriteComplete(continuation); |
89 BrowserThread::FILE, | 91 } else { // len < 0 |
90 FROM_HERE, | |
91 base::Bind(&Operation::WriteChunk, | |
92 this, | |
93 base::Passed(&reader), | |
94 base::Passed(&writer), | |
95 bytes_written + len)); | |
96 } else { | |
97 WriteCleanUp(reader.Pass(), writer.Pass()); | |
98 Error(error::kDeviceWriteError); | |
99 } | |
100 } else if (len == 0) { | |
101 if (bytes_written == image_size) { | |
102 if (WriteCleanUp(reader.Pass(), writer.Pass())) { | |
103 BrowserThread::PostTask( | |
104 BrowserThread::FILE, | |
105 FROM_HERE, | |
106 base::Bind(&Operation::WriteComplete, | |
107 this)); | |
108 } | |
109 } else { | |
110 WriteCleanUp(reader.Pass(), writer.Pass()); | |
111 Error(error::kImageReadError); | 92 Error(error::kImageReadError); |
112 } | 93 } |
113 } else { // len < 0 | |
114 WriteCleanUp(reader.Pass(), writer.Pass()); | |
115 Error(error::kImageReadError); | |
116 } | 94 } |
95 | |
96 base::ClosePlatformFile(image_file_); | |
97 base::ClosePlatformFile(device_file_); | |
117 } | 98 } |
118 | 99 |
119 bool Operation::WriteCleanUp( | 100 void Operation::WriteComplete(const base::Closure& continuation) { |
120 scoped_ptr<image_writer_utils::ImageReader> reader, | 101 SetProgress(kProgressComplete); |
121 scoped_ptr<image_writer_utils::ImageWriter> writer) { | 102 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
122 | |
123 bool success = true; | |
124 if (!reader->Close()) { | |
125 Error(error::kImageCloseError); | |
126 success = false; | |
127 } | |
128 | |
129 if (!writer->Close()) { | |
130 Error(error::kDeviceCloseError); | |
131 success = false; | |
132 } | |
133 return success; | |
134 } | 103 } |
135 | 104 |
136 void Operation::WriteComplete() { | 105 void Operation::VerifyWrite(const base::Closure& continuation) { |
137 | |
138 DVLOG(2) << "Completed write of " << image_path_.value(); | |
139 SetProgress(kProgressComplete); | |
140 | |
141 if (verify_write_) { | |
142 BrowserThread::PostTask(BrowserThread::FILE, | |
143 FROM_HERE, | |
144 base::Bind(&Operation::VerifyWriteStart, this)); | |
145 } else { | |
146 BrowserThread::PostTask(BrowserThread::FILE, | |
147 FROM_HERE, | |
148 base::Bind(&Operation::Finish, this)); | |
149 } | |
150 } | |
151 | |
152 void Operation::VerifyWriteStart() { | |
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
154 if (IsCancelled()) { | 107 if (IsCancelled()) { |
155 return; | 108 return; |
156 } | 109 } |
157 | 110 |
158 DVLOG(1) << "Starting verification stage."; | |
159 | |
160 SetStage(image_writer_api::STAGE_VERIFYWRITE); | 111 SetStage(image_writer_api::STAGE_VERIFYWRITE); |
161 | 112 |
162 scoped_ptr<base::FilePath> image_path(new base::FilePath(image_path_)); | 113 base::PlatformFileError result; |
163 | 114 |
164 GetMD5SumOfFile( | 115 image_file_ = base::CreatePlatformFile( |
165 image_path.Pass(), | 116 image_path_, |
166 -1, | 117 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
167 0, // progress_offset | 118 NULL, |
168 50, // progress_scale | 119 &result); |
169 base::Bind(&Operation::VerifyWriteStage2, | 120 if (result != base::PLATFORM_FILE_OK) { |
170 this)); | 121 Error(error::kImageOpenError); |
171 } | |
172 | |
173 void Operation::VerifyWriteStage2( | |
174 scoped_ptr<std::string> image_hash) { | |
175 DVLOG(1) << "Building MD5 sum of device: " << storage_unit_id_; | |
176 | |
177 int64 image_size; | |
178 scoped_ptr<base::FilePath> device_path(new base::FilePath(storage_unit_id_)); | |
179 | |
180 if (!base::GetFileSize(image_path_, &image_size)){ | |
181 Error(error::kImageSizeError); | |
182 return; | 122 return; |
183 } | 123 } |
184 | 124 |
185 GetMD5SumOfFile( | 125 device_file_ = base::CreatePlatformFile( |
186 device_path.Pass(), | 126 device_path_, |
187 image_size, | 127 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
188 50, // progress_offset | 128 NULL, |
189 50, // progress_scale | 129 &result); |
190 base::Bind(&Operation::VerifyWriteCompare, | 130 if (result != base::PLATFORM_FILE_OK) { |
191 this, | 131 Error(error::kDeviceOpenError); |
192 base::Passed(&image_hash))); | 132 base::ClosePlatformFile(image_file_); |
193 } | |
194 | |
195 void Operation::VerifyWriteCompare( | |
196 scoped_ptr<std::string> image_hash, | |
197 scoped_ptr<std::string> device_hash) { | |
198 DVLOG(1) << "Comparing hashes: " << *image_hash << " vs " << *device_hash; | |
199 | |
200 if (*image_hash != *device_hash) { | |
201 Error(error::kVerificationFailed); | |
202 return; | 133 return; |
203 } | 134 } |
204 | 135 |
205 DVLOG(2) << "Completed write verification of " << image_path_.value(); | 136 int64 total_size; |
137 base::GetFileSize(image_path_, &total_size); | |
206 | 138 |
139 BrowserThread::PostTask( | |
140 BrowserThread::FILE, | |
141 FROM_HERE, | |
142 base::Bind( | |
143 &Operation::VerifyWriteChunk, this, 0, total_size, continuation)); | |
144 } | |
145 | |
146 void Operation::VerifyWriteChunk(const int64& bytes_processed, | |
147 const int64& total_size, | |
148 const base::Closure& continuation) { | |
149 if (!IsCancelled()) { | |
150 scoped_ptr<char[]> source_buffer(new char[kBurningBlockSize]); | |
151 scoped_ptr<char[]> target_buffer(new char[kBurningBlockSize]); | |
152 | |
153 int64 image_bytes_read = base::ReadPlatformFile( | |
154 image_file_, bytes_processed, source_buffer.get(), kBurningBlockSize); | |
155 | |
156 if (image_bytes_read > 0) { | |
157 int64 device_bytes_read = base::ReadPlatformFile( | |
158 device_file_, bytes_processed, target_buffer.get(), image_bytes_read); | |
159 if (image_bytes_read == device_bytes_read && | |
160 memcmp(source_buffer.get(), target_buffer.get(), image_bytes_read) == | |
161 0) { | |
162 int percent_curr = kProgressComplete * | |
tbarzic
2014/02/13 23:42:47
nit: move kProgressComplete to a new line
Drew Haven
2014/02/14 02:44:30
This is what `git cl format` does automatically.
| |
163 (bytes_processed + image_bytes_read) / total_size; | |
164 | |
165 SetProgress(percent_curr); | |
166 | |
167 BrowserThread::PostTask(BrowserThread::FILE, | |
168 FROM_HERE, | |
169 base::Bind(&Operation::VerifyWriteChunk, | |
170 this, | |
171 bytes_processed + image_bytes_read, | |
172 total_size, | |
173 continuation)); | |
174 return; | |
175 } else { | |
176 Error(error::kVerificationFailed); | |
177 } | |
178 } else if (image_bytes_read == 0) { | |
179 VerifyWriteComplete(continuation); | |
180 } else { // len < 0 | |
181 Error(error::kImageReadError); | |
182 } | |
183 } | |
184 | |
185 base::ClosePlatformFile(image_file_); | |
186 base::ClosePlatformFile(device_file_); | |
187 } | |
188 | |
189 void Operation::VerifyWriteComplete(const base::Closure& continuation) { | |
207 SetProgress(kProgressComplete); | 190 SetProgress(kProgressComplete); |
208 | 191 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
209 Finish(); | |
210 } | 192 } |
211 | 193 |
212 } // namespace image_writer | 194 } // namespace image_writer |
213 } // namespace extensions | 195 } // namespace extensions |
OLD | NEW |