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 | |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 7 | #include "build/build_config.h" |
| 8 | |
| 9 | #if BUILDFLAG(IS_WIN) |
jeremy@chromium.org | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 10 | #include <io.h> |
| 11 | #endif |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | #include <fstream> |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 15 | #include <limits> |
Victor Costan | b5a0a9700 | 2019-09-08 04:55:15 | [diff] [blame] | 16 | #include <memory> |
Victor Hugo Vianna Silva | 05f1454 | 2021-08-05 16:08:48 | [diff] [blame] | 17 | #include <vector> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 19 | #include "base/check_op.h" |
brettw@chromium.org | 25a4c1c | 2013-06-08 04:53:36 | [diff] [blame] | 20 | #include "base/files/file_enumerator.h" |
brettw@chromium.org | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 21 | #include "base/files/file_path.h" |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 22 | #include "base/posix/eintr_wrapper.h" |
tfarina@chromium.org | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 23 | #include "base/strings/string_piece.h" |
avi@chromium.org | 251cd6e5 | 2013-06-11 13:36:37 | [diff] [blame] | 24 | #include "base/strings/string_util.h" |
| 25 | #include "base/strings/stringprintf.h" |
avi@chromium.org | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 26 | #include "base/strings/utf_string_conversions.h" |
Etienne Pierre-Doray | 1da5859 | 2018-09-21 14:47:20 | [diff] [blame] | 27 | #include "base/threading/scoped_blocking_call.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 28 | #include "build/build_config.h" |
estade@chromium.org | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 29 | |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 30 | #if BUILDFLAG(IS_WIN) |
Bruce Dawson | 1ad438d | 2021-07-09 20:10:03 | [diff] [blame] | 31 | #include <windows.h> |
| 32 | #endif |
| 33 | |
brettw@chromium.org | 0408c75 | 2013-06-22 22:15:46 | [diff] [blame] | 34 | namespace base { |
brettw@chromium.org | 04af979a | 2013-02-16 04:12:26 | [diff] [blame] | 35 | |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 36 | #if !BUILDFLAG(IS_WIN) |
Lei Zhang | e3aa0b9a | 2020-06-11 08:59:23 | [diff] [blame] | 37 | OnceCallback<void(const FilePath&)> GetDeleteFileCallback() { |
Lei Zhang | 9382efe | 2020-07-25 20:52:14 | [diff] [blame] | 38 | return BindOnce(IgnoreResult(&DeleteFile)); |
Lei Zhang | e3aa0b9a | 2020-06-11 08:59:23 | [diff] [blame] | 39 | } |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 40 | #endif // !BUILDFLAG(IS_WIN) |
Lei Zhang | e3aa0b9a | 2020-06-11 08:59:23 | [diff] [blame] | 41 | |
Lei Zhang | 3190449f | 2020-06-12 05:14:04 | [diff] [blame] | 42 | OnceCallback<void(const FilePath&)> GetDeletePathRecursivelyCallback() { |
Lei Zhang | 746ce47 | 2020-07-01 04:39:45 | [diff] [blame] | 43 | return BindOnce(IgnoreResult(&DeletePathRecursively)); |
Lei Zhang | 3190449f | 2020-06-12 05:14:04 | [diff] [blame] | 44 | } |
| 45 | |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 46 | int64_t ComputeDirectorySize(const FilePath& root_path) { |
| 47 | int64_t running_size = 0; |
brettw@chromium.org | 0408c75 | 2013-06-22 22:15:46 | [diff] [blame] | 48 | FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); |
| 49 | while (!file_iter.Next().empty()) |
| 50 | running_size += file_iter.GetInfo().GetSize(); |
| 51 | return running_size; |
| 52 | } |
| 53 | |
brettw@chromium.org | 5553d5b | 2013-07-01 23:07:36 | [diff] [blame] | 54 | bool Move(const FilePath& from_path, const FilePath& to_path) { |
| 55 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
| 56 | return false; |
brettw@chromium.org | f0ff2ad | 2013-07-09 17:42:26 | [diff] [blame] | 57 | return internal::MoveUnsafe(from_path, to_path); |
| 58 | } |
| 59 | |
Alexander Bolodurin | 53bfc89c2 | 2020-11-25 22:43:29 | [diff] [blame] | 60 | bool CopyFileContents(File& infile, File& outfile) { |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 61 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) |
Brian Geffon | 591e8f2 | 2021-04-08 18:33:23 | [diff] [blame] | 62 | bool retry_slow = false; |
| 63 | bool res = |
| 64 | internal::CopyFileContentsWithSendfile(infile, outfile, retry_slow); |
| 65 | if (res || !retry_slow) { |
| 66 | return res; |
| 67 | } |
| 68 | // Any failures which allow retrying using read/write will not have modified |
| 69 | // either file offset or size. |
| 70 | #endif |
| 71 | |
Alexander Bolodurin | 53bfc89c2 | 2020-11-25 22:43:29 | [diff] [blame] | 72 | static constexpr size_t kBufferSize = 32768; |
| 73 | std::vector<char> buffer(kBufferSize); |
| 74 | |
| 75 | for (;;) { |
| 76 | int bytes_read = infile.ReadAtCurrentPos(buffer.data(), buffer.size()); |
| 77 | if (bytes_read < 0) { |
| 78 | return false; |
| 79 | } |
| 80 | if (bytes_read == 0) { |
| 81 | return true; |
| 82 | } |
| 83 | // Allow for partial writes |
| 84 | int bytes_written_per_read = 0; |
| 85 | do { |
| 86 | int bytes_written_partial = outfile.WriteAtCurrentPos( |
| 87 | &buffer[bytes_written_per_read], bytes_read - bytes_written_per_read); |
| 88 | if (bytes_written_partial < 0) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | bytes_written_per_read += bytes_written_partial; |
| 93 | } while (bytes_written_per_read < bytes_read); |
| 94 | } |
| 95 | |
| 96 | NOTREACHED(); |
| 97 | return false; |
| 98 | } |
| 99 | |
evanm@google.com | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 100 | bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 101 | // We open the file in binary format even if they are text files because |
| 102 | // we are just comparing that bytes are exactly same in both files and not |
| 103 | // doing anything smart with text formatting. |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 104 | #if BUILDFLAG(IS_WIN) |
Jan Wilken Dörrie | 9bb441d | 2019-11-01 17:48:40 | [diff] [blame] | 105 | std::ifstream file1(filename1.value().c_str(), |
erikkay@google.com | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 106 | std::ios::in | std::ios::binary); |
Jan Wilken Dörrie | 9bb441d | 2019-11-01 17:48:40 | [diff] [blame] | 107 | std::ifstream file2(filename2.value().c_str(), |
erikkay@google.com | 5af2edb9 | 2008-08-08 20:16:08 | [diff] [blame] | 108 | std::ios::in | std::ios::binary); |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 109 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
jdoerrie | 983968a | 2019-01-29 12:54:41 | [diff] [blame] | 110 | std::ifstream file1(filename1.value(), std::ios::in | std::ios::binary); |
| 111 | std::ifstream file2(filename2.value(), std::ios::in | std::ios::binary); |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 112 | #endif // BUILDFLAG(IS_WIN) |
estade@chromium.org | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 113 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 114 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 115 | // any unusable file yields a result of "false". |
| 116 | if (!file1.is_open() || !file2.is_open()) |
| 117 | return false; |
| 118 | |
| 119 | const int BUFFER_SIZE = 2056; |
| 120 | char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; |
| 121 | do { |
| 122 | file1.read(buffer1, BUFFER_SIZE); |
| 123 | file2.read(buffer2, BUFFER_SIZE); |
| 124 | |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 125 | if ((file1.eof() != file2.eof()) || |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 126 | (file1.gcount() != file2.gcount()) || |
pkasting | 9cf9b94 | 2014-10-01 22:18:43 | [diff] [blame] | 127 | (memcmp(buffer1, buffer2, static_cast<size_t>(file1.gcount())))) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 128 | file1.close(); |
| 129 | file2.close(); |
| 130 | return false; |
| 131 | } |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 132 | } while (!file1.eof() || !file2.eof()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 133 | |
| 134 | file1.close(); |
| 135 | file2.close(); |
| 136 | return true; |
| 137 | } |
| 138 | |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 139 | bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 140 | #if BUILDFLAG(IS_WIN) |
Jan Wilken Dörrie | 9bb441d | 2019-11-01 17:48:40 | [diff] [blame] | 141 | std::ifstream file1(filename1.value().c_str(), std::ios::in); |
| 142 | std::ifstream file2(filename2.value().c_str(), std::ios::in); |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 143 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
jdoerrie | 983968a | 2019-01-29 12:54:41 | [diff] [blame] | 144 | std::ifstream file1(filename1.value(), std::ios::in); |
| 145 | std::ifstream file2(filename2.value(), std::ios::in); |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 146 | #endif // BUILDFLAG(IS_WIN) |
mark@chromium.org | b81637c3 | 2009-06-26 21:17:24 | [diff] [blame] | 147 | |
| 148 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 149 | // any unusable file yields a result of "false". |
| 150 | if (!file1.is_open() || !file2.is_open()) |
| 151 | return false; |
| 152 | |
| 153 | do { |
| 154 | std::string line1, line2; |
| 155 | getline(file1, line1); |
| 156 | getline(file2, line2); |
| 157 | |
| 158 | // Check for mismatched EOF states, or any error state. |
| 159 | if ((file1.eof() != file2.eof()) || |
| 160 | file1.bad() || file2.bad()) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | // Trim all '\r' and '\n' characters from the end of the line. |
| 165 | std::string::size_type end1 = line1.find_last_not_of("\r\n"); |
| 166 | if (end1 == std::string::npos) |
| 167 | line1.clear(); |
| 168 | else if (end1 + 1 < line1.length()) |
| 169 | line1.erase(end1 + 1); |
| 170 | |
| 171 | std::string::size_type end2 = line2.find_last_not_of("\r\n"); |
| 172 | if (end2 == std::string::npos) |
| 173 | line2.clear(); |
| 174 | else if (end2 + 1 < line2.length()) |
| 175 | line2.erase(end2 + 1); |
| 176 | |
| 177 | if (line1 != line2) |
| 178 | return false; |
| 179 | } while (!file1.eof() || !file2.eof()); |
| 180 | |
| 181 | return true; |
| 182 | } |
| 183 | |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 184 | bool ReadStreamToString(FILE* stream, std::string* contents) { |
| 185 | return ReadStreamToStringWithMaxSize( |
| 186 | stream, std::numeric_limits<size_t>::max(), contents); |
| 187 | } |
| 188 | |
| 189 | bool ReadStreamToStringWithMaxSize(FILE* stream, |
| 190 | size_t max_size, |
| 191 | std::string* contents) { |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 192 | if (contents) |
| 193 | contents->clear(); |
Abhijith Nair | 1b5b6ee2 | 2021-11-18 21:01:59 | [diff] [blame] | 194 | if (!stream) |
| 195 | return false; |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 196 | // Seeking to the beginning is best-effort -- it is expected to fail for |
| 197 | // certain non-file stream (e.g., pipes). |
| 198 | HANDLE_EINTR(fseek(stream, 0, SEEK_SET)); |
| 199 | |
| 200 | // Many files have incorrect size (proc files etc). Hence, the file is read |
| 201 | // sequentially as opposed to a one-shot read, using file size as a hint for |
| 202 | // chunk size if available. |
Mikhail Istomin | 1ede155 | 2018-05-16 17:40:24 | [diff] [blame] | 203 | constexpr int64_t kDefaultChunkSize = 1 << 16; |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 204 | int64_t chunk_size = kDefaultChunkSize - 1; |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 205 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 206 | #if BUILDFLAG(IS_WIN) |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 207 | BY_HANDLE_FILE_INFORMATION file_info = {}; |
| 208 | if (::GetFileInformationByHandle( |
| 209 | reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(stream))), |
| 210 | &file_info)) { |
| 211 | LARGE_INTEGER size; |
| 212 | size.HighPart = file_info.nFileSizeHigh; |
| 213 | size.LowPart = file_info.nFileSizeLow; |
| 214 | if (size.QuadPart > 0) |
| 215 | chunk_size = size.QuadPart; |
| 216 | } |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 217 | #else // BUILDFLAG(IS_WIN) |
Anand K Mistry | 53fa72d | 2021-07-15 01:15:26 | [diff] [blame] | 218 | // In cases where the reported file size is 0, use a smaller chunk size to |
| 219 | // minimize memory allocated and cost of string::resize() in case the read |
| 220 | // size is small (i.e. proc files). If the file is larger than this, the read |
| 221 | // loop will reset |chunk_size| to kDefaultChunkSize. |
| 222 | constexpr int64_t kSmallChunkSize = 4096; |
| 223 | chunk_size = kSmallChunkSize - 1; |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 224 | stat_wrapper_t file_info = {}; |
| 225 | if (!File::Fstat(fileno(stream), &file_info) && file_info.st_size > 0) |
| 226 | chunk_size = file_info.st_size; |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 227 | #endif // BUILDFLAG(IS_WIN) |
Mikhail Istomin | 1ede155 | 2018-05-16 17:40:24 | [diff] [blame] | 228 | // We need to attempt to read at EOF for feof flag to be set so here we |
| 229 | // use |chunk_size| + 1. |
| 230 | chunk_size = std::min<uint64_t>(chunk_size, max_size) + 1; |
Mikhail Istomin | 1ede155 | 2018-05-16 17:40:24 | [diff] [blame] | 231 | size_t bytes_read_this_pass; |
| 232 | size_t bytes_read_so_far = 0; |
| 233 | bool read_status = true; |
| 234 | std::string local_contents; |
| 235 | local_contents.resize(chunk_size); |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 236 | |
Mikhail Istomin | 1ede155 | 2018-05-16 17:40:24 | [diff] [blame] | 237 | while ((bytes_read_this_pass = fread(&local_contents[bytes_read_so_far], 1, |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 238 | chunk_size, stream)) > 0) { |
Mikhail Istomin | 1ede155 | 2018-05-16 17:40:24 | [diff] [blame] | 239 | if ((max_size - bytes_read_so_far) < bytes_read_this_pass) { |
| 240 | // Read more than max_size bytes, bail out. |
| 241 | bytes_read_so_far = max_size; |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 242 | read_status = false; |
| 243 | break; |
| 244 | } |
Mikhail Istomin | 1ede155 | 2018-05-16 17:40:24 | [diff] [blame] | 245 | // In case EOF was not reached, iterate again but revert to the default |
| 246 | // chunk size. |
| 247 | if (bytes_read_so_far == 0) |
| 248 | chunk_size = kDefaultChunkSize; |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 249 | |
Mikhail Istomin | 1ede155 | 2018-05-16 17:40:24 | [diff] [blame] | 250 | bytes_read_so_far += bytes_read_this_pass; |
| 251 | // Last fread syscall (after EOF) can be avoided via feof, which is just a |
| 252 | // flag check. |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 253 | if (feof(stream)) |
Mikhail Istomin | 1ede155 | 2018-05-16 17:40:24 | [diff] [blame] | 254 | break; |
| 255 | local_contents.resize(bytes_read_so_far + chunk_size); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 256 | } |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 257 | read_status = read_status && !ferror(stream); |
Mikhail Istomin | 1ede155 | 2018-05-16 17:40:24 | [diff] [blame] | 258 | if (contents) { |
| 259 | contents->swap(local_contents); |
| 260 | contents->resize(bytes_read_so_far); |
| 261 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 262 | |
kaliamoorthi@chromium.org | 0710fdd | 2014-02-18 16:31:51 | [diff] [blame] | 263 | return read_status; |
| 264 | } |
| 265 | |
| 266 | bool ReadFileToString(const FilePath& path, std::string* contents) { |
hashimoto | 6da2fef | 2016-02-24 03:39:58 | [diff] [blame] | 267 | return ReadFileToStringWithMaxSize(path, contents, |
| 268 | std::numeric_limits<size_t>::max()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 269 | } |
| 270 | |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 271 | bool ReadFileToStringWithMaxSize(const FilePath& path, |
| 272 | std::string* contents, |
| 273 | size_t max_size) { |
| 274 | if (contents) |
| 275 | contents->clear(); |
| 276 | if (path.ReferencesParent()) |
| 277 | return false; |
| 278 | ScopedFILE file_stream(OpenFile(path, "rb")); |
| 279 | if (!file_stream) |
| 280 | return false; |
| 281 | return ReadStreamToStringWithMaxSize(file_stream.get(), max_size, contents); |
| 282 | } |
| 283 | |
brettw@chromium.org | fb4bcfa3 | 2013-12-02 18:55:49 | [diff] [blame] | 284 | bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 285 | FileEnumerator files(dir_path, false, |
| 286 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
| 287 | if (files.Next().empty()) |
| 288 | return true; |
| 289 | return false; |
| 290 | } |
| 291 | |
Greg Thompson | 3f7e20f4 | 2020-05-02 19:01:11 | [diff] [blame] | 292 | bool CreateTemporaryFile(FilePath* path) { |
| 293 | FilePath temp_dir; |
| 294 | return GetTempDir(&temp_dir) && CreateTemporaryFileInDir(temp_dir, path); |
| 295 | } |
| 296 | |
Greg Thompson | f75f5fb | 2020-04-30 08:13:25 | [diff] [blame] | 297 | ScopedFILE CreateAndOpenTemporaryStream(FilePath* path) { |
brettw@chromium.org | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 298 | FilePath directory; |
| 299 | if (!GetTempDir(&directory)) |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 300 | return nullptr; |
brettw@chromium.org | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 301 | |
Greg Thompson | f75f5fb | 2020-04-30 08:13:25 | [diff] [blame] | 302 | return CreateAndOpenTemporaryStreamInDir(directory, path); |
brettw@chromium.org | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 303 | } |
| 304 | |
brettw@chromium.org | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 305 | bool CreateDirectory(const FilePath& full_path) { |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 306 | return CreateDirectoryAndGetError(full_path, nullptr); |
brettw@chromium.org | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 307 | } |
| 308 | |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 309 | bool GetFileSize(const FilePath& file_path, int64_t* file_size) { |
rvargas@chromium.org | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 310 | File::Info info; |
brettw@chromium.org | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 311 | if (!GetFileInfo(file_path, &info)) |
brettw@chromium.org | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 312 | return false; |
| 313 | *file_size = info.size; |
| 314 | return true; |
| 315 | } |
| 316 | |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 317 | bool TouchFile(const FilePath& path, |
| 318 | const Time& last_accessed, |
| 319 | const Time& last_modified) { |
Alexei Svitkine | 670d67ee | 2021-11-01 21:51:49 | [diff] [blame] | 320 | int flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES; |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 321 | |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 322 | #if BUILDFLAG(IS_WIN) |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 323 | // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. |
| 324 | if (DirectoryExists(path)) |
Alexei Svitkine | c417b2cd | 2021-10-21 14:22:40 | [diff] [blame] | 325 | flags |= File::FLAG_WIN_BACKUP_SEMANTICS; |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 326 | #elif BUILDFLAG(IS_FUCHSIA) |
Wez | 432ea8d89 | 2019-03-31 01:17:08 | [diff] [blame] | 327 | // On Fuchsia, we need O_RDONLY for directories, or O_WRONLY for files. |
| 328 | // TODO(https://crbug.com/947802): Find a cleaner workaround for this. |
| 329 | flags |= (DirectoryExists(path) ? File::FLAG_READ : File::FLAG_WRITE); |
| 330 | #endif |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 331 | |
rvargas@chromium.org | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 332 | File file(path, flags); |
| 333 | if (!file.IsValid()) |
| 334 | return false; |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 335 | |
rvargas@chromium.org | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 336 | return file.SetTimes(last_accessed, last_modified); |
brettw@chromium.org | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 337 | } |
| 338 | |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 339 | bool CloseFile(FILE* file) { |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 340 | if (file == nullptr) |
sidchat@google.com | a1a1950 | 2008-10-21 17:14:45 | [diff] [blame] | 341 | return true; |
mark@chromium.org | 836f134 | 2008-10-01 17:40:13 | [diff] [blame] | 342 | return fclose(file) == 0; |
| 343 | } |
| 344 | |
jeremy@chromium.org | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 345 | bool TruncateFile(FILE* file) { |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 346 | if (file == nullptr) |
jeremy@chromium.org | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 347 | return false; |
| 348 | long current_offset = ftell(file); |
| 349 | if (current_offset == -1) |
| 350 | return false; |
Xiaohan Wang | b705a64a | 2022-01-15 18:31:13 | [diff] [blame^] | 351 | #if BUILDFLAG(IS_WIN) |
jeremy@chromium.org | c2c998c | 2009-01-27 19:08:39 | [diff] [blame] | 352 | int fd = _fileno(file); |
| 353 | if (_chsize(fd, current_offset) != 0) |
| 354 | return false; |
| 355 | #else |
| 356 | int fd = fileno(file); |
| 357 | if (ftruncate(fd, current_offset) != 0) |
| 358 | return false; |
| 359 | #endif |
| 360 | return true; |
| 361 | } |
| 362 | |
Lei Zhang | eebbef6 | 2020-05-05 20:16:05 | [diff] [blame] | 363 | bool WriteFile(const FilePath& filename, span<const uint8_t> data) { |
| 364 | int size = checked_cast<int>(data.size()); |
| 365 | return WriteFile(filename, reinterpret_cast<const char*>(data.data()), |
| 366 | size) == size; |
| 367 | } |
| 368 | |
| 369 | bool WriteFile(const FilePath& filename, StringPiece data) { |
| 370 | int size = checked_cast<int>(data.size()); |
| 371 | return WriteFile(filename, data.data(), size) == size; |
| 372 | } |
| 373 | |
Greg Thompson | b4abcb4 | 2019-08-23 01:42:56 | [diff] [blame] | 374 | int GetUniquePathNumber(const FilePath& path) { |
| 375 | DCHECK(!path.empty()); |
| 376 | if (!PathExists(path)) |
Greg Thompson | 42373544a | 2019-08-14 10:11:10 | [diff] [blame] | 377 | return 0; |
| 378 | |
| 379 | std::string number; |
| 380 | for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
| 381 | StringAppendF(&number, " (%d)", count); |
Greg Thompson | b4abcb4 | 2019-08-23 01:42:56 | [diff] [blame] | 382 | if (!PathExists(path.InsertBeforeExtensionASCII(number))) |
Greg Thompson | 42373544a | 2019-08-14 10:11:10 | [diff] [blame] | 383 | return count; |
| 384 | number.clear(); |
jam@chromium.org | e285afa | 2012-01-31 23:16:39 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | return -1; |
| 388 | } |
Bruce Long | d096e48 | 2019-02-28 17:50:00 | [diff] [blame] | 389 | |
| 390 | FilePath GetUniquePath(const FilePath& path) { |
Greg Thompson | b4abcb4 | 2019-08-23 01:42:56 | [diff] [blame] | 391 | DCHECK(!path.empty()); |
| 392 | const int uniquifier = GetUniquePathNumber(path); |
| 393 | if (uniquifier > 0) |
| 394 | return path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", uniquifier)); |
| 395 | return uniquifier == 0 ? path : base::FilePath(); |
Bruce Long | d096e48 | 2019-02-28 17:50:00 | [diff] [blame] | 396 | } |
Victor Costan | b5a0a9700 | 2019-09-08 04:55:15 | [diff] [blame] | 397 | |
| 398 | namespace internal { |
| 399 | |
| 400 | bool PreReadFileSlow(const FilePath& file_path, int64_t max_bytes) { |
| 401 | DCHECK_GE(max_bytes, 0); |
| 402 | |
| 403 | File file(file_path, File::FLAG_OPEN | File::FLAG_READ | |
Alexei Svitkine | c417b2cd | 2021-10-21 14:22:40 | [diff] [blame] | 404 | File::FLAG_WIN_SEQUENTIAL_SCAN | |
| 405 | File::FLAG_WIN_SHARE_DELETE); |
Victor Costan | b5a0a9700 | 2019-09-08 04:55:15 | [diff] [blame] | 406 | if (!file.IsValid()) |
| 407 | return false; |
| 408 | |
| 409 | constexpr int kBufferSize = 1024 * 1024; |
| 410 | // Ensures the buffer is deallocated at function exit. |
| 411 | std::unique_ptr<char[]> buffer_deleter(new char[kBufferSize]); |
| 412 | char* const buffer = buffer_deleter.get(); |
| 413 | |
| 414 | while (max_bytes > 0) { |
| 415 | // The static_cast<int> is safe because kBufferSize is int, and both values |
| 416 | // are non-negative. So, the minimum is guaranteed to fit in int. |
| 417 | const int read_size = |
| 418 | static_cast<int>(std::min<int64_t>(max_bytes, kBufferSize)); |
| 419 | DCHECK_GE(read_size, 0); |
| 420 | DCHECK_LE(read_size, kBufferSize); |
| 421 | |
| 422 | const int read_bytes = file.ReadAtCurrentPos(buffer, read_size); |
| 423 | if (read_bytes < 0) |
| 424 | return false; |
| 425 | if (read_bytes == 0) |
| 426 | break; |
| 427 | |
| 428 | max_bytes -= read_bytes; |
| 429 | } |
| 430 | |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | } // namespace internal |
| 435 | |
brettw@chromium.org | a26f4ae | 2014-03-13 17:26:21 | [diff] [blame] | 436 | } // namespace base |