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

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

Issue 603683005: base::CopyFile can copy *from* Android's content scheme. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Modified CopyFile test to assert inability to copy to parent dir. Created 6 years, 1 month 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 | « no previous file | base/files/file_util_unittest.cc » ('j') | base/files/file_util_unittest.cc » ('J')
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 862 matching lines...) Expand 10 before | Expand all | Expand 10 after
873 return false; 873 return false;
874 874
875 DeleteFile(from_path, true); 875 DeleteFile(from_path, true);
876 return true; 876 return true;
877 } 877 }
878 878
879 #if !defined(OS_MACOSX) 879 #if !defined(OS_MACOSX)
880 // Mac has its own implementation, this is for all other Posix systems. 880 // Mac has its own implementation, this is for all other Posix systems.
881 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { 881 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
882 ThreadRestrictions::AssertIOAllowed(); 882 ThreadRestrictions::AssertIOAllowed();
883 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY)); 883 File infile;
884 if (infile < 0) 884 #if defined(OS_ANDROID)
885 if (from_path.IsContentUri()) {
886 infile = OpenContentUriForRead(from_path);
887 } else {
888 infile = File(from_path, File::FLAG_OPEN | File::FLAG_READ);
889 }
890 #else
891 infile = File(from_path, File::FLAG_OPEN | File::FLAG_READ);
892 #endif
893 if (!infile.IsValid())
885 return false; 894 return false;
886 895
887 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666)); 896 File outfile(to_path, File::FLAG_WRITE | File::FLAG_CREATE_ALWAYS);
888 if (outfile < 0) { 897 if (!outfile.IsValid())
889 close(infile);
890 return false; 898 return false;
891 }
892 899
893 const size_t kBufferSize = 32768; 900 const size_t kBufferSize = 32768;
894 std::vector<char> buffer(kBufferSize); 901 std::vector<char> buffer(kBufferSize);
895 bool result = true; 902 bool result = true;
896 903
897 while (result) { 904 while (result) {
898 ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size())); 905 ssize_t bytes_read = infile.ReadAtCurrentPos(&buffer[0], buffer.size());
899 if (bytes_read < 0) { 906 if (bytes_read < 0) {
900 result = false; 907 result = false;
901 break; 908 break;
902 } 909 }
903 if (bytes_read == 0) 910 if (bytes_read == 0)
904 break; 911 break;
905 // Allow for partial writes 912 // Allow for partial writes
906 ssize_t bytes_written_per_read = 0; 913 ssize_t bytes_written_per_read = 0;
907 do { 914 do {
908 ssize_t bytes_written_partial = HANDLE_EINTR(write( 915 ssize_t bytes_written_partial = outfile.WriteAtCurrentPos(
909 outfile, 916 &buffer[bytes_written_per_read], bytes_read - bytes_written_per_read);
910 &buffer[bytes_written_per_read],
911 bytes_read - bytes_written_per_read));
912 if (bytes_written_partial < 0) { 917 if (bytes_written_partial < 0) {
913 result = false; 918 result = false;
914 break; 919 break;
915 } 920 }
916 bytes_written_per_read += bytes_written_partial; 921 bytes_written_per_read += bytes_written_partial;
917 } while (bytes_written_per_read < bytes_read); 922 } while (bytes_written_per_read < bytes_read);
918 } 923 }
919 924
920 if (IGNORE_EINTR(close(infile)) < 0)
921 result = false;
922 if (IGNORE_EINTR(close(outfile)) < 0)
923 result = false;
924
925 return result; 925 return result;
926 } 926 }
927 #endif // !defined(OS_MACOSX) 927 #endif // !defined(OS_MACOSX)
928 928
929 } // namespace internal 929 } // namespace internal
930 930
931 #endif // !defined(OS_NACL_NONSFI) 931 #endif // !defined(OS_NACL_NONSFI)
932 } // namespace base 932 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/files/file_util_unittest.cc » ('j') | base/files/file_util_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698