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