OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/platform_file.h" |
| 6 #include "chrome/utility/image_writer/error_messages.h" |
| 7 #include "chrome/utility/image_writer/image_writer.h" |
| 8 #include "chrome/utility/image_writer/image_writer_handler.h" |
| 9 #include "content/public/utility/utility_thread.h" |
| 10 |
| 11 namespace image_writer { |
| 12 |
| 13 // Since block devices like large sequential access and IPC is expensive we're |
| 14 // doing work in 1MB chunks. |
| 15 const int kBurningBlockSize = 1 << 20; |
| 16 |
| 17 ImageWriter::ImageWriter(ImageWriterHandler* handler) |
| 18 : image_file_(base::kInvalidPlatformFileValue), |
| 19 device_file_(base::kInvalidPlatformFileValue), |
| 20 bytes_processed_(0), |
| 21 handler_(handler) {} |
| 22 |
| 23 ImageWriter::~ImageWriter() { CleanUp(); } |
| 24 |
| 25 void ImageWriter::Write(const base::FilePath& image_path, |
| 26 const base::FilePath& device_path) { |
| 27 if (IsRunning()) { |
| 28 handler_->SendFailed(error::kOperationAlreadyInProgress); |
| 29 return; |
| 30 } |
| 31 |
| 32 image_path_ = image_path; |
| 33 device_path_ = device_path; |
| 34 bytes_processed_ = 0; |
| 35 |
| 36 base::PlatformFileError error; |
| 37 |
| 38 image_file_ = base::CreatePlatformFile( |
| 39 image_path_, |
| 40 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| 41 NULL, |
| 42 &error); |
| 43 |
| 44 if (error != base::PLATFORM_FILE_OK) { |
| 45 DLOG(ERROR) << "Unable to open file for read: " << image_path_.value(); |
| 46 Error(error::kOpenImage); |
| 47 return; |
| 48 } |
| 49 |
| 50 #if defined(OS_WIN) |
| 51 // Windows requires that device files be opened with FILE_FLAG_NO_BUFFERING |
| 52 // and FILE_FLAG_WRITE_THROUGH. These two flags are not part of |
| 53 // PlatformFile::CreatePlatformFile. |
| 54 device_file_ = CreateFile(device_path.value().c_str(), |
| 55 GENERIC_WRITE, |
| 56 FILE_SHARE_WRITE, |
| 57 NULL, |
| 58 OPEN_EXISTING, |
| 59 FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, |
| 60 INVALID_HANDLE_VALUE); |
| 61 |
| 62 if (device_file_ == base::kInvalidPlatformFileValue) { |
| 63 Error(error::kOpenDevice); |
| 64 return; |
| 65 } |
| 66 #else |
| 67 device_file_ = base::CreatePlatformFile( |
| 68 device_path_, |
| 69 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 70 NULL, |
| 71 &error); |
| 72 |
| 73 if (error != base::PLATFORM_FILE_OK) { |
| 74 DLOG(ERROR) << "Unable to open file for write(" << error |
| 75 << "): " << device_path_.value(); |
| 76 Error(error::kOpenDevice); |
| 77 return; |
| 78 } |
| 79 #endif |
| 80 |
| 81 PostProgress(0); |
| 82 |
| 83 PostTask(base::Bind(&ImageWriter::WriteChunk, AsWeakPtr())); |
| 84 } |
| 85 |
| 86 void ImageWriter::Verify(const base::FilePath& image_path, |
| 87 const base::FilePath& device_path) { |
| 88 if (IsRunning()) { |
| 89 handler_->SendFailed(error::kOperationAlreadyInProgress); |
| 90 return; |
| 91 } |
| 92 |
| 93 image_path_ = image_path; |
| 94 device_path_ = device_path; |
| 95 bytes_processed_ = 0; |
| 96 |
| 97 base::PlatformFileError error; |
| 98 |
| 99 image_file_ = base::CreatePlatformFile( |
| 100 image_path_, |
| 101 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| 102 NULL, |
| 103 &error); |
| 104 |
| 105 if (error != base::PLATFORM_FILE_OK) { |
| 106 DLOG(ERROR) << "Unable to open file for read: " << image_path_.value(); |
| 107 Error(error::kOpenImage); |
| 108 return; |
| 109 } |
| 110 |
| 111 device_file_ = base::CreatePlatformFile( |
| 112 device_path_, |
| 113 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| 114 NULL, |
| 115 &error); |
| 116 |
| 117 if (error != base::PLATFORM_FILE_OK) { |
| 118 DLOG(ERROR) << "Unable to open file for read: " << device_path_.value(); |
| 119 Error(error::kOpenDevice); |
| 120 return; |
| 121 } |
| 122 |
| 123 PostProgress(0); |
| 124 |
| 125 PostTask(base::Bind(&ImageWriter::VerifyChunk, AsWeakPtr())); |
| 126 } |
| 127 |
| 128 void ImageWriter::Cancel() { |
| 129 CleanUp(); |
| 130 handler_->SendCancelled(); |
| 131 } |
| 132 |
| 133 bool ImageWriter::IsRunning() const { |
| 134 return image_file_ != base::kInvalidPlatformFileValue || |
| 135 device_file_ != base::kInvalidPlatformFileValue; |
| 136 } |
| 137 |
| 138 void ImageWriter::PostTask(const base::Closure& task) { |
| 139 base::MessageLoop::current()->PostTask(FROM_HERE, task); |
| 140 } |
| 141 |
| 142 void ImageWriter::PostProgress(int64 progress) { |
| 143 handler_->SendProgress(progress); |
| 144 } |
| 145 |
| 146 void ImageWriter::Error(const std::string& message) { |
| 147 CleanUp(); |
| 148 handler_->SendFailed(message); |
| 149 } |
| 150 |
| 151 void ImageWriter::WriteChunk() { |
| 152 if (!IsRunning()) { |
| 153 return; |
| 154 } |
| 155 |
| 156 scoped_ptr<char[]> buffer(new char[kBurningBlockSize]); |
| 157 base::PlatformFileInfo info; |
| 158 |
| 159 int bytes_read = base::ReadPlatformFile( |
| 160 image_file_, bytes_processed_, buffer.get(), kBurningBlockSize); |
| 161 |
| 162 if (bytes_read > 0) { |
| 163 // Always attempt to write a whole block, as Windows requires 512-byte |
| 164 // aligned writes to devices. |
| 165 int bytes_written = base::WritePlatformFile( |
| 166 device_file_, bytes_processed_, buffer.get(), kBurningBlockSize); |
| 167 |
| 168 if (bytes_written < bytes_read) { |
| 169 Error(error::kWriteImage); |
| 170 return; |
| 171 } |
| 172 |
| 173 bytes_processed_ += bytes_read; |
| 174 PostProgress(bytes_processed_); |
| 175 |
| 176 PostTask(base::Bind(&ImageWriter::WriteChunk, AsWeakPtr())); |
| 177 } else if (bytes_read == 0) { |
| 178 // End of file. |
| 179 base::FlushPlatformFile(device_file_); |
| 180 CleanUp(); |
| 181 handler_->SendSucceeded(); |
| 182 } else { |
| 183 // Unable to read entire file. |
| 184 Error(error::kReadImage); |
| 185 } |
| 186 } |
| 187 |
| 188 void ImageWriter::VerifyChunk() { |
| 189 if (!IsRunning()) { |
| 190 return; |
| 191 } |
| 192 |
| 193 scoped_ptr<char[]> image_buffer(new char[kBurningBlockSize]); |
| 194 scoped_ptr<char[]> device_buffer(new char[kBurningBlockSize]); |
| 195 |
| 196 int bytes_read = base::ReadPlatformFile( |
| 197 image_file_, bytes_processed_, image_buffer.get(), kBurningBlockSize); |
| 198 |
| 199 if (bytes_read > 0) { |
| 200 if (base::ReadPlatformFile(device_file_, |
| 201 bytes_processed_, |
| 202 device_buffer.get(), |
| 203 kBurningBlockSize) < 0) { |
| 204 LOG(ERROR) << "Failed to read " << kBurningBlockSize << " bytes of " |
| 205 << "device at offset " << bytes_processed_; |
| 206 Error(error::kReadDevice); |
| 207 return; |
| 208 } |
| 209 |
| 210 if (memcmp(image_buffer.get(), device_buffer.get(), bytes_read) != 0) { |
| 211 LOG(ERROR) << "Write verification failed when comparing " << bytes_read |
| 212 << " bytes at " << bytes_processed_; |
| 213 Error(error::kVerificationFailed); |
| 214 return; |
| 215 } |
| 216 |
| 217 bytes_processed_ += bytes_read; |
| 218 PostProgress(bytes_processed_); |
| 219 |
| 220 PostTask(base::Bind(&ImageWriter::VerifyChunk, AsWeakPtr())); |
| 221 } else if (bytes_read == 0) { |
| 222 // End of file. |
| 223 CleanUp(); |
| 224 handler_->SendSucceeded(); |
| 225 } else { |
| 226 // Unable to read entire file. |
| 227 LOG(ERROR) << "Failed to read " << kBurningBlockSize << " bytes of image " |
| 228 << "at offset " << bytes_processed_; |
| 229 Error(error::kReadImage); |
| 230 } |
| 231 } |
| 232 |
| 233 void ImageWriter::CleanUp() { |
| 234 if (image_file_ != base::kInvalidPlatformFileValue) { |
| 235 base::ClosePlatformFile(image_file_); |
| 236 image_file_ = base::kInvalidPlatformFileValue; |
| 237 } |
| 238 if (device_file_ != base::kInvalidPlatformFileValue) { |
| 239 base::ClosePlatformFile(device_file_); |
| 240 device_file_ = base::kInvalidPlatformFileValue; |
| 241 } |
| 242 } |
| 243 |
| 244 } // namespace image_writer |
OLD | NEW |