[email protected] | 011352fdf | 2012-03-10 00:18:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/file_util.h" |
| 6 | |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 7 | #include <dirent.h> |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 8 | #include <errno.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 9 | #include <fcntl.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 10 | #include <libgen.h> |
[email protected] | e92dffe | 2010-05-12 21:36:39 | [diff] [blame] | 11 | #include <limits.h> |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 12 | #include <stdio.h> |
[email protected] | 1c65785 | 2010-04-22 23:28:05 | [diff] [blame] | 13 | #include <stdlib.h> |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 14 | #include <string.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 15 | #include <sys/errno.h> |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 16 | #include <sys/mman.h> |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 17 | #include <sys/param.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 18 | #include <sys/stat.h> |
[email protected] | ec3d145 | 2010-02-18 10:02:26 | [diff] [blame] | 19 | #include <sys/time.h> |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 20 | #include <sys/types.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 21 | #include <time.h> |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 22 | #include <unistd.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 23 | |
[email protected] | 3224dcd | 2009-09-16 17:31:25 | [diff] [blame] | 24 | #if defined(OS_MACOSX) |
| 25 | #include <AvailabilityMacros.h> |
[email protected] | 151c4a6 | 2011-04-22 04:15:13 | [diff] [blame] | 26 | #include "base/mac/foundation_util.h" |
[email protected] | 6ec70cc7 | 2013-11-20 05:33:46 | [diff] [blame] | 27 | #elif !defined(OS_CHROMEOS) && defined(USE_GLIB) |
[email protected] | 4ab6501 | 2013-11-07 11:38:32 | [diff] [blame] | 28 | #include <glib.h> // for g_get_home_dir() |
[email protected] | 3224dcd | 2009-09-16 17:31:25 | [diff] [blame] | 29 | #endif |
| 30 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 31 | #include <fstream> |
| 32 | |
| 33 | #include "base/basictypes.h" |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 34 | #include "base/files/file_enumerator.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 35 | #include "base/files/file_path.h" |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 36 | #include "base/files/scoped_file.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 37 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 38 | #include "base/memory/scoped_ptr.h" |
| 39 | #include "base/memory/singleton.h" |
[email protected] | 0eae7eb | 2012-05-17 20:09:06 | [diff] [blame] | 40 | #include "base/path_service.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 41 | #include "base/posix/eintr_wrapper.h" |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 42 | #include "base/stl_util.h" |
[email protected] | 251cd6e5 | 2013-06-11 13:36:37 | [diff] [blame] | 43 | #include "base/strings/string_util.h" |
| 44 | #include "base/strings/stringprintf.h" |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 45 | #include "base/strings/sys_string_conversions.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 46 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 49c4cf85 | 2013-09-27 19:28:24 | [diff] [blame] | 47 | #include "base/sys_info.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 48 | #include "base/threading/thread_restrictions.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 49 | #include "base/time/time.h" |
[email protected] | 172c550 | 2009-06-24 03:29:26 | [diff] [blame] | 50 | |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 51 | #if defined(OS_ANDROID) |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 52 | #include "base/android/content_uri_utils.h" |
[email protected] | f7d6997 | 2011-06-21 22:34:50 | [diff] [blame] | 53 | #include "base/os_compat_android.h" |
| 54 | #endif |
| 55 | |
[email protected] | 4bd4679 | 2012-07-09 14:40:39 | [diff] [blame] | 56 | #if !defined(OS_IOS) |
| 57 | #include <grp.h> |
| 58 | #endif |
| 59 | |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 60 | namespace base { |
| 61 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 62 | namespace { |
| 63 | |
[email protected] | f745d72 | 2014-04-05 02:39:18 | [diff] [blame] | 64 | #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 65 | static int CallStat(const char *path, stat_wrapper_t *sb) { |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 66 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 67 | return stat(path, sb); |
| 68 | } |
| 69 | static int CallLstat(const char *path, stat_wrapper_t *sb) { |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 70 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 71 | return lstat(path, sb); |
| 72 | } |
[email protected] | f745d72 | 2014-04-05 02:39:18 | [diff] [blame] | 73 | #else // defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 74 | static int CallStat(const char *path, stat_wrapper_t *sb) { |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 75 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 76 | return stat64(path, sb); |
| 77 | } |
| 78 | static int CallLstat(const char *path, stat_wrapper_t *sb) { |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 79 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 80 | return lstat64(path, sb); |
| 81 | } |
[email protected] | 68b16d4 | 2014-04-29 11:21:50 | [diff] [blame] | 82 | #endif // !(defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)) |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 83 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 84 | // Helper for NormalizeFilePath(), defined below. |
| 85 | bool RealPath(const FilePath& path, FilePath* real_path) { |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 86 | ThreadRestrictions::AssertIOAllowed(); // For realpath(). |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 87 | FilePath::CharType buf[PATH_MAX]; |
| 88 | if (!realpath(path.value().c_str(), buf)) |
| 89 | return false; |
| 90 | |
| 91 | *real_path = FilePath(buf); |
| 92 | return true; |
| 93 | } |
| 94 | |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 95 | // Helper for VerifyPathControlledByUser. |
| 96 | bool VerifySpecificPathControlledByUser(const FilePath& path, |
| 97 | uid_t owner_uid, |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 98 | const std::set<gid_t>& group_gids) { |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 99 | stat_wrapper_t stat_info; |
| 100 | if (CallLstat(path.value().c_str(), &stat_info) != 0) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 101 | DPLOG(ERROR) << "Failed to get information on path " |
| 102 | << path.value(); |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 103 | return false; |
| 104 | } |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 105 | |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 106 | if (S_ISLNK(stat_info.st_mode)) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 107 | DLOG(ERROR) << "Path " << path.value() |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 108 | << " is a symbolic link."; |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | if (stat_info.st_uid != owner_uid) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 113 | DLOG(ERROR) << "Path " << path.value() |
| 114 | << " is owned by the wrong user."; |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 118 | if ((stat_info.st_mode & S_IWGRP) && |
| 119 | !ContainsKey(group_gids, stat_info.st_gid)) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 120 | DLOG(ERROR) << "Path " << path.value() |
| 121 | << " is writable by an unprivileged group."; |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
| 125 | if (stat_info.st_mode & S_IWOTH) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 126 | DLOG(ERROR) << "Path " << path.value() |
| 127 | << " is writable by any user."; |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return true; |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 132 | } |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 133 | |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 134 | std::string TempFileName() { |
| 135 | #if defined(OS_MACOSX) |
| 136 | return StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID()); |
| 137 | #endif |
| 138 | |
| 139 | #if defined(GOOGLE_CHROME_BUILD) |
| 140 | return std::string(".com.google.Chrome.XXXXXX"); |
| 141 | #else |
| 142 | return std::string(".org.chromium.Chromium.XXXXXX"); |
| 143 | #endif |
| 144 | } |
| 145 | |
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 146 | // Creates and opens a temporary file in |directory|, returning the |
| 147 | // file descriptor. |path| is set to the temporary file path. |
| 148 | // This function does NOT unlink() the file. |
| 149 | int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { |
| 150 | ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp(). |
| 151 | *path = directory.Append(base::TempFileName()); |
| 152 | const std::string& tmpdir_string = path->value(); |
| 153 | // this should be OK since mkstemp just replaces characters in place |
| 154 | char* buffer = const_cast<char*>(tmpdir_string.c_str()); |
| 155 | |
| 156 | return HANDLE_EINTR(mkstemp(buffer)); |
| 157 | } |
| 158 | |
| 159 | #if defined(OS_LINUX) |
| 160 | // Determine if /dev/shm files can be mapped and then mprotect'd PROT_EXEC. |
| 161 | // This depends on the mount options used for /dev/shm, which vary among |
| 162 | // different Linux distributions and possibly local configuration. It also |
| 163 | // depends on details of kernel--ChromeOS uses the noexec option for /dev/shm |
| 164 | // but its kernel allows mprotect with PROT_EXEC anyway. |
| 165 | bool DetermineDevShmExecutable() { |
| 166 | bool result = false; |
| 167 | FilePath path; |
[email protected] | 42f558fd | 2014-03-17 19:02:35 | [diff] [blame] | 168 | |
| 169 | ScopedFD fd(CreateAndOpenFdForTemporaryFile(FilePath("/dev/shm"), &path)); |
| 170 | if (fd.is_valid()) { |
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 171 | DeleteFile(path, false); |
| 172 | long sysconf_result = sysconf(_SC_PAGESIZE); |
| 173 | CHECK_GE(sysconf_result, 0); |
| 174 | size_t pagesize = static_cast<size_t>(sysconf_result); |
| 175 | CHECK_GE(sizeof(pagesize), sizeof(sysconf_result)); |
[email protected] | 68b16d4 | 2014-04-29 11:21:50 | [diff] [blame] | 176 | void* mapping = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd.get(), 0); |
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 177 | if (mapping != MAP_FAILED) { |
| 178 | if (mprotect(mapping, pagesize, PROT_READ | PROT_EXEC) == 0) |
| 179 | result = true; |
| 180 | munmap(mapping, pagesize); |
| 181 | } |
| 182 | } |
| 183 | return result; |
| 184 | } |
| 185 | #endif // defined(OS_LINUX) |
| 186 | |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 187 | } // namespace |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 188 | |
[email protected] | 918efbf | 2013-07-01 19:41:02 | [diff] [blame] | 189 | FilePath MakeAbsoluteFilePath(const FilePath& input) { |
| 190 | ThreadRestrictions::AssertIOAllowed(); |
| 191 | char full_path[PATH_MAX]; |
| 192 | if (realpath(input.value().c_str(), full_path) == NULL) |
| 193 | return FilePath(); |
| 194 | return FilePath(full_path); |
[email protected] | 151c4a6 | 2011-04-22 04:15:13 | [diff] [blame] | 195 | } |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 196 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 197 | // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" |
| 198 | // which works both with and without the recursive flag. I'm not sure we need |
| 199 | // that functionality. If not, remove from file_util_win.cc, otherwise add it |
| 200 | // here. |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 201 | bool DeleteFile(const FilePath& path, bool recursive) { |
[email protected] | 918efbf | 2013-07-01 19:41:02 | [diff] [blame] | 202 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 203 | const char* path_str = path.value().c_str(); |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 204 | stat_wrapper_t file_info; |
[email protected] | 7122db2 | 2012-06-30 05:26:59 | [diff] [blame] | 205 | int test = CallLstat(path_str, &file_info); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 206 | if (test != 0) { |
| 207 | // The Windows version defines this condition as success. |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 208 | bool ret = (errno == ENOENT || errno == ENOTDIR); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 209 | return ret; |
| 210 | } |
| 211 | if (!S_ISDIR(file_info.st_mode)) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 212 | return (unlink(path_str) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 213 | if (!recursive) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 214 | return (rmdir(path_str) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 215 | |
| 216 | bool success = true; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 217 | std::stack<std::string> directories; |
| 218 | directories.push(path.value()); |
[email protected] | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 219 | FileEnumerator traversal(path, true, |
| 220 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES | |
| 221 | FileEnumerator::SHOW_SYM_LINKS); |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 222 | for (FilePath current = traversal.Next(); success && !current.empty(); |
| 223 | current = traversal.Next()) { |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 224 | if (traversal.GetInfo().IsDirectory()) |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 225 | directories.push(current.value()); |
| 226 | else |
| 227 | success = (unlink(current.value().c_str()) == 0); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 228 | } |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 229 | |
| 230 | while (success && !directories.empty()) { |
| 231 | FilePath dir = FilePath(directories.top()); |
| 232 | directories.pop(); |
| 233 | success = (rmdir(dir.value().c_str()) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 234 | } |
| 235 | return success; |
| 236 | } |
| 237 | |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 238 | bool ReplaceFile(const FilePath& from_path, |
| 239 | const FilePath& to_path, |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 240 | File::Error* error) { |
[email protected] | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 241 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 6f539941 | 2013-05-08 22:02:36 | [diff] [blame] | 242 | if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) |
| 243 | return true; |
| 244 | if (error) |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 245 | *error = File::OSErrorToFileError(errno); |
[email protected] | 6f539941 | 2013-05-08 22:02:36 | [diff] [blame] | 246 | return false; |
[email protected] | c5866dca | 2009-05-19 17:21:07 | [diff] [blame] | 247 | } |
| 248 | |
[email protected] | 9e66a9b | 2013-05-08 05:46:20 | [diff] [blame] | 249 | bool CopyDirectory(const FilePath& from_path, |
| 250 | const FilePath& to_path, |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 251 | bool recursive) { |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 252 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 253 | // Some old callers of CopyDirectory want it to support wildcards. |
| 254 | // After some discussion, we decided to fix those callers. |
| 255 | // Break loudly here if anyone tries to do this. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 256 | DCHECK(to_path.value().find('*') == std::string::npos); |
| 257 | DCHECK(from_path.value().find('*') == std::string::npos); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 258 | |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 259 | if (from_path.value().size() >= PATH_MAX) { |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 263 | // This function does not properly handle destinations within the source |
| 264 | FilePath real_to_path = to_path; |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 265 | if (PathExists(real_to_path)) { |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 266 | real_to_path = MakeAbsoluteFilePath(real_to_path); |
| 267 | if (real_to_path.empty()) |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 268 | return false; |
| 269 | } else { |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 270 | real_to_path = MakeAbsoluteFilePath(real_to_path.DirName()); |
| 271 | if (real_to_path.empty()) |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 272 | return false; |
| 273 | } |
[email protected] | 1547693 | 2013-04-12 05:17:15 | [diff] [blame] | 274 | FilePath real_from_path = MakeAbsoluteFilePath(from_path); |
| 275 | if (real_from_path.empty()) |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 276 | return false; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 277 | if (real_to_path.value().size() >= real_from_path.value().size() && |
| 278 | real_to_path.value().compare(0, real_from_path.value().size(), |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 279 | real_from_path.value()) == 0) { |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 280 | return false; |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 281 | } |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 282 | |
[email protected] | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 283 | int traverse_type = FileEnumerator::FILES | FileEnumerator::SHOW_SYM_LINKS; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 284 | if (recursive) |
[email protected] | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 285 | traverse_type |= FileEnumerator::DIRECTORIES; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 286 | FileEnumerator traversal(from_path, recursive, traverse_type); |
| 287 | |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 288 | // We have to mimic windows behavior here. |to_path| may not exist yet, |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 289 | // start the loop with |to_path|. |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 290 | struct stat from_stat; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 291 | FilePath current = from_path; |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 292 | if (stat(from_path.value().c_str(), &from_stat) < 0) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 293 | DLOG(ERROR) << "CopyDirectory() couldn't stat source directory: " |
| 294 | << from_path.value() << " errno = " << errno; |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 295 | return false; |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 296 | } |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 297 | struct stat to_path_stat; |
| 298 | FilePath from_path_base = from_path; |
| 299 | if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 && |
| 300 | S_ISDIR(to_path_stat.st_mode)) { |
| 301 | // If the destination already exists and is a directory, then the |
| 302 | // top level of source needs to be copied. |
| 303 | from_path_base = from_path.DirName(); |
| 304 | } |
| 305 | |
| 306 | // The Windows version of this function assumes that non-recursive calls |
| 307 | // will always have a directory for from_path. |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 308 | // TODO(maruel): This is not necessary anymore. |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 309 | DCHECK(recursive || S_ISDIR(from_stat.st_mode)); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 310 | |
[email protected] | 0d378d2b | 2014-02-05 19:55:52 | [diff] [blame] | 311 | bool success = true; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 312 | while (success && !current.empty()) { |
[email protected] | 92e0649 | 2013-01-30 11:38:02 | [diff] [blame] | 313 | // current is the source path, including from_path, so append |
| 314 | // the suffix after from_path to to_path to create the target_path. |
| 315 | FilePath target_path(to_path); |
| 316 | if (from_path_base != current) { |
| 317 | if (!from_path_base.AppendRelativePath(current, &target_path)) { |
| 318 | success = false; |
| 319 | break; |
| 320 | } |
[email protected] | ca020961 | 2009-01-13 18:57:46 | [diff] [blame] | 321 | } |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 322 | |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 323 | if (S_ISDIR(from_stat.st_mode)) { |
| 324 | if (mkdir(target_path.value().c_str(), from_stat.st_mode & 01777) != 0 && |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 325 | errno != EEXIST) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 326 | DLOG(ERROR) << "CopyDirectory() couldn't create directory: " |
| 327 | << target_path.value() << " errno = " << errno; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 328 | success = false; |
| 329 | } |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 330 | } else if (S_ISREG(from_stat.st_mode)) { |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 331 | if (!CopyFile(current, target_path)) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 332 | DLOG(ERROR) << "CopyDirectory() couldn't create file: " |
| 333 | << target_path.value(); |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 334 | success = false; |
| 335 | } |
| 336 | } else { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 337 | DLOG(WARNING) << "CopyDirectory() skipping non-regular file: " |
| 338 | << current.value(); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 339 | } |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 340 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 341 | current = traversal.Next(); |
[email protected] | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 342 | if (!current.empty()) |
| 343 | from_stat = traversal.GetInfo().stat(); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 346 | return success; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 347 | } |
| 348 | |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 349 | bool PathExists(const FilePath& path) { |
| 350 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | f12d1e1 | 2013-11-20 07:04:55 | [diff] [blame] | 351 | #if defined(OS_ANDROID) |
| 352 | if (path.IsContentUri()) { |
| 353 | return ContentUriExists(path); |
| 354 | } |
| 355 | #endif |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 356 | return access(path.value().c_str(), F_OK) == 0; |
| 357 | } |
| 358 | |
[email protected] | dcd1661 | 2013-07-15 20:18:09 | [diff] [blame] | 359 | bool PathIsWritable(const FilePath& path) { |
| 360 | ThreadRestrictions::AssertIOAllowed(); |
| 361 | return access(path.value().c_str(), W_OK) == 0; |
| 362 | } |
| 363 | |
| 364 | bool DirectoryExists(const FilePath& path) { |
| 365 | ThreadRestrictions::AssertIOAllowed(); |
| 366 | stat_wrapper_t file_info; |
| 367 | if (CallStat(path.value().c_str(), &file_info) == 0) |
| 368 | return S_ISDIR(file_info.st_mode); |
| 369 | return false; |
| 370 | } |
| 371 | |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 372 | bool ReadFromFD(int fd, char* buffer, size_t bytes) { |
| 373 | size_t total_read = 0; |
| 374 | while (total_read < bytes) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 375 | ssize_t bytes_read = |
| 376 | HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read)); |
| 377 | if (bytes_read <= 0) |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 378 | break; |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 379 | total_read += bytes_read; |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 380 | } |
| 381 | return total_read == bytes; |
| 382 | } |
| 383 | |
[email protected] | 2e733d10 | 2010-11-30 00:43:37 | [diff] [blame] | 384 | bool CreateSymbolicLink(const FilePath& target_path, |
| 385 | const FilePath& symlink_path) { |
| 386 | DCHECK(!symlink_path.empty()); |
| 387 | DCHECK(!target_path.empty()); |
| 388 | return ::symlink(target_path.value().c_str(), |
| 389 | symlink_path.value().c_str()) != -1; |
| 390 | } |
| 391 | |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 392 | bool ReadSymbolicLink(const FilePath& symlink_path, FilePath* target_path) { |
[email protected] | 2e733d10 | 2010-11-30 00:43:37 | [diff] [blame] | 393 | DCHECK(!symlink_path.empty()); |
| 394 | DCHECK(target_path); |
| 395 | char buf[PATH_MAX]; |
| 396 | ssize_t count = ::readlink(symlink_path.value().c_str(), buf, arraysize(buf)); |
| 397 | |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 398 | if (count <= 0) { |
| 399 | target_path->clear(); |
[email protected] | 2e733d10 | 2010-11-30 00:43:37 | [diff] [blame] | 400 | return false; |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 401 | } |
[email protected] | 2e733d10 | 2010-11-30 00:43:37 | [diff] [blame] | 402 | |
| 403 | *target_path = FilePath(FilePath::StringType(buf, count)); |
[email protected] | 2e733d10 | 2010-11-30 00:43:37 | [diff] [blame] | 404 | return true; |
| 405 | } |
| 406 | |
[email protected] | 5085e44 | 2012-07-11 01:24:02 | [diff] [blame] | 407 | bool GetPosixFilePermissions(const FilePath& path, int* mode) { |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 408 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 5085e44 | 2012-07-11 01:24:02 | [diff] [blame] | 409 | DCHECK(mode); |
| 410 | |
| 411 | stat_wrapper_t file_info; |
| 412 | // Uses stat(), because on symbolic link, lstat() does not return valid |
| 413 | // permission bits in st_mode |
| 414 | if (CallStat(path.value().c_str(), &file_info) != 0) |
| 415 | return false; |
| 416 | |
| 417 | *mode = file_info.st_mode & FILE_PERMISSION_MASK; |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | bool SetPosixFilePermissions(const FilePath& path, |
| 422 | int mode) { |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 423 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 5085e44 | 2012-07-11 01:24:02 | [diff] [blame] | 424 | DCHECK((mode & ~FILE_PERMISSION_MASK) == 0); |
| 425 | |
| 426 | // Calls stat() so that we can preserve the higher bits like S_ISGID. |
| 427 | stat_wrapper_t stat_buf; |
| 428 | if (CallStat(path.value().c_str(), &stat_buf) != 0) |
| 429 | return false; |
| 430 | |
| 431 | // Clears the existing permission bits, and adds the new ones. |
| 432 | mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK; |
| 433 | updated_mode_bits |= mode & FILE_PERMISSION_MASK; |
| 434 | |
| 435 | if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0) |
| 436 | return false; |
| 437 | |
| 438 | return true; |
| 439 | } |
| 440 | |
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 441 | #if !defined(OS_MACOSX) |
| 442 | // This is implemented in file_util_mac.mm for Mac. |
| 443 | bool GetTempDir(FilePath* path) { |
| 444 | const char* tmp = getenv("TMPDIR"); |
| 445 | if (tmp) { |
| 446 | *path = FilePath(tmp); |
| 447 | } else { |
| 448 | #if defined(OS_ANDROID) |
| 449 | return PathService::Get(base::DIR_CACHE, path); |
| 450 | #else |
| 451 | *path = FilePath("/tmp"); |
| 452 | #endif |
| 453 | } |
| 454 | return true; |
| 455 | } |
| 456 | #endif // !defined(OS_MACOSX) |
| 457 | |
[email protected] | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 458 | #if !defined(OS_MACOSX) // Mac implementation is in file_util_mac.mm. |
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 459 | FilePath GetHomeDir() { |
| 460 | #if defined(OS_CHROMEOS) |
[email protected] | 6bdc5227 | 2014-05-27 00:12:33 | [diff] [blame] | 461 | if (SysInfo::IsRunningOnChromeOS()) { |
| 462 | // On Chrome OS chrome::DIR_USER_DATA is overriden with a primary user |
| 463 | // homedir once it becomes available. |
| 464 | NOTREACHED() << "Called GetHomeDir() without base::DIR_HOME override"; |
| 465 | return FilePath("/"); |
| 466 | } |
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 467 | #endif |
| 468 | |
| 469 | const char* home_dir = getenv("HOME"); |
| 470 | if (home_dir && home_dir[0]) |
| 471 | return FilePath(home_dir); |
| 472 | |
| 473 | #if defined(OS_ANDROID) |
| 474 | DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented."; |
| 475 | #elif defined(USE_GLIB) && !defined(OS_CHROMEOS) |
[email protected] | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 476 | // g_get_home_dir calls getpwent, which can fall through to LDAP calls so |
| 477 | // this may do I/O. However, it should be rare that $HOME is not defined and |
| 478 | // this is typically called from the path service which has no threading |
| 479 | // restrictions. The path service will cache the result which limits the |
| 480 | // badness of blocking on I/O. As a result, we don't have a thread |
| 481 | // restriction here. |
[email protected] | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 482 | home_dir = g_get_home_dir(); |
| 483 | if (home_dir && home_dir[0]) |
| 484 | return FilePath(home_dir); |
| 485 | #endif |
| 486 | |
| 487 | FilePath rv; |
| 488 | if (GetTempDir(&rv)) |
| 489 | return rv; |
| 490 | |
| 491 | // Last resort. |
| 492 | return FilePath("/tmp"); |
| 493 | } |
| 494 | #endif // !defined(OS_MACOSX) |
| 495 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 496 | bool CreateTemporaryFile(FilePath* path) { |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 497 | ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 498 | FilePath directory; |
| 499 | if (!GetTempDir(&directory)) |
| 500 | return false; |
| 501 | int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 502 | if (fd < 0) |
| 503 | return false; |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 504 | close(fd); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 505 | return true; |
| 506 | } |
| 507 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 508 | FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
| 509 | int fd = CreateAndOpenFdForTemporaryFile(dir, path); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 510 | if (fd < 0) |
| 511 | return NULL; |
| 512 | |
[email protected] | 139063bd | 2011-04-18 19:05:53 | [diff] [blame] | 513 | FILE* file = fdopen(fd, "a+"); |
| 514 | if (!file) |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 515 | close(fd); |
[email protected] | 139063bd | 2011-04-18 19:05:53 | [diff] [blame] | 516 | return file; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 517 | } |
[email protected] | 6445c40 | 2009-09-11 20:06:27 | [diff] [blame] | 518 | |
| 519 | bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 520 | ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
[email protected] | 6445c40 | 2009-09-11 20:06:27 | [diff] [blame] | 521 | int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 522 | return ((fd >= 0) && !IGNORE_EINTR(close(fd))); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 523 | } |
| 524 | |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 525 | static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, |
| 526 | const FilePath::StringType& name_tmpl, |
| 527 | FilePath* new_dir) { |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 528 | ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp(). |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 529 | DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) |
| 530 | << "Directory name template must contain \"XXXXXX\"."; |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 531 | |
| 532 | FilePath sub_dir = base_dir.Append(name_tmpl); |
| 533 | std::string sub_dir_string = sub_dir.value(); |
| 534 | |
| 535 | // this should be OK since mkdtemp just replaces characters in place |
| 536 | char* buffer = const_cast<char*>(sub_dir_string.c_str()); |
| 537 | char* dtemp = mkdtemp(buffer); |
[email protected] | 3ad035d2 | 2010-07-28 21:00:51 | [diff] [blame] | 538 | if (!dtemp) { |
| 539 | DPLOG(ERROR) << "mkdtemp"; |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 540 | return false; |
[email protected] | 3ad035d2 | 2010-07-28 21:00:51 | [diff] [blame] | 541 | } |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 542 | *new_dir = FilePath(dtemp); |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | bool CreateTemporaryDirInDir(const FilePath& base_dir, |
| 547 | const FilePath::StringType& prefix, |
[email protected] | 046062e8 | 2010-06-30 07:19:11 | [diff] [blame] | 548 | FilePath* new_dir) { |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 549 | FilePath::StringType mkdtemp_template = prefix; |
| 550 | mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX")); |
| 551 | return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir); |
| 552 | } |
| 553 | |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 554 | bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 555 | FilePath* new_temp_path) { |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 556 | FilePath tmpdir; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 557 | if (!GetTempDir(&tmpdir)) |
| 558 | return false; |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 559 | |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 560 | return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 561 | } |
| 562 | |
[email protected] | cfd23d2 | 2013-06-11 03:50:25 | [diff] [blame] | 563 | bool CreateDirectoryAndGetError(const FilePath& full_path, |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 564 | File::Error* error) { |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 565 | ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 566 | std::vector<FilePath> subpaths; |
| 567 | |
| 568 | // Collect a list of all parent directories. |
| 569 | FilePath last_path = full_path; |
| 570 | subpaths.push_back(full_path); |
| 571 | for (FilePath path = full_path.DirName(); |
| 572 | path.value() != last_path.value(); path = path.DirName()) { |
| 573 | subpaths.push_back(path); |
| 574 | last_path = path; |
| 575 | } |
| 576 | |
| 577 | // Iterate through the parents and create the missing ones. |
| 578 | for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); |
| 579 | i != subpaths.rend(); ++i) { |
[email protected] | 0ba86e4 | 2010-03-17 21:39:42 | [diff] [blame] | 580 | if (DirectoryExists(*i)) |
| 581 | continue; |
| 582 | if (mkdir(i->value().c_str(), 0700) == 0) |
| 583 | continue; |
| 584 | // Mkdir failed, but it might have failed with EEXIST, or some other error |
| 585 | // due to the the directory appearing out of thin air. This can occur if |
| 586 | // two processes are trying to create the same file system tree at the same |
| 587 | // time. Check to see if it exists and make sure it is a directory. |
[email protected] | cfd23d2 | 2013-06-11 03:50:25 | [diff] [blame] | 588 | int saved_errno = errno; |
| 589 | if (!DirectoryExists(*i)) { |
| 590 | if (error) |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 591 | *error = File::OSErrorToFileError(saved_errno); |
[email protected] | 0ba86e4 | 2010-03-17 21:39:42 | [diff] [blame] | 592 | return false; |
[email protected] | cfd23d2 | 2013-06-11 03:50:25 | [diff] [blame] | 593 | } |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 594 | } |
| 595 | return true; |
| 596 | } |
| 597 | |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 598 | bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) { |
| 599 | FilePath real_path_result; |
| 600 | if (!RealPath(path, &real_path_result)) |
| 601 | return false; |
| 602 | |
| 603 | // To be consistant with windows, fail if |real_path_result| is a |
| 604 | // directory. |
| 605 | stat_wrapper_t file_info; |
| 606 | if (CallStat(real_path_result.value().c_str(), &file_info) != 0 || |
| 607 | S_ISDIR(file_info.st_mode)) |
| 608 | return false; |
| 609 | |
| 610 | *normalized_path = real_path_result; |
| 611 | return true; |
| 612 | } |
| 613 | |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 614 | // TODO(rkc): Refactor GetFileInfo and FileEnumerator to handle symlinks |
| 615 | // correctly. http://code.google.com/p/chromium-os/issues/detail?id=15948 |
| 616 | bool IsLink(const FilePath& file_path) { |
| 617 | stat_wrapper_t st; |
| 618 | // If we can't lstat the file, it's safe to assume that the file won't at |
| 619 | // least be a 'followable' link. |
| 620 | if (CallLstat(file_path.value().c_str(), &st) != 0) |
| 621 | return false; |
| 622 | |
| 623 | if (S_ISLNK(st.st_mode)) |
| 624 | return true; |
| 625 | else |
| 626 | return false; |
| 627 | } |
| 628 | |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 629 | bool GetFileInfo(const FilePath& file_path, File::Info* results) { |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 630 | stat_wrapper_t file_info; |
| 631 | #if defined(OS_ANDROID) |
| 632 | if (file_path.IsContentUri()) { |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 633 | File file = OpenContentUriForRead(file_path); |
| 634 | if (!file.IsValid()) |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 635 | return false; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 636 | return file.GetInfo(results); |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 637 | } else { |
| 638 | #endif // defined(OS_ANDROID) |
| 639 | if (CallStat(file_path.value().c_str(), &file_info) != 0) |
| 640 | return false; |
| 641 | #if defined(OS_ANDROID) |
| 642 | } |
| 643 | #endif // defined(OS_ANDROID) |
[email protected] | f745d72 | 2014-04-05 02:39:18 | [diff] [blame] | 644 | |
| 645 | results->FromStat(file_info); |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 646 | return true; |
| 647 | } |
| 648 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 649 | FILE* OpenFile(const FilePath& filename, const char* mode) { |
| 650 | ThreadRestrictions::AssertIOAllowed(); |
| 651 | FILE* result = NULL; |
| 652 | do { |
| 653 | result = fopen(filename.value().c_str(), mode); |
| 654 | } while (!result && errno == EINTR); |
| 655 | return result; |
| 656 | } |
| 657 | |
[email protected] | d8b565d | 2014-04-18 10:34:15 | [diff] [blame] | 658 | int ReadFile(const FilePath& filename, char* data, int max_size) { |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 659 | ThreadRestrictions::AssertIOAllowed(); |
| 660 | int fd = HANDLE_EINTR(open(filename.value().c_str(), O_RDONLY)); |
| 661 | if (fd < 0) |
| 662 | return -1; |
| 663 | |
[email protected] | d8b565d | 2014-04-18 10:34:15 | [diff] [blame] | 664 | ssize_t bytes_read = HANDLE_EINTR(read(fd, data, max_size)); |
| 665 | if (IGNORE_EINTR(close(fd)) < 0) |
| 666 | return -1; |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 667 | return bytes_read; |
| 668 | } |
| 669 | |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 670 | int WriteFile(const FilePath& filename, const char* data, int size) { |
| 671 | ThreadRestrictions::AssertIOAllowed(); |
| 672 | int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666)); |
| 673 | if (fd < 0) |
| 674 | return -1; |
| 675 | |
| 676 | int bytes_written = WriteFileDescriptor(fd, data, size); |
[email protected] | d8b565d | 2014-04-18 10:34:15 | [diff] [blame] | 677 | if (IGNORE_EINTR(close(fd)) < 0) |
| 678 | return -1; |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 679 | return bytes_written; |
| 680 | } |
| 681 | |
| 682 | int WriteFileDescriptor(const int fd, const char* data, int size) { |
| 683 | // Allow for partial writes. |
| 684 | ssize_t bytes_written_total = 0; |
| 685 | for (ssize_t bytes_written_partial = 0; bytes_written_total < size; |
| 686 | bytes_written_total += bytes_written_partial) { |
| 687 | bytes_written_partial = |
| 688 | HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 689 | size - bytes_written_total)); |
| 690 | if (bytes_written_partial < 0) |
| 691 | return -1; |
| 692 | } |
| 693 | |
| 694 | return bytes_written_total; |
| 695 | } |
| 696 | |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 697 | int AppendToFile(const FilePath& filename, const char* data, int size) { |
| 698 | ThreadRestrictions::AssertIOAllowed(); |
| 699 | int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND)); |
| 700 | if (fd < 0) |
| 701 | return -1; |
| 702 | |
| 703 | int bytes_written = WriteFileDescriptor(fd, data, size); |
[email protected] | d8b565d | 2014-04-18 10:34:15 | [diff] [blame] | 704 | if (IGNORE_EINTR(close(fd)) < 0) |
| 705 | return -1; |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 706 | return bytes_written; |
| 707 | } |
| 708 | |
| 709 | // Gets the current working directory for the process. |
| 710 | bool GetCurrentDirectory(FilePath* dir) { |
| 711 | // getcwd can return ENOENT, which implies it checks against the disk. |
| 712 | ThreadRestrictions::AssertIOAllowed(); |
| 713 | |
| 714 | char system_buffer[PATH_MAX] = ""; |
| 715 | if (!getcwd(system_buffer, sizeof(system_buffer))) { |
| 716 | NOTREACHED(); |
| 717 | return false; |
| 718 | } |
| 719 | *dir = FilePath(system_buffer); |
| 720 | return true; |
| 721 | } |
| 722 | |
| 723 | // Sets the current working directory for the process. |
| 724 | bool SetCurrentDirectory(const FilePath& path) { |
| 725 | ThreadRestrictions::AssertIOAllowed(); |
| 726 | int ret = chdir(path.value().c_str()); |
| 727 | return !ret; |
| 728 | } |
| 729 | |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 730 | bool VerifyPathControlledByUser(const FilePath& base, |
| 731 | const FilePath& path, |
| 732 | uid_t owner_uid, |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 733 | const std::set<gid_t>& group_gids) { |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 734 | if (base != path && !base.IsParent(path)) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 735 | DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \"" |
| 736 | << base.value() << "\", path = \"" << path.value() << "\""; |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 737 | return false; |
| 738 | } |
| 739 | |
| 740 | std::vector<FilePath::StringType> base_components; |
| 741 | std::vector<FilePath::StringType> path_components; |
| 742 | |
| 743 | base.GetComponents(&base_components); |
| 744 | path.GetComponents(&path_components); |
| 745 | |
| 746 | std::vector<FilePath::StringType>::const_iterator ib, ip; |
| 747 | for (ib = base_components.begin(), ip = path_components.begin(); |
| 748 | ib != base_components.end(); ++ib, ++ip) { |
| 749 | // |base| must be a subpath of |path|, so all components should match. |
| 750 | // If these CHECKs fail, look at the test that base is a parent of |
| 751 | // path at the top of this function. |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 752 | DCHECK(ip != path_components.end()); |
| 753 | DCHECK(*ip == *ib); |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | FilePath current_path = base; |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 757 | if (!VerifySpecificPathControlledByUser(current_path, owner_uid, group_gids)) |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 758 | return false; |
| 759 | |
| 760 | for (; ip != path_components.end(); ++ip) { |
| 761 | current_path = current_path.Append(*ip); |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 762 | if (!VerifySpecificPathControlledByUser( |
| 763 | current_path, owner_uid, group_gids)) |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 764 | return false; |
| 765 | } |
| 766 | return true; |
| 767 | } |
| 768 | |
[email protected] | 4bd4679 | 2012-07-09 14:40:39 | [diff] [blame] | 769 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 770 | bool VerifyPathControlledByAdmin(const FilePath& path) { |
| 771 | const unsigned kRootUid = 0; |
| 772 | const FilePath kFileSystemRoot("/"); |
| 773 | |
| 774 | // The name of the administrator group on mac os. |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 775 | const char* const kAdminGroupNames[] = { |
| 776 | "admin", |
| 777 | "wheel" |
| 778 | }; |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 779 | |
| 780 | // Reading the groups database may touch the file system. |
[email protected] | a26f4ae | 2014-03-13 17:26:21 | [diff] [blame] | 781 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 782 | |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 783 | std::set<gid_t> allowed_group_ids; |
| 784 | for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) { |
| 785 | struct group *group_record = getgrnam(kAdminGroupNames[i]); |
| 786 | if (!group_record) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 787 | DPLOG(ERROR) << "Could not get the group ID of group \"" |
| 788 | << kAdminGroupNames[i] << "\"."; |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 789 | continue; |
| 790 | } |
| 791 | |
| 792 | allowed_group_ids.insert(group_record->gr_gid); |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return VerifyPathControlledByUser( |
[email protected] | 7312f42a | 2011-10-17 21:30:29 | [diff] [blame] | 796 | kFileSystemRoot, path, kRootUid, allowed_group_ids); |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 797 | } |
[email protected] | eba2993 | 2012-07-24 11:23:32 | [diff] [blame] | 798 | #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
[email protected] | 73e4c36 | 2011-09-22 14:47:18 | [diff] [blame] | 799 | |
[email protected] | 5948030 | 2013-02-21 03:24:08 | [diff] [blame] | 800 | int GetMaximumPathComponentLength(const FilePath& path) { |
[email protected] | a26f4ae | 2014-03-13 17:26:21 | [diff] [blame] | 801 | ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 5948030 | 2013-02-21 03:24:08 | [diff] [blame] | 802 | return pathconf(path.value().c_str(), _PC_NAME_MAX); |
| 803 | } |
| 804 | |
[email protected] | 271b8da | 2014-03-20 21:33:39 | [diff] [blame] | 805 | #if !defined(OS_ANDROID) |
| 806 | // This is implemented in file_util_android.cc for that platform. |
[email protected] | 6f5418ba | 2014-03-20 07:06:40 | [diff] [blame] | 807 | bool GetShmemTempDir(bool executable, FilePath* path) { |
| 808 | #if defined(OS_LINUX) |
| 809 | bool use_dev_shm = true; |
| 810 | if (executable) { |
| 811 | static const bool s_dev_shm_executable = DetermineDevShmExecutable(); |
| 812 | use_dev_shm = s_dev_shm_executable; |
| 813 | } |
| 814 | if (use_dev_shm) { |
| 815 | *path = FilePath("/dev/shm"); |
| 816 | return true; |
| 817 | } |
| 818 | #endif |
| 819 | return GetTempDir(path); |
| 820 | } |
[email protected] | 271b8da | 2014-03-20 21:33:39 | [diff] [blame] | 821 | #endif // !defined(OS_ANDROID) |
[email protected] | 6f5418ba | 2014-03-20 07:06:40 | [diff] [blame] | 822 | |
[email protected] | a26f4ae | 2014-03-13 17:26:21 | [diff] [blame] | 823 | // ----------------------------------------------------------------------------- |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 824 | |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 825 | namespace internal { |
| 826 | |
| 827 | bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { |
| 828 | ThreadRestrictions::AssertIOAllowed(); |
| 829 | // Windows compatibility: if to_path exists, from_path and to_path |
| 830 | // must be the same type, either both files, or both directories. |
| 831 | stat_wrapper_t to_file_info; |
| 832 | if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { |
| 833 | stat_wrapper_t from_file_info; |
| 834 | if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { |
| 835 | if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode)) |
| 836 | return false; |
| 837 | } else { |
| 838 | return false; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) |
| 843 | return true; |
| 844 | |
| 845 | if (!CopyDirectory(from_path, to_path, true)) |
| 846 | return false; |
| 847 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 848 | DeleteFile(from_path, true); |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 849 | return true; |
| 850 | } |
| 851 | |
| 852 | #if !defined(OS_MACOSX) |
| 853 | // Mac has its own implementation, this is for all other Posix systems. |
| 854 | bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { |
| 855 | ThreadRestrictions::AssertIOAllowed(); |
| 856 | int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY)); |
| 857 | if (infile < 0) |
| 858 | return false; |
| 859 | |
| 860 | int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666)); |
| 861 | if (outfile < 0) { |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 862 | close(infile); |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 863 | return false; |
| 864 | } |
| 865 | |
| 866 | const size_t kBufferSize = 32768; |
| 867 | std::vector<char> buffer(kBufferSize); |
| 868 | bool result = true; |
| 869 | |
| 870 | while (result) { |
| 871 | ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size())); |
| 872 | if (bytes_read < 0) { |
| 873 | result = false; |
| 874 | break; |
| 875 | } |
| 876 | if (bytes_read == 0) |
| 877 | break; |
| 878 | // Allow for partial writes |
| 879 | ssize_t bytes_written_per_read = 0; |
| 880 | do { |
| 881 | ssize_t bytes_written_partial = HANDLE_EINTR(write( |
| 882 | outfile, |
| 883 | &buffer[bytes_written_per_read], |
| 884 | bytes_read - bytes_written_per_read)); |
| 885 | if (bytes_written_partial < 0) { |
| 886 | result = false; |
| 887 | break; |
| 888 | } |
| 889 | bytes_written_per_read += bytes_written_partial; |
| 890 | } while (bytes_written_per_read < bytes_read); |
| 891 | } |
| 892 | |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 893 | if (IGNORE_EINTR(close(infile)) < 0) |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 894 | result = false; |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 895 | if (IGNORE_EINTR(close(outfile)) < 0) |
[email protected] | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 896 | result = false; |
| 897 | |
| 898 | return result; |
| 899 | } |
| 900 | #endif // !defined(OS_MACOSX) |
| 901 | |
| 902 | } // namespace internal |
| 903 | } // namespace base |