[go: nahoru, domu]

blob: 20b07381fe05c52fe7e0d61f1d7dcce251c60d62 [file] [log] [blame]
thestig@chromium.orgb2f28d22012-03-03 01:54:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
brettw@chromium.orge3177dd52014-08-13 20:22:145#include "base/files/file_util.h"
initial.commitd7cae122008-07-26 21:49:386
jeremy@chromium.orgc2c998c2009-01-27 19:08:397#if defined(OS_WIN)
8#include <io.h>
9#endif
mark@chromium.org836f1342008-10-01 17:40:1310#include <stdio.h>
11
initial.commitd7cae122008-07-26 21:49:3812#include <fstream>
kaliamoorthi@chromium.org0710fdd2014-02-18 16:31:5113#include <limits>
Victor Costanb5a0a97002019-09-08 04:55:1514#include <memory>
initial.commitd7cae122008-07-26 21:49:3815
Hans Wennborgc3cffa62020-04-27 10:09:1216#include "base/check_op.h"
brettw@chromium.org25a4c1c2013-06-08 04:53:3617#include "base/files/file_enumerator.h"
brettw@chromium.org57999812013-02-24 05:40:5218#include "base/files/file_path.h"
Greg Thompson3f7e20f42020-05-02 19:01:1119#include "base/posix/eintr_wrapper.h"
tfarina@chromium.orgeb62f7262013-03-30 14:29:0020#include "base/strings/string_piece.h"
avi@chromium.org251cd6e52013-06-11 13:36:3721#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
avi@chromium.orga4ea1f12013-06-07 18:37:0723#include "base/strings/utf_string_conversions.h"
Etienne Pierre-Doray1da58592018-09-21 14:47:2024#include "base/threading/scoped_blocking_call.h"
avi543540e2015-12-24 05:15:3225#include "build/build_config.h"
estade@chromium.orgb9e04f02008-11-27 04:03:5726
brettw@chromium.org0408c752013-06-22 22:15:4627namespace base {
brettw@chromium.org04af979a2013-02-16 04:12:2628
hidehiko8fc7c822015-06-09 02:50:5729#if !defined(OS_NACL_NONSFI)
Lei Zhange3aa0b9a2020-06-11 08:59:2330OnceCallback<void(const FilePath&)> GetDeleteFileCallback() {
Lei Zhang9382efe2020-07-25 20:52:1431 return BindOnce(IgnoreResult(&DeleteFile));
Lei Zhange3aa0b9a2020-06-11 08:59:2332}
33
Lei Zhang3190449f2020-06-12 05:14:0434OnceCallback<void(const FilePath&)> GetDeletePathRecursivelyCallback() {
Lei Zhang746ce472020-07-01 04:39:4535 return BindOnce(IgnoreResult(&DeletePathRecursively));
Lei Zhang3190449f2020-06-12 05:14:0436}
37
avi543540e2015-12-24 05:15:3238int64_t ComputeDirectorySize(const FilePath& root_path) {
39 int64_t running_size = 0;
brettw@chromium.org0408c752013-06-22 22:15:4640 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.org5553d5b2013-07-01 23:07:3646bool Move(const FilePath& from_path, const FilePath& to_path) {
47 if (from_path.ReferencesParent() || to_path.ReferencesParent())
48 return false;
brettw@chromium.orgf0ff2ad2013-07-09 17:42:2649 return internal::MoveUnsafe(from_path, to_path);
50}
51
Alexander Bolodurin53bfc89c22020-11-25 22:43:2952bool CopyFileContents(File& infile, File& outfile) {
Brian Geffon591e8f22021-04-08 18:33:2353#if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_ANDROID)
54 bool retry_slow = false;
55 bool res =
56 internal::CopyFileContentsWithSendfile(infile, outfile, retry_slow);
57 if (res || !retry_slow) {
58 return res;
59 }
60 // Any failures which allow retrying using read/write will not have modified
61 // either file offset or size.
62#endif
63
Alexander Bolodurin53bfc89c22020-11-25 22:43:2964 static constexpr size_t kBufferSize = 32768;
65 std::vector<char> buffer(kBufferSize);
66
67 for (;;) {
68 int bytes_read = infile.ReadAtCurrentPos(buffer.data(), buffer.size());
69 if (bytes_read < 0) {
70 return false;
71 }
72 if (bytes_read == 0) {
73 return true;
74 }
75 // Allow for partial writes
76 int bytes_written_per_read = 0;
77 do {
78 int bytes_written_partial = outfile.WriteAtCurrentPos(
79 &buffer[bytes_written_per_read], bytes_read - bytes_written_per_read);
80 if (bytes_written_partial < 0) {
81 return false;
82 }
83
84 bytes_written_per_read += bytes_written_partial;
85 } while (bytes_written_per_read < bytes_read);
86 }
87
88 NOTREACHED();
89 return false;
90}
91
evanm@google.com640517f2008-10-30 23:54:0492bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) {
initial.commitd7cae122008-07-26 21:49:3893 // We open the file in binary format even if they are text files because
94 // we are just comparing that bytes are exactly same in both files and not
95 // doing anything smart with text formatting.
jdoerrie983968a2019-01-29 12:54:4196#if defined(OS_WIN)
Jan Wilken Dörrie9bb441d2019-11-01 17:48:4097 std::ifstream file1(filename1.value().c_str(),
erikkay@google.com5af2edb92008-08-08 20:16:0898 std::ios::in | std::ios::binary);
Jan Wilken Dörrie9bb441d2019-11-01 17:48:4099 std::ifstream file2(filename2.value().c_str(),
erikkay@google.com5af2edb92008-08-08 20:16:08100 std::ios::in | std::ios::binary);
jdoerrie983968a2019-01-29 12:54:41101#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
102 std::ifstream file1(filename1.value(), std::ios::in | std::ios::binary);
103 std::ifstream file2(filename2.value(), std::ios::in | std::ios::binary);
104#endif // OS_WIN
estade@chromium.orgb9e04f02008-11-27 04:03:57105
initial.commitd7cae122008-07-26 21:49:38106 // Even if both files aren't openable (and thus, in some sense, "equal"),
107 // any unusable file yields a result of "false".
108 if (!file1.is_open() || !file2.is_open())
109 return false;
110
111 const int BUFFER_SIZE = 2056;
112 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
113 do {
114 file1.read(buffer1, BUFFER_SIZE);
115 file2.read(buffer2, BUFFER_SIZE);
116
mark@chromium.orgb81637c32009-06-26 21:17:24117 if ((file1.eof() != file2.eof()) ||
initial.commitd7cae122008-07-26 21:49:38118 (file1.gcount() != file2.gcount()) ||
pkasting9cf9b942014-10-01 22:18:43119 (memcmp(buffer1, buffer2, static_cast<size_t>(file1.gcount())))) {
initial.commitd7cae122008-07-26 21:49:38120 file1.close();
121 file2.close();
122 return false;
123 }
mark@chromium.orgb81637c32009-06-26 21:17:24124 } while (!file1.eof() || !file2.eof());
initial.commitd7cae122008-07-26 21:49:38125
126 file1.close();
127 file2.close();
128 return true;
129}
130
mark@chromium.orgb81637c32009-06-26 21:17:24131bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) {
jdoerrie983968a2019-01-29 12:54:41132#if defined(OS_WIN)
Jan Wilken Dörrie9bb441d2019-11-01 17:48:40133 std::ifstream file1(filename1.value().c_str(), std::ios::in);
134 std::ifstream file2(filename2.value().c_str(), std::ios::in);
jdoerrie983968a2019-01-29 12:54:41135#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
136 std::ifstream file1(filename1.value(), std::ios::in);
137 std::ifstream file2(filename2.value(), std::ios::in);
138#endif // OS_WIN
mark@chromium.orgb81637c32009-06-26 21:17:24139
140 // Even if both files aren't openable (and thus, in some sense, "equal"),
141 // any unusable file yields a result of "false".
142 if (!file1.is_open() || !file2.is_open())
143 return false;
144
145 do {
146 std::string line1, line2;
147 getline(file1, line1);
148 getline(file2, line2);
149
150 // Check for mismatched EOF states, or any error state.
151 if ((file1.eof() != file2.eof()) ||
152 file1.bad() || file2.bad()) {
153 return false;
154 }
155
156 // Trim all '\r' and '\n' characters from the end of the line.
157 std::string::size_type end1 = line1.find_last_not_of("\r\n");
158 if (end1 == std::string::npos)
159 line1.clear();
160 else if (end1 + 1 < line1.length())
161 line1.erase(end1 + 1);
162
163 std::string::size_type end2 = line2.find_last_not_of("\r\n");
164 if (end2 == std::string::npos)
165 line2.clear();
166 else if (end2 + 1 < line2.length())
167 line2.erase(end2 + 1);
168
169 if (line1 != line2)
170 return false;
171 } while (!file1.eof() || !file2.eof());
172
173 return true;
174}
hidehiko8fc7c822015-06-09 02:50:57175#endif // !defined(OS_NACL_NONSFI)
mark@chromium.orgb81637c32009-06-26 21:17:24176
Greg Thompson3f7e20f42020-05-02 19:01:11177bool ReadStreamToString(FILE* stream, std::string* contents) {
178 return ReadStreamToStringWithMaxSize(
179 stream, std::numeric_limits<size_t>::max(), contents);
180}
181
182bool ReadStreamToStringWithMaxSize(FILE* stream,
183 size_t max_size,
184 std::string* contents) {
kaliamoorthi@chromium.org0710fdd2014-02-18 16:31:51185 if (contents)
186 contents->clear();
initial.commitd7cae122008-07-26 21:49:38187
Greg Thompson3f7e20f42020-05-02 19:01:11188 // Seeking to the beginning is best-effort -- it is expected to fail for
189 // certain non-file stream (e.g., pipes).
190 HANDLE_EINTR(fseek(stream, 0, SEEK_SET));
191
192 // Many files have incorrect size (proc files etc). Hence, the file is read
193 // sequentially as opposed to a one-shot read, using file size as a hint for
194 // chunk size if available.
Mikhail Istomin1ede1552018-05-16 17:40:24195 constexpr int64_t kDefaultChunkSize = 1 << 16;
Greg Thompson3f7e20f42020-05-02 19:01:11196 int64_t chunk_size = kDefaultChunkSize - 1;
Mikhail Istomin1ede1552018-05-16 17:40:24197#if !defined(OS_NACL_NONSFI)
Greg Thompson3f7e20f42020-05-02 19:01:11198 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
199#if defined(OS_WIN)
200 BY_HANDLE_FILE_INFORMATION file_info = {};
201 if (::GetFileInformationByHandle(
202 reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(stream))),
203 &file_info)) {
204 LARGE_INTEGER size;
205 size.HighPart = file_info.nFileSizeHigh;
206 size.LowPart = file_info.nFileSizeLow;
207 if (size.QuadPart > 0)
208 chunk_size = size.QuadPart;
209 }
210#else // defined(OS_WIN)
211 stat_wrapper_t file_info = {};
212 if (!File::Fstat(fileno(stream), &file_info) && file_info.st_size > 0)
213 chunk_size = file_info.st_size;
214#endif // defined(OS_WIN)
Mikhail Istomin1ede1552018-05-16 17:40:24215 // We need to attempt to read at EOF for feof flag to be set so here we
216 // use |chunk_size| + 1.
217 chunk_size = std::min<uint64_t>(chunk_size, max_size) + 1;
Greg Thompson3f7e20f42020-05-02 19:01:11218#else // !defined(OS_NACL_NONSFI)
Mikhail Istomin1ede1552018-05-16 17:40:24219 chunk_size = kDefaultChunkSize;
220#endif // !defined(OS_NACL_NONSFI)
221 size_t bytes_read_this_pass;
222 size_t bytes_read_so_far = 0;
223 bool read_status = true;
224 std::string local_contents;
225 local_contents.resize(chunk_size);
kaliamoorthi@chromium.org0710fdd2014-02-18 16:31:51226
Mikhail Istomin1ede1552018-05-16 17:40:24227 while ((bytes_read_this_pass = fread(&local_contents[bytes_read_so_far], 1,
Greg Thompson3f7e20f42020-05-02 19:01:11228 chunk_size, stream)) > 0) {
Mikhail Istomin1ede1552018-05-16 17:40:24229 if ((max_size - bytes_read_so_far) < bytes_read_this_pass) {
230 // Read more than max_size bytes, bail out.
231 bytes_read_so_far = max_size;
kaliamoorthi@chromium.org0710fdd2014-02-18 16:31:51232 read_status = false;
233 break;
234 }
Mikhail Istomin1ede1552018-05-16 17:40:24235 // In case EOF was not reached, iterate again but revert to the default
236 // chunk size.
237 if (bytes_read_so_far == 0)
238 chunk_size = kDefaultChunkSize;
kaliamoorthi@chromium.org0710fdd2014-02-18 16:31:51239
Mikhail Istomin1ede1552018-05-16 17:40:24240 bytes_read_so_far += bytes_read_this_pass;
241 // Last fread syscall (after EOF) can be avoided via feof, which is just a
242 // flag check.
Greg Thompson3f7e20f42020-05-02 19:01:11243 if (feof(stream))
Mikhail Istomin1ede1552018-05-16 17:40:24244 break;
245 local_contents.resize(bytes_read_so_far + chunk_size);
initial.commitd7cae122008-07-26 21:49:38246 }
Greg Thompson3f7e20f42020-05-02 19:01:11247 read_status = read_status && !ferror(stream);
Mikhail Istomin1ede1552018-05-16 17:40:24248 if (contents) {
249 contents->swap(local_contents);
250 contents->resize(bytes_read_so_far);
251 }
initial.commitd7cae122008-07-26 21:49:38252
kaliamoorthi@chromium.org0710fdd2014-02-18 16:31:51253 return read_status;
254}
255
256bool ReadFileToString(const FilePath& path, std::string* contents) {
hashimoto6da2fef2016-02-24 03:39:58257 return ReadFileToStringWithMaxSize(path, contents,
258 std::numeric_limits<size_t>::max());
initial.commitd7cae122008-07-26 21:49:38259}
260
Greg Thompson3f7e20f42020-05-02 19:01:11261bool ReadFileToStringWithMaxSize(const FilePath& path,
262 std::string* contents,
263 size_t max_size) {
264 if (contents)
265 contents->clear();
266 if (path.ReferencesParent())
267 return false;
268 ScopedFILE file_stream(OpenFile(path, "rb"));
269 if (!file_stream)
270 return false;
271 return ReadStreamToStringWithMaxSize(file_stream.get(), max_size, contents);
272}
273
hidehiko8fc7c822015-06-09 02:50:57274#if !defined(OS_NACL_NONSFI)
brettw@chromium.orgfb4bcfa32013-12-02 18:55:49275bool IsDirectoryEmpty(const FilePath& dir_path) {
276 FileEnumerator files(dir_path, false,
277 FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
278 if (files.Next().empty())
279 return true;
280 return false;
281}
282
Greg Thompson3f7e20f42020-05-02 19:01:11283bool CreateTemporaryFile(FilePath* path) {
284 FilePath temp_dir;
285 return GetTempDir(&temp_dir) && CreateTemporaryFileInDir(temp_dir, path);
286}
287
Greg Thompsonf75f5fb2020-04-30 08:13:25288ScopedFILE CreateAndOpenTemporaryStream(FilePath* path) {
brettw@chromium.org03d9afc02013-12-03 17:55:52289 FilePath directory;
290 if (!GetTempDir(&directory))
Ivan Kotenkova16212a52017-11-08 12:37:33291 return nullptr;
brettw@chromium.org03d9afc02013-12-03 17:55:52292
Greg Thompsonf75f5fb2020-04-30 08:13:25293 return CreateAndOpenTemporaryStreamInDir(directory, path);
brettw@chromium.org03d9afc02013-12-03 17:55:52294}
295
brettw@chromium.org426d1c92013-12-03 20:08:54296bool CreateDirectory(const FilePath& full_path) {
Ivan Kotenkova16212a52017-11-08 12:37:33297 return CreateDirectoryAndGetError(full_path, nullptr);
brettw@chromium.org426d1c92013-12-03 20:08:54298}
299
avi543540e2015-12-24 05:15:32300bool GetFileSize(const FilePath& file_path, int64_t* file_size) {
rvargas@chromium.org54124ed02014-01-07 10:06:58301 File::Info info;
brettw@chromium.org9eae4e62013-12-04 20:56:49302 if (!GetFileInfo(file_path, &info))
brettw@chromium.org56285702013-12-04 18:22:49303 return false;
304 *file_size = info.size;
305 return true;
306}
307
brettw@chromium.orgc0d508162013-12-04 22:49:00308bool TouchFile(const FilePath& path,
309 const Time& last_accessed,
310 const Time& last_modified) {
rvargas@chromium.org54124ed02014-01-07 10:06:58311 int flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES;
brettw@chromium.orgc0d508162013-12-04 22:49:00312
313#if defined(OS_WIN)
314 // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory.
315 if (DirectoryExists(path))
rvargas@chromium.org54124ed02014-01-07 10:06:58316 flags |= File::FLAG_BACKUP_SEMANTICS;
Wez432ea8d892019-03-31 01:17:08317#elif defined(OS_FUCHSIA)
318 // On Fuchsia, we need O_RDONLY for directories, or O_WRONLY for files.
319 // TODO(https://crbug.com/947802): Find a cleaner workaround for this.
320 flags |= (DirectoryExists(path) ? File::FLAG_READ : File::FLAG_WRITE);
321#endif
brettw@chromium.orgc0d508162013-12-04 22:49:00322
rvargas@chromium.org54124ed02014-01-07 10:06:58323 File file(path, flags);
324 if (!file.IsValid())
325 return false;
brettw@chromium.orgc0d508162013-12-04 22:49:00326
rvargas@chromium.org54124ed02014-01-07 10:06:58327 return file.SetTimes(last_accessed, last_modified);
brettw@chromium.orgc0d508162013-12-04 22:49:00328}
hidehiko8fc7c822015-06-09 02:50:57329#endif // !defined(OS_NACL_NONSFI)
brettw@chromium.orgc0d508162013-12-04 22:49:00330
mark@chromium.org836f1342008-10-01 17:40:13331bool CloseFile(FILE* file) {
Ivan Kotenkova16212a52017-11-08 12:37:33332 if (file == nullptr)
sidchat@google.coma1a19502008-10-21 17:14:45333 return true;
mark@chromium.org836f1342008-10-01 17:40:13334 return fclose(file) == 0;
335}
336
hidehiko8fc7c822015-06-09 02:50:57337#if !defined(OS_NACL_NONSFI)
jeremy@chromium.orgc2c998c2009-01-27 19:08:39338bool TruncateFile(FILE* file) {
Ivan Kotenkova16212a52017-11-08 12:37:33339 if (file == nullptr)
jeremy@chromium.orgc2c998c2009-01-27 19:08:39340 return false;
341 long current_offset = ftell(file);
342 if (current_offset == -1)
343 return false;
344#if defined(OS_WIN)
345 int fd = _fileno(file);
346 if (_chsize(fd, current_offset) != 0)
347 return false;
348#else
349 int fd = fileno(file);
350 if (ftruncate(fd, current_offset) != 0)
351 return false;
352#endif
353 return true;
354}
355
Lei Zhangeebbef62020-05-05 20:16:05356bool WriteFile(const FilePath& filename, span<const uint8_t> data) {
357 int size = checked_cast<int>(data.size());
358 return WriteFile(filename, reinterpret_cast<const char*>(data.data()),
359 size) == size;
360}
361
362bool WriteFile(const FilePath& filename, StringPiece data) {
363 int size = checked_cast<int>(data.size());
364 return WriteFile(filename, data.data(), size) == size;
365}
366
Greg Thompsonb4abcb42019-08-23 01:42:56367int GetUniquePathNumber(const FilePath& path) {
368 DCHECK(!path.empty());
369 if (!PathExists(path))
Greg Thompson42373544a2019-08-14 10:11:10370 return 0;
371
372 std::string number;
373 for (int count = 1; count <= kMaxUniqueFiles; ++count) {
374 StringAppendF(&number, " (%d)", count);
Greg Thompsonb4abcb42019-08-23 01:42:56375 if (!PathExists(path.InsertBeforeExtensionASCII(number)))
Greg Thompson42373544a2019-08-14 10:11:10376 return count;
377 number.clear();
jam@chromium.orge285afa2012-01-31 23:16:39378 }
379
380 return -1;
381}
Bruce Longd096e482019-02-28 17:50:00382
383FilePath GetUniquePath(const FilePath& path) {
Greg Thompsonb4abcb42019-08-23 01:42:56384 DCHECK(!path.empty());
385 const int uniquifier = GetUniquePathNumber(path);
386 if (uniquifier > 0)
387 return path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", uniquifier));
388 return uniquifier == 0 ? path : base::FilePath();
Bruce Longd096e482019-02-28 17:50:00389}
Victor Costanb5a0a97002019-09-08 04:55:15390
391namespace internal {
392
393bool PreReadFileSlow(const FilePath& file_path, int64_t max_bytes) {
394 DCHECK_GE(max_bytes, 0);
395
396 File file(file_path, File::FLAG_OPEN | File::FLAG_READ |
397 File::FLAG_SEQUENTIAL_SCAN |
398 File::FLAG_SHARE_DELETE);
399 if (!file.IsValid())
400 return false;
401
402 constexpr int kBufferSize = 1024 * 1024;
403 // Ensures the buffer is deallocated at function exit.
404 std::unique_ptr<char[]> buffer_deleter(new char[kBufferSize]);
405 char* const buffer = buffer_deleter.get();
406
407 while (max_bytes > 0) {
408 // The static_cast<int> is safe because kBufferSize is int, and both values
409 // are non-negative. So, the minimum is guaranteed to fit in int.
410 const int read_size =
411 static_cast<int>(std::min<int64_t>(max_bytes, kBufferSize));
412 DCHECK_GE(read_size, 0);
413 DCHECK_LE(read_size, kBufferSize);
414
415 const int read_bytes = file.ReadAtCurrentPos(buffer, read_size);
416 if (read_bytes < 0)
417 return false;
418 if (read_bytes == 0)
419 break;
420
421 max_bytes -= read_bytes;
422 }
423
424 return true;
425}
426
427} // namespace internal
428
hidehiko8fc7c822015-06-09 02:50:57429#endif // !defined(OS_NACL_NONSFI)
jam@chromium.orge285afa2012-01-31 23:16:39430
brettw@chromium.orga26f4ae2014-03-13 17:26:21431} // namespace base