thestig@chromium.org | b2f28d2 | 2012-03-03 01:54:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [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 | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include "base/file_util.h" |
| 6 | |
jeremy@chromium.org | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 7 | #if defined(OS_WIN) |
| 8 | #include <io.h> |
| 9 | #endif |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | #include <fstream> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | |
brettw@chromium.org | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame^] | 14 | #include "base/files/file_enumerator.h" |
brettw@chromium.org | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | #include "base/logging.h" |
brettw@chromium.org | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 17 | #include "base/string_util.h" |
brettw@chromium.org | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 18 | #include "base/stringprintf.h" |
tfarina@chromium.org | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 19 | #include "base/strings/string_piece.h" |
avi@chromium.org | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
estade@chromium.org | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 21 | |
brettw@chromium.org | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame^] | 22 | using base::FileEnumerator; |
brettw@chromium.org | 04af979a | 2013-02-16 04:12:26 | [diff] [blame] | 23 | using base::FilePath; |
| 24 | |
estade@chromium.org | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); |
jam@chromium.org | e285afa | 2012-01-31 23:16:39 | [diff] [blame] | 28 | |
| 29 | // The maximum number of 'uniquified' files we will try to create. |
| 30 | // This is used when the filename we're trying to download is already in use, |
| 31 | // so we create a new unique filename by appending " (nnn)" before the |
| 32 | // extension, where 1 <= nnn <= kMaxUniqueFiles. |
| 33 | // Also used by code that cleans up said files. |
| 34 | static const int kMaxUniqueFiles = 100; |
estade@chromium.org | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 35 | |
willchan@chromium.org | c145cbdd | 2009-04-24 17:44:39 | [diff] [blame] | 36 | } // namespace |
estade@chromium.org | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 37 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 38 | namespace file_util { |
| 39 | |
aa@chromium.org | 354002bb | 2012-02-05 03:22:38 | [diff] [blame] | 40 | bool g_bug108724_debug = false; |
| 41 | |
estade@chromium.org | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 42 | void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) { |
| 43 | FilePath::StringType& value = |
| 44 | const_cast<FilePath::StringType&>(path->value()); |
| 45 | |
| 46 | const FilePath::StringType::size_type last_dot = |
| 47 | value.rfind(kExtensionSeparator); |
| 48 | const FilePath::StringType::size_type last_separator = |
| 49 | value.find_last_of(FilePath::StringType(FilePath::kSeparators)); |
| 50 | |
| 51 | if (last_dot == FilePath::StringType::npos || |
| 52 | (last_separator != std::wstring::npos && last_dot < last_separator)) { |
| 53 | // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo". |
| 54 | // We should just append the suffix to the entire path. |
| 55 | value.append(suffix); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | value.insert(last_dot, suffix); |
| 60 | } |
| 61 | |
cevans@chromium.org | 3cd2c1c | 2013-02-06 20:38:07 | [diff] [blame] | 62 | bool Move(const FilePath& from_path, const FilePath& to_path) { |
| 63 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
| 64 | return false; |
| 65 | return MoveUnsafe(from_path, to_path); |
| 66 | } |
| 67 | |
dgrogan@chromium.org | 6f539941 | 2013-05-08 22:02:36 | [diff] [blame] | 68 | bool ReplaceFile(const base::FilePath& from_path, |
| 69 | const base::FilePath& to_path) { |
| 70 | return ReplaceFileAndGetError(from_path, to_path, NULL); |
| 71 | } |
| 72 | |
cevans@chromium.org | 3cd2c1c | 2013-02-06 20:38:07 | [diff] [blame] | 73 | bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| 74 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
| 75 | return false; |
| 76 | return CopyFileUnsafe(from_path, to_path); |
| 77 | } |
| 78 | |
evanm@google.com | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 79 | bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 80 | // We open the file in binary format even if they are text files because |
| 81 | // we are just comparing that bytes are exactly same in both files and not |
| 82 | // doing anything smart with text formatting. |
evanm@google.com | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 83 | std::ifstream file1(filename1.value().c_str(), |
erikkay@google.com | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 84 | std::ios::in | std::ios::binary); |
evanm@google.com | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 85 | std::ifstream file2(filename2.value().c_str(), |
erikkay@google.com | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 86 | std::ios::in | std::ios::binary); |
estade@chromium.org | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 87 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 88 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 89 | // any unusable file yields a result of "false". |
| 90 | if (!file1.is_open() || !file2.is_open()) |
| 91 | return false; |
| 92 | |
| 93 | const int BUFFER_SIZE = 2056; |
| 94 | char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; |
| 95 | do { |
| 96 | file1.read(buffer1, BUFFER_SIZE); |
| 97 | file2.read(buffer2, BUFFER_SIZE); |
| 98 | |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 99 | if ((file1.eof() != file2.eof()) || |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 100 | (file1.gcount() != file2.gcount()) || |
| 101 | (memcmp(buffer1, buffer2, file1.gcount()))) { |
| 102 | file1.close(); |
| 103 | file2.close(); |
| 104 | return false; |
| 105 | } |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 106 | } while (!file1.eof() || !file2.eof()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 107 | |
| 108 | file1.close(); |
| 109 | file2.close(); |
| 110 | return true; |
| 111 | } |
| 112 | |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 113 | bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
| 114 | std::ifstream file1(filename1.value().c_str(), std::ios::in); |
| 115 | std::ifstream file2(filename2.value().c_str(), std::ios::in); |
| 116 | |
| 117 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 118 | // any unusable file yields a result of "false". |
| 119 | if (!file1.is_open() || !file2.is_open()) |
| 120 | return false; |
| 121 | |
| 122 | do { |
| 123 | std::string line1, line2; |
| 124 | getline(file1, line1); |
| 125 | getline(file2, line2); |
| 126 | |
| 127 | // Check for mismatched EOF states, or any error state. |
| 128 | if ((file1.eof() != file2.eof()) || |
| 129 | file1.bad() || file2.bad()) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | // Trim all '\r' and '\n' characters from the end of the line. |
| 134 | std::string::size_type end1 = line1.find_last_not_of("\r\n"); |
| 135 | if (end1 == std::string::npos) |
| 136 | line1.clear(); |
| 137 | else if (end1 + 1 < line1.length()) |
| 138 | line1.erase(end1 + 1); |
| 139 | |
| 140 | std::string::size_type end2 = line2.find_last_not_of("\r\n"); |
| 141 | if (end2 == std::string::npos) |
| 142 | line2.clear(); |
| 143 | else if (end2 + 1 < line2.length()) |
| 144 | line2.erase(end2 + 1); |
| 145 | |
| 146 | if (line1 != line2) |
| 147 | return false; |
| 148 | } while (!file1.eof() || !file2.eof()); |
| 149 | |
| 150 | return true; |
| 151 | } |
| 152 | |
erikkay@google.com | 3c528102 | 2009-01-28 00:22:46 | [diff] [blame] | 153 | bool ReadFileToString(const FilePath& path, std::string* contents) { |
cpu@chromium.org | 9fea5a9 | 2013-01-09 00:38:59 | [diff] [blame] | 154 | if (path.ReferencesParent()) |
| 155 | return false; |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 156 | FILE* file = OpenFile(path, "rb"); |
| 157 | if (!file) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 158 | return false; |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 159 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | |
| 161 | char buf[1 << 16]; |
| 162 | size_t len; |
| 163 | while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
evan@chromium.org | 4e074bae | 2010-05-19 11:07:55 | [diff] [blame] | 164 | if (contents) |
| 165 | contents->append(buf, len); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | } |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 167 | CloseFile(file); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 168 | |
| 169 | return true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | } |
| 171 | |
tfarina@chromium.org | b33f1d9 | 2010-05-26 01:40:12 | [diff] [blame] | 172 | bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 173 | FileEnumerator files(dir_path, false, |
haruki@chromium.org | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 174 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
brettw@chromium.org | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame^] | 175 | if (files.Next().empty()) |
tfarina@chromium.org | b33f1d9 | 2010-05-26 01:40:12 | [diff] [blame] | 176 | return true; |
| 177 | return false; |
| 178 | } |
| 179 | |
phajdan.jr@chromium.org | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 180 | FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
| 181 | FilePath directory; |
| 182 | if (!GetTempDir(&directory)) |
evan@chromium.org | 628476aa | 2010-06-10 22:56:23 | [diff] [blame] | 183 | return NULL; |
phajdan.jr@chromium.org | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 184 | |
| 185 | return CreateAndOpenTemporaryFileInDir(directory, path); |
| 186 | } |
| 187 | |
dkegel@google.com | eac0709a | 2008-11-04 21:00:46 | [diff] [blame] | 188 | bool GetFileSize(const FilePath& file_path, int64* file_size) { |
dumi@chromium.org | 2f0193c2 | 2010-09-03 02:28:37 | [diff] [blame] | 189 | base::PlatformFileInfo info; |
darin@google.com | f5e3da4d | 2008-09-26 01:04:08 | [diff] [blame] | 190 | if (!GetFileInfo(file_path, &info)) |
| 191 | return false; |
| 192 | *file_size = info.size; |
| 193 | return true; |
| 194 | } |
| 195 | |
dumi@chromium.org | 507fb9a | 2010-09-23 23:28:22 | [diff] [blame] | 196 | bool TouchFile(const FilePath& path, |
| 197 | const base::Time& last_accessed, |
| 198 | const base::Time& last_modified) { |
nhiroki@chromium.org | 307a825a | 2012-11-01 11:48:52 | [diff] [blame] | 199 | int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE_ATTRIBUTES; |
| 200 | |
| 201 | #if defined(OS_WIN) |
| 202 | // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. |
| 203 | if (DirectoryExists(path)) |
| 204 | flags |= base::PLATFORM_FILE_BACKUP_SEMANTICS; |
| 205 | #endif // OS_WIN |
| 206 | |
| 207 | const base::PlatformFile file = |
| 208 | base::CreatePlatformFile(path, flags, NULL, NULL); |
dumi@chromium.org | 507fb9a | 2010-09-23 23:28:22 | [diff] [blame] | 209 | if (file != base::kInvalidPlatformFileValue) { |
| 210 | bool result = base::TouchPlatformFile(file, last_accessed, last_modified); |
| 211 | base::ClosePlatformFile(file); |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | bool SetLastModifiedTime(const FilePath& path, |
| 219 | const base::Time& last_modified) { |
| 220 | return TouchFile(path, last_modified, last_modified); |
| 221 | } |
| 222 | |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 223 | bool CloseFile(FILE* file) { |
sidchat@google.com | a1a1950 | 2008-10-21 17:14:45 | [diff] [blame] | 224 | if (file == NULL) |
| 225 | return true; |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 226 | return fclose(file) == 0; |
jeremy@chromium.org | 6e01dae | 2009-01-27 17:13:02 | [diff] [blame] | 227 | } |
| 228 | |
jeremy@chromium.org | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 229 | bool TruncateFile(FILE* file) { |
| 230 | if (file == NULL) |
| 231 | return false; |
| 232 | long current_offset = ftell(file); |
| 233 | if (current_offset == -1) |
| 234 | return false; |
| 235 | #if defined(OS_WIN) |
| 236 | int fd = _fileno(file); |
| 237 | if (_chsize(fd, current_offset) != 0) |
| 238 | return false; |
| 239 | #else |
| 240 | int fd = fileno(file); |
| 241 | if (ftruncate(fd, current_offset) != 0) |
| 242 | return false; |
| 243 | #endif |
| 244 | return true; |
| 245 | } |
| 246 | |
jam@chromium.org | e285afa | 2012-01-31 23:16:39 | [diff] [blame] | 247 | int GetUniquePathNumber( |
| 248 | const FilePath& path, |
| 249 | const FilePath::StringType& suffix) { |
| 250 | bool have_suffix = !suffix.empty(); |
| 251 | if (!PathExists(path) && |
| 252 | (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | FilePath new_path; |
| 257 | for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
groby@chromium.org | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 258 | new_path = |
| 259 | path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", count)); |
jam@chromium.org | e285afa | 2012-01-31 23:16:39 | [diff] [blame] | 260 | if (!PathExists(new_path) && |
| 261 | (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 262 | return count; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return -1; |
aa@chromium.org | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 267 | } |
| 268 | |
cpu@chromium.org | c2c132c | 2010-03-24 21:56:26 | [diff] [blame] | 269 | int64 ComputeDirectorySize(const FilePath& root_path) { |
| 270 | int64 running_size = 0; |
| 271 | FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); |
brettw@chromium.org | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame^] | 272 | while (!file_iter.Next().empty()) |
| 273 | running_size += file_iter.GetInfo().GetSize(); |
cpu@chromium.org | c2c132c | 2010-03-24 21:56:26 | [diff] [blame] | 274 | return running_size; |
dbeam@chromium.org | 9e66a9b | 2013-05-08 05:46:20 | [diff] [blame] | 275 | } |
| 276 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 277 | } // namespace |