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