OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/file_util.h" | 5 #include "base/files/file_util.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <io.h> | 8 #include <io.h> |
9 #include <psapi.h> | 9 #include <psapi.h> |
10 #include <shellapi.h> | 10 #include <shellapi.h> |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 // function uses is explained in the following msdn article: | 497 // function uses is explained in the following msdn article: |
498 // http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx | 498 // http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx |
499 base::win::ScopedHandle file_handle( | 499 base::win::ScopedHandle file_handle( |
500 ::CreateFile(path.value().c_str(), | 500 ::CreateFile(path.value().c_str(), |
501 GENERIC_READ, | 501 GENERIC_READ, |
502 kFileShareAll, | 502 kFileShareAll, |
503 NULL, | 503 NULL, |
504 OPEN_EXISTING, | 504 OPEN_EXISTING, |
505 FILE_ATTRIBUTE_NORMAL, | 505 FILE_ATTRIBUTE_NORMAL, |
506 NULL)); | 506 NULL)); |
507 if (!file_handle) | 507 if (!file_handle.IsValid()) |
508 return false; | 508 return false; |
509 | 509 |
510 // Create a file mapping object. Can't easily use MemoryMappedFile, because | 510 // Create a file mapping object. Can't easily use MemoryMappedFile, because |
511 // we only map the first byte, and need direct access to the handle. You can | 511 // we only map the first byte, and need direct access to the handle. You can |
512 // not map an empty file, this call fails in that case. | 512 // not map an empty file, this call fails in that case. |
513 base::win::ScopedHandle file_map_handle( | 513 base::win::ScopedHandle file_map_handle( |
514 ::CreateFileMapping(file_handle.Get(), | 514 ::CreateFileMapping(file_handle.Get(), |
515 NULL, | 515 NULL, |
516 PAGE_READONLY, | 516 PAGE_READONLY, |
517 0, | 517 0, |
518 1, // Just one byte. No need to look at the data. | 518 1, // Just one byte. No need to look at the data. |
519 NULL)); | 519 NULL)); |
520 if (!file_map_handle) | 520 if (!file_map_handle.IsValid()) |
521 return false; | 521 return false; |
522 | 522 |
523 // Use a view of the file to get the path to the file. | 523 // Use a view of the file to get the path to the file. |
524 void* file_view = MapViewOfFile(file_map_handle.Get(), | 524 void* file_view = MapViewOfFile(file_map_handle.Get(), |
525 FILE_MAP_READ, 0, 0, 1); | 525 FILE_MAP_READ, 0, 0, 1); |
526 if (!file_view) | 526 if (!file_view) |
527 return false; | 527 return false; |
528 | 528 |
529 // The expansion of |path| into a full path may make it longer. | 529 // The expansion of |path| into a full path may make it longer. |
530 // GetMappedFileName() will fail if the result is longer than MAX_PATH. | 530 // GetMappedFileName() will fail if the result is longer than MAX_PATH. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 | 595 |
596 int ReadFile(const FilePath& filename, char* data, int max_size) { | 596 int ReadFile(const FilePath& filename, char* data, int max_size) { |
597 ThreadRestrictions::AssertIOAllowed(); | 597 ThreadRestrictions::AssertIOAllowed(); |
598 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), | 598 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
599 GENERIC_READ, | 599 GENERIC_READ, |
600 FILE_SHARE_READ | FILE_SHARE_WRITE, | 600 FILE_SHARE_READ | FILE_SHARE_WRITE, |
601 NULL, | 601 NULL, |
602 OPEN_EXISTING, | 602 OPEN_EXISTING, |
603 FILE_FLAG_SEQUENTIAL_SCAN, | 603 FILE_FLAG_SEQUENTIAL_SCAN, |
604 NULL)); | 604 NULL)); |
605 if (!file) | 605 if (!file.IsValid()) |
606 return -1; | 606 return -1; |
607 | 607 |
608 DWORD read; | 608 DWORD read; |
609 if (::ReadFile(file, data, max_size, &read, NULL)) | 609 if (::ReadFile(file.Get(), data, max_size, &read, NULL)) |
610 return read; | 610 return read; |
611 | 611 |
612 return -1; | 612 return -1; |
613 } | 613 } |
614 | 614 |
615 int WriteFile(const FilePath& filename, const char* data, int size) { | 615 int WriteFile(const FilePath& filename, const char* data, int size) { |
616 ThreadRestrictions::AssertIOAllowed(); | 616 ThreadRestrictions::AssertIOAllowed(); |
617 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), | 617 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
618 GENERIC_WRITE, | 618 GENERIC_WRITE, |
619 0, | 619 0, |
620 NULL, | 620 NULL, |
621 CREATE_ALWAYS, | 621 CREATE_ALWAYS, |
622 0, | 622 0, |
623 NULL)); | 623 NULL)); |
624 if (!file) { | 624 if (!file.IsValid()) { |
625 DPLOG(WARNING) << "CreateFile failed for path " | 625 DPLOG(WARNING) << "CreateFile failed for path " |
626 << UTF16ToUTF8(filename.value()); | 626 << UTF16ToUTF8(filename.value()); |
627 return -1; | 627 return -1; |
628 } | 628 } |
629 | 629 |
630 DWORD written; | 630 DWORD written; |
631 BOOL result = ::WriteFile(file, data, size, &written, NULL); | 631 BOOL result = ::WriteFile(file.Get(), data, size, &written, NULL); |
632 if (result && static_cast<int>(written) == size) | 632 if (result && static_cast<int>(written) == size) |
633 return written; | 633 return written; |
634 | 634 |
635 if (!result) { | 635 if (!result) { |
636 // WriteFile failed. | 636 // WriteFile failed. |
637 DPLOG(WARNING) << "writing file " << UTF16ToUTF8(filename.value()) | 637 DPLOG(WARNING) << "writing file " << UTF16ToUTF8(filename.value()) |
638 << " failed"; | 638 << " failed"; |
639 } else { | 639 } else { |
640 // Didn't write all the bytes. | 640 // Didn't write all the bytes. |
641 DLOG(WARNING) << "wrote" << written << " bytes to " | 641 DLOG(WARNING) << "wrote" << written << " bytes to " |
642 << UTF16ToUTF8(filename.value()) << " expected " << size; | 642 << UTF16ToUTF8(filename.value()) << " expected " << size; |
643 } | 643 } |
644 return -1; | 644 return -1; |
645 } | 645 } |
646 | 646 |
647 int AppendToFile(const FilePath& filename, const char* data, int size) { | 647 int AppendToFile(const FilePath& filename, const char* data, int size) { |
648 ThreadRestrictions::AssertIOAllowed(); | 648 ThreadRestrictions::AssertIOAllowed(); |
649 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), | 649 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
650 FILE_APPEND_DATA, | 650 FILE_APPEND_DATA, |
651 0, | 651 0, |
652 NULL, | 652 NULL, |
653 OPEN_EXISTING, | 653 OPEN_EXISTING, |
654 0, | 654 0, |
655 NULL)); | 655 NULL)); |
656 if (!file) { | 656 if (!file.IsValid()) { |
657 DPLOG(WARNING) << "CreateFile failed for path " | 657 DPLOG(WARNING) << "CreateFile failed for path " |
658 << UTF16ToUTF8(filename.value()); | 658 << UTF16ToUTF8(filename.value()); |
659 return -1; | 659 return -1; |
660 } | 660 } |
661 | 661 |
662 DWORD written; | 662 DWORD written; |
663 BOOL result = ::WriteFile(file, data, size, &written, NULL); | 663 BOOL result = ::WriteFile(file.Get(), data, size, &written, NULL); |
664 if (result && static_cast<int>(written) == size) | 664 if (result && static_cast<int>(written) == size) |
665 return written; | 665 return written; |
666 | 666 |
667 if (!result) { | 667 if (!result) { |
668 // WriteFile failed. | 668 // WriteFile failed. |
669 DPLOG(WARNING) << "writing file " << UTF16ToUTF8(filename.value()) | 669 DPLOG(WARNING) << "writing file " << UTF16ToUTF8(filename.value()) |
670 << " failed"; | 670 << " failed"; |
671 } else { | 671 } else { |
672 // Didn't write all the bytes. | 672 // Didn't write all the bytes. |
673 DLOG(WARNING) << "wrote" << written << " bytes to " | 673 DLOG(WARNING) << "wrote" << written << " bytes to " |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
801 // Like Move, this function is not transactional, so we just | 801 // Like Move, this function is not transactional, so we just |
802 // leave the copied bits behind if deleting from_path fails. | 802 // leave the copied bits behind if deleting from_path fails. |
803 // If to_path exists previously then we have already overwritten | 803 // If to_path exists previously then we have already overwritten |
804 // it by now, we don't get better off by deleting the new bits. | 804 // it by now, we don't get better off by deleting the new bits. |
805 } | 805 } |
806 return false; | 806 return false; |
807 } | 807 } |
808 | 808 |
809 } // namespace internal | 809 } // namespace internal |
810 } // namespace base | 810 } // namespace base |
OLD | NEW |