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