| OLD | NEW |
| 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 15 matching lines...) Expand all Loading... |
| 26 #include "base/mac/foundation_util.h" | 26 #include "base/mac/foundation_util.h" |
| 27 #elif !defined(OS_CHROMEOS) && defined(USE_GLIB) | 27 #elif !defined(OS_CHROMEOS) && defined(USE_GLIB) |
| 28 #include <glib.h> // for g_get_home_dir() | 28 #include <glib.h> // for g_get_home_dir() |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 #include <fstream> | 31 #include <fstream> |
| 32 | 32 |
| 33 #include "base/basictypes.h" | 33 #include "base/basictypes.h" |
| 34 #include "base/files/file_enumerator.h" | 34 #include "base/files/file_enumerator.h" |
| 35 #include "base/files/file_path.h" | 35 #include "base/files/file_path.h" |
| 36 #include "base/files/scoped_file.h" |
| 36 #include "base/logging.h" | 37 #include "base/logging.h" |
| 37 #include "base/memory/scoped_ptr.h" | 38 #include "base/memory/scoped_ptr.h" |
| 38 #include "base/memory/singleton.h" | 39 #include "base/memory/singleton.h" |
| 39 #include "base/path_service.h" | 40 #include "base/path_service.h" |
| 40 #include "base/posix/eintr_wrapper.h" | 41 #include "base/posix/eintr_wrapper.h" |
| 41 #include "base/stl_util.h" | 42 #include "base/stl_util.h" |
| 42 #include "base/strings/string_util.h" | 43 #include "base/strings/string_util.h" |
| 43 #include "base/strings/stringprintf.h" | 44 #include "base/strings/stringprintf.h" |
| 44 #include "base/strings/sys_string_conversions.h" | 45 #include "base/strings/sys_string_conversions.h" |
| 45 #include "base/strings/utf_string_conversions.h" | 46 #include "base/strings/utf_string_conversions.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 166 |
| 166 #if defined(OS_LINUX) | 167 #if defined(OS_LINUX) |
| 167 // Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC. | 168 // Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC. |
| 168 // This depends on the mount options used for /dev/shm, which vary among | 169 // This depends on the mount options used for /dev/shm, which vary among |
| 169 // different Linux distributions and possibly local configuration. It also | 170 // different Linux distributions and possibly local configuration. It also |
| 170 // depends on details of kernel--ChromeOS uses the noexec option for /dev/shm | 171 // depends on details of kernel--ChromeOS uses the noexec option for /dev/shm |
| 171 // but its kernel allows mprotect with PROT_EXEC anyway. | 172 // but its kernel allows mprotect with PROT_EXEC anyway. |
| 172 bool DetermineDevShmExecutable() { | 173 bool DetermineDevShmExecutable() { |
| 173 bool result = false; | 174 bool result = false; |
| 174 FilePath path; | 175 FilePath path; |
| 175 int fd = CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path); | 176 |
| 176 if (fd >= 0) { | 177 ScopedFD fd(CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path)); |
| 177 file_util::ScopedFD shm_fd_closer(&fd); | 178 if (fd.is_valid()) { |
| 178 DeleteFile(path, false); | 179 DeleteFile(path, false); |
| 179 long sysconf_result = sysconf(_SC_PAGESIZE); | 180 long sysconf_result = sysconf(_SC_PAGESIZE); |
| 180 CHECK_GE(sysconf_result, 0); | 181 CHECK_GE(sysconf_result, 0); |
| 181 size_t pagesize = static_cast<size_t>(sysconf_result); | 182 size_t pagesize = static_cast<size_t>(sysconf_result); |
| 182 CHECK_GE(sizeof(pagesize), sizeof(sysconf_result)); | 183 CHECK_GE(sizeof(pagesize), sizeof(sysconf_result)); |
| 183 void *mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0); | 184 void *mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd.get(), 0); |
| 184 if (mapping != MAP_FAILED) { | 185 if (mapping != MAP_FAILED) { |
| 185 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0) | 186 if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0) |
| 186 result = true; | 187 result = true; |
| 187 munmap(mapping, pagesize); | 188 munmap(mapping, pagesize); |
| 188 } | 189 } |
| 189 } | 190 } |
| 190 return result; | 191 return result; |
| 191 } | 192 } |
| 192 #endif // defined(OS_LINUX) | 193 #endif // defined(OS_LINUX) |
| 193 | 194 |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 653 if (S_ISLNK(st.st_mode)) | 654 if (S_ISLNK(st.st_mode)) |
| 654 return true; | 655 return true; |
| 655 else | 656 else |
| 656 return false; | 657 return false; |
| 657 } | 658 } |
| 658 | 659 |
| 659 bool GetFileInfo(const FilePath& file_path, File::Info* results) { | 660 bool GetFileInfo(const FilePath& file_path, File::Info* results) { |
| 660 stat_wrapper_t file_info; | 661 stat_wrapper_t file_info; |
| 661 #if defined(OS_ANDROID) | 662 #if defined(OS_ANDROID) |
| 662 if (file_path.IsContentUri()) { | 663 if (file_path.IsContentUri()) { |
| 663 int fd = OpenContentUriForRead(file_path); | 664 ScopedFD fd(OpenContentUriForRead(file_path)); |
| 664 if (fd < 0) | 665 if (!fd.is_valid()) |
| 665 return false; | 666 return false; |
| 666 file_util::ScopedFD scoped_fd(&fd); | 667 if (CallFstat(fd.get(), &file_info) != 0) |
| 667 if (CallFstat(fd, &file_info) != 0) | |
| 668 return false; | 668 return false; |
| 669 } else { | 669 } else { |
| 670 #endif // defined(OS_ANDROID) | 670 #endif // defined(OS_ANDROID) |
| 671 if (CallStat(file_path.value().c_str(), &file_info) != 0) | 671 if (CallStat(file_path.value().c_str(), &file_info) != 0) |
| 672 return false; | 672 return false; |
| 673 #if defined(OS_ANDROID) | 673 #if defined(OS_ANDROID) |
| 674 } | 674 } |
| 675 #endif // defined(OS_ANDROID) | 675 #endif // defined(OS_ANDROID) |
| 676 results->is_directory = S_ISDIR(file_info.st_mode); | 676 results->is_directory = S_ISDIR(file_info.st_mode); |
| 677 results->size = file_info.st_size; | 677 results->size = file_info.st_size; |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 921 result = false; | 921 result = false; |
| 922 if (IGNORE_EINTR(close(outfile)) < 0) | 922 if (IGNORE_EINTR(close(outfile)) < 0) |
| 923 result = false; | 923 result = false; |
| 924 | 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 } // namespace base | 930 } // namespace base |
| OLD | NEW |