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

Side by Side Diff: base/file_util_win.cc

Issue 184563006: Move WriteFile and WriteFileDescriptor from file_util to base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « base/file_util_unittest.cc ('k') | base/files/file_path_watcher_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/file_util.h" 5 #include "base/file_util.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <psapi.h> 8 #include <psapi.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 if (!file) 592 if (!file)
593 return -1; 593 return -1;
594 594
595 DWORD read; 595 DWORD read;
596 if (::ReadFile(file, data, size, &read, NULL) && 596 if (::ReadFile(file, data, size, &read, NULL) &&
597 static_cast<int>(read) == size) 597 static_cast<int>(read) == size)
598 return read; 598 return read;
599 return -1; 599 return -1;
600 } 600 }
601 601
602 } // namespace base
603
604 // -----------------------------------------------------------------------------
605
606 namespace file_util {
607
608 using base::DirectoryExists;
609 using base::FilePath;
610 using base::kFileShareAll;
611
612 FILE* OpenFile(const std::string& filename, const char* mode) {
613 base::ThreadRestrictions::AssertIOAllowed();
614 return _fsopen(filename.c_str(), mode, _SH_DENYNO);
615 }
616
617 int WriteFile(const FilePath& filename, const char* data, int size) { 602 int WriteFile(const FilePath& filename, const char* data, int size) {
618 base::ThreadRestrictions::AssertIOAllowed(); 603 ThreadRestrictions::AssertIOAllowed();
619 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), 604 base::win::ScopedHandle file(CreateFile(filename.value().c_str(),
620 GENERIC_WRITE, 605 GENERIC_WRITE,
621 0, 606 0,
622 NULL, 607 NULL,
623 CREATE_ALWAYS, 608 CREATE_ALWAYS,
624 0, 609 0,
625 NULL)); 610 NULL));
626 if (!file) { 611 if (!file) {
627 DLOG_GETLASTERROR(WARNING) << "CreateFile failed for path " 612 DLOG_GETLASTERROR(WARNING) << "CreateFile failed for path "
628 << filename.value(); 613 << UTF16ToUTF8(filename.value());
629 return -1; 614 return -1;
630 } 615 }
631 616
632 DWORD written; 617 DWORD written;
633 BOOL result = ::WriteFile(file, data, size, &written, NULL); 618 BOOL result = ::WriteFile(file, data, size, &written, NULL);
634 if (result && static_cast<int>(written) == size) 619 if (result && static_cast<int>(written) == size)
635 return written; 620 return written;
636 621
637 if (!result) { 622 if (!result) {
638 // WriteFile failed. 623 // WriteFile failed.
639 DLOG_GETLASTERROR(WARNING) << "writing file " << filename.value() 624 DLOG_GETLASTERROR(WARNING) << "writing file "
640 << " failed"; 625 << UTF16ToUTF8(filename.value()) << " failed";
641 } else { 626 } else {
642 // Didn't write all the bytes. 627 // Didn't write all the bytes.
643 DLOG(WARNING) << "wrote" << written << " bytes to " 628 DLOG(WARNING) << "wrote" << written << " bytes to "
644 << filename.value() << " expected " << size; 629 << UTF16ToUTF8(filename.value()) << " expected " << size;
645 } 630 }
646 return -1; 631 return -1;
647 } 632 }
648 633
634 } // namespace base
635
636 // -----------------------------------------------------------------------------
637
638 namespace file_util {
639
640 using base::DirectoryExists;
641 using base::FilePath;
642 using base::kFileShareAll;
643
644 FILE* OpenFile(const std::string& filename, const char* mode) {
645 base::ThreadRestrictions::AssertIOAllowed();
646 return _fsopen(filename.c_str(), mode, _SH_DENYNO);
647 }
648
649 int AppendToFile(const FilePath& filename, const char* data, int size) { 649 int AppendToFile(const FilePath& filename, const char* data, int size) {
650 base::ThreadRestrictions::AssertIOAllowed(); 650 base::ThreadRestrictions::AssertIOAllowed();
651 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), 651 base::win::ScopedHandle file(CreateFile(filename.value().c_str(),
652 FILE_APPEND_DATA, 652 FILE_APPEND_DATA,
653 0, 653 0,
654 NULL, 654 NULL,
655 OPEN_EXISTING, 655 OPEN_EXISTING,
656 0, 656 0,
657 NULL)); 657 NULL));
658 if (!file) { 658 if (!file) {
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 // Like Move, this function is not transactional, so we just 804 // Like Move, this function is not transactional, so we just
805 // leave the copied bits behind if deleting from_path fails. 805 // leave the copied bits behind if deleting from_path fails.
806 // If to_path exists previously then we have already overwritten 806 // If to_path exists previously then we have already overwritten
807 // it by now, we don't get better off by deleting the new bits. 807 // it by now, we don't get better off by deleting the new bits.
808 } 808 }
809 return false; 809 return false;
810 } 810 }
811 811
812 } // namespace internal 812 } // namespace internal
813 } // namespace base 813 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util_unittest.cc ('k') | base/files/file_path_watcher_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698