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 | |
brettw@chromium.org | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 5 | #include "base/files/file_util.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 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> |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 13 | #include <limits> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
brettw@chromium.org | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 15 | #include "base/files/file_enumerator.h" |
brettw@chromium.org | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | #include "base/logging.h" |
tfarina@chromium.org | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 18 | #include "base/strings/string_piece.h" |
avi@chromium.org | 251cd6e5 | 2013-06-11 13:36:37 | [diff] [blame] | 19 | #include "base/strings/string_util.h" |
| 20 | #include "base/strings/stringprintf.h" |
avi@chromium.org | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 22 | #include "build/build_config.h" |
estade@chromium.org | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 23 | |
brettw@chromium.org | 0408c75 | 2013-06-22 22:15:46 | [diff] [blame] | 24 | namespace base { |
brettw@chromium.org | 04af979a | 2013-02-16 04:12:26 | [diff] [blame] | 25 | |
hidehiko | 8fc7c82 | 2015-06-09 02:50:57 | [diff] [blame] | 26 | #if !defined(OS_NACL_NONSFI) |
estade@chromium.org | ceeb87e | 2008-12-04 20:46:06 | [diff] [blame] | 27 | namespace { |
| 28 | |
jam@chromium.org | e285afa | 2012-01-31 23:16:39 | [diff] [blame] | 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 | |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 38 | int64_t ComputeDirectorySize(const FilePath& root_path) { |
| 39 | int64_t running_size = 0; |
brettw@chromium.org | 0408c75 | 2013-06-22 22:15:46 | [diff] [blame] | 40 | FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); |
| 41 | while (!file_iter.Next().empty()) |
| 42 | running_size += file_iter.GetInfo().GetSize(); |
| 43 | return running_size; |
| 44 | } |
| 45 | |
brettw@chromium.org | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 46 | bool Move(const FilePath& from_path, const FilePath& to_path) { |
| 47 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
| 48 | return false; |
brettw@chromium.org | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 49 | return internal::MoveUnsafe(from_path, to_path); |
| 50 | } |
| 51 | |
evanm@google.com | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 52 | bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 53 | // We open the file in binary format even if they are text files because |
| 54 | // we are just comparing that bytes are exactly same in both files and not |
| 55 | // doing anything smart with text formatting. |
evanm@google.com | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 56 | std::ifstream file1(filename1.value().c_str(), |
erikkay@google.com | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 57 | std::ios::in | std::ios::binary); |
evanm@google.com | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 58 | std::ifstream file2(filename2.value().c_str(), |
erikkay@google.com | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 59 | std::ios::in | std::ios::binary); |
estade@chromium.org | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 60 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 62 | // any unusable file yields a result of "false". |
| 63 | if (!file1.is_open() || !file2.is_open()) |
| 64 | return false; |
| 65 | |
| 66 | const int BUFFER_SIZE = 2056; |
| 67 | char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; |
| 68 | do { |
| 69 | file1.read(buffer1, BUFFER_SIZE); |
| 70 | file2.read(buffer2, BUFFER_SIZE); |
| 71 | |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 72 | if ((file1.eof() != file2.eof()) || |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 73 | (file1.gcount() != file2.gcount()) || |
pkasting | 9cf9b94 | 2014-10-01 22:18:43 | [diff] [blame] | 74 | (memcmp(buffer1, buffer2, static_cast<size_t>(file1.gcount())))) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 75 | file1.close(); |
| 76 | file2.close(); |
| 77 | return false; |
| 78 | } |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 79 | } while (!file1.eof() || !file2.eof()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 80 | |
| 81 | file1.close(); |
| 82 | file2.close(); |
| 83 | return true; |
| 84 | } |
| 85 | |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 86 | bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
| 87 | std::ifstream file1(filename1.value().c_str(), std::ios::in); |
| 88 | std::ifstream file2(filename2.value().c_str(), std::ios::in); |
| 89 | |
| 90 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 91 | // any unusable file yields a result of "false". |
| 92 | if (!file1.is_open() || !file2.is_open()) |
| 93 | return false; |
| 94 | |
| 95 | do { |
| 96 | std::string line1, line2; |
| 97 | getline(file1, line1); |
| 98 | getline(file2, line2); |
| 99 | |
| 100 | // Check for mismatched EOF states, or any error state. |
| 101 | if ((file1.eof() != file2.eof()) || |
| 102 | file1.bad() || file2.bad()) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // Trim all '\r' and '\n' characters from the end of the line. |
| 107 | std::string::size_type end1 = line1.find_last_not_of("\r\n"); |
| 108 | if (end1 == std::string::npos) |
| 109 | line1.clear(); |
| 110 | else if (end1 + 1 < line1.length()) |
| 111 | line1.erase(end1 + 1); |
| 112 | |
| 113 | std::string::size_type end2 = line2.find_last_not_of("\r\n"); |
| 114 | if (end2 == std::string::npos) |
| 115 | line2.clear(); |
| 116 | else if (end2 + 1 < line2.length()) |
| 117 | line2.erase(end2 + 1); |
| 118 | |
| 119 | if (line1 != line2) |
| 120 | return false; |
| 121 | } while (!file1.eof() || !file2.eof()); |
| 122 | |
| 123 | return true; |
| 124 | } |
hidehiko | 8fc7c82 | 2015-06-09 02:50:57 | [diff] [blame] | 125 | #endif // !defined(OS_NACL_NONSFI) |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 126 | |
hashimoto | 6da2fef | 2016-02-24 03:39:58 | [diff] [blame^] | 127 | bool ReadFileToStringWithMaxSize(const FilePath& path, |
| 128 | std::string* contents, |
| 129 | size_t max_size) { |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 130 | if (contents) |
| 131 | contents->clear(); |
cpu@chromium.org | 9fea5a9 | 2013-01-09 00:38:59 | [diff] [blame] | 132 | if (path.ReferencesParent()) |
| 133 | return false; |
thakis@chromium.org | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 134 | FILE* file = OpenFile(path, "rb"); |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 135 | if (!file) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 136 | return false; |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 137 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 138 | |
cpu | 9deb575 | 2014-10-21 01:20:40 | [diff] [blame] | 139 | const size_t kBufferSize = 1 << 16; |
| 140 | scoped_ptr<char[]> buf(new char[kBufferSize]); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 141 | size_t len; |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 142 | size_t size = 0; |
| 143 | bool read_status = true; |
| 144 | |
| 145 | // Many files supplied in |path| have incorrect size (proc files etc). |
| 146 | // Hence, the file is read sequentially as opposed to a one-shot read. |
cpu | 9deb575 | 2014-10-21 01:20:40 | [diff] [blame] | 147 | while ((len = fread(buf.get(), 1, kBufferSize, file)) > 0) { |
evan@chromium.org | 4e074bae | 2010-05-19 11:07:55 | [diff] [blame] | 148 | if (contents) |
cpu | 9deb575 | 2014-10-21 01:20:40 | [diff] [blame] | 149 | contents->append(buf.get(), std::min(len, max_size - size)); |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 150 | |
| 151 | if ((max_size - size) < len) { |
| 152 | read_status = false; |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | size += len; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 157 | } |
tnagel@chromium.org | 731daa1 | 2014-05-01 10:40:51 | [diff] [blame] | 158 | read_status = read_status && !ferror(file); |
thakis@chromium.org | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 159 | CloseFile(file); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 161 | return read_status; |
| 162 | } |
| 163 | |
| 164 | bool ReadFileToString(const FilePath& path, std::string* contents) { |
hashimoto | 6da2fef | 2016-02-24 03:39:58 | [diff] [blame^] | 165 | return ReadFileToStringWithMaxSize(path, contents, |
| 166 | std::numeric_limits<size_t>::max()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 167 | } |
| 168 | |
hidehiko | 8fc7c82 | 2015-06-09 02:50:57 | [diff] [blame] | 169 | #if !defined(OS_NACL_NONSFI) |
brettw@chromium.org | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 170 | bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 171 | FileEnumerator files(dir_path, false, |
| 172 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
| 173 | if (files.Next().empty()) |
| 174 | return true; |
| 175 | return false; |
| 176 | } |
| 177 | |
brettw@chromium.org | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 178 | FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
| 179 | FilePath directory; |
| 180 | if (!GetTempDir(&directory)) |
| 181 | return NULL; |
| 182 | |
| 183 | return CreateAndOpenTemporaryFileInDir(directory, path); |
| 184 | } |
| 185 | |
brettw@chromium.org | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 186 | bool CreateDirectory(const FilePath& full_path) { |
| 187 | return CreateDirectoryAndGetError(full_path, NULL); |
| 188 | } |
| 189 | |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 190 | bool GetFileSize(const FilePath& file_path, int64_t* file_size) { |
rvargas@chromium.org | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 191 | File::Info info; |
brettw@chromium.org | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 192 | if (!GetFileInfo(file_path, &info)) |
brettw@chromium.org | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 193 | return false; |
| 194 | *file_size = info.size; |
| 195 | return true; |
| 196 | } |
| 197 | |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 198 | bool TouchFile(const FilePath& path, |
| 199 | const Time& last_accessed, |
| 200 | const Time& last_modified) { |
rvargas@chromium.org | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 201 | int flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES; |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 202 | |
| 203 | #if defined(OS_WIN) |
| 204 | // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. |
| 205 | if (DirectoryExists(path)) |
rvargas@chromium.org | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 206 | flags |= File::FLAG_BACKUP_SEMANTICS; |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 207 | #endif // OS_WIN |
| 208 | |
rvargas@chromium.org | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 209 | File file(path, flags); |
| 210 | if (!file.IsValid()) |
| 211 | return false; |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 212 | |
rvargas@chromium.org | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 213 | return file.SetTimes(last_accessed, last_modified); |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 214 | } |
hidehiko | 8fc7c82 | 2015-06-09 02:50:57 | [diff] [blame] | 215 | #endif // !defined(OS_NACL_NONSFI) |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 216 | |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 217 | bool CloseFile(FILE* file) { |
sidchat@google.com | a1a1950 | 2008-10-21 17:14:45 | [diff] [blame] | 218 | if (file == NULL) |
| 219 | return true; |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 220 | return fclose(file) == 0; |
jeremy@chromium.org | 6e01dae | 2009-01-27 17:13:02 | [diff] [blame] | 221 | } |
| 222 | |
hidehiko | 8fc7c82 | 2015-06-09 02:50:57 | [diff] [blame] | 223 | #if !defined(OS_NACL_NONSFI) |
jeremy@chromium.org | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 224 | bool TruncateFile(FILE* file) { |
| 225 | if (file == NULL) |
| 226 | return false; |
| 227 | long current_offset = ftell(file); |
| 228 | if (current_offset == -1) |
| 229 | return false; |
| 230 | #if defined(OS_WIN) |
| 231 | int fd = _fileno(file); |
| 232 | if (_chsize(fd, current_offset) != 0) |
| 233 | return false; |
| 234 | #else |
| 235 | int fd = fileno(file); |
| 236 | if (ftruncate(fd, current_offset) != 0) |
| 237 | return false; |
| 238 | #endif |
| 239 | return true; |
| 240 | } |
brettw@chromium.org | 5a6ddb1 | 2013-12-07 00:04:53 | [diff] [blame] | 241 | |
brettw@chromium.org | a26f4ae | 2014-03-13 17:26:21 | [diff] [blame] | 242 | int GetUniquePathNumber(const FilePath& path, |
| 243 | const FilePath::StringType& suffix) { |
jam@chromium.org | e285afa | 2012-01-31 23:16:39 | [diff] [blame] | 244 | bool have_suffix = !suffix.empty(); |
| 245 | if (!PathExists(path) && |
| 246 | (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | FilePath new_path; |
| 251 | for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
brettw@chromium.org | a26f4ae | 2014-03-13 17:26:21 | [diff] [blame] | 252 | new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); |
jam@chromium.org | e285afa | 2012-01-31 23:16:39 | [diff] [blame] | 253 | if (!PathExists(new_path) && |
| 254 | (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 255 | return count; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return -1; |
aa@chromium.org | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 260 | } |
hidehiko | 8fc7c82 | 2015-06-09 02:50:57 | [diff] [blame] | 261 | #endif // !defined(OS_NACL_NONSFI) |
aa@chromium.org | ee5c29da | 2009-01-09 22:14:27 | [diff] [blame] | 262 | |
brettw@chromium.org | a26f4ae | 2014-03-13 17:26:21 | [diff] [blame] | 263 | } // namespace base |