mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 7 | #include <dirent.h> |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 8 | #include <errno.h> |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 9 | #include <fcntl.h> |
| 10 | #include <fnmatch.h> |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 11 | #include <libgen.h> |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 12 | #include <stdio.h> |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 13 | #include <string.h> |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 14 | #include <sys/errno.h> |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 15 | #include <sys/mman.h> |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 16 | #include <sys/stat.h> |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 17 | #include <sys/types.h> |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 18 | #include <time.h> |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 19 | #include <unistd.h> |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 20 | |
mark@chromium.org | 0e56c16 | 2009-09-17 02:31:25 +0900 | [diff] [blame] | 21 | #if defined(OS_MACOSX) |
| 22 | #include <AvailabilityMacros.h> |
| 23 | #endif |
| 24 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 25 | #include <fstream> |
| 26 | |
| 27 | #include "base/basictypes.h" |
agl@chromium.org | d263ad7 | 2009-05-02 06:37:31 +0900 | [diff] [blame] | 28 | #include "base/eintr_wrapper.h" |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 29 | #include "base/file_path.h" |
yuzo@chromium.org | c846e45 | 2009-07-15 10:32:02 +0900 | [diff] [blame] | 30 | #include "base/lock.h" |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 31 | #include "base/logging.h" |
yuzo@chromium.org | c846e45 | 2009-07-15 10:32:02 +0900 | [diff] [blame] | 32 | #include "base/scoped_ptr.h" |
| 33 | #include "base/singleton.h" |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 34 | #include "base/string_util.h" |
yuzo@chromium.org | c846e45 | 2009-07-15 10:32:02 +0900 | [diff] [blame] | 35 | #include "base/sys_string_conversions.h" |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 36 | #include "base/time.h" |
brettw@chromium.org | 50c9465 | 2009-10-07 11:10:20 +0900 | [diff] [blame] | 37 | #include "base/utf_string_conversions.h" |
estade@chromium.org | 868ecbc | 2009-06-24 12:29:26 +0900 | [diff] [blame] | 38 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 39 | namespace file_util { |
| 40 | |
mark@chromium.org | 0e56c16 | 2009-09-17 02:31:25 +0900 | [diff] [blame] | 41 | #if defined(OS_FREEBSD) || \ |
| 42 | (defined(OS_MACOSX) && \ |
| 43 | MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 44 | typedef struct stat stat_wrapper_t; |
| 45 | static int CallStat(const char *path, stat_wrapper_t *sb) { |
| 46 | return stat(path, sb); |
| 47 | } |
| 48 | #else |
| 49 | typedef struct stat64 stat_wrapper_t; |
| 50 | static int CallStat(const char *path, stat_wrapper_t *sb) { |
| 51 | return stat64(path, sb); |
| 52 | } |
| 53 | #endif |
| 54 | |
| 55 | |
estade@chromium.org | f3b7320 | 2009-03-18 04:17:43 +0900 | [diff] [blame] | 56 | #if defined(GOOGLE_CHROME_BUILD) |
estade@chromium.org | f474a1b | 2008-11-11 09:01:38 +0900 | [diff] [blame] | 57 | static const char* kTempFileName = "com.google.chrome.XXXXXX"; |
estade@chromium.org | f3b7320 | 2009-03-18 04:17:43 +0900 | [diff] [blame] | 58 | #else |
| 59 | static const char* kTempFileName = "org.chromium.XXXXXX"; |
| 60 | #endif |
mark@chromium.org | 8ca0d27 | 2008-09-12 02:36:23 +0900 | [diff] [blame] | 61 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 62 | std::wstring GetDirectoryFromPath(const std::wstring& path) { |
| 63 | if (EndsWithSeparator(path)) { |
tkent@chromium.org | fce07c7 | 2009-10-15 14:00:25 +0900 | [diff] [blame] | 64 | return FilePath::FromWStringHack(path) |
| 65 | .StripTrailingSeparators() |
| 66 | .ToWStringHack(); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 67 | } else { |
| 68 | char full_path[PATH_MAX]; |
| 69 | base::strlcpy(full_path, WideToUTF8(path).c_str(), arraysize(full_path)); |
| 70 | return UTF8ToWide(dirname(full_path)); |
| 71 | } |
| 72 | } |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 73 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 74 | bool AbsolutePath(FilePath* path) { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 75 | char full_path[PATH_MAX]; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 76 | if (realpath(path->value().c_str(), full_path) == NULL) |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 77 | return false; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 78 | *path = FilePath(full_path); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 82 | int CountFilesCreatedAfter(const FilePath& path, |
| 83 | const base::Time& comparison_time) { |
| 84 | int file_count = 0; |
| 85 | |
| 86 | DIR* dir = opendir(path.value().c_str()); |
| 87 | if (dir) { |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 88 | #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 89 | #error Depending on the definition of struct dirent, additional space for \ |
| 90 | pathname may be needed |
| 91 | #endif |
erikkay@google.com | 8d133f6 | 2009-04-24 00:05:19 +0900 | [diff] [blame] | 92 | struct dirent ent_buf; |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 93 | struct dirent* ent; |
erikkay@google.com | 8d133f6 | 2009-04-24 00:05:19 +0900 | [diff] [blame] | 94 | while (readdir_r(dir, &ent_buf, &ent) == 0 && ent) { |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 95 | if ((strcmp(ent->d_name, ".") == 0) || |
| 96 | (strcmp(ent->d_name, "..") == 0)) |
| 97 | continue; |
| 98 | |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 99 | stat_wrapper_t st; |
| 100 | int test = CallStat(path.Append(ent->d_name).value().c_str(), &st); |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 101 | if (test != 0) { |
tschmelcher@chromium.org | 90a3f8a | 2009-10-14 03:27:40 +0900 | [diff] [blame] | 102 | PLOG(ERROR) << "stat64 failed"; |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 103 | continue; |
| 104 | } |
erikkay@google.com | 8d133f6 | 2009-04-24 00:05:19 +0900 | [diff] [blame] | 105 | // Here, we use Time::TimeT(), which discards microseconds. This |
| 106 | // means that files which are newer than |comparison_time| may |
| 107 | // be considered older. If we don't discard microseconds, it |
| 108 | // introduces another issue. Suppose the following case: |
| 109 | // |
| 110 | // 1. Get |comparison_time| by Time::Now() and the value is 10.1 (secs). |
| 111 | // 2. Create a file and the current time is 10.3 (secs). |
| 112 | // |
| 113 | // As POSIX doesn't have microsecond precision for |st_ctime|, |
| 114 | // the creation time of the file created in the step 2 is 10 and |
| 115 | // the file is considered older than |comparison_time|. After |
| 116 | // all, we may have to accept either of the two issues: 1. files |
| 117 | // which are older than |comparison_time| are considered newer |
| 118 | // (current implementation) 2. files newer than |
| 119 | // |comparison_time| are considered older. |
evan@chromium.org | 3730132 | 2009-04-21 10:50:39 +0900 | [diff] [blame] | 120 | if (st.st_ctime >= comparison_time.ToTimeT()) |
| 121 | ++file_count; |
| 122 | } |
| 123 | closedir(dir); |
| 124 | } |
| 125 | return file_count; |
| 126 | } |
| 127 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 128 | // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" |
| 129 | // which works both with and without the recursive flag. I'm not sure we need |
| 130 | // that functionality. If not, remove from file_util_win.cc, otherwise add it |
| 131 | // here. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 132 | bool Delete(const FilePath& path, bool recursive) { |
| 133 | const char* path_str = path.value().c_str(); |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 134 | stat_wrapper_t file_info; |
| 135 | int test = CallStat(path_str, &file_info); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 136 | if (test != 0) { |
| 137 | // The Windows version defines this condition as success. |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 138 | bool ret = (errno == ENOENT || errno == ENOTDIR); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | if (!S_ISDIR(file_info.st_mode)) |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 142 | return (unlink(path_str) == 0); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 143 | if (!recursive) |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 144 | return (rmdir(path_str) == 0); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 145 | |
| 146 | bool success = true; |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 147 | std::stack<std::string> directories; |
| 148 | directories.push(path.value()); |
| 149 | FileEnumerator traversal(path, true, static_cast<FileEnumerator::FILE_TYPE>( |
| 150 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES | |
| 151 | FileEnumerator::SHOW_SYM_LINKS)); |
| 152 | for (FilePath current = traversal.Next(); success && !current.empty(); |
| 153 | current = traversal.Next()) { |
| 154 | FileEnumerator::FindInfo info; |
| 155 | traversal.GetFindInfo(&info); |
| 156 | |
| 157 | if (S_ISDIR(info.stat.st_mode)) |
| 158 | directories.push(current.value()); |
| 159 | else |
| 160 | success = (unlink(current.value().c_str()) == 0); |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 161 | } |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 162 | |
| 163 | while (success && !directories.empty()) { |
| 164 | FilePath dir = FilePath(directories.top()); |
| 165 | directories.pop(); |
| 166 | success = (rmdir(dir.value().c_str()) == 0); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 167 | } |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 168 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 169 | return success; |
| 170 | } |
| 171 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 172 | bool Move(const FilePath& from_path, const FilePath& to_path) { |
vandebo@chromium.org | c0cf77e | 2009-10-15 10:11:44 +0900 | [diff] [blame] | 173 | // Windows compatibility: if to_path exists, from_path and to_path |
| 174 | // must be the same type, either both files, or both directories. |
| 175 | stat_wrapper_t to_file_info; |
| 176 | if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { |
| 177 | stat_wrapper_t from_file_info; |
| 178 | if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { |
| 179 | if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode)) |
| 180 | return false; |
| 181 | } else { |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
estade@chromium.org | a2fd8d8 | 2009-03-14 05:01:43 +0900 | [diff] [blame] | 186 | if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) |
| 187 | return true; |
| 188 | |
| 189 | if (!CopyDirectory(from_path, to_path, true)) |
| 190 | return false; |
| 191 | |
| 192 | Delete(from_path, true); |
| 193 | return true; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 194 | } |
| 195 | |
phajdan.jr@chromium.org | d86bea0 | 2009-05-20 02:21:07 +0900 | [diff] [blame] | 196 | bool ReplaceFile(const FilePath& from_path, const FilePath& to_path) { |
| 197 | return (rename(from_path.value().c_str(), to_path.value().c_str()) == 0); |
| 198 | } |
| 199 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 200 | bool CopyDirectory(const FilePath& from_path, |
| 201 | const FilePath& to_path, |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 202 | bool recursive) { |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 203 | // Some old callers of CopyDirectory want it to support wildcards. |
| 204 | // After some discussion, we decided to fix those callers. |
| 205 | // Break loudly here if anyone tries to do this. |
| 206 | // TODO(evanm): remove this once we're sure it's ok. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 207 | DCHECK(to_path.value().find('*') == std::string::npos); |
| 208 | DCHECK(from_path.value().find('*') == std::string::npos); |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 209 | |
| 210 | char top_dir[PATH_MAX]; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 211 | if (base::strlcpy(top_dir, from_path.value().c_str(), |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 212 | arraysize(top_dir)) >= arraysize(top_dir)) { |
| 213 | return false; |
| 214 | } |
| 215 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 216 | // This function does not properly handle destinations within the source |
| 217 | FilePath real_to_path = to_path; |
| 218 | if (PathExists(real_to_path)) { |
| 219 | if (!AbsolutePath(&real_to_path)) |
| 220 | return false; |
| 221 | } else { |
| 222 | real_to_path = real_to_path.DirName(); |
| 223 | if (!AbsolutePath(&real_to_path)) |
| 224 | return false; |
| 225 | } |
| 226 | FilePath real_from_path = from_path; |
| 227 | if (!AbsolutePath(&real_from_path)) |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 228 | return false; |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 229 | if (real_to_path.value().size() >= real_from_path.value().size() && |
| 230 | real_to_path.value().compare(0, real_from_path.value().size(), |
| 231 | real_from_path.value()) == 0) |
| 232 | return false; |
| 233 | |
| 234 | bool success = true; |
| 235 | FileEnumerator::FILE_TYPE traverse_type = |
| 236 | static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES | |
| 237 | FileEnumerator::SHOW_SYM_LINKS); |
| 238 | if (recursive) |
| 239 | traverse_type = static_cast<FileEnumerator::FILE_TYPE>( |
| 240 | traverse_type | FileEnumerator::DIRECTORIES); |
| 241 | FileEnumerator traversal(from_path, recursive, traverse_type); |
| 242 | |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 243 | // We have to mimic windows behavior here. |to_path| may not exist yet, |
vandebo@chromium.org | c0cf77e | 2009-10-15 10:11:44 +0900 | [diff] [blame] | 244 | // start the loop with |to_path|. |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 245 | FileEnumerator::FindInfo info; |
| 246 | FilePath current = from_path; |
| 247 | if (stat(from_path.value().c_str(), &info.stat) < 0) { |
| 248 | LOG(ERROR) << "CopyDirectory() couldn't stat source directory: " << |
| 249 | from_path.value() << " errno = " << errno; |
| 250 | success = false; |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 251 | } |
vandebo@chromium.org | c0cf77e | 2009-10-15 10:11:44 +0900 | [diff] [blame] | 252 | struct stat to_path_stat; |
| 253 | FilePath from_path_base = from_path; |
| 254 | if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 && |
| 255 | S_ISDIR(to_path_stat.st_mode)) { |
| 256 | // If the destination already exists and is a directory, then the |
| 257 | // top level of source needs to be copied. |
| 258 | from_path_base = from_path.DirName(); |
| 259 | } |
| 260 | |
| 261 | // The Windows version of this function assumes that non-recursive calls |
| 262 | // will always have a directory for from_path. |
| 263 | DCHECK(recursive || S_ISDIR(info.stat.st_mode)); |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 264 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 265 | while (success && !current.empty()) { |
| 266 | // current is the source path, including from_path, so paste |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 267 | // the suffix after from_path onto to_path to create the target_path. |
vandebo@chromium.org | 70cf3f1 | 2009-10-14 02:57:27 +0900 | [diff] [blame] | 268 | std::string suffix(¤t.value().c_str()[from_path_base.value().size()]); |
phajdan.jr@chromium.org | ecf5075 | 2009-01-14 03:57:46 +0900 | [diff] [blame] | 269 | // Strip the leading '/' (if any). |
| 270 | if (!suffix.empty()) { |
phajdan.jr@chromium.org | 8c9b451 | 2009-03-06 18:56:28 +0900 | [diff] [blame] | 271 | DCHECK_EQ('/', suffix[0]); |
phajdan.jr@chromium.org | ecf5075 | 2009-01-14 03:57:46 +0900 | [diff] [blame] | 272 | suffix.erase(0, 1); |
| 273 | } |
| 274 | const FilePath target_path = to_path.Append(suffix); |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 275 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 276 | if (S_ISDIR(info.stat.st_mode)) { |
| 277 | if (mkdir(target_path.value().c_str(), info.stat.st_mode & 01777) != 0 && |
| 278 | errno != EEXIST) { |
| 279 | LOG(ERROR) << "CopyDirectory() couldn't create directory: " << |
| 280 | target_path.value() << " errno = " << errno; |
| 281 | success = false; |
| 282 | } |
| 283 | } else if (S_ISREG(info.stat.st_mode)) { |
| 284 | if (!CopyFile(current, target_path)) { |
| 285 | LOG(ERROR) << "CopyDirectory() couldn't create file: " << |
| 286 | target_path.value(); |
| 287 | success = false; |
| 288 | } |
| 289 | } else { |
| 290 | LOG(WARNING) << "CopyDirectory() skipping non-regular file: " << |
| 291 | current.value(); |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 292 | } |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 293 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 294 | current = traversal.Next(); |
| 295 | traversal.GetFindInfo(&info); |
evanm@google.com | 5c1d39b | 2008-09-19 04:15:54 +0900 | [diff] [blame] | 296 | } |
| 297 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 298 | return success; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 299 | } |
| 300 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 301 | bool PathExists(const FilePath& path) { |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 302 | stat_wrapper_t file_info; |
| 303 | return CallStat(path.value().c_str(), &file_info) == 0; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 304 | } |
| 305 | |
erikkay@google.com | cce8382 | 2008-12-24 05:20:10 +0900 | [diff] [blame] | 306 | bool PathIsWritable(const FilePath& path) { |
| 307 | FilePath test_path(path); |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 308 | stat_wrapper_t file_info; |
| 309 | if (CallStat(test_path.value().c_str(), &file_info) != 0) { |
erikkay@google.com | cce8382 | 2008-12-24 05:20:10 +0900 | [diff] [blame] | 310 | // If the path doesn't exist, test the parent dir. |
| 311 | test_path = test_path.DirName(); |
| 312 | // If the parent dir doesn't exist, then return false (the path is not |
| 313 | // directly writable). |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 314 | if (CallStat(test_path.value().c_str(), &file_info) != 0) |
erikkay@google.com | cce8382 | 2008-12-24 05:20:10 +0900 | [diff] [blame] | 315 | return false; |
| 316 | } |
| 317 | if (S_IWOTH & file_info.st_mode) |
| 318 | return true; |
| 319 | if (getegid() == file_info.st_gid && (S_IWGRP & file_info.st_mode)) |
| 320 | return true; |
| 321 | if (geteuid() == file_info.st_uid && (S_IWUSR & file_info.st_mode)) |
| 322 | return true; |
| 323 | return false; |
| 324 | } |
| 325 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 326 | bool DirectoryExists(const FilePath& path) { |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 327 | stat_wrapper_t file_info; |
| 328 | if (CallStat(path.value().c_str(), &file_info) == 0) |
mmoss@google.com | 733df6b | 2008-09-12 01:09:11 +0900 | [diff] [blame] | 329 | return S_ISDIR(file_info.st_mode); |
| 330 | return false; |
| 331 | } |
| 332 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 333 | // TODO(erikkay): implement |
| 334 | #if 0 |
| 335 | bool GetFileCreationLocalTimeFromHandle(int fd, |
| 336 | LPSYSTEMTIME creation_time) { |
| 337 | if (!file_handle) |
| 338 | return false; |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 339 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 340 | FILETIME utc_filetime; |
| 341 | if (!GetFileTime(file_handle, &utc_filetime, NULL, NULL)) |
| 342 | return false; |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 343 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 344 | FILETIME local_filetime; |
| 345 | if (!FileTimeToLocalFileTime(&utc_filetime, &local_filetime)) |
| 346 | return false; |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 347 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 348 | return !!FileTimeToSystemTime(&local_filetime, creation_time); |
| 349 | } |
| 350 | |
| 351 | bool GetFileCreationLocalTime(const std::string& filename, |
| 352 | LPSYSTEMTIME creation_time) { |
| 353 | ScopedHandle file_handle( |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 354 | CreateFile(filename.c_str(), GENERIC_READ, |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 355 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 356 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); |
| 357 | return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); |
| 358 | } |
| 359 | #endif |
| 360 | |
phajdan.jr@chromium.org | 2372593 | 2009-04-23 21:38:08 +0900 | [diff] [blame] | 361 | bool ReadFromFD(int fd, char* buffer, size_t bytes) { |
| 362 | size_t total_read = 0; |
| 363 | while (total_read < bytes) { |
agl@chromium.org | d263ad7 | 2009-05-02 06:37:31 +0900 | [diff] [blame] | 364 | ssize_t bytes_read = |
| 365 | HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read)); |
| 366 | if (bytes_read <= 0) |
phajdan.jr@chromium.org | 2372593 | 2009-04-23 21:38:08 +0900 | [diff] [blame] | 367 | break; |
agl@chromium.org | d263ad7 | 2009-05-02 06:37:31 +0900 | [diff] [blame] | 368 | total_read += bytes_read; |
phajdan.jr@chromium.org | 2372593 | 2009-04-23 21:38:08 +0900 | [diff] [blame] | 369 | } |
| 370 | return total_read == bytes; |
| 371 | } |
| 372 | |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 373 | // Creates and opens a temporary file in |directory|, returning the |
erikkay@chromium.org | 3a9a642 | 2009-09-12 02:33:50 +0900 | [diff] [blame] | 374 | // file descriptor. |path| is set to the temporary file path. |
| 375 | // This function does NOT unlink() the file. |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 376 | int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { |
| 377 | *path = directory.Append(kTempFileName); |
| 378 | const std::string& tmpdir_string = path->value(); |
mark@chromium.org | 8ca0d27 | 2008-09-12 02:36:23 +0900 | [diff] [blame] | 379 | // this should be OK since mkstemp just replaces characters in place |
| 380 | char* buffer = const_cast<char*>(tmpdir_string.c_str()); |
estade@chromium.org | f474a1b | 2008-11-11 09:01:38 +0900 | [diff] [blame] | 381 | |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 382 | return mkstemp(buffer); |
| 383 | } |
| 384 | |
erikkay@chromium.org | 18f0dde | 2009-08-19 01:07:55 +0900 | [diff] [blame] | 385 | bool CreateTemporaryFile(FilePath* path) { |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 386 | FilePath directory; |
| 387 | if (!GetTempDir(&directory)) |
| 388 | return false; |
| 389 | int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 390 | if (fd < 0) |
| 391 | return false; |
estade@chromium.org | f474a1b | 2008-11-11 09:01:38 +0900 | [diff] [blame] | 392 | close(fd); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 393 | return true; |
| 394 | } |
| 395 | |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 396 | FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { |
| 397 | FilePath directory; |
| 398 | if (!GetShmemTempDir(&directory)) |
| 399 | return false; |
| 400 | |
phajdan.jr@chromium.org | 8139fe1 | 2009-04-28 15:50:36 +0900 | [diff] [blame] | 401 | return CreateAndOpenTemporaryFileInDir(directory, path); |
| 402 | } |
| 403 | |
| 404 | FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
| 405 | int fd = CreateAndOpenFdForTemporaryFile(dir, path); |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 406 | if (fd < 0) |
| 407 | return NULL; |
| 408 | |
phajdan.jr@chromium.org | 8139fe1 | 2009-04-28 15:50:36 +0900 | [diff] [blame] | 409 | return fdopen(fd, "a+"); |
jrg@chromium.org | d505c3a | 2009-02-04 09:58:39 +0900 | [diff] [blame] | 410 | } |
dumi@chromium.org | 13e715d | 2009-09-12 05:06:27 +0900 | [diff] [blame] | 411 | |
| 412 | bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
| 413 | int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); |
| 414 | return ((fd >= 0) && !close(fd)); |
jcampan@chromium.org | bf29e60 | 2008-10-11 03:50:32 +0900 | [diff] [blame] | 415 | } |
| 416 | |
erikkay@google.com | cce8382 | 2008-12-24 05:20:10 +0900 | [diff] [blame] | 417 | bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 418 | FilePath* new_temp_path) { |
estade@chromium.org | f474a1b | 2008-11-11 09:01:38 +0900 | [diff] [blame] | 419 | FilePath tmpdir; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 420 | if (!GetTempDir(&tmpdir)) |
| 421 | return false; |
estade@chromium.org | f474a1b | 2008-11-11 09:01:38 +0900 | [diff] [blame] | 422 | tmpdir = tmpdir.Append(kTempFileName); |
| 423 | std::string tmpdir_string = tmpdir.value(); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 424 | // this should be OK since mkdtemp just replaces characters in place |
mark@chromium.org | 8ca0d27 | 2008-09-12 02:36:23 +0900 | [diff] [blame] | 425 | char* buffer = const_cast<char*>(tmpdir_string.c_str()); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 426 | char* dtemp = mkdtemp(buffer); |
| 427 | if (!dtemp) |
| 428 | return false; |
erikkay@google.com | cce8382 | 2008-12-24 05:20:10 +0900 | [diff] [blame] | 429 | *new_temp_path = FilePath(dtemp); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 430 | return true; |
| 431 | } |
| 432 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 433 | bool CreateDirectory(const FilePath& full_path) { |
| 434 | std::vector<FilePath> subpaths; |
| 435 | |
| 436 | // Collect a list of all parent directories. |
| 437 | FilePath last_path = full_path; |
| 438 | subpaths.push_back(full_path); |
| 439 | for (FilePath path = full_path.DirName(); |
| 440 | path.value() != last_path.value(); path = path.DirName()) { |
| 441 | subpaths.push_back(path); |
| 442 | last_path = path; |
| 443 | } |
| 444 | |
| 445 | // Iterate through the parents and create the missing ones. |
| 446 | for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); |
| 447 | i != subpaths.rend(); ++i) { |
| 448 | if (!DirectoryExists(*i)) { |
evan@chromium.org | dff7b7f | 2009-05-13 05:12:24 +0900 | [diff] [blame] | 449 | if (mkdir(i->value().c_str(), 0700) != 0) |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 450 | return false; |
| 451 | } |
| 452 | } |
| 453 | return true; |
| 454 | } |
| 455 | |
dkegel@google.com | 4498268 | 2008-11-05 06:00:46 +0900 | [diff] [blame] | 456 | bool GetFileInfo(const FilePath& file_path, FileInfo* results) { |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 457 | stat_wrapper_t file_info; |
| 458 | if (CallStat(file_path.value().c_str(), &file_info) != 0) |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 459 | return false; |
darin@google.com | 7f479f2 | 2008-09-26 10:04:08 +0900 | [diff] [blame] | 460 | results->is_directory = S_ISDIR(file_info.st_mode); |
| 461 | results->size = file_info.st_size; |
brettw@chromium.org | dbc9b5a | 2009-07-25 01:13:53 +0900 | [diff] [blame] | 462 | results->last_modified = base::Time::FromTimeT(file_info.st_mtime); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 463 | return true; |
| 464 | } |
| 465 | |
phajdan.jr@chromium.org | 99aec93 | 2009-05-15 02:49:23 +0900 | [diff] [blame] | 466 | bool GetInode(const FilePath& path, ino_t* inode) { |
| 467 | struct stat buffer; |
| 468 | int result = stat(path.value().c_str(), &buffer); |
| 469 | if (result < 0) |
| 470 | return false; |
| 471 | |
| 472 | *inode = buffer.st_ino; |
| 473 | return true; |
| 474 | } |
| 475 | |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 476 | FILE* OpenFile(const std::string& filename, const char* mode) { |
estade@chromium.org | b1d358a | 2008-11-18 06:01:19 +0900 | [diff] [blame] | 477 | return OpenFile(FilePath(filename), mode); |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 478 | } |
| 479 | |
estade@chromium.org | b1d358a | 2008-11-18 06:01:19 +0900 | [diff] [blame] | 480 | FILE* OpenFile(const FilePath& filename, const char* mode) { |
| 481 | return fopen(filename.value().c_str(), mode); |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 482 | } |
| 483 | |
estade@chromium.org | 9d32ed8 | 2009-01-28 14:47:15 +0900 | [diff] [blame] | 484 | int ReadFile(const FilePath& filename, char* data, int size) { |
| 485 | int fd = open(filename.value().c_str(), O_RDONLY); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 486 | if (fd < 0) |
| 487 | return -1; |
estade@chromium.org | b1d358a | 2008-11-18 06:01:19 +0900 | [diff] [blame] | 488 | |
agl@chromium.org | d263ad7 | 2009-05-02 06:37:31 +0900 | [diff] [blame] | 489 | int ret_value = HANDLE_EINTR(read(fd, data, size)); |
| 490 | HANDLE_EINTR(close(fd)); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 491 | return ret_value; |
| 492 | } |
| 493 | |
estade@chromium.org | 9d32ed8 | 2009-01-28 14:47:15 +0900 | [diff] [blame] | 494 | int WriteFile(const FilePath& filename, const char* data, int size) { |
| 495 | int fd = creat(filename.value().c_str(), 0666); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 496 | if (fd < 0) |
| 497 | return -1; |
mark@chromium.org | 8ca0d27 | 2008-09-12 02:36:23 +0900 | [diff] [blame] | 498 | |
estade@chromium.org | 557da51 | 2009-09-16 09:29:22 +0900 | [diff] [blame] | 499 | int rv = WriteFileDescriptor(fd, data, size); |
agl@chromium.org | d263ad7 | 2009-05-02 06:37:31 +0900 | [diff] [blame] | 500 | HANDLE_EINTR(close(fd)); |
estade@chromium.org | 557da51 | 2009-09-16 09:29:22 +0900 | [diff] [blame] | 501 | return rv; |
| 502 | } |
| 503 | |
| 504 | int WriteFileDescriptor(const int fd, const char* data, int size) { |
| 505 | // Allow for partial writes. |
| 506 | ssize_t bytes_written_total = 0; |
| 507 | for (ssize_t bytes_written_partial = 0; bytes_written_total < size; |
| 508 | bytes_written_total += bytes_written_partial) { |
| 509 | bytes_written_partial = |
| 510 | HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 511 | size - bytes_written_total)); |
| 512 | if (bytes_written_partial < 0) |
| 513 | return -1; |
| 514 | } |
| 515 | |
mark@chromium.org | 8ca0d27 | 2008-09-12 02:36:23 +0900 | [diff] [blame] | 516 | return bytes_written_total; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // Gets the current working directory for the process. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 520 | bool GetCurrentDirectory(FilePath* dir) { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 521 | char system_buffer[PATH_MAX] = ""; |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 522 | if (!getcwd(system_buffer, sizeof(system_buffer))) { |
| 523 | NOTREACHED(); |
| 524 | return false; |
| 525 | } |
| 526 | *dir = FilePath(system_buffer); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 527 | return true; |
| 528 | } |
| 529 | |
| 530 | // Sets the current working directory for the process. |
estade@chromium.org | b1d358a | 2008-11-18 06:01:19 +0900 | [diff] [blame] | 531 | bool SetCurrentDirectory(const FilePath& path) { |
| 532 | int ret = chdir(path.value().c_str()); |
| 533 | return !ret; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 534 | } |
estade@chromium.org | b1d358a | 2008-11-18 06:01:19 +0900 | [diff] [blame] | 535 | |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 536 | /////////////////////////////////////////////// |
| 537 | // FileEnumerator |
| 538 | |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 539 | FileEnumerator::FileEnumerator(const FilePath& root_path, |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 540 | bool recursive, |
| 541 | FileEnumerator::FILE_TYPE file_type) |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 542 | : root_path_(root_path), |
| 543 | recursive_(recursive), |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 544 | file_type_(file_type), |
| 545 | is_in_find_op_(false), |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 546 | current_directory_entry_(0) { |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 547 | // INCLUDE_DOT_DOT must not be specified if recursive. |
| 548 | DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 549 | pending_paths_.push(root_path); |
| 550 | } |
| 551 | |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 552 | FileEnumerator::FileEnumerator(const FilePath& root_path, |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 553 | bool recursive, |
| 554 | FileEnumerator::FILE_TYPE file_type, |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 555 | const FilePath::StringType& pattern) |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 556 | : root_path_(root_path), |
| 557 | recursive_(recursive), |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 558 | file_type_(file_type), |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 559 | pattern_(root_path.Append(pattern)), |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 560 | is_in_find_op_(false), |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 561 | current_directory_entry_(0) { |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 562 | // INCLUDE_DOT_DOT must not be specified if recursive. |
| 563 | DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 564 | // The Windows version of this code appends the pattern to the root_path, |
| 565 | // potentially only matching against items in the top-most directory. |
| 566 | // Do the same here. |
| 567 | if (pattern.size() == 0) |
| 568 | pattern_ = FilePath(); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 569 | pending_paths_.push(root_path); |
| 570 | } |
estade@chromium.org | b1d358a | 2008-11-18 06:01:19 +0900 | [diff] [blame] | 571 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 572 | FileEnumerator::~FileEnumerator() { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 573 | } |
| 574 | |
estade@chromium.org | b23e8e6 | 2008-11-21 07:30:13 +0900 | [diff] [blame] | 575 | void FileEnumerator::GetFindInfo(FindInfo* info) { |
| 576 | DCHECK(info); |
| 577 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 578 | if (current_directory_entry_ >= directory_entries_.size()) |
estade@chromium.org | b23e8e6 | 2008-11-21 07:30:13 +0900 | [diff] [blame] | 579 | return; |
| 580 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 581 | DirectoryEntryInfo* cur_entry = &directory_entries_[current_directory_entry_]; |
| 582 | memcpy(&(info->stat), &(cur_entry->stat), sizeof(info->stat)); |
| 583 | info->filename.assign(cur_entry->filename.value()); |
estade@chromium.org | b23e8e6 | 2008-11-21 07:30:13 +0900 | [diff] [blame] | 584 | } |
| 585 | |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 586 | FilePath FileEnumerator::Next() { |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 587 | ++current_directory_entry_; |
| 588 | |
| 589 | // While we've exhausted the entries in the current directory, do the next |
| 590 | while (current_directory_entry_ >= directory_entries_.size()) { |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 591 | if (pending_paths_.empty()) |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 592 | return FilePath(); |
estade@chromium.org | b23e8e6 | 2008-11-21 07:30:13 +0900 | [diff] [blame] | 593 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 594 | root_path_ = pending_paths_.top(); |
avi@google.com | 5cb7935 | 2008-12-11 23:55:12 +0900 | [diff] [blame] | 595 | root_path_ = root_path_.StripTrailingSeparators(); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 596 | pending_paths_.pop(); |
estade@chromium.org | b23e8e6 | 2008-11-21 07:30:13 +0900 | [diff] [blame] | 597 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 598 | std::vector<DirectoryEntryInfo> entries; |
| 599 | if (!ReadDirectory(&entries, root_path_, file_type_ & SHOW_SYM_LINKS)) |
| 600 | continue; |
estade@chromium.org | b23e8e6 | 2008-11-21 07:30:13 +0900 | [diff] [blame] | 601 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 602 | directory_entries_.clear(); |
| 603 | current_directory_entry_ = 0; |
| 604 | for (std::vector<DirectoryEntryInfo>::const_iterator |
| 605 | i = entries.begin(); i != entries.end(); ++i) { |
| 606 | FilePath full_path = root_path_.Append(i->filename); |
| 607 | if (ShouldSkip(full_path)) |
| 608 | continue; |
estade@chromium.org | b23e8e6 | 2008-11-21 07:30:13 +0900 | [diff] [blame] | 609 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 610 | if (pattern_.value().size() && |
| 611 | fnmatch(pattern_.value().c_str(), full_path.value().c_str(), |
| 612 | FNM_NOESCAPE)) |
| 613 | continue; |
| 614 | |
| 615 | if (recursive_ && S_ISDIR(i->stat.st_mode)) |
| 616 | pending_paths_.push(full_path); |
| 617 | |
| 618 | if ((S_ISDIR(i->stat.st_mode) && (file_type_ & DIRECTORIES)) || |
| 619 | (!S_ISDIR(i->stat.st_mode) && (file_type_ & FILES))) |
| 620 | directory_entries_.push_back(*i); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 621 | } |
| 622 | } |
estade@chromium.org | b23e8e6 | 2008-11-21 07:30:13 +0900 | [diff] [blame] | 623 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 624 | return root_path_.Append(directory_entries_[current_directory_entry_ |
| 625 | ].filename); |
| 626 | } |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 627 | |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 628 | bool FileEnumerator::ReadDirectory(std::vector<DirectoryEntryInfo>* entries, |
| 629 | const FilePath& source, bool show_links) { |
| 630 | DIR* dir = opendir(source.value().c_str()); |
| 631 | if (!dir) |
| 632 | return false; |
| 633 | |
benl@chromium.org | 6b6b216 | 2009-09-08 01:39:46 +0900 | [diff] [blame] | 634 | #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 635 | #error Depending on the definition of struct dirent, additional space for \ |
| 636 | pathname may be needed |
| 637 | #endif |
| 638 | struct dirent dent_buf; |
| 639 | struct dirent* dent; |
| 640 | while (readdir_r(dir, &dent_buf, &dent) == 0 && dent) { |
| 641 | DirectoryEntryInfo info; |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 642 | info.filename = FilePath(dent->d_name); |
evan@chromium.org | 1c79837 | 2009-09-04 07:39:34 +0900 | [diff] [blame] | 643 | |
| 644 | FilePath full_name = source.Append(dent->d_name); |
| 645 | int ret; |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 646 | if (show_links) |
evan@chromium.org | 1c79837 | 2009-09-04 07:39:34 +0900 | [diff] [blame] | 647 | ret = lstat(full_name.value().c_str(), &info.stat); |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 648 | else |
evan@chromium.org | 1c79837 | 2009-09-04 07:39:34 +0900 | [diff] [blame] | 649 | ret = stat(full_name.value().c_str(), &info.stat); |
| 650 | if (ret < 0) { |
| 651 | // Print the stat() error message unless it was ENOENT and we're |
| 652 | // following symlinks. |
| 653 | if (!(ret == ENOENT && !show_links)) { |
tschmelcher@chromium.org | 90a3f8a | 2009-10-14 03:27:40 +0900 | [diff] [blame] | 654 | PLOG(ERROR) << "Couldn't stat " |
| 655 | << source.Append(dent->d_name).value(); |
evan@chromium.org | 1c79837 | 2009-09-04 07:39:34 +0900 | [diff] [blame] | 656 | } |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 657 | memset(&info.stat, 0, sizeof(info.stat)); |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 658 | } |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 659 | entries->push_back(info); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 660 | } |
thestig@chromium.org | 3217c82 | 2009-08-07 06:23:07 +0900 | [diff] [blame] | 661 | |
| 662 | closedir(dir); |
| 663 | return true; |
| 664 | } |
| 665 | |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 666 | /////////////////////////////////////////////// |
| 667 | // MemoryMappedFile |
| 668 | |
| 669 | MemoryMappedFile::MemoryMappedFile() |
estade@chromium.org | 2131ee2 | 2009-11-06 12:05:46 +0900 | [diff] [blame] | 670 | : data_(NULL), |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 671 | length_(0) { |
| 672 | } |
| 673 | |
estade@chromium.org | 2131ee2 | 2009-11-06 12:05:46 +0900 | [diff] [blame] | 674 | bool MemoryMappedFile::Initialize(const base::FileDescriptor& fd) { |
| 675 | if (IsValid()) |
| 676 | return false; |
agl@chromium.org | 28a12e7 | 2009-06-13 02:36:55 +0900 | [diff] [blame] | 677 | |
estade@chromium.org | 2131ee2 | 2009-11-06 12:05:46 +0900 | [diff] [blame] | 678 | file_ = fd; |
| 679 | |
| 680 | if (!MapFileToMemoryInternal()) { |
| 681 | CloseHandles(); |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { |
| 689 | file_ = base::FileDescriptor(open(file_name.value().c_str(), O_RDONLY), true); |
| 690 | |
| 691 | if (file_.fd == -1) { |
dkegel@google.com | 44a1720 | 2009-06-07 04:59:36 +0900 | [diff] [blame] | 692 | LOG(ERROR) << "Couldn't open " << file_name.value(); |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 693 | return false; |
dkegel@google.com | 44a1720 | 2009-06-07 04:59:36 +0900 | [diff] [blame] | 694 | } |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 695 | |
estade@chromium.org | 2131ee2 | 2009-11-06 12:05:46 +0900 | [diff] [blame] | 696 | return MapFileToMemoryInternal(); |
| 697 | } |
| 698 | |
| 699 | bool MemoryMappedFile::MapFileToMemoryInternal() { |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 700 | struct stat file_stat; |
estade@chromium.org | 2131ee2 | 2009-11-06 12:05:46 +0900 | [diff] [blame] | 701 | if (fstat(file_.fd, &file_stat) == -1) { |
| 702 | LOG(ERROR) << "Couldn't fstat " << file_.fd << ", errno " << errno; |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 703 | return false; |
dkegel@google.com | 44a1720 | 2009-06-07 04:59:36 +0900 | [diff] [blame] | 704 | } |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 705 | length_ = file_stat.st_size; |
| 706 | |
| 707 | data_ = static_cast<uint8*>( |
estade@chromium.org | 2131ee2 | 2009-11-06 12:05:46 +0900 | [diff] [blame] | 708 | mmap(NULL, length_, PROT_READ, MAP_SHARED, file_.fd, 0)); |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 709 | if (data_ == MAP_FAILED) |
estade@chromium.org | 2131ee2 | 2009-11-06 12:05:46 +0900 | [diff] [blame] | 710 | LOG(ERROR) << "Couldn't mmap " << file_.fd << ", errno " << errno; |
dkegel@google.com | 44a1720 | 2009-06-07 04:59:36 +0900 | [diff] [blame] | 711 | |
| 712 | return data_ != MAP_FAILED; |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void MemoryMappedFile::CloseHandles() { |
| 716 | if (data_ != NULL) |
| 717 | munmap(data_, length_); |
estade@chromium.org | 2131ee2 | 2009-11-06 12:05:46 +0900 | [diff] [blame] | 718 | if (file_.auto_close && file_.fd != -1) |
| 719 | close(file_.fd); |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 720 | |
| 721 | data_ = NULL; |
| 722 | length_ = 0; |
estade@chromium.org | 2131ee2 | 2009-11-06 12:05:46 +0900 | [diff] [blame] | 723 | file_ = base::FileDescriptor(); |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 724 | } |
estade@chromium.org | b23e8e6 | 2008-11-21 07:30:13 +0900 | [diff] [blame] | 725 | |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 726 | } // namespace file_util |