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> |
kaliamoorthi@chromium.org | 8d3c40d | 2014-02-19 01:31:51 +0900 | [diff] [blame] | 13 | #include <limits> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 14 | |
brettw@chromium.org | 5694672 | 2013-06-08 13:53:36 +0900 | [diff] [blame] | 15 | #include "base/files/file_enumerator.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 17 | #include "base/logging.h" |
tfarina@chromium.org | b6d4911 | 2013-03-30 23:29:00 +0900 | [diff] [blame] | 18 | #include "base/strings/string_piece.h" |
avi@chromium.org | 94bd573 | 2013-06-11 22:36:37 +0900 | [diff] [blame] | 19 | #include "base/strings/string_util.h" |
| 20 | #include "base/strings/stringprintf.h" |
avi@chromium.org | 17f6062 | 2013-06-08 03:37:07 +0900 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 22 | |
brettw@chromium.org | 55d5ee6 | 2013-06-23 07:15:46 +0900 | [diff] [blame] | 23 | namespace base { |
brettw@chromium.org | c02b603 | 2013-02-16 13:12:26 +0900 | [diff] [blame] | 24 | |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 25 | namespace { |
| 26 | |
jam@chromium.org | c693fe3 | 2012-02-01 08:16:39 +0900 | [diff] [blame] | 27 | // The maximum number of 'uniquified' files we will try to create. |
| 28 | // This is used when the filename we're trying to download is already in use, |
| 29 | // so we create a new unique filename by appending " (nnn)" before the |
| 30 | // extension, where 1 <= nnn <= kMaxUniqueFiles. |
| 31 | // Also used by code that cleans up said files. |
| 32 | static const int kMaxUniqueFiles = 100; |
| 33 | |
willchan@chromium.org | 2f0b26b | 2009-04-25 02:44:39 +0900 | [diff] [blame] | 34 | } // namespace |
estade@chromium.org | 6334320 | 2008-12-05 05:46:06 +0900 | [diff] [blame] | 35 | |
brettw@chromium.org | 55d5ee6 | 2013-06-23 07:15:46 +0900 | [diff] [blame] | 36 | int64 ComputeDirectorySize(const FilePath& root_path) { |
| 37 | int64 running_size = 0; |
| 38 | FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); |
| 39 | while (!file_iter.Next().empty()) |
| 40 | running_size += file_iter.GetInfo().GetSize(); |
| 41 | return running_size; |
| 42 | } |
| 43 | |
brettw@chromium.org | 0878fea | 2013-07-02 08:07:36 +0900 | [diff] [blame] | 44 | bool Move(const FilePath& from_path, const FilePath& to_path) { |
| 45 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
| 46 | return false; |
brettw@chromium.org | aecf7a3 | 2013-07-10 02:42:26 +0900 | [diff] [blame] | 47 | return internal::MoveUnsafe(from_path, to_path); |
| 48 | } |
| 49 | |
| 50 | bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| 51 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
| 52 | return false; |
| 53 | return internal::CopyFileUnsafe(from_path, to_path); |
brettw@chromium.org | 0878fea | 2013-07-02 08:07:36 +0900 | [diff] [blame] | 54 | } |
| 55 | |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 56 | bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 57 | // We open the file in binary format even if they are text files because |
| 58 | // we are just comparing that bytes are exactly same in both files and not |
| 59 | // doing anything smart with text formatting. |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 60 | std::ifstream file1(filename1.value().c_str(), |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 61 | std::ios::in | std::ios::binary); |
evanm@google.com | 874d167 | 2008-10-31 08:54:04 +0900 | [diff] [blame] | 62 | std::ifstream file2(filename2.value().c_str(), |
erikkay@google.com | 9fc57d0 | 2008-08-09 05:16:08 +0900 | [diff] [blame] | 63 | std::ios::in | std::ios::binary); |
estade@chromium.org | 97e3782 | 2008-11-27 13:03:57 +0900 | [diff] [blame] | 64 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 65 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 66 | // any unusable file yields a result of "false". |
| 67 | if (!file1.is_open() || !file2.is_open()) |
| 68 | return false; |
| 69 | |
| 70 | const int BUFFER_SIZE = 2056; |
| 71 | char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; |
| 72 | do { |
| 73 | file1.read(buffer1, BUFFER_SIZE); |
| 74 | file2.read(buffer2, BUFFER_SIZE); |
| 75 | |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 76 | if ((file1.eof() != file2.eof()) || |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 77 | (file1.gcount() != file2.gcount()) || |
| 78 | (memcmp(buffer1, buffer2, file1.gcount()))) { |
| 79 | file1.close(); |
| 80 | file2.close(); |
| 81 | return false; |
| 82 | } |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 83 | } while (!file1.eof() || !file2.eof()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 84 | |
| 85 | file1.close(); |
| 86 | file2.close(); |
| 87 | return true; |
| 88 | } |
| 89 | |
mark@chromium.org | 95c9ec9 | 2009-06-27 06:17:24 +0900 | [diff] [blame] | 90 | bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
| 91 | std::ifstream file1(filename1.value().c_str(), std::ios::in); |
| 92 | std::ifstream file2(filename2.value().c_str(), std::ios::in); |
| 93 | |
| 94 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 95 | // any unusable file yields a result of "false". |
| 96 | if (!file1.is_open() || !file2.is_open()) |
| 97 | return false; |
| 98 | |
| 99 | do { |
| 100 | std::string line1, line2; |
| 101 | getline(file1, line1); |
| 102 | getline(file2, line2); |
| 103 | |
| 104 | // Check for mismatched EOF states, or any error state. |
| 105 | if ((file1.eof() != file2.eof()) || |
| 106 | file1.bad() || file2.bad()) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | // Trim all '\r' and '\n' characters from the end of the line. |
| 111 | std::string::size_type end1 = line1.find_last_not_of("\r\n"); |
| 112 | if (end1 == std::string::npos) |
| 113 | line1.clear(); |
| 114 | else if (end1 + 1 < line1.length()) |
| 115 | line1.erase(end1 + 1); |
| 116 | |
| 117 | std::string::size_type end2 = line2.find_last_not_of("\r\n"); |
| 118 | if (end2 == std::string::npos) |
| 119 | line2.clear(); |
| 120 | else if (end2 + 1 < line2.length()) |
| 121 | line2.erase(end2 + 1); |
| 122 | |
| 123 | if (line1 != line2) |
| 124 | return false; |
| 125 | } while (!file1.eof() || !file2.eof()); |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
kaliamoorthi@chromium.org | 8d3c40d | 2014-02-19 01:31:51 +0900 | [diff] [blame] | 130 | bool ReadFileToString(const FilePath& path, |
| 131 | std::string* contents, |
| 132 | size_t max_size) { |
| 133 | if (contents) |
| 134 | contents->clear(); |
cpu@chromium.org | 93db53b | 2013-01-09 09:38:59 +0900 | [diff] [blame] | 135 | if (path.ReferencesParent()) |
| 136 | return false; |
thakis@chromium.org | f01de7e | 2013-12-09 06:43:30 +0900 | [diff] [blame] | 137 | FILE* file = OpenFile(path, "rb"); |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 138 | if (!file) { |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 139 | return false; |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 140 | } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 141 | |
| 142 | char buf[1 << 16]; |
| 143 | size_t len; |
kaliamoorthi@chromium.org | 8d3c40d | 2014-02-19 01:31:51 +0900 | [diff] [blame] | 144 | size_t size = 0; |
| 145 | bool read_status = true; |
| 146 | |
| 147 | // Many files supplied in |path| have incorrect size (proc files etc). |
| 148 | // Hence, the file is read sequentially as opposed to a one-shot read. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 149 | while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
evan@chromium.org | a75b2e8 | 2010-05-19 20:07:55 +0900 | [diff] [blame] | 150 | if (contents) |
kaliamoorthi@chromium.org | 8d3c40d | 2014-02-19 01:31:51 +0900 | [diff] [blame] | 151 | contents->append(buf, std::min(len, max_size - size)); |
| 152 | |
| 153 | if ((max_size - size) < len) { |
| 154 | read_status = false; |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | size += len; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 159 | } |
thakis@chromium.org | f01de7e | 2013-12-09 06:43:30 +0900 | [diff] [blame] | 160 | CloseFile(file); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 161 | |
kaliamoorthi@chromium.org | 8d3c40d | 2014-02-19 01:31:51 +0900 | [diff] [blame] | 162 | return read_status; |
| 163 | } |
| 164 | |
| 165 | bool ReadFileToString(const FilePath& path, std::string* contents) { |
| 166 | return ReadFileToString(path, contents, std::numeric_limits<size_t>::max()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 167 | } |
| 168 | |
brettw@chromium.org | 83c44c8 | 2013-12-03 03:55:49 +0900 | [diff] [blame] | 169 | bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 170 | FileEnumerator files(dir_path, false, |
| 171 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
| 172 | if (files.Next().empty()) |
| 173 | return true; |
| 174 | return false; |
| 175 | } |
| 176 | |
brettw@chromium.org | 735d11d | 2013-12-04 02:55:52 +0900 | [diff] [blame] | 177 | FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
| 178 | FilePath directory; |
| 179 | if (!GetTempDir(&directory)) |
| 180 | return NULL; |
| 181 | |
| 182 | return CreateAndOpenTemporaryFileInDir(directory, path); |
| 183 | } |
| 184 | |
brettw@chromium.org | 738669a | 2013-12-04 05:08:54 +0900 | [diff] [blame] | 185 | bool CreateDirectory(const FilePath& full_path) { |
| 186 | return CreateDirectoryAndGetError(full_path, NULL); |
| 187 | } |
| 188 | |
brettw@chromium.org | 7068424 | 2013-12-05 03:22:49 +0900 | [diff] [blame] | 189 | bool GetFileSize(const FilePath& file_path, int64* file_size) { |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 190 | File::Info info; |
brettw@chromium.org | a915403 | 2013-12-05 05:56:49 +0900 | [diff] [blame] | 191 | if (!GetFileInfo(file_path, &info)) |
brettw@chromium.org | 7068424 | 2013-12-05 03:22:49 +0900 | [diff] [blame] | 192 | return false; |
| 193 | *file_size = info.size; |
| 194 | return true; |
| 195 | } |
| 196 | |
brettw@chromium.org | 458d1e3 | 2013-12-05 07:49:00 +0900 | [diff] [blame] | 197 | bool TouchFile(const FilePath& path, |
| 198 | const Time& last_accessed, |
| 199 | const Time& last_modified) { |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 200 | int flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES; |
brettw@chromium.org | 458d1e3 | 2013-12-05 07:49:00 +0900 | [diff] [blame] | 201 | |
| 202 | #if defined(OS_WIN) |
| 203 | // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. |
| 204 | if (DirectoryExists(path)) |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 205 | flags |= File::FLAG_BACKUP_SEMANTICS; |
brettw@chromium.org | 458d1e3 | 2013-12-05 07:49:00 +0900 | [diff] [blame] | 206 | #endif // OS_WIN |
| 207 | |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 208 | File file(path, flags); |
| 209 | if (!file.IsValid()) |
| 210 | return false; |
brettw@chromium.org | 458d1e3 | 2013-12-05 07:49:00 +0900 | [diff] [blame] | 211 | |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 212 | return file.SetTimes(last_accessed, last_modified); |
brettw@chromium.org | 458d1e3 | 2013-12-05 07:49:00 +0900 | [diff] [blame] | 213 | } |
| 214 | |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 215 | bool CloseFile(FILE* file) { |
sidchat@google.com | d3b2643 | 2008-10-22 02:14:45 +0900 | [diff] [blame] | 216 | if (file == NULL) |
| 217 | return true; |
mark@chromium.org | d1bafc6 | 2008-10-02 02:40:13 +0900 | [diff] [blame] | 218 | return fclose(file) == 0; |
| 219 | } |
| 220 | |
jeremy@chromium.org | a5f404d | 2009-01-28 04:08:39 +0900 | [diff] [blame] | 221 | bool TruncateFile(FILE* file) { |
| 222 | if (file == NULL) |
| 223 | return false; |
| 224 | long current_offset = ftell(file); |
| 225 | if (current_offset == -1) |
| 226 | return false; |
| 227 | #if defined(OS_WIN) |
| 228 | int fd = _fileno(file); |
| 229 | if (_chsize(fd, current_offset) != 0) |
| 230 | return false; |
| 231 | #else |
| 232 | int fd = fileno(file); |
| 233 | if (ftruncate(fd, current_offset) != 0) |
| 234 | return false; |
| 235 | #endif |
| 236 | return true; |
| 237 | } |
| 238 | |
brettw@chromium.org | aa82a77 | 2014-03-14 02:26:21 +0900 | [diff] [blame] | 239 | int GetUniquePathNumber(const FilePath& path, |
| 240 | const FilePath::StringType& suffix) { |
jam@chromium.org | c693fe3 | 2012-02-01 08:16:39 +0900 | [diff] [blame] | 241 | bool have_suffix = !suffix.empty(); |
| 242 | if (!PathExists(path) && |
| 243 | (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | FilePath new_path; |
| 248 | for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
brettw@chromium.org | aa82a77 | 2014-03-14 02:26:21 +0900 | [diff] [blame] | 249 | new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); |
jam@chromium.org | c693fe3 | 2012-02-01 08:16:39 +0900 | [diff] [blame] | 250 | if (!PathExists(new_path) && |
| 251 | (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 252 | return count; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return -1; |
| 257 | } |
| 258 | |
brettw@chromium.org | aa82a77 | 2014-03-14 02:26:21 +0900 | [diff] [blame] | 259 | } // namespace base |