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