[email protected] | ec3d145 | 2010-02-18 10:02:26 | [diff] [blame] | 1 | // Copyright (c) 2010 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> |
| 10 | #include <fnmatch.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 11 | #include <libgen.h> |
[email protected] | e92dffe | 2010-05-12 21:36:39 | [diff] [blame] | 12 | #include <limits.h> |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 13 | #include <stdio.h> |
[email protected] | 1c65785 | 2010-04-22 23:28:05 | [diff] [blame] | 14 | #include <stdlib.h> |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 15 | #include <string.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 16 | #include <sys/errno.h> |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 17 | #include <sys/mman.h> |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 18 | #include <sys/param.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 19 | #include <sys/stat.h> |
[email protected] | ec3d145 | 2010-02-18 10:02:26 | [diff] [blame] | 20 | #include <sys/time.h> |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 21 | #include <sys/types.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 22 | #include <time.h> |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 23 | #include <unistd.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 24 | |
[email protected] | 3224dcd | 2009-09-16 17:31:25 | [diff] [blame] | 25 | #if defined(OS_MACOSX) |
| 26 | #include <AvailabilityMacros.h> |
[email protected] | 1c65785 | 2010-04-22 23:28:05 | [diff] [blame] | 27 | #else |
| 28 | #include <glib.h> |
[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] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 34 | #include "base/eintr_wrapper.h" |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 35 | #include "base/file_path.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 36 | #include "base/logging.h" |
[email protected] | 807bc04 | 2009-07-15 01:32:02 | [diff] [blame] | 37 | #include "base/scoped_ptr.h" |
| 38 | #include "base/singleton.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 39 | #include "base/string_util.h" |
[email protected] | 807bc04 | 2009-07-15 01:32:02 | [diff] [blame] | 40 | #include "base/sys_string_conversions.h" |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 41 | #include "base/thread_restrictions.h" |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 42 | #include "base/time.h" |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 43 | #include "base/utf_string_conversions.h" |
[email protected] | 172c550 | 2009-06-24 03:29:26 | [diff] [blame] | 44 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 45 | namespace file_util { |
| 46 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 47 | namespace { |
| 48 | |
| 49 | // Helper for NormalizeFilePath(), defined below. |
| 50 | bool RealPath(const FilePath& path, FilePath* real_path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 51 | base::ThreadRestrictions::AssertIOAllowed(); // For realpath(). |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 52 | FilePath::CharType buf[PATH_MAX]; |
| 53 | if (!realpath(path.value().c_str(), buf)) |
| 54 | return false; |
| 55 | |
| 56 | *real_path = FilePath(buf); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
[email protected] | 66700d4 | 2010-03-10 07:46:43 | [diff] [blame] | 62 | #if defined(OS_OPENBSD) || defined(OS_FREEBSD) || \ |
[email protected] | 3224dcd | 2009-09-16 17:31:25 | [diff] [blame] | 63 | (defined(OS_MACOSX) && \ |
| 64 | MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 65 | typedef struct stat stat_wrapper_t; |
| 66 | static int CallStat(const char *path, stat_wrapper_t *sb) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 67 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 68 | return stat(path, sb); |
| 69 | } |
| 70 | #else |
| 71 | typedef struct stat64 stat_wrapper_t; |
| 72 | static int CallStat(const char *path, stat_wrapper_t *sb) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 73 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 74 | return stat64(path, sb); |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | |
[email protected] | 22a087f | 2009-03-17 19:17:43 | [diff] [blame] | 79 | #if defined(GOOGLE_CHROME_BUILD) |
[email protected] | 390433f | 2010-06-10 17:11:29 | [diff] [blame] | 80 | static const char* kTempFileName = ".com.google.chrome.XXXXXX"; |
[email protected] | 22a087f | 2009-03-17 19:17:43 | [diff] [blame] | 81 | #else |
[email protected] | 390433f | 2010-06-10 17:11:29 | [diff] [blame] | 82 | static const char* kTempFileName = ".org.chromium.XXXXXX"; |
[email protected] | 22a087f | 2009-03-17 19:17:43 | [diff] [blame] | 83 | #endif |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 84 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 85 | bool AbsolutePath(FilePath* path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 86 | base::ThreadRestrictions::AssertIOAllowed(); // For realpath(). |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 87 | char full_path[PATH_MAX]; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 88 | if (realpath(path->value().c_str(), full_path) == NULL) |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 89 | return false; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 90 | *path = FilePath(full_path); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 94 | int CountFilesCreatedAfter(const FilePath& path, |
| 95 | const base::Time& comparison_time) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 96 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 97 | int file_count = 0; |
| 98 | |
| 99 | DIR* dir = opendir(path.value().c_str()); |
| 100 | if (dir) { |
[email protected] | 8d57882 | 2010-01-25 23:54:54 | [diff] [blame] | 101 | #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) && \ |
[email protected] | b5c72b82 | 2010-02-18 16:11:37 | [diff] [blame] | 102 | !defined(OS_OPENBSD) && !defined(OS_SOLARIS) |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 103 | #error Port warning: depending on the definition of struct dirent, \ |
| 104 | additional space for pathname may be needed |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 105 | #endif |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 106 | struct dirent ent_buf; |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 107 | struct dirent* ent; |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 108 | while (readdir_r(dir, &ent_buf, &ent) == 0 && ent) { |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 109 | if ((strcmp(ent->d_name, ".") == 0) || |
| 110 | (strcmp(ent->d_name, "..") == 0)) |
| 111 | continue; |
| 112 | |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 113 | stat_wrapper_t st; |
| 114 | int test = CallStat(path.Append(ent->d_name).value().c_str(), &st); |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 115 | if (test != 0) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 116 | PLOG(ERROR) << "stat64 failed"; |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 117 | continue; |
| 118 | } |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 119 | // Here, we use Time::TimeT(), which discards microseconds. This |
| 120 | // means that files which are newer than |comparison_time| may |
| 121 | // be considered older. If we don't discard microseconds, it |
| 122 | // introduces another issue. Suppose the following case: |
| 123 | // |
| 124 | // 1. Get |comparison_time| by Time::Now() and the value is 10.1 (secs). |
| 125 | // 2. Create a file and the current time is 10.3 (secs). |
| 126 | // |
| 127 | // As POSIX doesn't have microsecond precision for |st_ctime|, |
| 128 | // the creation time of the file created in the step 2 is 10 and |
| 129 | // the file is considered older than |comparison_time|. After |
| 130 | // all, we may have to accept either of the two issues: 1. files |
| 131 | // which are older than |comparison_time| are considered newer |
| 132 | // (current implementation) 2. files newer than |
| 133 | // |comparison_time| are considered older. |
[email protected] | e9ad435 | 2010-11-17 11:19:15 | [diff] [blame] | 134 | if (static_cast<time_t>(st.st_ctime) >= comparison_time.ToTimeT()) |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 135 | ++file_count; |
| 136 | } |
| 137 | closedir(dir); |
| 138 | } |
| 139 | return file_count; |
| 140 | } |
| 141 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 142 | // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" |
| 143 | // which works both with and without the recursive flag. I'm not sure we need |
| 144 | // that functionality. If not, remove from file_util_win.cc, otherwise add it |
| 145 | // here. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 146 | bool Delete(const FilePath& path, bool recursive) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 147 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 148 | const char* path_str = path.value().c_str(); |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 149 | stat_wrapper_t file_info; |
| 150 | int test = CallStat(path_str, &file_info); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 151 | if (test != 0) { |
| 152 | // The Windows version defines this condition as success. |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 153 | bool ret = (errno == ENOENT || errno == ENOTDIR); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 154 | return ret; |
| 155 | } |
| 156 | if (!S_ISDIR(file_info.st_mode)) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 157 | return (unlink(path_str) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 158 | if (!recursive) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 159 | return (rmdir(path_str) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 160 | |
| 161 | bool success = true; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 162 | std::stack<std::string> directories; |
| 163 | directories.push(path.value()); |
| 164 | FileEnumerator traversal(path, true, static_cast<FileEnumerator::FILE_TYPE>( |
| 165 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES | |
| 166 | FileEnumerator::SHOW_SYM_LINKS)); |
| 167 | for (FilePath current = traversal.Next(); success && !current.empty(); |
| 168 | current = traversal.Next()) { |
| 169 | FileEnumerator::FindInfo info; |
| 170 | traversal.GetFindInfo(&info); |
| 171 | |
| 172 | if (S_ISDIR(info.stat.st_mode)) |
| 173 | directories.push(current.value()); |
| 174 | else |
| 175 | success = (unlink(current.value().c_str()) == 0); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 176 | } |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 177 | |
| 178 | while (success && !directories.empty()) { |
| 179 | FilePath dir = FilePath(directories.top()); |
| 180 | directories.pop(); |
| 181 | success = (rmdir(dir.value().c_str()) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 182 | } |
| 183 | return success; |
| 184 | } |
| 185 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 186 | bool Move(const FilePath& from_path, const FilePath& to_path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 187 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 188 | // Windows compatibility: if to_path exists, from_path and to_path |
| 189 | // must be the same type, either both files, or both directories. |
| 190 | stat_wrapper_t to_file_info; |
| 191 | if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { |
| 192 | stat_wrapper_t from_file_info; |
| 193 | if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { |
| 194 | if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode)) |
| 195 | return false; |
| 196 | } else { |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | |
[email protected] | cc7948a | 2009-03-13 20:01:43 | [diff] [blame] | 201 | if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) |
| 202 | return true; |
| 203 | |
| 204 | if (!CopyDirectory(from_path, to_path, true)) |
| 205 | return false; |
| 206 | |
| 207 | Delete(from_path, true); |
| 208 | return true; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | c5866dca | 2009-05-19 17:21:07 | [diff] [blame] | 211 | bool ReplaceFile(const FilePath& from_path, const FilePath& to_path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 212 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | c5866dca | 2009-05-19 17:21:07 | [diff] [blame] | 213 | return (rename(from_path.value().c_str(), to_path.value().c_str()) == 0); |
| 214 | } |
| 215 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 216 | bool CopyDirectory(const FilePath& from_path, |
| 217 | const FilePath& to_path, |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 218 | bool recursive) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 219 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 220 | // Some old callers of CopyDirectory want it to support wildcards. |
| 221 | // After some discussion, we decided to fix those callers. |
| 222 | // Break loudly here if anyone tries to do this. |
| 223 | // TODO(evanm): remove this once we're sure it's ok. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 224 | DCHECK(to_path.value().find('*') == std::string::npos); |
| 225 | DCHECK(from_path.value().find('*') == std::string::npos); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 226 | |
| 227 | char top_dir[PATH_MAX]; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 228 | if (base::strlcpy(top_dir, from_path.value().c_str(), |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 229 | arraysize(top_dir)) >= arraysize(top_dir)) { |
| 230 | return false; |
| 231 | } |
| 232 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 233 | // This function does not properly handle destinations within the source |
| 234 | FilePath real_to_path = to_path; |
| 235 | if (PathExists(real_to_path)) { |
| 236 | if (!AbsolutePath(&real_to_path)) |
| 237 | return false; |
| 238 | } else { |
| 239 | real_to_path = real_to_path.DirName(); |
| 240 | if (!AbsolutePath(&real_to_path)) |
| 241 | return false; |
| 242 | } |
| 243 | FilePath real_from_path = from_path; |
| 244 | if (!AbsolutePath(&real_from_path)) |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 245 | return false; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 246 | if (real_to_path.value().size() >= real_from_path.value().size() && |
| 247 | real_to_path.value().compare(0, real_from_path.value().size(), |
| 248 | real_from_path.value()) == 0) |
| 249 | return false; |
| 250 | |
| 251 | bool success = true; |
| 252 | FileEnumerator::FILE_TYPE traverse_type = |
| 253 | static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES | |
| 254 | FileEnumerator::SHOW_SYM_LINKS); |
| 255 | if (recursive) |
| 256 | traverse_type = static_cast<FileEnumerator::FILE_TYPE>( |
| 257 | traverse_type | FileEnumerator::DIRECTORIES); |
| 258 | FileEnumerator traversal(from_path, recursive, traverse_type); |
| 259 | |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 260 | // We have to mimic windows behavior here. |to_path| may not exist yet, |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 261 | // start the loop with |to_path|. |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 262 | FileEnumerator::FindInfo info; |
| 263 | FilePath current = from_path; |
| 264 | if (stat(from_path.value().c_str(), &info.stat) < 0) { |
| 265 | LOG(ERROR) << "CopyDirectory() couldn't stat source directory: " << |
| 266 | from_path.value() << " errno = " << errno; |
| 267 | success = false; |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 268 | } |
[email protected] | bc6a901 | 2009-10-15 01:11:44 | [diff] [blame] | 269 | struct stat to_path_stat; |
| 270 | FilePath from_path_base = from_path; |
| 271 | if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 && |
| 272 | S_ISDIR(to_path_stat.st_mode)) { |
| 273 | // If the destination already exists and is a directory, then the |
| 274 | // top level of source needs to be copied. |
| 275 | from_path_base = from_path.DirName(); |
| 276 | } |
| 277 | |
| 278 | // The Windows version of this function assumes that non-recursive calls |
| 279 | // will always have a directory for from_path. |
| 280 | DCHECK(recursive || S_ISDIR(info.stat.st_mode)); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 281 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 282 | while (success && !current.empty()) { |
| 283 | // current is the source path, including from_path, so paste |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 284 | // the suffix after from_path onto to_path to create the target_path. |
[email protected] | abbc573 | 2009-10-13 17:57:27 | [diff] [blame] | 285 | std::string suffix(¤t.value().c_str()[from_path_base.value().size()]); |
[email protected] | ca020961 | 2009-01-13 18:57:46 | [diff] [blame] | 286 | // Strip the leading '/' (if any). |
| 287 | if (!suffix.empty()) { |
[email protected] | 6ae340f | 2009-03-06 09:56:28 | [diff] [blame] | 288 | DCHECK_EQ('/', suffix[0]); |
[email protected] | ca020961 | 2009-01-13 18:57:46 | [diff] [blame] | 289 | suffix.erase(0, 1); |
| 290 | } |
| 291 | const FilePath target_path = to_path.Append(suffix); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 292 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 293 | if (S_ISDIR(info.stat.st_mode)) { |
| 294 | if (mkdir(target_path.value().c_str(), info.stat.st_mode & 01777) != 0 && |
| 295 | errno != EEXIST) { |
| 296 | LOG(ERROR) << "CopyDirectory() couldn't create directory: " << |
| 297 | target_path.value() << " errno = " << errno; |
| 298 | success = false; |
| 299 | } |
| 300 | } else if (S_ISREG(info.stat.st_mode)) { |
| 301 | if (!CopyFile(current, target_path)) { |
| 302 | LOG(ERROR) << "CopyDirectory() couldn't create file: " << |
| 303 | target_path.value(); |
| 304 | success = false; |
| 305 | } |
| 306 | } else { |
| 307 | LOG(WARNING) << "CopyDirectory() skipping non-regular file: " << |
| 308 | current.value(); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 309 | } |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 310 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 311 | current = traversal.Next(); |
| 312 | traversal.GetFindInfo(&info); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 315 | return success; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 316 | } |
| 317 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 318 | bool PathExists(const FilePath& path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 319 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | ef944e05 | 2010-05-17 15:01:22 | [diff] [blame] | 320 | return access(path.value().c_str(), F_OK) == 0; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 321 | } |
| 322 | |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 323 | bool PathIsWritable(const FilePath& path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 324 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | ef944e05 | 2010-05-17 15:01:22 | [diff] [blame] | 325 | return access(path.value().c_str(), W_OK) == 0; |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 328 | bool DirectoryExists(const FilePath& path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 329 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 330 | stat_wrapper_t file_info; |
| 331 | if (CallStat(path.value().c_str(), &file_info) == 0) |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 332 | return S_ISDIR(file_info.st_mode); |
| 333 | return false; |
| 334 | } |
| 335 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 336 | // TODO(erikkay): implement |
| 337 | #if 0 |
| 338 | bool GetFileCreationLocalTimeFromHandle(int fd, |
| 339 | LPSYSTEMTIME creation_time) { |
| 340 | if (!file_handle) |
| 341 | return false; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 342 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 343 | FILETIME utc_filetime; |
| 344 | if (!GetFileTime(file_handle, &utc_filetime, NULL, NULL)) |
| 345 | return false; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 346 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 347 | FILETIME local_filetime; |
| 348 | if (!FileTimeToLocalFileTime(&utc_filetime, &local_filetime)) |
| 349 | return false; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 350 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 351 | return !!FileTimeToSystemTime(&local_filetime, creation_time); |
| 352 | } |
| 353 | |
| 354 | bool GetFileCreationLocalTime(const std::string& filename, |
| 355 | LPSYSTEMTIME creation_time) { |
| 356 | ScopedHandle file_handle( |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 357 | CreateFile(filename.c_str(), GENERIC_READ, |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 358 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 359 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); |
| 360 | return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); |
| 361 | } |
| 362 | #endif |
| 363 | |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 364 | bool ReadFromFD(int fd, char* buffer, size_t bytes) { |
| 365 | size_t total_read = 0; |
| 366 | while (total_read < bytes) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 367 | ssize_t bytes_read = |
| 368 | HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read)); |
| 369 | if (bytes_read <= 0) |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 370 | break; |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 371 | total_read += bytes_read; |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 372 | } |
| 373 | return total_read == bytes; |
| 374 | } |
| 375 | |
[email protected] | 2e733d10 | 2010-11-30 00:43:37 | [diff] [blame^] | 376 | bool CreateSymbolicLink(const FilePath& target_path, |
| 377 | const FilePath& symlink_path) { |
| 378 | DCHECK(!symlink_path.empty()); |
| 379 | DCHECK(!target_path.empty()); |
| 380 | return ::symlink(target_path.value().c_str(), |
| 381 | symlink_path.value().c_str()) != -1; |
| 382 | } |
| 383 | |
| 384 | bool ReadSymbolicLink(const FilePath& symlink_path, |
| 385 | FilePath* target_path) { |
| 386 | DCHECK(!symlink_path.empty()); |
| 387 | DCHECK(target_path); |
| 388 | char buf[PATH_MAX]; |
| 389 | ssize_t count = ::readlink(symlink_path.value().c_str(), buf, arraysize(buf)); |
| 390 | |
| 391 | if (count <= 0) |
| 392 | return false; |
| 393 | |
| 394 | *target_path = FilePath(FilePath::StringType(buf, count)); |
| 395 | |
| 396 | return true; |
| 397 | } |
| 398 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 399 | // Creates and opens a temporary file in |directory|, returning the |
[email protected] | 16eac0a7 | 2009-09-11 17:33:50 | [diff] [blame] | 400 | // file descriptor. |path| is set to the temporary file path. |
| 401 | // This function does NOT unlink() the file. |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 402 | int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 403 | base::ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp(). |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 404 | *path = directory.Append(kTempFileName); |
| 405 | const std::string& tmpdir_string = path->value(); |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 406 | // this should be OK since mkstemp just replaces characters in place |
| 407 | char* buffer = const_cast<char*>(tmpdir_string.c_str()); |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 408 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 409 | return mkstemp(buffer); |
| 410 | } |
| 411 | |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 412 | bool CreateTemporaryFile(FilePath* path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 413 | base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 414 | FilePath directory; |
| 415 | if (!GetTempDir(&directory)) |
| 416 | return false; |
| 417 | int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 418 | if (fd < 0) |
| 419 | return false; |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 420 | close(fd); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 421 | return true; |
| 422 | } |
| 423 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 424 | FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { |
| 425 | FilePath directory; |
| 426 | if (!GetShmemTempDir(&directory)) |
[email protected] | 628476aa | 2010-06-10 22:56:23 | [diff] [blame] | 427 | return NULL; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 428 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 429 | return CreateAndOpenTemporaryFileInDir(directory, path); |
| 430 | } |
| 431 | |
| 432 | FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
| 433 | int fd = CreateAndOpenFdForTemporaryFile(dir, path); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 434 | if (fd < 0) |
| 435 | return NULL; |
| 436 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 437 | return fdopen(fd, "a+"); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 438 | } |
[email protected] | 6445c40 | 2009-09-11 20:06:27 | [diff] [blame] | 439 | |
| 440 | bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 441 | base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
[email protected] | 6445c40 | 2009-09-11 20:06:27 | [diff] [blame] | 442 | int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); |
| 443 | return ((fd >= 0) && !close(fd)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 444 | } |
| 445 | |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 446 | static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, |
| 447 | const FilePath::StringType& name_tmpl, |
| 448 | FilePath* new_dir) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 449 | base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp(). |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 450 | CHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) |
| 451 | << "Directory name template must contain \"XXXXXX\"."; |
| 452 | |
| 453 | FilePath sub_dir = base_dir.Append(name_tmpl); |
| 454 | std::string sub_dir_string = sub_dir.value(); |
| 455 | |
| 456 | // this should be OK since mkdtemp just replaces characters in place |
| 457 | char* buffer = const_cast<char*>(sub_dir_string.c_str()); |
| 458 | char* dtemp = mkdtemp(buffer); |
[email protected] | 3ad035d2 | 2010-07-28 21:00:51 | [diff] [blame] | 459 | if (!dtemp) { |
| 460 | DPLOG(ERROR) << "mkdtemp"; |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 461 | return false; |
[email protected] | 3ad035d2 | 2010-07-28 21:00:51 | [diff] [blame] | 462 | } |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 463 | *new_dir = FilePath(dtemp); |
| 464 | return true; |
| 465 | } |
| 466 | |
| 467 | bool CreateTemporaryDirInDir(const FilePath& base_dir, |
| 468 | const FilePath::StringType& prefix, |
[email protected] | 046062e8 | 2010-06-30 07:19:11 | [diff] [blame] | 469 | FilePath* new_dir) { |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 470 | FilePath::StringType mkdtemp_template = prefix; |
| 471 | mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX")); |
| 472 | return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir); |
| 473 | } |
| 474 | |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 475 | bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 476 | FilePath* new_temp_path) { |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 477 | FilePath tmpdir; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 478 | if (!GetTempDir(&tmpdir)) |
| 479 | return false; |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 480 | |
| 481 | return CreateTemporaryDirInDirImpl(tmpdir, kTempFileName, new_temp_path); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 482 | } |
| 483 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 484 | bool CreateDirectory(const FilePath& full_path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 485 | base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 486 | std::vector<FilePath> subpaths; |
| 487 | |
| 488 | // Collect a list of all parent directories. |
| 489 | FilePath last_path = full_path; |
| 490 | subpaths.push_back(full_path); |
| 491 | for (FilePath path = full_path.DirName(); |
| 492 | path.value() != last_path.value(); path = path.DirName()) { |
| 493 | subpaths.push_back(path); |
| 494 | last_path = path; |
| 495 | } |
| 496 | |
| 497 | // Iterate through the parents and create the missing ones. |
| 498 | for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); |
| 499 | i != subpaths.rend(); ++i) { |
[email protected] | 0ba86e4 | 2010-03-17 21:39:42 | [diff] [blame] | 500 | if (DirectoryExists(*i)) |
| 501 | continue; |
| 502 | if (mkdir(i->value().c_str(), 0700) == 0) |
| 503 | continue; |
| 504 | // Mkdir failed, but it might have failed with EEXIST, or some other error |
| 505 | // due to the the directory appearing out of thin air. This can occur if |
| 506 | // two processes are trying to create the same file system tree at the same |
| 507 | // time. Check to see if it exists and make sure it is a directory. |
| 508 | if (!DirectoryExists(*i)) |
| 509 | return false; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 510 | } |
| 511 | return true; |
| 512 | } |
| 513 | |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 514 | bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* results) { |
[email protected] | fb66f9d | 2009-09-07 16:39:46 | [diff] [blame] | 515 | stat_wrapper_t file_info; |
| 516 | if (CallStat(file_path.value().c_str(), &file_info) != 0) |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 517 | return false; |
[email protected] | f5e3da4d | 2008-09-26 01:04:08 | [diff] [blame] | 518 | results->is_directory = S_ISDIR(file_info.st_mode); |
| 519 | results->size = file_info.st_size; |
[email protected] | 5ef32389 | 2009-07-24 16:13:53 | [diff] [blame] | 520 | results->last_modified = base::Time::FromTimeT(file_info.st_mtime); |
[email protected] | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 521 | results->last_accessed = base::Time::FromTimeT(file_info.st_atime); |
| 522 | results->creation_time = base::Time::FromTimeT(file_info.st_ctime); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 523 | return true; |
| 524 | } |
| 525 | |
[email protected] | 825003f | 2009-05-14 17:49:23 | [diff] [blame] | 526 | bool GetInode(const FilePath& path, ino_t* inode) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 527 | base::ThreadRestrictions::AssertIOAllowed(); // For call to stat(). |
[email protected] | 825003f | 2009-05-14 17:49:23 | [diff] [blame] | 528 | struct stat buffer; |
| 529 | int result = stat(path.value().c_str(), &buffer); |
| 530 | if (result < 0) |
| 531 | return false; |
| 532 | |
| 533 | *inode = buffer.st_ino; |
| 534 | return true; |
| 535 | } |
| 536 | |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 537 | FILE* OpenFile(const std::string& filename, const char* mode) { |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 538 | return OpenFile(FilePath(filename), mode); |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 539 | } |
| 540 | |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 541 | FILE* OpenFile(const FilePath& filename, const char* mode) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 542 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 543 | return fopen(filename.value().c_str(), mode); |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 544 | } |
| 545 | |
[email protected] | c870c76 | 2009-01-28 05:47:15 | [diff] [blame] | 546 | int ReadFile(const FilePath& filename, char* data, int size) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 547 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | c870c76 | 2009-01-28 05:47:15 | [diff] [blame] | 548 | int fd = open(filename.value().c_str(), O_RDONLY); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 549 | if (fd < 0) |
| 550 | return -1; |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 551 | |
[email protected] | cabe39c | 2010-02-02 02:28:16 | [diff] [blame] | 552 | ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); |
| 553 | if (int ret = HANDLE_EINTR(close(fd)) < 0) |
| 554 | return ret; |
| 555 | return bytes_read; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 556 | } |
| 557 | |
[email protected] | c870c76 | 2009-01-28 05:47:15 | [diff] [blame] | 558 | int WriteFile(const FilePath& filename, const char* data, int size) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 559 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | c870c76 | 2009-01-28 05:47:15 | [diff] [blame] | 560 | int fd = creat(filename.value().c_str(), 0666); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 561 | if (fd < 0) |
| 562 | return -1; |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 563 | |
[email protected] | cabe39c | 2010-02-02 02:28:16 | [diff] [blame] | 564 | int bytes_written = WriteFileDescriptor(fd, data, size); |
| 565 | if (int ret = HANDLE_EINTR(close(fd)) < 0) |
| 566 | return ret; |
| 567 | return bytes_written; |
[email protected] | fbea023 | 2009-09-16 00:29:22 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | int WriteFileDescriptor(const int fd, const char* data, int size) { |
| 571 | // Allow for partial writes. |
| 572 | ssize_t bytes_written_total = 0; |
| 573 | for (ssize_t bytes_written_partial = 0; bytes_written_total < size; |
| 574 | bytes_written_total += bytes_written_partial) { |
| 575 | bytes_written_partial = |
| 576 | HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 577 | size - bytes_written_total)); |
| 578 | if (bytes_written_partial < 0) |
| 579 | return -1; |
| 580 | } |
| 581 | |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 582 | return bytes_written_total; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | // Gets the current working directory for the process. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 586 | bool GetCurrentDirectory(FilePath* dir) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 587 | // getcwd can return ENOENT, which implies it checks against the disk. |
| 588 | base::ThreadRestrictions::AssertIOAllowed(); |
| 589 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 590 | char system_buffer[PATH_MAX] = ""; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 591 | if (!getcwd(system_buffer, sizeof(system_buffer))) { |
| 592 | NOTREACHED(); |
| 593 | return false; |
| 594 | } |
| 595 | *dir = FilePath(system_buffer); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 596 | return true; |
| 597 | } |
| 598 | |
| 599 | // Sets the current working directory for the process. |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 600 | bool SetCurrentDirectory(const FilePath& path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 601 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 602 | int ret = chdir(path.value().c_str()); |
| 603 | return !ret; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 604 | } |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 605 | |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 606 | /////////////////////////////////////////////// |
| 607 | // FileEnumerator |
| 608 | |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 609 | FileEnumerator::FileEnumerator(const FilePath& root_path, |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 610 | bool recursive, |
| 611 | FileEnumerator::FILE_TYPE file_type) |
[email protected] | fbf745a | 2009-12-08 22:05:59 | [diff] [blame] | 612 | : current_directory_entry_(0), |
| 613 | root_path_(root_path), |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 614 | recursive_(recursive), |
[email protected] | a82026e | 2010-10-14 23:43:29 | [diff] [blame] | 615 | file_type_(file_type) { |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 616 | // INCLUDE_DOT_DOT must not be specified if recursive. |
| 617 | DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 618 | pending_paths_.push(root_path); |
| 619 | } |
| 620 | |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 621 | FileEnumerator::FileEnumerator(const FilePath& root_path, |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 622 | bool recursive, |
| 623 | FileEnumerator::FILE_TYPE file_type, |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 624 | const FilePath::StringType& pattern) |
[email protected] | fbf745a | 2009-12-08 22:05:59 | [diff] [blame] | 625 | : current_directory_entry_(0), |
| 626 | root_path_(root_path), |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 627 | recursive_(recursive), |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 628 | file_type_(file_type), |
[email protected] | a82026e | 2010-10-14 23:43:29 | [diff] [blame] | 629 | pattern_(root_path.Append(pattern).value()) { |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 630 | // INCLUDE_DOT_DOT must not be specified if recursive. |
| 631 | DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 632 | // The Windows version of this code appends the pattern to the root_path, |
| 633 | // potentially only matching against items in the top-most directory. |
| 634 | // Do the same here. |
| 635 | if (pattern.size() == 0) |
[email protected] | fbf745a | 2009-12-08 22:05:59 | [diff] [blame] | 636 | pattern_ = FilePath::StringType(); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 637 | pending_paths_.push(root_path); |
| 638 | } |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 639 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 640 | FileEnumerator::~FileEnumerator() { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 641 | } |
| 642 | |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 643 | void FileEnumerator::GetFindInfo(FindInfo* info) { |
| 644 | DCHECK(info); |
| 645 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 646 | if (current_directory_entry_ >= directory_entries_.size()) |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 647 | return; |
| 648 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 649 | DirectoryEntryInfo* cur_entry = &directory_entries_[current_directory_entry_]; |
| 650 | memcpy(&(info->stat), &(cur_entry->stat), sizeof(info->stat)); |
| 651 | info->filename.assign(cur_entry->filename.value()); |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 652 | } |
| 653 | |
[email protected] | 9fa8c2f5 | 2009-12-11 21:12:29 | [diff] [blame] | 654 | bool FileEnumerator::IsDirectory(const FindInfo& info) { |
| 655 | return S_ISDIR(info.stat.st_mode); |
| 656 | } |
| 657 | |
[email protected] | 1525c68 | 2010-02-11 23:27:47 | [diff] [blame] | 658 | // static |
| 659 | FilePath FileEnumerator::GetFilename(const FindInfo& find_info) { |
| 660 | return FilePath(find_info.filename); |
| 661 | } |
| 662 | |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 663 | FilePath FileEnumerator::Next() { |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 664 | ++current_directory_entry_; |
| 665 | |
| 666 | // While we've exhausted the entries in the current directory, do the next |
| 667 | while (current_directory_entry_ >= directory_entries_.size()) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 668 | if (pending_paths_.empty()) |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 669 | return FilePath(); |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 670 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 671 | root_path_ = pending_paths_.top(); |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 672 | root_path_ = root_path_.StripTrailingSeparators(); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 673 | pending_paths_.pop(); |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 674 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 675 | std::vector<DirectoryEntryInfo> entries; |
| 676 | if (!ReadDirectory(&entries, root_path_, file_type_ & SHOW_SYM_LINKS)) |
| 677 | continue; |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 678 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 679 | directory_entries_.clear(); |
| 680 | current_directory_entry_ = 0; |
| 681 | for (std::vector<DirectoryEntryInfo>::const_iterator |
| 682 | i = entries.begin(); i != entries.end(); ++i) { |
| 683 | FilePath full_path = root_path_.Append(i->filename); |
| 684 | if (ShouldSkip(full_path)) |
| 685 | continue; |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 686 | |
[email protected] | fbf745a | 2009-12-08 22:05:59 | [diff] [blame] | 687 | if (pattern_.size() && |
| 688 | fnmatch(pattern_.c_str(), full_path.value().c_str(), FNM_NOESCAPE)) |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 689 | continue; |
| 690 | |
| 691 | if (recursive_ && S_ISDIR(i->stat.st_mode)) |
| 692 | pending_paths_.push(full_path); |
| 693 | |
| 694 | if ((S_ISDIR(i->stat.st_mode) && (file_type_ & DIRECTORIES)) || |
| 695 | (!S_ISDIR(i->stat.st_mode) && (file_type_ & FILES))) |
| 696 | directory_entries_.push_back(*i); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 697 | } |
| 698 | } |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 699 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 700 | return root_path_.Append(directory_entries_[current_directory_entry_ |
| 701 | ].filename); |
| 702 | } |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 703 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 704 | bool FileEnumerator::ReadDirectory(std::vector<DirectoryEntryInfo>* entries, |
| 705 | const FilePath& source, bool show_links) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 706 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 707 | DIR* dir = opendir(source.value().c_str()); |
| 708 | if (!dir) |
| 709 | return false; |
| 710 | |
[email protected] | 8d57882 | 2010-01-25 23:54:54 | [diff] [blame] | 711 | #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) && \ |
[email protected] | b5c72b82 | 2010-02-18 16:11:37 | [diff] [blame] | 712 | !defined(OS_OPENBSD) && !defined(OS_SOLARIS) |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 713 | #error Port warning: depending on the definition of struct dirent, \ |
| 714 | additional space for pathname may be needed |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 715 | #endif |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 716 | |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 717 | struct dirent dent_buf; |
| 718 | struct dirent* dent; |
| 719 | while (readdir_r(dir, &dent_buf, &dent) == 0 && dent) { |
| 720 | DirectoryEntryInfo info; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 721 | info.filename = FilePath(dent->d_name); |
[email protected] | 2237f5d7 | 2009-09-03 22:39:34 | [diff] [blame] | 722 | |
| 723 | FilePath full_name = source.Append(dent->d_name); |
| 724 | int ret; |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 725 | if (show_links) |
[email protected] | 2237f5d7 | 2009-09-03 22:39:34 | [diff] [blame] | 726 | ret = lstat(full_name.value().c_str(), &info.stat); |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 727 | else |
[email protected] | 2237f5d7 | 2009-09-03 22:39:34 | [diff] [blame] | 728 | ret = stat(full_name.value().c_str(), &info.stat); |
| 729 | if (ret < 0) { |
| 730 | // Print the stat() error message unless it was ENOENT and we're |
| 731 | // following symlinks. |
[email protected] | e131cf5 | 2010-03-25 19:10:28 | [diff] [blame] | 732 | if (!(errno == ENOENT && !show_links)) { |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 733 | PLOG(ERROR) << "Couldn't stat " |
| 734 | << source.Append(dent->d_name).value(); |
[email protected] | 2237f5d7 | 2009-09-03 22:39:34 | [diff] [blame] | 735 | } |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 736 | memset(&info.stat, 0, sizeof(info.stat)); |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 737 | } |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 738 | entries->push_back(info); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 739 | } |
[email protected] | 49930c3a | 2009-08-06 21:23:07 | [diff] [blame] | 740 | |
| 741 | closedir(dir); |
| 742 | return true; |
| 743 | } |
| 744 | |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 745 | /////////////////////////////////////////////// |
| 746 | // MemoryMappedFile |
| 747 | |
| 748 | MemoryMappedFile::MemoryMappedFile() |
[email protected] | cb6037d | 2009-11-16 22:55:17 | [diff] [blame] | 749 | : file_(base::kInvalidPlatformFileValue), |
| 750 | data_(NULL), |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 751 | length_(0) { |
| 752 | } |
| 753 | |
[email protected] | 85c55dc | 2009-11-06 03:05:46 | [diff] [blame] | 754 | bool MemoryMappedFile::MapFileToMemoryInternal() { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 755 | base::ThreadRestrictions::AssertIOAllowed(); |
| 756 | |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 757 | struct stat file_stat; |
[email protected] | cb6037d | 2009-11-16 22:55:17 | [diff] [blame] | 758 | if (fstat(file_, &file_stat) == base::kInvalidPlatformFileValue) { |
| 759 | LOG(ERROR) << "Couldn't fstat " << file_ << ", errno " << errno; |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 760 | return false; |
[email protected] | 4883a4e | 2009-06-06 19:59:36 | [diff] [blame] | 761 | } |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 762 | length_ = file_stat.st_size; |
| 763 | |
| 764 | data_ = static_cast<uint8*>( |
[email protected] | cb6037d | 2009-11-16 22:55:17 | [diff] [blame] | 765 | mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0)); |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 766 | if (data_ == MAP_FAILED) |
[email protected] | cb6037d | 2009-11-16 22:55:17 | [diff] [blame] | 767 | LOG(ERROR) << "Couldn't mmap " << file_ << ", errno " << errno; |
[email protected] | 4883a4e | 2009-06-06 19:59:36 | [diff] [blame] | 768 | |
| 769 | return data_ != MAP_FAILED; |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void MemoryMappedFile::CloseHandles() { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 773 | base::ThreadRestrictions::AssertIOAllowed(); |
| 774 | |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 775 | if (data_ != NULL) |
| 776 | munmap(data_, length_); |
[email protected] | cb6037d | 2009-11-16 22:55:17 | [diff] [blame] | 777 | if (file_ != base::kInvalidPlatformFileValue) |
| 778 | close(file_); |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 779 | |
| 780 | data_ = NULL; |
| 781 | length_ = 0; |
[email protected] | cb6037d | 2009-11-16 22:55:17 | [diff] [blame] | 782 | file_ = base::kInvalidPlatformFileValue; |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 783 | } |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 784 | |
[email protected] | 35b9be0 | 2009-11-26 00:37:06 | [diff] [blame] | 785 | bool HasFileBeenModifiedSince(const FileEnumerator::FindInfo& find_info, |
| 786 | const base::Time& cutoff_time) { |
[email protected] | e9ad435 | 2010-11-17 11:19:15 | [diff] [blame] | 787 | return static_cast<time_t>(find_info.stat.st_mtime) >= cutoff_time.ToTimeT(); |
[email protected] | 35b9be0 | 2009-11-26 00:37:06 | [diff] [blame] | 788 | } |
| 789 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 790 | bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) { |
| 791 | FilePath real_path_result; |
| 792 | if (!RealPath(path, &real_path_result)) |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 793 | return false; |
| 794 | |
[email protected] | 6f5f432 | 2010-06-09 22:56:48 | [diff] [blame] | 795 | // To be consistant with windows, fail if |real_path_result| is a |
| 796 | // directory. |
| 797 | stat_wrapper_t file_info; |
| 798 | if (CallStat(real_path_result.value().c_str(), &file_info) != 0 || |
| 799 | S_ISDIR(file_info.st_mode)) |
| 800 | return false; |
| 801 | |
| 802 | *normalized_path = real_path_result; |
[email protected] | 01e2a1f | 2010-05-12 15:13:57 | [diff] [blame] | 803 | return true; |
| 804 | } |
| 805 | |
[email protected] | 84cb191 | 2010-04-21 21:11:36 | [diff] [blame] | 806 | #if !defined(OS_MACOSX) |
| 807 | bool GetTempDir(FilePath* path) { |
| 808 | const char* tmp = getenv("TMPDIR"); |
| 809 | if (tmp) |
| 810 | *path = FilePath(tmp); |
| 811 | else |
| 812 | *path = FilePath("/tmp"); |
| 813 | return true; |
| 814 | } |
| 815 | |
| 816 | bool GetShmemTempDir(FilePath* path) { |
| 817 | *path = FilePath("/dev/shm"); |
| 818 | return true; |
| 819 | } |
| 820 | |
[email protected] | 1c65785 | 2010-04-22 23:28:05 | [diff] [blame] | 821 | FilePath GetHomeDir() { |
| 822 | const char* home_dir = getenv("HOME"); |
| 823 | if (home_dir && home_dir[0]) |
| 824 | return FilePath(home_dir); |
| 825 | |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 826 | // g_get_home_dir calls getpwent, which can fall through to LDAP calls. |
| 827 | base::ThreadRestrictions::AssertIOAllowed(); |
| 828 | |
[email protected] | 1c65785 | 2010-04-22 23:28:05 | [diff] [blame] | 829 | home_dir = g_get_home_dir(); |
| 830 | if (home_dir && home_dir[0]) |
| 831 | return FilePath(home_dir); |
| 832 | |
| 833 | FilePath rv; |
| 834 | if (file_util::GetTempDir(&rv)) |
| 835 | return rv; |
| 836 | |
| 837 | // Last resort. |
| 838 | return FilePath("/tmp"); |
| 839 | } |
| 840 | |
[email protected] | 84cb191 | 2010-04-21 21:11:36 | [diff] [blame] | 841 | bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame] | 842 | base::ThreadRestrictions::AssertIOAllowed(); |
[email protected] | 84cb191 | 2010-04-21 21:11:36 | [diff] [blame] | 843 | int infile = open(from_path.value().c_str(), O_RDONLY); |
| 844 | if (infile < 0) |
| 845 | return false; |
| 846 | |
| 847 | int outfile = creat(to_path.value().c_str(), 0666); |
| 848 | if (outfile < 0) { |
| 849 | close(infile); |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | const size_t kBufferSize = 32768; |
| 854 | std::vector<char> buffer(kBufferSize); |
| 855 | bool result = true; |
| 856 | |
| 857 | while (result) { |
| 858 | ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size())); |
| 859 | if (bytes_read < 0) { |
| 860 | result = false; |
| 861 | break; |
| 862 | } |
| 863 | if (bytes_read == 0) |
| 864 | break; |
| 865 | // Allow for partial writes |
| 866 | ssize_t bytes_written_per_read = 0; |
| 867 | do { |
| 868 | ssize_t bytes_written_partial = HANDLE_EINTR(write( |
| 869 | outfile, |
| 870 | &buffer[bytes_written_per_read], |
| 871 | bytes_read - bytes_written_per_read)); |
| 872 | if (bytes_written_partial < 0) { |
| 873 | result = false; |
| 874 | break; |
| 875 | } |
| 876 | bytes_written_per_read += bytes_written_partial; |
| 877 | } while (bytes_written_per_read < bytes_read); |
| 878 | } |
| 879 | |
| 880 | if (HANDLE_EINTR(close(infile)) < 0) |
| 881 | result = false; |
| 882 | if (HANDLE_EINTR(close(outfile)) < 0) |
| 883 | result = false; |
| 884 | |
| 885 | return result; |
| 886 | } |
| 887 | #endif // defined(OS_MACOSX) |
| 888 | |
[email protected] | 33e585a | 2010-11-20 03:38:15 | [diff] [blame] | 889 | } // namespace file_util |