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