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

Side by Side Diff: base/file_util_posix.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.h ('k') | base/file_util_unittest.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 <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <libgen.h> 10 #include <libgen.h>
(...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); 705 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY));
706 if (fd < 0) 706 if (fd < 0)
707 return -1; 707 return -1;
708 708
709 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); 709 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size));
710 if (int ret = IGNORE_EINTR(close(fd)) < 0) 710 if (int ret = IGNORE_EINTR(close(fd)) < 0)
711 return ret; 711 return ret;
712 return bytes_read; 712 return bytes_read;
713 } 713 }
714 714
715 int WriteFile(const FilePath& filename, const char* data, int size) {
716 ThreadRestrictions::AssertIOAllowed();
717 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666));
718 if (fd < 0)
719 return -1;
720
721 int bytes_written = WriteFileDescriptor(fd, data, size);
722 if (int ret = IGNORE_EINTR(close(fd)) < 0)
723 return ret;
724 return bytes_written;
725 }
726
727 int WriteFileDescriptor(const int fd, const char* data, int size) {
728 // Allow for partial writes.
729 ssize_t bytes_written_total = 0;
730 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
731 bytes_written_total += bytes_written_partial) {
732 bytes_written_partial =
733 HANDLE_EINTR(write(fd, data + bytes_written_total,
734 size - bytes_written_total));
735 if (bytes_written_partial < 0)
736 return -1;
737 }
738
739 return bytes_written_total;
740 }
741
715 } // namespace base 742 } // namespace base
716 743
717 // ----------------------------------------------------------------------------- 744 // -----------------------------------------------------------------------------
718 745
719 namespace file_util { 746 namespace file_util {
720 747
721 using base::stat_wrapper_t; 748 using base::stat_wrapper_t;
722 using base::CallStat; 749 using base::CallStat;
723 using base::CallLstat; 750 using base::CallLstat;
724 using base::CreateAndOpenFdForTemporaryFile; 751 using base::CreateAndOpenFdForTemporaryFile;
(...skipping 18 matching lines...) Expand all
743 else if (errno != EEXIST) 770 else if (errno != EEXIST)
744 break; 771 break;
745 } 772 }
746 return base::FilePath(); 773 return base::FilePath();
747 } 774 }
748 775
749 FILE* OpenFile(const std::string& filename, const char* mode) { 776 FILE* OpenFile(const std::string& filename, const char* mode) {
750 return OpenFile(FilePath(filename), mode); 777 return OpenFile(FilePath(filename), mode);
751 } 778 }
752 779
753 int WriteFile(const FilePath& filename, const char* data, int size) {
754 base::ThreadRestrictions::AssertIOAllowed();
755 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666));
756 if (fd < 0)
757 return -1;
758
759 int bytes_written = WriteFileDescriptor(fd, data, size);
760 if (int ret = IGNORE_EINTR(close(fd)) < 0)
761 return ret;
762 return bytes_written;
763 }
764
765 int WriteFileDescriptor(const int fd, const char* data, int size) {
766 // Allow for partial writes.
767 ssize_t bytes_written_total = 0;
768 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
769 bytes_written_total += bytes_written_partial) {
770 bytes_written_partial =
771 HANDLE_EINTR(write(fd, data + bytes_written_total,
772 size - bytes_written_total));
773 if (bytes_written_partial < 0)
774 return -1;
775 }
776
777 return bytes_written_total;
778 }
779
780 int AppendToFile(const FilePath& filename, const char* data, int size) { 780 int AppendToFile(const FilePath& filename, const char* data, int size) {
781 base::ThreadRestrictions::AssertIOAllowed(); 781 base::ThreadRestrictions::AssertIOAllowed();
782 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); 782 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
783 if (fd < 0) 783 if (fd < 0)
784 return -1; 784 return -1;
785 785
786 int bytes_written = WriteFileDescriptor(fd, data, size); 786 int bytes_written = base::WriteFileDescriptor(fd, data, size);
787 if (int ret = IGNORE_EINTR(close(fd)) < 0) 787 if (int ret = IGNORE_EINTR(close(fd)) < 0)
788 return ret; 788 return ret;
789 return bytes_written; 789 return bytes_written;
790 } 790 }
791 791
792 // Gets the current working directory for the process. 792 // Gets the current working directory for the process.
793 bool GetCurrentDirectory(FilePath* dir) { 793 bool GetCurrentDirectory(FilePath* dir) {
794 // getcwd can return ENOENT, which implies it checks against the disk. 794 // getcwd can return ENOENT, which implies it checks against the disk.
795 base::ThreadRestrictions::AssertIOAllowed(); 795 base::ThreadRestrictions::AssertIOAllowed();
796 796
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
960 result = false; 960 result = false;
961 if (IGNORE_EINTR(close(outfile)) < 0) 961 if (IGNORE_EINTR(close(outfile)) < 0)
962 result = false; 962 result = false;
963 963
964 return result; 964 return result;
965 } 965 }
966 #endif // !defined(OS_MACOSX) 966 #endif // !defined(OS_MACOSX)
967 967
968 } // namespace internal 968 } // namespace internal
969 } // namespace base 969 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698