thestig@chromium.org | f1a9ce1 | 2012-03-03 10:54:35 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #include "base/file_util.h" |
| 6 | |
jeremy@chromium.org | a5f404d | 2009-01-28 04:08:39 +0900 | [diff] [blame] | 7 | #if defined(OS_WIN) |
| 8 | #include <io.h> |
| 9 | #endif |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 12 | #include <fstream> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 13 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 14 | #include "base/file_path.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 15 | #include "base/logging.h" |
jam@chromium.org | c693fe3 | 2012-02-01 08:16:39 +0900 | [diff] [blame] | 16 | #include "base/stringprintf.h" |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 17 | #include "base/string_piece.h" |
brettw@chromium.org | 50c9465 | 2009-10-07 11:10:20 +0900 | [diff] [blame] | 18 | #include "base/string_util.h" |
| 19 | #include "base/utf_string_conversions.h" |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 20 | |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); |
| 24 | |
jam@chromium.org | c693fe3 | 2012-02-01 08:16:39 +0900 | [diff] [blame] | 25 | // The maximum number of 'uniquified' files we will try to create. |
| 26 | // This is used when the filename we're trying to download is already in use, |
| 27 | // so we create a new unique filename by appending " (nnn)" before the |
| 28 | // extension, where 1 <= nnn <= kMaxUniqueFiles. |
| 29 | // Also used by code that cleans up said files. |
| 30 | static const int kMaxUniqueFiles = 100; |
| 31 | |
willchan@chromium.org | 2f0b26b | 2009-04-25 02:44:39 +0900 | [diff] [blame] | 32 | } // namespace |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 33 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 34 | namespace file_util { |
| 35 | |
aa@chromium.org | 8dac1e4 | 2012-02-05 12:22:38 +0900 | [diff] [blame] | 36 | bool g_bug108724_debug = false; |
| 37 | |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 38 | bool EndsWithSeparator(const FilePath& path) { |
| 39 | FilePath::StringType value = path.value(); |
| 40 | if (value.empty()) |
| 41 | return false; |
| 42 | |
| 43 | return FilePath::IsSeparator(value[value.size() - 1]); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 44 | } |
| 45 | |
estade@chromium.org | d778b8f | 2008-11-26 07:04:37 +0900 | [diff] [blame] | 46 | bool EnsureEndsWithSeparator(FilePath* path) { |
| 47 | if (!DirectoryExists(*path)) |
| 48 | return false; |
| 49 | |
| 50 | if (EndsWithSeparator(*path)) |
| 51 | return true; |
| 52 | |
| 53 | FilePath::StringType& path_str = |
| 54 | const_cast<FilePath::StringType&>(path->value()); |
| 55 | path_str.append(&FilePath::kSeparators[0], 1); |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 60 | void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) { |
| 61 | FilePath::StringType& value = |
| 62 | const_cast<FilePath::StringType&>(path->value()); |
| 63 | |
| 64 | const FilePath::StringType::size_type last_dot = |
| 65 | value.rfind(kExtensionSeparator); |
| 66 | const FilePath::StringType::size_type last_separator = |
| 67 | value.find_last_of(FilePath::StringType(FilePath::kSeparators)); |
| 68 | |
| 69 | if (last_dot == FilePath::StringType::npos || |
| 70 | (last_separator != std::wstring::npos && last_dot < last_separator)) { |
| 71 | // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". |
| 72 | // We should just append the suffix to the entire path. |
| 73 | value.append(suffix); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | value.insert(last_dot, suffix); |
| 78 | } |
| 79 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 80 | bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 81 | // We open the file in binary format even if they are text files because |
| 82 | // we are just comparing that bytes are exactly same in both files and not |
| 83 | // doing anything smart with text formatting. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 84 | std::ifstream file1(filename1.value().c_str(), |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 85 | std::ios::in | std::ios::binary); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 86 | std::ifstream file2(filename2.value().c_str(), |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 87 | std::ios::in | std::ios::binary); |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 88 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 89 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 90 | // any unusable file yields a result of "false". |
| 91 | if (!file1.is_open() || !file2.is_open()) |
| 92 | return false; |
| 93 | |
| 94 | const int BUFFER_SIZE = 2056; |
| 95 | char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; |
| 96 | do { |
| 97 | file1.read(buffer1, BUFFER_SIZE); |
| 98 | file2.read(buffer2, BUFFER_SIZE); |
| 99 | |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 100 | if ((file1.eof() != file2.eof()) || |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 101 | (file1.gcount() != file2.gcount()) || |
| 102 | (memcmp(buffer1, buffer2, file1.gcount()))) { |
| 103 | file1.close(); |
| 104 | file2.close(); |
| 105 | return false; |
| 106 | } |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 107 | } while (!file1.eof() || !file2.eof()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 108 | |
| 109 | file1.close(); |
| 110 | file2.close(); |
| 111 | return true; |
| 112 | } |
| 113 | |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 114 | bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
| 115 | std::ifstream file1(filename1.value().c_str(), std::ios::in); |
| 116 | std::ifstream file2(filename2.value().c_str(), std::ios::in); |
| 117 | |
| 118 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 119 | // any unusable file yields a result of "false". |
| 120 | if (!file1.is_open() || !file2.is_open()) |
| 121 | return false; |
| 122 | |
| 123 | do { |
| 124 | std::string line1, line2; |
| 125 | getline(file1, line1); |
| 126 | getline(file2, line2); |
| 127 | |
| 128 | // Check for mismatched EOF states, or any error state. |
| 129 | if ((file1.eof() != file2.eof()) || |
| 130 | file1.bad() || file2.bad()) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // Trim all '\r' and '\n' characters from the end of the line. |
| 135 | std::string::size_type end1 = line1.find_last_not_of("\r\n"); |
| 136 | if (end1 == std::string::npos) |
| 137 | line1.clear(); |
| 138 | else if (end1 + 1 < line1.length()) |
| 139 | line1.erase(end1 + 1); |
| 140 | |
| 141 | std::string::size_type end2 = line2.find_last_not_of("\r\n"); |
| 142 | if (end2 == std::string::npos) |
| 143 | line2.clear(); |
| 144 | else if (end2 + 1 < line2.length()) |
| 145 | line2.erase(end2 + 1); |
| 146 | |
| 147 | if (line1 != line2) |
| 148 | return false; |
| 149 | } while (!file1.eof() || !file2.eof()); |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
erikkay@google.com | 52460fb | 2009-01-28 09:22:46 +0900 | [diff] [blame] | 154 | bool ReadFileToString(const FilePath& path, std::string* contents) { |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 155 | FILE* file = OpenFile(path, "rb"); |
| 156 | if (!file) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 157 | return false; |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 158 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 159 | |
| 160 | char buf[1 << 16]; |
| 161 | size_t len; |
| 162 | while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
evan@chromium.org | a75b2e8 | 2010-05-19 20:07:55 +0900 | [diff] [blame] | 163 | if (contents) |
| 164 | contents->append(buf, len); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 165 | } |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 166 | CloseFile(file); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
tfarina@chromium.org | 3482822 | 2010-05-26 10:40:12 +0900 | [diff] [blame] | 171 | bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 172 | FileEnumerator files(dir_path, false, |
tfarina@chromium.org | a3a4db7 | 2011-08-15 22:09:27 +0900 | [diff] [blame] | 173 | static_cast<FileEnumerator::FileType>( |
tfarina@chromium.org | 3482822 | 2010-05-26 10:40:12 +0900 | [diff] [blame] | 174 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES)); |
| 175 | if (files.Next().value().empty()) |
| 176 | return true; |
| 177 | return false; |
| 178 | } |
| 179 | |
phajdan.jr@chromium.org | 8139fe1 | 2009-04-28 15:50:36 +0900 | [diff] [blame] | 180 | FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
| 181 | FilePath directory; |
| 182 | if (!GetTempDir(&directory)) |
evan@chromium.org | 2abe0b4 | 2010-06-11 07:56:23 +0900 | [diff] [blame] | 183 | return NULL; |
phajdan.jr@chromium.org | 8139fe1 | 2009-04-28 15:50:36 +0900 | [diff] [blame] | 184 | |
| 185 | return CreateAndOpenTemporaryFileInDir(directory, path); |
| 186 | } |
| 187 | |
dkegel@google.com | 4498268 | 2008-11-05 06:00:46 +0900 | [diff] [blame] | 188 | bool GetFileSize(const FilePath& file_path, int64* file_size) { |
dumi@chromium.org | 97ae261 | 2010-09-03 11:28:37 +0900 | [diff] [blame] | 189 | base::PlatformFileInfo info; |
darin@google.com | 7f479f2 | 2008-09-26 10:04:08 +0900 | [diff] [blame] | 190 | if (!GetFileInfo(file_path, &info)) |
| 191 | return false; |
| 192 | *file_size = info.size; |
| 193 | return true; |
| 194 | } |
| 195 | |
estade@chromium.org | a99d0e1 | 2010-02-12 08:27:47 +0900 | [diff] [blame] | 196 | bool IsDot(const FilePath& path) { |
| 197 | return FILE_PATH_LITERAL(".") == path.BaseName().value(); |
| 198 | } |
| 199 | |
| 200 | bool IsDotDot(const FilePath& path) { |
| 201 | return FILE_PATH_LITERAL("..") == path.BaseName().value(); |
| 202 | } |
| 203 | |
dumi@chromium.org | c941a18 | 2010-09-24 08:28:22 +0900 | [diff] [blame] | 204 | bool TouchFile(const FilePath& path, |
| 205 | const base::Time& last_accessed, |
| 206 | const base::Time& last_modified) { |
| 207 | base::PlatformFile file = |
| 208 | base::CreatePlatformFile(path, |
| 209 | base::PLATFORM_FILE_OPEN | |
| 210 | base::PLATFORM_FILE_WRITE_ATTRIBUTES, |
| 211 | NULL, NULL); |
| 212 | if (file != base::kInvalidPlatformFileValue) { |
| 213 | bool result = base::TouchPlatformFile(file, last_accessed, last_modified); |
| 214 | base::ClosePlatformFile(file); |
| 215 | return result; |
| 216 | } |
| 217 | |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | bool SetLastModifiedTime(const FilePath& path, |
| 222 | const base::Time& last_modified) { |
| 223 | return TouchFile(path, last_modified, last_modified); |
| 224 | } |
| 225 | |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 226 | bool CloseFile(FILE* file) { |
sidchat@google.com | d3b2643 | 2008-10-22 02:14:45 +0900 | [diff] [blame] | 227 | if (file == NULL) |
| 228 | return true; |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 229 | return fclose(file) == 0; |
| 230 | } |
| 231 | |
jeremy@chromium.org | a5f404d | 2009-01-28 04:08:39 +0900 | [diff] [blame] | 232 | bool TruncateFile(FILE* file) { |
| 233 | if (file == NULL) |
| 234 | return false; |
| 235 | long current_offset = ftell(file); |
| 236 | if (current_offset == -1) |
| 237 | return false; |
| 238 | #if defined(OS_WIN) |
| 239 | int fd = _fileno(file); |
| 240 | if (_chsize(fd, current_offset) != 0) |
| 241 | return false; |
| 242 | #else |
| 243 | int fd = fileno(file); |
| 244 | if (ftruncate(fd, current_offset) != 0) |
| 245 | return false; |
| 246 | #endif |
| 247 | return true; |
| 248 | } |
| 249 | |
jam@chromium.org | c693fe3 | 2012-02-01 08:16:39 +0900 | [diff] [blame] | 250 | int GetUniquePathNumber( |
| 251 | const FilePath& path, |
| 252 | const FilePath::StringType& suffix) { |
| 253 | bool have_suffix = !suffix.empty(); |
| 254 | if (!PathExists(path) && |
| 255 | (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | FilePath new_path; |
| 260 | for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
| 261 | new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); |
| 262 | if (!PathExists(new_path) && |
| 263 | (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 264 | return count; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return -1; |
| 269 | } |
| 270 | |
aa@chromium.org | a4dbdf2 | 2009-01-10 07:14:27 +0900 | [diff] [blame] | 271 | bool ContainsPath(const FilePath &parent, const FilePath& child) { |
| 272 | FilePath abs_parent = FilePath(parent); |
| 273 | FilePath abs_child = FilePath(child); |
| 274 | |
| 275 | if (!file_util::AbsolutePath(&abs_parent) || |
| 276 | !file_util::AbsolutePath(&abs_child)) |
| 277 | return false; |
| 278 | |
| 279 | #if defined(OS_WIN) |
| 280 | // file_util::AbsolutePath() does not flatten case on Windows, so we must do |
| 281 | // a case-insensitive compare. |
| 282 | if (!StartsWith(abs_child.value(), abs_parent.value(), false)) |
| 283 | #else |
| 284 | if (!StartsWithASCII(abs_child.value(), abs_parent.value(), true)) |
| 285 | #endif |
| 286 | return false; |
| 287 | |
| 288 | // file_util::AbsolutePath() normalizes '/' to '\' on Windows, so we only need |
| 289 | // to check kSeparators[0]. |
| 290 | if (abs_child.value().length() <= abs_parent.value().length() || |
| 291 | abs_child.value()[abs_parent.value().length()] != |
| 292 | FilePath::kSeparators[0]) |
| 293 | return false; |
| 294 | |
| 295 | return true; |
| 296 | } |
| 297 | |
cpu@chromium.org | 83f07be | 2010-03-25 06:56:26 +0900 | [diff] [blame] | 298 | int64 ComputeDirectorySize(const FilePath& root_path) { |
| 299 | int64 running_size = 0; |
| 300 | FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); |
| 301 | for (FilePath current = file_iter.Next(); !current.empty(); |
| 302 | current = file_iter.Next()) { |
| 303 | FileEnumerator::FindInfo info; |
| 304 | file_iter.GetFindInfo(&info); |
| 305 | #if defined(OS_WIN) |
| 306 | LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh }; |
| 307 | running_size += li.QuadPart; |
| 308 | #else |
| 309 | running_size += info.stat.st_size; |
| 310 | #endif |
| 311 | } |
| 312 | return running_size; |
| 313 | } |
| 314 | |
rvargas@google.com | aa24e11 | 2010-06-12 07:53:43 +0900 | [diff] [blame] | 315 | int64 ComputeFilesSize(const FilePath& directory, |
| 316 | const FilePath::StringType& pattern) { |
| 317 | int64 running_size = 0; |
| 318 | FileEnumerator file_iter(directory, false, FileEnumerator::FILES, pattern); |
| 319 | for (FilePath current = file_iter.Next(); !current.empty(); |
| 320 | current = file_iter.Next()) { |
| 321 | FileEnumerator::FindInfo info; |
| 322 | file_iter.GetFindInfo(&info); |
| 323 | #if defined(OS_WIN) |
| 324 | LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh }; |
| 325 | running_size += li.QuadPart; |
| 326 | #else |
| 327 | running_size += info.stat.st_size; |
| 328 | #endif |
| 329 | } |
| 330 | return running_size; |
| 331 | } |
| 332 | |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 333 | /////////////////////////////////////////////// |
| 334 | // MemoryMappedFile |
| 335 | |
| 336 | MemoryMappedFile::~MemoryMappedFile() { |
| 337 | CloseHandles(); |
| 338 | } |
| 339 | |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 340 | bool MemoryMappedFile::Initialize(const FilePath& file_name) { |
| 341 | if (IsValid()) |
| 342 | return false; |
| 343 | |
| 344 | if (!MapFileToMemory(file_name)) { |
| 345 | CloseHandles(); |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | return true; |
| 350 | } |
| 351 | |
estade@chromium.org | 3ac3206 | 2009-11-17 07:55:17 +0900 | [diff] [blame] | 352 | bool MemoryMappedFile::Initialize(base::PlatformFile file) { |
| 353 | if (IsValid()) |
| 354 | return false; |
| 355 | |
| 356 | file_ = file; |
| 357 | |
| 358 | if (!MapFileToMemoryInternal()) { |
| 359 | CloseHandles(); |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | return true; |
| 364 | } |
| 365 | |
tommi@chromium.org | 3920995 | 2011-02-24 06:42:48 +0900 | [diff] [blame] | 366 | bool MemoryMappedFile::IsValid() const { |
erg@google.com | 37c078e | 2011-01-11 09:50:59 +0900 | [diff] [blame] | 367 | return data_ != NULL; |
estade@chromium.org | 2c23353 | 2008-12-13 08:43:03 +0900 | [diff] [blame] | 368 | } |
| 369 | |
estade@chromium.org | 3ac3206 | 2009-11-17 07:55:17 +0900 | [diff] [blame] | 370 | bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { |
dumi@chromium.org | 50f197d | 2010-09-01 04:30:27 +0900 | [diff] [blame] | 371 | file_ = base::CreatePlatformFile( |
| 372 | file_name, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| 373 | NULL, NULL); |
estade@chromium.org | 3ac3206 | 2009-11-17 07:55:17 +0900 | [diff] [blame] | 374 | |
| 375 | if (file_ == base::kInvalidPlatformFileValue) { |
brettw@chromium.org | 5faed3c | 2011-10-27 06:48:00 +0900 | [diff] [blame] | 376 | DLOG(ERROR) << "Couldn't open " << file_name.value(); |
estade@chromium.org | 3ac3206 | 2009-11-17 07:55:17 +0900 | [diff] [blame] | 377 | return false; |
| 378 | } |
| 379 | |
| 380 | return MapFileToMemoryInternal(); |
| 381 | } |
| 382 | |
yuzo@chromium.org | 2da0f82 | 2009-06-09 14:57:38 +0900 | [diff] [blame] | 383 | /////////////////////////////////////////////// |
| 384 | // FileEnumerator |
| 385 | // |
| 386 | // Note: the main logic is in file_util_<platform>.cc |
| 387 | |
| 388 | bool FileEnumerator::ShouldSkip(const FilePath& path) { |
| 389 | FilePath::StringType basename = path.BaseName().value(); |
| 390 | return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_)); |
| 391 | } |
| 392 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 393 | } // namespace |