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

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

Issue 614893004: Refactor AppendToFile and WriteFileDescriptor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address nits 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
« no previous file with comments | « base/files/file_util.h ('k') | base/files/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/files/file_util.h" 5 #include "base/files/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 664 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 return -1; 675 return -1;
676 return bytes_read; 676 return bytes_read;
677 } 677 }
678 678
679 int WriteFile(const FilePath& filename, const char* data, int size) { 679 int WriteFile(const FilePath& filename, const char* data, int size) {
680 ThreadRestrictions::AssertIOAllowed(); 680 ThreadRestrictions::AssertIOAllowed();
681 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0640)); 681 int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0640));
682 if (fd < 0) 682 if (fd < 0)
683 return -1; 683 return -1;
684 684
685 int bytes_written = WriteFileDescriptor(fd, data, size); 685 int bytes_written = WriteFileDescriptor(fd, data, size) ? size : -1;
686 if (IGNORE_EINTR(close(fd)) < 0) 686 if (IGNORE_EINTR(close(fd)) < 0)
687 return -1; 687 return -1;
688 return bytes_written; 688 return bytes_written;
689 } 689 }
690 690
691 int WriteFileDescriptor(const int fd, const char* data, int size) { 691 bool WriteFileDescriptor(const int fd, const char* data, int size) {
692 // Allow for partial writes. 692 // Allow for partial writes.
693 ssize_t bytes_written_total = 0; 693 ssize_t bytes_written_total = 0;
694 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; 694 for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
695 bytes_written_total += bytes_written_partial) { 695 bytes_written_total += bytes_written_partial) {
696 bytes_written_partial = 696 bytes_written_partial =
697 HANDLE_EINTR(write(fd, data + bytes_written_total, 697 HANDLE_EINTR(write(fd, data + bytes_written_total,
698 size - bytes_written_total)); 698 size - bytes_written_total));
699 if (bytes_written_partial < 0) 699 if (bytes_written_partial < 0)
700 return -1; 700 return false;
701 } 701 }
702 702
703 return bytes_written_total; 703 return true;
704 } 704 }
705 705
706 int AppendToFile(const FilePath& filename, const char* data, int size) { 706 bool AppendToFile(const FilePath& filename, const char* data, int size) {
707 ThreadRestrictions::AssertIOAllowed(); 707 ThreadRestrictions::AssertIOAllowed();
708 bool ret = true;
708 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); 709 int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
709 if (fd < 0) 710 if (fd < 0) {
710 return -1; 711 VPLOG(1) << "Unable to create file " << filename.value();
712 return false;
713 }
711 714
712 int bytes_written = WriteFileDescriptor(fd, data, size); 715 // This call will either write all of the data or return false.
713 if (IGNORE_EINTR(close(fd)) < 0) 716 if (!WriteFileDescriptor(fd, data, size)) {
714 return -1; 717 VPLOG(1) << "Error while writing to file " << filename.value();
715 return bytes_written; 718 ret = false;
719 }
720
721 if (IGNORE_EINTR(close(fd)) < 0) {
722 VPLOG(1) << "Error while closing file " << filename.value();
723 return false;
724 }
725
726 return ret;
716 } 727 }
717 728
718 // Gets the current working directory for the process. 729 // Gets the current working directory for the process.
719 bool GetCurrentDirectory(FilePath* dir) { 730 bool GetCurrentDirectory(FilePath* dir) {
720 // getcwd can return ENOENT, which implies it checks against the disk. 731 // getcwd can return ENOENT, which implies it checks against the disk.
721 ThreadRestrictions::AssertIOAllowed(); 732 ThreadRestrictions::AssertIOAllowed();
722 733
723 char system_buffer[PATH_MAX] = ""; 734 char system_buffer[PATH_MAX] = "";
724 if (!getcwd(system_buffer, sizeof(system_buffer))) { 735 if (!getcwd(system_buffer, sizeof(system_buffer))) {
725 NOTREACHED(); 736 NOTREACHED();
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
903 result = false; 914 result = false;
904 if (IGNORE_EINTR(close(outfile)) < 0) 915 if (IGNORE_EINTR(close(outfile)) < 0)
905 result = false; 916 result = false;
906 917
907 return result; 918 return result;
908 } 919 }
909 #endif // !defined(OS_MACOSX) 920 #endif // !defined(OS_MACOSX)
910 921
911 } // namespace internal 922 } // namespace internal
912 } // namespace base 923 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util.h ('k') | base/files/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698