[email protected] | b2e9729 | 2008-09-02 18:20:34 | [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 | |
[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> |
| 11 | #include <fts.h> |
| 12 | #include <libgen.h> |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 13 | #include <stdio.h> |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 14 | #include <string.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 15 | #include <sys/errno.h> |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 16 | #include <sys/mman.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 17 | #include <sys/stat.h> |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 18 | #include <sys/types.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 19 | #include <time.h> |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 20 | #include <unistd.h> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 21 | |
| 22 | #include <fstream> |
| 23 | |
| 24 | #include "base/basictypes.h" |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 25 | #include "base/eintr_wrapper.h" |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 26 | #include "base/file_path.h" |
[email protected] | 807bc04 | 2009-07-15 01:32:02 | [diff] [blame^] | 27 | #include "base/lock.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 28 | #include "base/logging.h" |
[email protected] | 807bc04 | 2009-07-15 01:32:02 | [diff] [blame^] | 29 | #include "base/scoped_ptr.h" |
| 30 | #include "base/singleton.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 31 | #include "base/string_util.h" |
[email protected] | 807bc04 | 2009-07-15 01:32:02 | [diff] [blame^] | 32 | #include "base/sys_string_conversions.h" |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 33 | #include "base/time.h" |
[email protected] | 807bc04 | 2009-07-15 01:32:02 | [diff] [blame^] | 34 | #include "unicode/coll.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 35 | |
[email protected] | 172c550 | 2009-06-24 03:29:26 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | bool IsDirectory(const FTSENT* file) { |
| 39 | switch (file->fts_info) { |
| 40 | case FTS_D: |
| 41 | case FTS_DC: |
| 42 | case FTS_DNR: |
| 43 | case FTS_DOT: |
| 44 | case FTS_DP: |
| 45 | return true; |
| 46 | default: |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
[email protected] | 807bc04 | 2009-07-15 01:32:02 | [diff] [blame^] | 51 | class LocaleAwareComparator { |
| 52 | public: |
| 53 | LocaleAwareComparator() { |
| 54 | UErrorCode error_code = U_ZERO_ERROR; |
| 55 | // Use the default collator. The default locale should have been properly |
| 56 | // set by the time this constructor is called. |
| 57 | collator_.reset(Collator::createInstance(error_code)); |
| 58 | DCHECK(U_SUCCESS(error_code)); |
| 59 | // Make it case-sensitive. |
| 60 | collator_->setStrength(Collator::TERTIARY); |
| 61 | // Note: We do not set UCOL_NORMALIZATION_MODE attribute. In other words, we |
| 62 | // do not pay performance penalty to guarantee sort order correctness for |
| 63 | // non-FCD (http://unicode.org/notes/tn5/#FCD) file names. This should be a |
| 64 | // reasonable tradeoff because such file names should be rare and the sort |
| 65 | // order doesn't change much anyway. |
| 66 | } |
| 67 | |
| 68 | // Note: A similar function is available in l10n_util. |
| 69 | // We cannot use it because base should not depend on l10n_util. |
| 70 | // TODO(yuzo): Move some of l10n_util to base. |
| 71 | int Compare(const string16& a, const string16& b) { |
| 72 | // We are not sure if Collator::compare is thread-safe. |
| 73 | // Use an AutoLock just in case. |
| 74 | AutoLock auto_lock(lock_); |
| 75 | |
| 76 | UErrorCode error_code = U_ZERO_ERROR; |
| 77 | UCollationResult result = collator_->compare( |
| 78 | static_cast<const UChar*>(a.c_str()), |
| 79 | static_cast<int>(a.length()), |
| 80 | static_cast<const UChar*>(b.c_str()), |
| 81 | static_cast<int>(b.length()), |
| 82 | error_code); |
| 83 | DCHECK(U_SUCCESS(error_code)); |
| 84 | return result; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | scoped_ptr<Collator> collator_; |
| 89 | Lock lock_; |
| 90 | friend struct DefaultSingletonTraits<LocaleAwareComparator>; |
| 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(LocaleAwareComparator); |
| 93 | }; |
| 94 | |
| 95 | int CompareFiles(const FTSENT** a, const FTSENT** b) { |
| 96 | // Order lexicographically with directories before other files. |
| 97 | const bool a_is_dir = IsDirectory(*a); |
| 98 | const bool b_is_dir = IsDirectory(*b); |
| 99 | if (a_is_dir != b_is_dir) |
| 100 | return a_is_dir ? -1 : 1; |
| 101 | |
| 102 | // On linux, the file system encoding is not defined. We assume |
| 103 | // SysNativeMBToWide takes care of it. |
| 104 | // |
| 105 | // ICU's collator can take strings in OS native encoding. But we convert the |
| 106 | // strings to UTF-16 ourselves to ensure conversion consistency. |
| 107 | // TODO(yuzo): Perhaps we should define SysNativeMBToUTF16? |
| 108 | return Singleton<LocaleAwareComparator>()->Compare( |
| 109 | WideToUTF16(base::SysNativeMBToWide((*a)->fts_name)), |
| 110 | WideToUTF16(base::SysNativeMBToWide((*b)->fts_name))); |
| 111 | } |
| 112 | |
[email protected] | 172c550 | 2009-06-24 03:29:26 | [diff] [blame] | 113 | } // namespace |
| 114 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 115 | namespace file_util { |
| 116 | |
[email protected] | 22a087f | 2009-03-17 19:17:43 | [diff] [blame] | 117 | #if defined(GOOGLE_CHROME_BUILD) |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 118 | static const char* kTempFileName = "com.google.chrome.XXXXXX"; |
[email protected] | 22a087f | 2009-03-17 19:17:43 | [diff] [blame] | 119 | #else |
| 120 | static const char* kTempFileName = "org.chromium.XXXXXX"; |
| 121 | #endif |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 122 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 123 | std::wstring GetDirectoryFromPath(const std::wstring& path) { |
| 124 | if (EndsWithSeparator(path)) { |
[email protected] | a97f2b4 | 2009-04-21 23:15:45 | [diff] [blame] | 125 | std::wstring dir = path; |
| 126 | TrimTrailingSeparator(&dir); |
| 127 | return dir; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 128 | } else { |
| 129 | char full_path[PATH_MAX]; |
| 130 | base::strlcpy(full_path, WideToUTF8(path).c_str(), arraysize(full_path)); |
| 131 | return UTF8ToWide(dirname(full_path)); |
| 132 | } |
| 133 | } |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 134 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 135 | bool AbsolutePath(FilePath* path) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 136 | char full_path[PATH_MAX]; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 137 | if (realpath(path->value().c_str(), full_path) == NULL) |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 138 | return false; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 139 | *path = FilePath(full_path); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 140 | return true; |
| 141 | } |
| 142 | |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 143 | int CountFilesCreatedAfter(const FilePath& path, |
| 144 | const base::Time& comparison_time) { |
| 145 | int file_count = 0; |
| 146 | |
| 147 | DIR* dir = opendir(path.value().c_str()); |
| 148 | if (dir) { |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 149 | struct dirent ent_buf; |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 150 | struct dirent* ent; |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 151 | while (readdir_r(dir, &ent_buf, &ent) == 0 && ent) { |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 152 | if ((strcmp(ent->d_name, ".") == 0) || |
| 153 | (strcmp(ent->d_name, "..") == 0)) |
| 154 | continue; |
| 155 | |
| 156 | struct stat64 st; |
| 157 | int test = stat64(path.Append(ent->d_name).value().c_str(), &st); |
| 158 | if (test != 0) { |
| 159 | LOG(ERROR) << "stat64 failed: " << strerror(errno); |
| 160 | continue; |
| 161 | } |
[email protected] | 2126577b | 2009-04-23 15:05:19 | [diff] [blame] | 162 | // Here, we use Time::TimeT(), which discards microseconds. This |
| 163 | // means that files which are newer than |comparison_time| may |
| 164 | // be considered older. If we don't discard microseconds, it |
| 165 | // introduces another issue. Suppose the following case: |
| 166 | // |
| 167 | // 1. Get |comparison_time| by Time::Now() and the value is 10.1 (secs). |
| 168 | // 2. Create a file and the current time is 10.3 (secs). |
| 169 | // |
| 170 | // As POSIX doesn't have microsecond precision for |st_ctime|, |
| 171 | // the creation time of the file created in the step 2 is 10 and |
| 172 | // the file is considered older than |comparison_time|. After |
| 173 | // all, we may have to accept either of the two issues: 1. files |
| 174 | // which are older than |comparison_time| are considered newer |
| 175 | // (current implementation) 2. files newer than |
| 176 | // |comparison_time| are considered older. |
[email protected] | 4b7743de | 2009-04-21 01:50:39 | [diff] [blame] | 177 | if (st.st_ctime >= comparison_time.ToTimeT()) |
| 178 | ++file_count; |
| 179 | } |
| 180 | closedir(dir); |
| 181 | } |
| 182 | return file_count; |
| 183 | } |
| 184 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 185 | // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" |
| 186 | // which works both with and without the recursive flag. I'm not sure we need |
| 187 | // that functionality. If not, remove from file_util_win.cc, otherwise add it |
| 188 | // here. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 189 | bool Delete(const FilePath& path, bool recursive) { |
| 190 | const char* path_str = path.value().c_str(); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 191 | struct stat64 file_info; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 192 | int test = stat64(path_str, &file_info); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 193 | if (test != 0) { |
| 194 | // The Windows version defines this condition as success. |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 195 | bool ret = (errno == ENOENT || errno == ENOTDIR); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 196 | return ret; |
| 197 | } |
| 198 | if (!S_ISDIR(file_info.st_mode)) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 199 | return (unlink(path_str) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 200 | if (!recursive) |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 201 | return (rmdir(path_str) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 202 | |
| 203 | bool success = true; |
| 204 | int ftsflags = FTS_PHYSICAL | FTS_NOSTAT; |
| 205 | char top_dir[PATH_MAX]; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 206 | if (base::strlcpy(top_dir, path_str, |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 207 | arraysize(top_dir)) >= arraysize(top_dir)) { |
| 208 | return false; |
| 209 | } |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 210 | char* dir_list[2] = { top_dir, NULL }; |
| 211 | FTS* fts = fts_open(dir_list, ftsflags, NULL); |
| 212 | if (fts) { |
| 213 | FTSENT* fts_ent = fts_read(fts); |
| 214 | while (success && fts_ent != NULL) { |
| 215 | switch (fts_ent->fts_info) { |
| 216 | case FTS_DNR: |
| 217 | case FTS_ERR: |
| 218 | // log error |
| 219 | success = false; |
| 220 | continue; |
| 221 | break; |
| 222 | case FTS_DP: |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 223 | success = (rmdir(fts_ent->fts_accpath) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 224 | break; |
| 225 | case FTS_D: |
| 226 | break; |
| 227 | case FTS_NSOK: |
| 228 | case FTS_F: |
| 229 | case FTS_SL: |
| 230 | case FTS_SLNONE: |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 231 | success = (unlink(fts_ent->fts_accpath) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 232 | break; |
| 233 | default: |
| 234 | DCHECK(false); |
| 235 | break; |
| 236 | } |
| 237 | fts_ent = fts_read(fts); |
| 238 | } |
| 239 | fts_close(fts); |
| 240 | } |
| 241 | return success; |
| 242 | } |
| 243 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 244 | bool Move(const FilePath& from_path, const FilePath& to_path) { |
[email protected] | cc7948a | 2009-03-13 20:01:43 | [diff] [blame] | 245 | if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) |
| 246 | return true; |
| 247 | |
| 248 | if (!CopyDirectory(from_path, to_path, true)) |
| 249 | return false; |
| 250 | |
| 251 | Delete(from_path, true); |
| 252 | return true; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 253 | } |
| 254 | |
[email protected] | c5866dca | 2009-05-19 17:21:07 | [diff] [blame] | 255 | bool ReplaceFile(const FilePath& from_path, const FilePath& to_path) { |
| 256 | return (rename(from_path.value().c_str(), to_path.value().c_str()) == 0); |
| 257 | } |
| 258 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 259 | bool CopyDirectory(const FilePath& from_path, |
| 260 | const FilePath& to_path, |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 261 | bool recursive) { |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 262 | // Some old callers of CopyDirectory want it to support wildcards. |
| 263 | // After some discussion, we decided to fix those callers. |
| 264 | // Break loudly here if anyone tries to do this. |
| 265 | // TODO(evanm): remove this once we're sure it's ok. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 266 | DCHECK(to_path.value().find('*') == std::string::npos); |
| 267 | DCHECK(from_path.value().find('*') == std::string::npos); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 268 | |
| 269 | char top_dir[PATH_MAX]; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 270 | if (base::strlcpy(top_dir, from_path.value().c_str(), |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 271 | arraysize(top_dir)) >= arraysize(top_dir)) { |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | char* dir_list[] = { top_dir, NULL }; |
| 276 | FTS* fts = fts_open(dir_list, FTS_PHYSICAL | FTS_NOSTAT, NULL); |
| 277 | if (!fts) { |
| 278 | LOG(ERROR) << "fts_open failed: " << strerror(errno); |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | int error = 0; |
| 283 | FTSENT* ent; |
| 284 | while (!error && (ent = fts_read(fts)) != NULL) { |
| 285 | // ent->fts_path is the source path, including from_path, so paste |
| 286 | // the suffix after from_path onto to_path to create the target_path. |
[email protected] | ca020961 | 2009-01-13 18:57:46 | [diff] [blame] | 287 | std::string suffix(&ent->fts_path[from_path.value().size()]); |
| 288 | // Strip the leading '/' (if any). |
| 289 | if (!suffix.empty()) { |
[email protected] | 6ae340f | 2009-03-06 09:56:28 | [diff] [blame] | 290 | DCHECK_EQ('/', suffix[0]); |
[email protected] | ca020961 | 2009-01-13 18:57:46 | [diff] [blame] | 291 | suffix.erase(0, 1); |
| 292 | } |
| 293 | const FilePath target_path = to_path.Append(suffix); |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 294 | switch (ent->fts_info) { |
| 295 | case FTS_D: // Preorder directory. |
| 296 | // If we encounter a subdirectory in a non-recursive copy, prune it |
| 297 | // from the traversal. |
| 298 | if (!recursive && ent->fts_level > 0) { |
| 299 | if (fts_set(fts, ent, FTS_SKIP) != 0) |
| 300 | error = errno; |
| 301 | continue; |
| 302 | } |
| 303 | |
| 304 | // Try creating the target dir, continuing on it if it exists already. |
[email protected] | 39a1a06 | 2009-05-12 20:12:24 | [diff] [blame] | 305 | if (mkdir(target_path.value().c_str(), 0700) != 0) { |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 306 | if (errno != EEXIST) |
| 307 | error = errno; |
| 308 | } |
| 309 | break; |
| 310 | case FTS_F: // Regular file. |
| 311 | case FTS_NSOK: // File, no stat info requested. |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 312 | errno = 0; |
[email protected] | ca020961 | 2009-01-13 18:57:46 | [diff] [blame] | 313 | if (!CopyFile(FilePath(ent->fts_path), target_path)) |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 314 | error = errno ? errno : EINVAL; |
| 315 | break; |
| 316 | case FTS_DP: // Postorder directory. |
| 317 | case FTS_DOT: // "." or ".." |
| 318 | // Skip it. |
| 319 | continue; |
| 320 | case FTS_DC: // Directory causing a cycle. |
| 321 | // Skip this branch. |
| 322 | if (fts_set(fts, ent, FTS_SKIP) != 0) |
| 323 | error = errno; |
| 324 | break; |
| 325 | case FTS_DNR: // Directory cannot be read. |
| 326 | case FTS_ERR: // Error. |
| 327 | case FTS_NS: // Stat failed. |
| 328 | // Abort with the error. |
| 329 | error = ent->fts_errno; |
| 330 | break; |
| 331 | case FTS_SL: // Symlink. |
| 332 | case FTS_SLNONE: // Symlink with broken target. |
[email protected] | 6ae340f | 2009-03-06 09:56:28 | [diff] [blame] | 333 | LOG(WARNING) << "CopyDirectory() skipping symbolic link: " << |
| 334 | ent->fts_path; |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 335 | continue; |
| 336 | case FTS_DEFAULT: // Some other sort of file. |
[email protected] | 6ae340f | 2009-03-06 09:56:28 | [diff] [blame] | 337 | LOG(WARNING) << "CopyDirectory() skipping file of unknown type: " << |
| 338 | ent->fts_path; |
[email protected] | 21dec387 | 2008-09-18 19:15:54 | [diff] [blame] | 339 | continue; |
| 340 | default: |
| 341 | NOTREACHED(); |
| 342 | continue; // Hope for the best! |
| 343 | } |
| 344 | } |
| 345 | // fts_read may have returned NULL and set errno to indicate an error. |
| 346 | if (!error && errno != 0) |
| 347 | error = errno; |
| 348 | |
| 349 | if (!fts_close(fts)) { |
| 350 | // If we already have an error, let's use that error instead of the error |
| 351 | // fts_close set. |
| 352 | if (!error) |
| 353 | error = errno; |
| 354 | } |
| 355 | |
| 356 | if (error) { |
| 357 | LOG(ERROR) << "CopyDirectory(): " << strerror(error); |
| 358 | return false; |
| 359 | } |
| 360 | return true; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 363 | bool PathExists(const FilePath& path) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 364 | struct stat64 file_info; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 365 | return (stat64(path.value().c_str(), &file_info) == 0); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 366 | } |
| 367 | |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 368 | bool PathIsWritable(const FilePath& path) { |
| 369 | FilePath test_path(path); |
| 370 | struct stat64 file_info; |
| 371 | if (stat64(test_path.value().c_str(), &file_info) != 0) { |
| 372 | // If the path doesn't exist, test the parent dir. |
| 373 | test_path = test_path.DirName(); |
| 374 | // If the parent dir doesn't exist, then return false (the path is not |
| 375 | // directly writable). |
| 376 | if (stat64(test_path.value().c_str(), &file_info) != 0) |
| 377 | return false; |
| 378 | } |
| 379 | if (S_IWOTH & file_info.st_mode) |
| 380 | return true; |
| 381 | if (getegid() == file_info.st_gid && (S_IWGRP & file_info.st_mode)) |
| 382 | return true; |
| 383 | if (geteuid() == file_info.st_uid && (S_IWUSR & file_info.st_mode)) |
| 384 | return true; |
| 385 | return false; |
| 386 | } |
| 387 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 388 | bool DirectoryExists(const FilePath& path) { |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 389 | struct stat64 file_info; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 390 | if (stat64(path.value().c_str(), &file_info) == 0) |
[email protected] | 806b9c6 | 2008-09-11 16:09:11 | [diff] [blame] | 391 | return S_ISDIR(file_info.st_mode); |
| 392 | return false; |
| 393 | } |
| 394 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 395 | // TODO(erikkay): implement |
| 396 | #if 0 |
| 397 | bool GetFileCreationLocalTimeFromHandle(int fd, |
| 398 | LPSYSTEMTIME creation_time) { |
| 399 | if (!file_handle) |
| 400 | return false; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 401 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 402 | FILETIME utc_filetime; |
| 403 | if (!GetFileTime(file_handle, &utc_filetime, NULL, NULL)) |
| 404 | return false; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 405 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 406 | FILETIME local_filetime; |
| 407 | if (!FileTimeToLocalFileTime(&utc_filetime, &local_filetime)) |
| 408 | return false; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 409 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 410 | return !!FileTimeToSystemTime(&local_filetime, creation_time); |
| 411 | } |
| 412 | |
| 413 | bool GetFileCreationLocalTime(const std::string& filename, |
| 414 | LPSYSTEMTIME creation_time) { |
| 415 | ScopedHandle file_handle( |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 416 | CreateFile(filename.c_str(), GENERIC_READ, |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 417 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 418 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); |
| 419 | return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); |
| 420 | } |
| 421 | #endif |
| 422 | |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 423 | bool ReadFromFD(int fd, char* buffer, size_t bytes) { |
| 424 | size_t total_read = 0; |
| 425 | while (total_read < bytes) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 426 | ssize_t bytes_read = |
| 427 | HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read)); |
| 428 | if (bytes_read <= 0) |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 429 | break; |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 430 | total_read += bytes_read; |
[email protected] | 4530149 | 2009-04-23 12:38:08 | [diff] [blame] | 431 | } |
| 432 | return total_read == bytes; |
| 433 | } |
| 434 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 435 | // Creates and opens a temporary file in |directory|, returning the |
| 436 | // file descriptor. |path| is set to the temporary file path. |
| 437 | // Note TODO(erikkay) comment in header for BlahFileName() calls; the |
| 438 | // intent is to rename these files BlahFile() (since they create |
| 439 | // files, not filenames). This function does NOT unlink() the file. |
| 440 | int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { |
| 441 | *path = directory.Append(kTempFileName); |
| 442 | const std::string& tmpdir_string = path->value(); |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 443 | // this should be OK since mkstemp just replaces characters in place |
| 444 | char* buffer = const_cast<char*>(tmpdir_string.c_str()); |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 445 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 446 | return mkstemp(buffer); |
| 447 | } |
| 448 | |
| 449 | bool CreateTemporaryFileName(FilePath* path) { |
| 450 | FilePath directory; |
| 451 | if (!GetTempDir(&directory)) |
| 452 | return false; |
| 453 | int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 454 | if (fd < 0) |
| 455 | return false; |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 456 | close(fd); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 460 | FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { |
| 461 | FilePath directory; |
| 462 | if (!GetShmemTempDir(&directory)) |
| 463 | return false; |
| 464 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 465 | return CreateAndOpenTemporaryFileInDir(directory, path); |
| 466 | } |
| 467 | |
| 468 | FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
| 469 | int fd = CreateAndOpenFdForTemporaryFile(dir, path); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 470 | if (fd < 0) |
| 471 | return NULL; |
| 472 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 473 | return fdopen(fd, "a+"); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 474 | } |
| 475 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 476 | bool CreateTemporaryFileNameInDir(const std::wstring& dir, |
| 477 | std::wstring* temp_file) { |
| 478 | // Not implemented yet. |
| 479 | NOTREACHED(); |
| 480 | return false; |
| 481 | } |
| 482 | |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 483 | bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 484 | FilePath* new_temp_path) { |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 485 | FilePath tmpdir; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 486 | if (!GetTempDir(&tmpdir)) |
| 487 | return false; |
[email protected] | 392264c | 2008-11-11 00:01:38 | [diff] [blame] | 488 | tmpdir = tmpdir.Append(kTempFileName); |
| 489 | std::string tmpdir_string = tmpdir.value(); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 490 | // this should be OK since mkdtemp just replaces characters in place |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 491 | char* buffer = const_cast<char*>(tmpdir_string.c_str()); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 492 | char* dtemp = mkdtemp(buffer); |
| 493 | if (!dtemp) |
| 494 | return false; |
[email protected] | 7e1fde6a | 2008-12-23 20:20:10 | [diff] [blame] | 495 | *new_temp_path = FilePath(dtemp); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 496 | return true; |
| 497 | } |
| 498 | |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 499 | bool CreateDirectory(const FilePath& full_path) { |
| 500 | std::vector<FilePath> subpaths; |
| 501 | |
| 502 | // Collect a list of all parent directories. |
| 503 | FilePath last_path = full_path; |
| 504 | subpaths.push_back(full_path); |
| 505 | for (FilePath path = full_path.DirName(); |
| 506 | path.value() != last_path.value(); path = path.DirName()) { |
| 507 | subpaths.push_back(path); |
| 508 | last_path = path; |
| 509 | } |
| 510 | |
| 511 | // Iterate through the parents and create the missing ones. |
| 512 | for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); |
| 513 | i != subpaths.rend(); ++i) { |
| 514 | if (!DirectoryExists(*i)) { |
[email protected] | 39a1a06 | 2009-05-12 20:12:24 | [diff] [blame] | 515 | if (mkdir(i->value().c_str(), 0700) != 0) |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | } |
| 519 | return true; |
| 520 | } |
| 521 | |
[email protected] | eac0709a | 2008-11-04 21:00:46 | [diff] [blame] | 522 | bool GetFileInfo(const FilePath& file_path, FileInfo* results) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 523 | struct stat64 file_info; |
[email protected] | eac0709a | 2008-11-04 21:00:46 | [diff] [blame] | 524 | if (stat64(file_path.value().c_str(), &file_info) != 0) |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 525 | return false; |
[email protected] | f5e3da4d | 2008-09-26 01:04:08 | [diff] [blame] | 526 | results->is_directory = S_ISDIR(file_info.st_mode); |
| 527 | results->size = file_info.st_size; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 528 | return true; |
| 529 | } |
| 530 | |
[email protected] | 825003f | 2009-05-14 17:49:23 | [diff] [blame] | 531 | bool GetInode(const FilePath& path, ino_t* inode) { |
| 532 | struct stat buffer; |
| 533 | int result = stat(path.value().c_str(), &buffer); |
| 534 | if (result < 0) |
| 535 | return false; |
| 536 | |
| 537 | *inode = buffer.st_ino; |
| 538 | return true; |
| 539 | } |
| 540 | |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 541 | FILE* OpenFile(const std::string& filename, const char* mode) { |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 542 | return OpenFile(FilePath(filename), mode); |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 543 | } |
| 544 | |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 545 | FILE* OpenFile(const FilePath& filename, const char* mode) { |
| 546 | return fopen(filename.value().c_str(), mode); |
[email protected] | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 547 | } |
| 548 | |
[email protected] | c870c76 | 2009-01-28 05:47:15 | [diff] [blame] | 549 | int ReadFile(const FilePath& filename, char* data, int size) { |
| 550 | int fd = open(filename.value().c_str(), O_RDONLY); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 551 | if (fd < 0) |
| 552 | return -1; |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 553 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 554 | int ret_value = HANDLE_EINTR(read(fd, data, size)); |
| 555 | HANDLE_EINTR(close(fd)); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 556 | return ret_value; |
| 557 | } |
| 558 | |
[email protected] | c870c76 | 2009-01-28 05:47:15 | [diff] [blame] | 559 | int WriteFile(const FilePath& filename, const char* data, int size) { |
| 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 | |
| 564 | // Allow for partial writes |
| 565 | ssize_t bytes_written_total = 0; |
| 566 | do { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 567 | ssize_t bytes_written_partial = |
| 568 | HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 569 | size - bytes_written_total)); |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 570 | if (bytes_written_partial < 0) { |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 571 | HANDLE_EINTR(close(fd)); |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 572 | return -1; |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 573 | } |
| 574 | bytes_written_total += bytes_written_partial; |
| 575 | } while (bytes_written_total < size); |
| 576 | |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 577 | HANDLE_EINTR(close(fd)); |
[email protected] | 778e8c5 | 2008-09-11 17:36:23 | [diff] [blame] | 578 | return bytes_written_total; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | // Gets the current working directory for the process. |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 582 | bool GetCurrentDirectory(FilePath* dir) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 583 | char system_buffer[PATH_MAX] = ""; |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 584 | if (!getcwd(system_buffer, sizeof(system_buffer))) { |
| 585 | NOTREACHED(); |
| 586 | return false; |
| 587 | } |
| 588 | *dir = FilePath(system_buffer); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 589 | return true; |
| 590 | } |
| 591 | |
| 592 | // Sets the current working directory for the process. |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 593 | bool SetCurrentDirectory(const FilePath& path) { |
| 594 | int ret = chdir(path.value().c_str()); |
| 595 | return !ret; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 596 | } |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 597 | |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 598 | /////////////////////////////////////////////// |
| 599 | // FileEnumerator |
| 600 | |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 601 | FileEnumerator::FileEnumerator(const FilePath& root_path, |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 602 | bool recursive, |
| 603 | FileEnumerator::FILE_TYPE file_type) |
| 604 | : recursive_(recursive), |
| 605 | file_type_(file_type), |
| 606 | is_in_find_op_(false), |
| 607 | fts_(NULL) { |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 608 | // INCLUDE_DOT_DOT must not be specified if recursive. |
| 609 | DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 610 | pending_paths_.push(root_path); |
| 611 | } |
| 612 | |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 613 | FileEnumerator::FileEnumerator(const FilePath& root_path, |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 614 | bool recursive, |
| 615 | FileEnumerator::FILE_TYPE file_type, |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 616 | const FilePath::StringType& pattern) |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 617 | : recursive_(recursive), |
| 618 | file_type_(file_type), |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 619 | pattern_(root_path.value()), |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 620 | is_in_find_op_(false), |
| 621 | fts_(NULL) { |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 622 | // INCLUDE_DOT_DOT must not be specified if recursive. |
| 623 | DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_))); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 624 | // The Windows version of this code only matches against items in the top-most |
| 625 | // directory, and we're comparing fnmatch against full paths, so this is the |
| 626 | // easiest way to get the right pattern. |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 627 | pattern_ = pattern_.Append(pattern); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 628 | pending_paths_.push(root_path); |
| 629 | } |
[email protected] | a9cd2a65 | 2008-11-17 21:01:19 | [diff] [blame] | 630 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 631 | FileEnumerator::~FileEnumerator() { |
| 632 | if (fts_) |
| 633 | fts_close(fts_); |
| 634 | } |
| 635 | |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 636 | void FileEnumerator::GetFindInfo(FindInfo* info) { |
| 637 | DCHECK(info); |
| 638 | |
| 639 | if (!is_in_find_op_) |
| 640 | return; |
| 641 | |
| 642 | memcpy(&(info->stat), fts_ent_->fts_statp, sizeof(info->stat)); |
| 643 | info->filename.assign(fts_ent_->fts_name); |
| 644 | } |
| 645 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 646 | // As it stands, this method calls itself recursively when the next item of |
| 647 | // the fts enumeration doesn't match (type, pattern, etc.). In the case of |
| 648 | // large directories with many files this can be quite deep. |
| 649 | // TODO(erikkay) - get rid of this recursive pattern |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 650 | FilePath FileEnumerator::Next() { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 651 | if (!is_in_find_op_) { |
| 652 | if (pending_paths_.empty()) |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 653 | return FilePath(); |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 654 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 655 | // The last find FindFirstFile operation is done, prepare a new one. |
| 656 | root_path_ = pending_paths_.top(); |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 657 | root_path_ = root_path_.StripTrailingSeparators(); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 658 | pending_paths_.pop(); |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 659 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 660 | // Start a new find operation. |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 661 | int ftsflags = FTS_LOGICAL | FTS_SEEDOT; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 662 | char top_dir[PATH_MAX]; |
[email protected] | e15c8e0 | 2009-01-02 12:57:53 | [diff] [blame] | 663 | base::strlcpy(top_dir, root_path_.value().c_str(), arraysize(top_dir)); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 664 | char* dir_list[2] = { top_dir, NULL }; |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 665 | fts_ = fts_open(dir_list, ftsflags, CompareFiles); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 666 | if (!fts_) |
| 667 | return Next(); |
| 668 | is_in_find_op_ = true; |
| 669 | } |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 670 | |
| 671 | fts_ent_ = fts_read(fts_); |
| 672 | if (fts_ent_ == NULL) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 673 | fts_close(fts_); |
| 674 | fts_ = NULL; |
| 675 | is_in_find_op_ = false; |
| 676 | return Next(); |
| 677 | } |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 678 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 679 | // Level 0 is the top, which is always skipped. |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 680 | if (fts_ent_->fts_level == 0) |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 681 | return Next(); |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 682 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 683 | // Patterns are only matched on the items in the top-most directory. |
| 684 | // (see Windows implementation) |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 685 | if (fts_ent_->fts_level == 1 && pattern_.value().length() > 0) { |
| 686 | if (fnmatch(pattern_.value().c_str(), fts_ent_->fts_path, 0) != 0) { |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 687 | if (fts_ent_->fts_info == FTS_D) |
| 688 | fts_set(fts_, fts_ent_, FTS_SKIP); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 689 | return Next(); |
| 690 | } |
| 691 | } |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 692 | |
[email protected] | 0b73322 | 2008-12-11 14:55:12 | [diff] [blame] | 693 | FilePath cur_file(fts_ent_->fts_path); |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 694 | if (ShouldSkip(cur_file)) |
| 695 | return Next(); |
| 696 | |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 697 | if (fts_ent_->fts_info == FTS_D) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 698 | // If not recursive, then prune children. |
| 699 | if (!recursive_) |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 700 | fts_set(fts_, fts_ent_, FTS_SKIP); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 701 | return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 702 | } else if (fts_ent_->fts_info == FTS_F) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 703 | return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); |
[email protected] | 8199b3a | 2009-06-09 05:57:38 | [diff] [blame] | 704 | } else if (fts_ent_->fts_info == FTS_DOT) { |
| 705 | if ((file_type_ & FileEnumerator::DIRECTORIES) && IsDotDot(cur_file)) { |
| 706 | return cur_file; |
| 707 | } |
| 708 | return Next(); |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 709 | } |
| 710 | // TODO(erikkay) - verify that the other fts_info types aren't interesting |
| 711 | return Next(); |
| 712 | } |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 713 | |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 714 | /////////////////////////////////////////////// |
| 715 | // MemoryMappedFile |
| 716 | |
| 717 | MemoryMappedFile::MemoryMappedFile() |
| 718 | : file_(-1), |
| 719 | data_(NULL), |
| 720 | length_(0) { |
| 721 | } |
| 722 | |
| 723 | bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 724 | file_ = open(file_name.value().c_str(), O_RDONLY); |
| 725 | |
[email protected] | 4883a4e | 2009-06-06 19:59:36 | [diff] [blame] | 726 | if (file_ == -1) { |
| 727 | LOG(ERROR) << "Couldn't open " << file_name.value(); |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 728 | return false; |
[email protected] | 4883a4e | 2009-06-06 19:59:36 | [diff] [blame] | 729 | } |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 730 | |
| 731 | struct stat file_stat; |
[email protected] | 4883a4e | 2009-06-06 19:59:36 | [diff] [blame] | 732 | if (fstat(file_, &file_stat) == -1) { |
| 733 | LOG(ERROR) << "Couldn't fstat " << file_name.value() << ", errno " << errno; |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 734 | return false; |
[email protected] | 4883a4e | 2009-06-06 19:59:36 | [diff] [blame] | 735 | } |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 736 | length_ = file_stat.st_size; |
| 737 | |
| 738 | data_ = static_cast<uint8*>( |
| 739 | mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0)); |
| 740 | if (data_ == MAP_FAILED) |
[email protected] | 4883a4e | 2009-06-06 19:59:36 | [diff] [blame] | 741 | LOG(ERROR) << "Couldn't mmap " << file_name.value() << ", errno " << errno; |
| 742 | |
| 743 | return data_ != MAP_FAILED; |
[email protected] | 7856bb8 | 2008-12-12 23:43:03 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | void MemoryMappedFile::CloseHandles() { |
| 747 | if (data_ != NULL) |
| 748 | munmap(data_, length_); |
| 749 | if (file_ != -1) |
| 750 | close(file_); |
| 751 | |
| 752 | data_ = NULL; |
| 753 | length_ = 0; |
| 754 | file_ = -1; |
| 755 | } |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 756 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 757 | } // namespace file_util |