| OLD | NEW |
| 1 // Copyright 2014 The Chromium OS Authors. All rights reserved. | 1 // Copyright 2014 The Chromium OS 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 "volume.h" | 5 #include "volume.h" |
| 6 | 6 |
| 7 #include <cstring> | 7 #include <cstring> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 | 9 |
| 10 #include "request.h" | 10 #include "request.h" |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 archive_size(archive_size) {} | 175 archive_size(archive_size) {} |
| 176 const std::string request_id; | 176 const std::string request_id; |
| 177 const int64_t index; | 177 const int64_t index; |
| 178 const std::string encoding; | 178 const std::string encoding; |
| 179 const int64_t archive_size; | 179 const int64_t archive_size; |
| 180 }; | 180 }; |
| 181 | 181 |
| 182 Volume::Volume(const pp::InstanceHandle& instance_handle, | 182 Volume::Volume(const pp::InstanceHandle& instance_handle, |
| 183 const std::string& file_system_id, | 183 const std::string& file_system_id, |
| 184 JavaScriptMessageSenderInterface* message_sender) | 184 JavaScriptMessageSenderInterface* message_sender) |
| 185 : volume_archive_(NULL), | 185 : volume_archive_(nullptr), |
| 186 file_system_id_(file_system_id), | 186 file_system_id_(file_system_id), |
| 187 message_sender_(message_sender), | 187 message_sender_(message_sender), |
| 188 worker_(instance_handle), | 188 worker_(instance_handle), |
| 189 callback_factory_(this) { | 189 callback_factory_(this) { |
| 190 requestor_ = new JavaScriptRequestor(this); | 190 requestor_ = new JavaScriptRequestor(this); |
| 191 volume_archive_factory_ = new VolumeArchiveFactory(); | 191 volume_archive_factory_ = new VolumeArchiveFactory(); |
| 192 volume_reader_factory_ = new VolumeReaderFactory(this); | 192 volume_reader_factory_ = new VolumeReaderFactory(this); |
| 193 // Delegating constructors only from c++11. | 193 // Delegating constructors only from c++11. |
| 194 } | 194 } |
| 195 | 195 |
| 196 Volume::Volume(const pp::InstanceHandle& instance_handle, | 196 Volume::Volume(const pp::InstanceHandle& instance_handle, |
| 197 const std::string& file_system_id, | 197 const std::string& file_system_id, |
| 198 JavaScriptMessageSenderInterface* message_sender, | 198 JavaScriptMessageSenderInterface* message_sender, |
| 199 VolumeArchiveFactoryInterface* volume_archive_factory, | 199 VolumeArchiveFactoryInterface* volume_archive_factory, |
| 200 VolumeReaderFactoryInterface* volume_reader_factory) | 200 VolumeReaderFactoryInterface* volume_reader_factory) |
| 201 : volume_archive_(NULL), | 201 : volume_archive_(nullptr), |
| 202 file_system_id_(file_system_id), | 202 file_system_id_(file_system_id), |
| 203 message_sender_(message_sender), | 203 message_sender_(message_sender), |
| 204 worker_(instance_handle), | 204 worker_(instance_handle), |
| 205 callback_factory_(this), | 205 callback_factory_(this), |
| 206 volume_archive_factory_(volume_archive_factory), | 206 volume_archive_factory_(volume_archive_factory), |
| 207 volume_reader_factory_(volume_reader_factory) { | 207 volume_reader_factory_(volume_reader_factory) { |
| 208 requestor_ = new JavaScriptRequestor(this); | 208 requestor_ = new JavaScriptRequestor(this); |
| 209 } | 209 } |
| 210 | 210 |
| 211 Volume::~Volume() { | 211 Volume::~Volume() { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 static_cast<VolumeReaderJavaScriptStream*>(volume_archive_->reader())-> | 316 static_cast<VolumeReaderJavaScriptStream*>(volume_archive_->reader())-> |
| 317 SetRequestId(request_id); | 317 SetRequestId(request_id); |
| 318 reader_request_id_ = request_id; | 318 reader_request_id_ = request_id; |
| 319 job_lock_.Release(); | 319 job_lock_.Release(); |
| 320 | 320 |
| 321 if (!volume_archive_->Init(encoding)) { | 321 if (!volume_archive_->Init(encoding)) { |
| 322 message_sender_->SendFileSystemError( | 322 message_sender_->SendFileSystemError( |
| 323 file_system_id_, request_id, volume_archive_->error_message()); | 323 file_system_id_, request_id, volume_archive_->error_message()); |
| 324 ClearJob(); | 324 ClearJob(); |
| 325 delete volume_archive_; | 325 delete volume_archive_; |
| 326 volume_archive_ = NULL; | 326 volume_archive_ = nullptr; |
| 327 return; | 327 return; |
| 328 } | 328 } |
| 329 | 329 |
| 330 // Read and construct metadata. | 330 // Read and construct metadata. |
| 331 pp::VarDictionary root_metadata = CreateEntry(-1, "" /* name */, true, 0, 0); | 331 pp::VarDictionary root_metadata = CreateEntry(-1, "" /* name */, true, 0, 0); |
| 332 | 332 |
| 333 const char* path_name = NULL; | 333 std::string path_name; |
| 334 int64_t size = 0; | 334 int64_t size = 0; |
| 335 bool is_directory = false; | 335 bool is_directory = false; |
| 336 time_t modification_time = 0; | 336 time_t modification_time = 0; |
| 337 int64_t index = 0; | 337 int64_t index = 0; |
| 338 | 338 |
| 339 for (;;) { | 339 for (;;) { |
| 340 VolumeArchive::Result ret = volume_archive_->GetNextHeader( | 340 path_name.clear(); |
| 341 &path_name, &size, &is_directory, &modification_time); | 341 if (volume_archive_->GetCurrentFileInfo( |
| 342 if (ret == VolumeArchive::RESULT_FAIL) { | 342 &path_name, |
| 343 message_sender_->SendFileSystemError( | 343 &size, |
| 344 file_system_id_, request_id, volume_archive_->error_message()); | 344 &is_directory, |
| 345 &modification_time) == VolumeArchive::RESULT_FAIL) { |
| 346 message_sender_->SendFileSystemError(file_system_id_, request_id, |
| 347 volume_archive_->error_message()); |
| 345 ClearJob(); | 348 ClearJob(); |
| 346 delete volume_archive_; | 349 delete volume_archive_; |
| 347 volume_archive_ = NULL; | 350 volume_archive_ = nullptr; |
| 348 return; | 351 return; |
| 349 } else if (ret == VolumeArchive::RESULT_EOF) | 352 } |
| 353 |
| 354 if (path_name.empty()) // End of archive. |
| 350 break; | 355 break; |
| 351 | 356 |
| 352 ConstructMetadata(index, path_name, size, is_directory, modification_time, | 357 ConstructMetadata(index, path_name.c_str(), size, is_directory, |
| 353 &root_metadata); | 358 modification_time, &root_metadata); |
| 359 |
| 360 index_to_pathname_[index] = path_name; |
| 354 | 361 |
| 355 ++index; | 362 ++index; |
| 363 |
| 364 int return_value = volume_archive_->GoToNextFile(); |
| 365 if (return_value == VolumeArchive::RESULT_FAIL) { |
| 366 message_sender_->SendFileSystemError(file_system_id_, request_id, |
| 367 volume_archive_->error_message()); |
| 368 ClearJob(); |
| 369 delete volume_archive_; |
| 370 volume_archive_ = nullptr; |
| 371 return; |
| 372 } |
| 373 if (return_value == VolumeArchive::RESULT_EOF) |
| 374 break; |
| 356 } | 375 } |
| 357 | 376 |
| 358 ClearJob(); | 377 ClearJob(); |
| 359 | 378 |
| 360 // Send metadata back to JavaScript. | 379 // Send metadata back to JavaScript. |
| 361 message_sender_->SendReadMetadataDone( | 380 message_sender_->SendReadMetadataDone(file_system_id_, request_id, |
| 362 file_system_id_, request_id, root_metadata); | 381 root_metadata); |
| 363 } | 382 } |
| 364 | 383 |
| 365 void Volume::OpenFileCallback(int32_t /*result*/, | 384 void Volume::OpenFileCallback(int32_t /*result*/, |
| 366 const OpenFileArgs& args) { | 385 const OpenFileArgs& args) { |
| 367 if (!volume_archive_) { | 386 if (!volume_archive_) { |
| 368 message_sender_->SendFileSystemError( | 387 message_sender_->SendFileSystemError( |
| 369 file_system_id_, args.request_id, "NOT_OPENED"); | 388 file_system_id_, args.request_id, "NOT_OPENED"); |
| 370 return; | 389 return; |
| 371 } | 390 } |
| 372 | 391 |
| 373 job_lock_.Acquire(); | 392 job_lock_.Acquire(); |
| 374 if (!reader_request_id_.empty()) { | 393 if (!reader_request_id_.empty()) { |
| 375 // It is illegal to open a file while another operation is in progress or | 394 // It is illegal to open a file while another operation is in progress or |
| 376 // another file is opened. | 395 // another file is opened. |
| 377 message_sender_->SendFileSystemError( | 396 message_sender_->SendFileSystemError( |
| 378 file_system_id_, args.request_id, "ILLEGAL"); | 397 file_system_id_, args.request_id, "ILLEGAL"); |
| 379 job_lock_.Release(); | 398 job_lock_.Release(); |
| 380 return; | 399 return; |
| 381 } | 400 } |
| 382 static_cast<VolumeReaderJavaScriptStream*>(volume_archive_->reader())-> | 401 |
| 383 SetRequestId(args.request_id); | 402 static_cast<VolumeReaderJavaScriptStream*>(volume_archive_->reader()) |
| 403 ->SetRequestId(args.request_id); |
| 384 reader_request_id_ = args.request_id; | 404 reader_request_id_ = args.request_id; |
| 385 job_lock_.Release(); | 405 job_lock_.Release(); |
| 386 | 406 |
| 387 if (!volume_archive_->SeekHeader(args.index)) { | 407 std::string path_name = index_to_pathname_[args.index]; |
| 388 message_sender_->SendFileSystemError( | 408 int64_t size = 0; |
| 389 file_system_id_, args.request_id, volume_archive_->error_message()); | 409 bool is_directory = false; |
| 410 time_t modification_time = 0; |
| 411 |
| 412 if (!volume_archive_->SeekHeader(path_name)) { |
| 413 message_sender_->SendFileSystemError(file_system_id_, args.request_id, |
| 414 volume_archive_->error_message()); |
| 390 ClearJob(); | 415 ClearJob(); |
| 391 return; | 416 return; |
| 392 } | 417 } |
| 393 | 418 |
| 394 if (volume_archive_->GetNextHeader() == VolumeArchive::RESULT_FAIL) { | 419 if (volume_archive_->GetCurrentFileInfo( |
| 395 message_sender_->SendFileSystemError( | 420 &path_name, |
| 396 file_system_id_, args.request_id, volume_archive_->error_message()); | 421 &size, |
| 422 &is_directory, |
| 423 &modification_time) != VolumeArchive::RESULT_SUCCESS) { |
| 424 message_sender_->SendFileSystemError(file_system_id_, args.request_id, |
| 425 volume_archive_->error_message()); |
| 397 ClearJob(); | 426 ClearJob(); |
| 398 return; | 427 return; |
| 399 } | 428 } |
| 400 | 429 |
| 401 // Send successful opened file response to NaCl. | 430 // Send successful opened file response to NaCl. |
| 402 message_sender_->SendOpenFileDone(file_system_id_, args.request_id); | 431 message_sender_->SendOpenFileDone(file_system_id_, args.request_id); |
| 403 } | 432 } |
| 404 | 433 |
| 405 void Volume::CloseFileCallback(int32_t /*result*/, | 434 void Volume::CloseFileCallback(int32_t /*result*/, |
| 406 const std::string& request_id, | 435 const std::string& request_id, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 437 file_system_id_, request_id, "FILE_NOT_OPENED"); | 466 file_system_id_, request_id, "FILE_NOT_OPENED"); |
| 438 job_lock_.Release(); | 467 job_lock_.Release(); |
| 439 return; | 468 return; |
| 440 } | 469 } |
| 441 job_lock_.Release(); | 470 job_lock_.Release(); |
| 442 | 471 |
| 443 // Decompress data and send it to JavaScript. Sending data is done in chunks | 472 // Decompress data and send it to JavaScript. Sending data is done in chunks |
| 444 // depending on how many bytes VolumeArchive::ReadData returns. | 473 // depending on how many bytes VolumeArchive::ReadData returns. |
| 445 int64_t left_length = length; | 474 int64_t left_length = length; |
| 446 while (left_length > 0) { | 475 while (left_length > 0) { |
| 447 const char* destination_buffer = NULL; | 476 const char* destination_buffer = nullptr; |
| 448 int64_t read_bytes = volume_archive_->ReadData( | 477 int64_t read_bytes = volume_archive_->ReadData( |
| 449 offset, left_length, &destination_buffer); | 478 offset, left_length, &destination_buffer); |
| 450 | 479 |
| 451 if (read_bytes < 0) { | 480 if (read_bytes < 0) { |
| 452 // Error messages should be sent to the read request (request_id), not | 481 // Error messages should be sent to the read request (request_id), not |
| 453 // open request (open_request_id), as the last one has finished and this | 482 // open request (open_request_id), as the last one has finished and this |
| 454 // is a read file. | 483 // is a read file. |
| 455 message_sender_->SendFileSystemError( | 484 message_sender_->SendFileSystemError( |
| 456 file_system_id_, request_id, volume_archive_->error_message()); | 485 file_system_id_, request_id, volume_archive_->error_message()); |
| 457 | 486 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 480 } | 509 } |
| 481 volume_archive_->MaybeDecompressAhead(); | 510 volume_archive_->MaybeDecompressAhead(); |
| 482 } | 511 } |
| 483 | 512 |
| 484 | 513 |
| 485 void Volume::ClearJob() { | 514 void Volume::ClearJob() { |
| 486 job_lock_.Acquire(); | 515 job_lock_.Acquire(); |
| 487 reader_request_id_ = ""; | 516 reader_request_id_ = ""; |
| 488 job_lock_.Release(); | 517 job_lock_.Release(); |
| 489 } | 518 } |
| OLD | NEW |