Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: base/files/file_util_win.cc

Issue 614893004: Refactor AppendToFile and WriteFileDescriptor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 626 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 bool 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.IsValid()) { 656 if (!file.IsValid())
657 DPLOG(WARNING) << "CreateFile failed for path " 657 return false;
658 << UTF16ToUTF8(filename.value()); 658
659 return -1; 659 DWORD saved_error = 0;
660 DWORD written;
661
662 // According to MSFT documentation the only time a partial write can occur is
663 // if we are writing to a pipe, which we aren't currently doing so if this
rvargas (doing something else) 2014/10/01 20:01:14 I don't think this code has enough context to know
664 // call succeeds we can assume that all the data was written out to the file.
665 if (!::WriteFile(file.Get(), data, size, &written, NULL))
666 saved_error = ::GetLastError();
667
668 // Close the file now so that we can restore the write error, if any.
669 file.Close();
670
671 // Restore any error that occurred while writing the data. This takes
672 // precedence over any errors that happened while closing the file.
673 if (saved_error != 0) {
674 ::SetLastError(saved_error);
675 return false;
660 } 676 }
661 677
662 DWORD written; 678 return true;
663 BOOL result = ::WriteFile(file.Get(), data, size, &written, NULL);
664 if (result && static_cast<int>(written) == size)
665 return written;
666
667 if (!result) {
668 // WriteFile failed.
669 DPLOG(WARNING) << "writing file " << UTF16ToUTF8(filename.value())
670 << " failed";
671 } else {
672 // Didn't write all the bytes.
673 DLOG(WARNING) << "wrote" << written << " bytes to "
674 << UTF16ToUTF8(filename.value()) << " expected " << size;
675 }
676 return -1;
677 } 679 }
678 680
679 // Gets the current working directory for the process. 681 // Gets the current working directory for the process.
680 bool GetCurrentDirectory(FilePath* dir) { 682 bool GetCurrentDirectory(FilePath* dir) {
681 ThreadRestrictions::AssertIOAllowed(); 683 ThreadRestrictions::AssertIOAllowed();
682 684
683 wchar_t system_buffer[MAX_PATH]; 685 wchar_t system_buffer[MAX_PATH];
684 system_buffer[0] = 0; 686 system_buffer[0] = 0;
685 DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); 687 DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer);
686 if (len == 0 || len > MAX_PATH) 688 if (len == 0 || len > MAX_PATH)
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 // Like Move, this function is not transactional, so we just 803 // Like Move, this function is not transactional, so we just
802 // leave the copied bits behind if deleting from_path fails. 804 // leave the copied bits behind if deleting from_path fails.
803 // If to_path exists previously then we have already overwritten 805 // 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. 806 // it by now, we don't get better off by deleting the new bits.
805 } 807 }
806 return false; 808 return false;
807 } 809 }
808 810
809 } // namespace internal 811 } // namespace internal
810 } // namespace base 812 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698