OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <shellapi.h> | 8 #include <shellapi.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 #include <time.h> | 10 #include <time.h> |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 } | 490 } |
491 | 491 |
492 FILE* OpenFile(const std::string& filename, const char* mode) { | 492 FILE* OpenFile(const std::string& filename, const char* mode) { |
493 FILE* file; | 493 FILE* file; |
494 if (fopen_s(&file, filename.c_str(), mode) != 0) { | 494 if (fopen_s(&file, filename.c_str(), mode) != 0) { |
495 return NULL; | 495 return NULL; |
496 } | 496 } |
497 return file; | 497 return file; |
498 } | 498 } |
499 | 499 |
500 int ReadFile(const std::wstring& filename, char* data, int size) { | 500 int ReadFile(const FilePath& filename, char* data, int size) { |
501 ScopedHandle file(CreateFile(filename.c_str(), | 501 ScopedHandle file(CreateFile(filename.value().c_str(), |
502 GENERIC_READ, | 502 GENERIC_READ, |
503 FILE_SHARE_READ | FILE_SHARE_WRITE, | 503 FILE_SHARE_READ | FILE_SHARE_WRITE, |
504 NULL, | 504 NULL, |
505 OPEN_EXISTING, | 505 OPEN_EXISTING, |
506 FILE_FLAG_SEQUENTIAL_SCAN, | 506 FILE_FLAG_SEQUENTIAL_SCAN, |
507 NULL)); | 507 NULL)); |
508 if (file == INVALID_HANDLE_VALUE) | 508 if (file == INVALID_HANDLE_VALUE) |
509 return -1; | 509 return -1; |
510 | 510 |
511 int ret_value; | 511 int ret_value; |
512 DWORD read; | 512 DWORD read; |
513 if (::ReadFile(file, data, size, &read, NULL) && read == size) { | 513 if (::ReadFile(file, data, size, &read, NULL) && read == size) { |
514 ret_value = static_cast<int>(read); | 514 ret_value = static_cast<int>(read); |
515 } else { | 515 } else { |
516 ret_value = -1; | 516 ret_value = -1; |
517 } | 517 } |
518 | 518 |
519 return ret_value; | 519 return ret_value; |
520 } | 520 } |
521 | 521 |
522 int WriteFile(const std::wstring& filename, const char* data, int size) { | 522 int WriteFile(const FilePath& filename, const char* data, int size) { |
523 ScopedHandle file(CreateFile(filename.c_str(), | 523 ScopedHandle file(CreateFile(filename.value().c_str(), |
524 GENERIC_WRITE, | 524 GENERIC_WRITE, |
525 0, | 525 0, |
526 NULL, | 526 NULL, |
527 CREATE_ALWAYS, | 527 CREATE_ALWAYS, |
528 0, | 528 0, |
529 NULL)); | 529 NULL)); |
530 if (file == INVALID_HANDLE_VALUE) { | 530 if (file == INVALID_HANDLE_VALUE) { |
531 LOG(WARNING) << "CreateFile failed for path " << filename << | 531 LOG(WARNING) << "CreateFile failed for path " << filename.value() << |
532 " error code=" << GetLastError() << | 532 " error code=" << GetLastError() << |
533 " error text=" << win_util::FormatLastWin32Error(); | 533 " error text=" << win_util::FormatLastWin32Error(); |
534 return -1; | 534 return -1; |
535 } | 535 } |
536 | 536 |
537 DWORD written; | 537 DWORD written; |
538 BOOL result = ::WriteFile(file, data, size, &written, NULL); | 538 BOOL result = ::WriteFile(file, data, size, &written, NULL); |
539 if (result && written == size) | 539 if (result && written == size) |
540 return static_cast<int>(written); | 540 return static_cast<int>(written); |
541 | 541 |
542 if (!result) { | 542 if (!result) { |
543 // WriteFile failed. | 543 // WriteFile failed. |
544 LOG(WARNING) << "writing file " << filename << | 544 LOG(WARNING) << "writing file " << filename.value() << |
545 " failed, error code=" << GetLastError() << | 545 " failed, error code=" << GetLastError() << |
546 " description=" << win_util::FormatLastWin32Error(); | 546 " description=" << win_util::FormatLastWin32Error(); |
547 } else { | 547 } else { |
548 // Didn't write all the bytes. | 548 // Didn't write all the bytes. |
549 LOG(WARNING) << "wrote" << written << " bytes to " << filename << | 549 LOG(WARNING) << "wrote" << written << " bytes to " << |
550 " expected " << size; | 550 filename.value() << " expected " << size; |
551 } | 551 } |
552 return -1; | 552 return -1; |
553 } | 553 } |
554 | 554 |
555 bool RenameFileAndResetSecurityDescriptor(const FilePath& source_file_path, | 555 bool RenameFileAndResetSecurityDescriptor(const FilePath& source_file_path, |
556 const FilePath& target_file_path) { | 556 const FilePath& target_file_path) { |
557 // The parameters to SHFileOperation must be terminated with 2 NULL chars. | 557 // The parameters to SHFileOperation must be terminated with 2 NULL chars. |
558 std::wstring source = source_file_path.value(); | 558 std::wstring source = source_file_path.value(); |
559 std::wstring target = target_file_path.value(); | 559 std::wstring target = target_file_path.value(); |
560 | 560 |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
751 void PathComponents(const std::wstring& path, | 751 void PathComponents(const std::wstring& path, |
752 std::vector<std::wstring>* components) { | 752 std::vector<std::wstring>* components) { |
753 PathComponents(FilePath(path), components); | 753 PathComponents(FilePath(path), components); |
754 } | 754 } |
755 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { | 755 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { |
756 FilePath path(*file_name); | 756 FilePath path(*file_name); |
757 ReplaceExtension(&path, extension); | 757 ReplaceExtension(&path, extension); |
758 file_name->assign(path.value()); | 758 file_name->assign(path.value()); |
759 } | 759 } |
760 } // namespace file_util | 760 } // namespace file_util |
OLD | NEW |