blob: abc755785d591cd816562aaac5901ecae1c4b9af [file] [log] [blame]
thestig@chromium.orgf1a9ce12012-03-03 10:54:35 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include "base/file_util.h"
6
jeremy@chromium.orga5f404d2009-01-28 04:08:39 +09007#if defined(OS_WIN)
8#include <io.h>
9#endif
mark@chromium.orgd1bafc62008-10-02 02:40:13 +090010#include <stdio.h>
11
initial.commit3f4a7322008-07-27 06:49:38 +090012#include <fstream>
initial.commit3f4a7322008-07-27 06:49:38 +090013
evanm@google.com874d1672008-10-31 08:54:04 +090014#include "base/file_path.h"
initial.commit3f4a7322008-07-27 06:49:38 +090015#include "base/logging.h"
jam@chromium.orgc693fe32012-02-01 08:16:39 +090016#include "base/stringprintf.h"
estade@chromium.org97e37822008-11-27 13:03:57 +090017#include "base/string_piece.h"
brettw@chromium.org50c94652009-10-07 11:10:20 +090018#include "base/string_util.h"
19#include "base/utf_string_conversions.h"
estade@chromium.org97e37822008-11-27 13:03:57 +090020
estade@chromium.org63343202008-12-05 05:46:06 +090021namespace {
22
23const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.');
24
jam@chromium.orgc693fe32012-02-01 08:16:39 +090025// The maximum number of 'uniquified' files we will try to create.
26// This is used when the filename we're trying to download is already in use,
27// so we create a new unique filename by appending " (nnn)" before the
28// extension, where 1 <= nnn <= kMaxUniqueFiles.
29// Also used by code that cleans up said files.
30static const int kMaxUniqueFiles = 100;
31
willchan@chromium.org2f0b26b2009-04-25 02:44:39 +090032} // namespace
estade@chromium.org63343202008-12-05 05:46:06 +090033
initial.commit3f4a7322008-07-27 06:49:38 +090034namespace file_util {
35
aa@chromium.org8dac1e42012-02-05 12:22:38 +090036bool g_bug108724_debug = false;
37
estade@chromium.org97e37822008-11-27 13:03:57 +090038bool EndsWithSeparator(const FilePath& path) {
39 FilePath::StringType value = path.value();
40 if (value.empty())
41 return false;
42
43 return FilePath::IsSeparator(value[value.size() - 1]);
initial.commit3f4a7322008-07-27 06:49:38 +090044}
45
estade@chromium.orgd778b8f2008-11-26 07:04:37 +090046bool EnsureEndsWithSeparator(FilePath* path) {
47 if (!DirectoryExists(*path))
48 return false;
49
50 if (EndsWithSeparator(*path))
51 return true;
52
53 FilePath::StringType& path_str =
54 const_cast<FilePath::StringType&>(path->value());
55 path_str.append(&FilePath::kSeparators[0], 1);
56
57 return true;
58}
59
estade@chromium.org63343202008-12-05 05:46:06 +090060void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) {
61 FilePath::StringType& value =
62 const_cast<FilePath::StringType&>(path->value());
63
64 const FilePath::StringType::size_type last_dot =
65 value.rfind(kExtensionSeparator);
66 const FilePath::StringType::size_type last_separator =
67 value.find_last_of(FilePath::StringType(FilePath::kSeparators));
68
69 if (last_dot == FilePath::StringType::npos ||
70 (last_separator != std::wstring::npos && last_dot < last_separator)) {
71 // The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo".
72 // We should just append the suffix to the entire path.
73 value.append(suffix);
74 return;
75 }
76
77 value.insert(last_dot, suffix);
78}
79
evanm@google.com874d1672008-10-31 08:54:04 +090080bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) {
initial.commit3f4a7322008-07-27 06:49:38 +090081 // We open the file in binary format even if they are text files because
82 // we are just comparing that bytes are exactly same in both files and not
83 // doing anything smart with text formatting.
evanm@google.com874d1672008-10-31 08:54:04 +090084 std::ifstream file1(filename1.value().c_str(),
erikkay@google.com9fc57d02008-08-09 05:16:08 +090085 std::ios::in | std::ios::binary);
evanm@google.com874d1672008-10-31 08:54:04 +090086 std::ifstream file2(filename2.value().c_str(),
erikkay@google.com9fc57d02008-08-09 05:16:08 +090087 std::ios::in | std::ios::binary);
estade@chromium.org97e37822008-11-27 13:03:57 +090088
initial.commit3f4a7322008-07-27 06:49:38 +090089 // Even if both files aren't openable (and thus, in some sense, "equal"),
90 // any unusable file yields a result of "false".
91 if (!file1.is_open() || !file2.is_open())
92 return false;
93
94 const int BUFFER_SIZE = 2056;
95 char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
96 do {
97 file1.read(buffer1, BUFFER_SIZE);
98 file2.read(buffer2, BUFFER_SIZE);
99
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900100 if ((file1.eof() != file2.eof()) ||
initial.commit3f4a7322008-07-27 06:49:38 +0900101 (file1.gcount() != file2.gcount()) ||
102 (memcmp(buffer1, buffer2, file1.gcount()))) {
103 file1.close();
104 file2.close();
105 return false;
106 }
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900107 } while (!file1.eof() || !file2.eof());
initial.commit3f4a7322008-07-27 06:49:38 +0900108
109 file1.close();
110 file2.close();
111 return true;
112}
113
mark@chromium.org95c9ec92009-06-27 06:17:24 +0900114bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) {
115 std::ifstream file1(filename1.value().c_str(), std::ios::in);
116 std::ifstream file2(filename2.value().c_str(), std::ios::in);
117
118 // Even if both files aren't openable (and thus, in some sense, "equal"),
119 // any unusable file yields a result of "false".
120 if (!file1.is_open() || !file2.is_open())
121 return false;
122
123 do {
124 std::string line1, line2;
125 getline(file1, line1);
126 getline(file2, line2);
127
128 // Check for mismatched EOF states, or any error state.
129 if ((file1.eof() != file2.eof()) ||
130 file1.bad() || file2.bad()) {
131 return false;
132 }
133
134 // Trim all '\r' and '\n' characters from the end of the line.
135 std::string::size_type end1 = line1.find_last_not_of("\r\n");
136 if (end1 == std::string::npos)
137 line1.clear();
138 else if (end1 + 1 < line1.length())
139 line1.erase(end1 + 1);
140
141 std::string::size_type end2 = line2.find_last_not_of("\r\n");
142 if (end2 == std::string::npos)
143 line2.clear();
144 else if (end2 + 1 < line2.length())
145 line2.erase(end2 + 1);
146
147 if (line1 != line2)
148 return false;
149 } while (!file1.eof() || !file2.eof());
150
151 return true;
152}
153
erikkay@google.com52460fb2009-01-28 09:22:46 +0900154bool ReadFileToString(const FilePath& path, std::string* contents) {
mark@chromium.orgd1bafc62008-10-02 02:40:13 +0900155 FILE* file = OpenFile(path, "rb");
156 if (!file) {
initial.commit3f4a7322008-07-27 06:49:38 +0900157 return false;
mark@chromium.orgd1bafc62008-10-02 02:40:13 +0900158 }
initial.commit3f4a7322008-07-27 06:49:38 +0900159
160 char buf[1 << 16];
161 size_t len;
162 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
evan@chromium.orga75b2e82010-05-19 20:07:55 +0900163 if (contents)
164 contents->append(buf, len);
initial.commit3f4a7322008-07-27 06:49:38 +0900165 }
mark@chromium.orgd1bafc62008-10-02 02:40:13 +0900166 CloseFile(file);
initial.commit3f4a7322008-07-27 06:49:38 +0900167
168 return true;
169}
170
tfarina@chromium.org34828222010-05-26 10:40:12 +0900171bool IsDirectoryEmpty(const FilePath& dir_path) {
172 FileEnumerator files(dir_path, false,
haruki@chromium.org0e1a70b2012-08-12 10:57:23 +0900173 FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
tfarina@chromium.org34828222010-05-26 10:40:12 +0900174 if (files.Next().value().empty())
175 return true;
176 return false;
177}
178
phajdan.jr@chromium.org8139fe12009-04-28 15:50:36 +0900179FILE* CreateAndOpenTemporaryFile(FilePath* path) {
180 FilePath directory;
181 if (!GetTempDir(&directory))
evan@chromium.org2abe0b42010-06-11 07:56:23 +0900182 return NULL;
phajdan.jr@chromium.org8139fe12009-04-28 15:50:36 +0900183
184 return CreateAndOpenTemporaryFileInDir(directory, path);
185}
186
dkegel@google.com44982682008-11-05 06:00:46 +0900187bool GetFileSize(const FilePath& file_path, int64* file_size) {
dumi@chromium.org97ae2612010-09-03 11:28:37 +0900188 base::PlatformFileInfo info;
darin@google.com7f479f22008-09-26 10:04:08 +0900189 if (!GetFileInfo(file_path, &info))
190 return false;
191 *file_size = info.size;
192 return true;
193}
194
estade@chromium.orga99d0e12010-02-12 08:27:47 +0900195bool IsDot(const FilePath& path) {
196 return FILE_PATH_LITERAL(".") == path.BaseName().value();
197}
198
199bool IsDotDot(const FilePath& path) {
200 return FILE_PATH_LITERAL("..") == path.BaseName().value();
201}
202
dumi@chromium.orgc941a182010-09-24 08:28:22 +0900203bool TouchFile(const FilePath& path,
204 const base::Time& last_accessed,
205 const base::Time& last_modified) {
206 base::PlatformFile file =
207 base::CreatePlatformFile(path,
208 base::PLATFORM_FILE_OPEN |
209 base::PLATFORM_FILE_WRITE_ATTRIBUTES,
210 NULL, NULL);
211 if (file != base::kInvalidPlatformFileValue) {
212 bool result = base::TouchPlatformFile(file, last_accessed, last_modified);
213 base::ClosePlatformFile(file);
214 return result;
215 }
216
217 return false;
218}
219
220bool SetLastModifiedTime(const FilePath& path,
221 const base::Time& last_modified) {
222 return TouchFile(path, last_modified, last_modified);
223}
224
mark@chromium.orgd1bafc62008-10-02 02:40:13 +0900225bool CloseFile(FILE* file) {
sidchat@google.comd3b26432008-10-22 02:14:45 +0900226 if (file == NULL)
227 return true;
mark@chromium.orgd1bafc62008-10-02 02:40:13 +0900228 return fclose(file) == 0;
229}
230
jeremy@chromium.orga5f404d2009-01-28 04:08:39 +0900231bool TruncateFile(FILE* file) {
232 if (file == NULL)
233 return false;
234 long current_offset = ftell(file);
235 if (current_offset == -1)
236 return false;
237#if defined(OS_WIN)
238 int fd = _fileno(file);
239 if (_chsize(fd, current_offset) != 0)
240 return false;
241#else
242 int fd = fileno(file);
243 if (ftruncate(fd, current_offset) != 0)
244 return false;
245#endif
246 return true;
247}
248
jam@chromium.orgc693fe32012-02-01 08:16:39 +0900249int GetUniquePathNumber(
250 const FilePath& path,
251 const FilePath::StringType& suffix) {
252 bool have_suffix = !suffix.empty();
253 if (!PathExists(path) &&
254 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) {
255 return 0;
256 }
257
258 FilePath new_path;
259 for (int count = 1; count <= kMaxUniqueFiles; ++count) {
260 new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count));
261 if (!PathExists(new_path) &&
262 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
263 return count;
264 }
265 }
266
267 return -1;
268}
269
aa@chromium.orga4dbdf22009-01-10 07:14:27 +0900270bool ContainsPath(const FilePath &parent, const FilePath& child) {
271 FilePath abs_parent = FilePath(parent);
272 FilePath abs_child = FilePath(child);
273
274 if (!file_util::AbsolutePath(&abs_parent) ||
275 !file_util::AbsolutePath(&abs_child))
276 return false;
277
278#if defined(OS_WIN)
279 // file_util::AbsolutePath() does not flatten case on Windows, so we must do
280 // a case-insensitive compare.
281 if (!StartsWith(abs_child.value(), abs_parent.value(), false))
282#else
283 if (!StartsWithASCII(abs_child.value(), abs_parent.value(), true))
284#endif
285 return false;
286
287 // file_util::AbsolutePath() normalizes '/' to '\' on Windows, so we only need
288 // to check kSeparators[0].
289 if (abs_child.value().length() <= abs_parent.value().length() ||
290 abs_child.value()[abs_parent.value().length()] !=
291 FilePath::kSeparators[0])
292 return false;
293
294 return true;
295}
296
cpu@chromium.org83f07be2010-03-25 06:56:26 +0900297int64 ComputeDirectorySize(const FilePath& root_path) {
298 int64 running_size = 0;
299 FileEnumerator file_iter(root_path, true, FileEnumerator::FILES);
300 for (FilePath current = file_iter.Next(); !current.empty();
301 current = file_iter.Next()) {
302 FileEnumerator::FindInfo info;
303 file_iter.GetFindInfo(&info);
304#if defined(OS_WIN)
305 LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh };
306 running_size += li.QuadPart;
307#else
308 running_size += info.stat.st_size;
309#endif
310 }
311 return running_size;
312}
313
rvargas@google.comaa24e112010-06-12 07:53:43 +0900314int64 ComputeFilesSize(const FilePath& directory,
315 const FilePath::StringType& pattern) {
316 int64 running_size = 0;
317 FileEnumerator file_iter(directory, false, FileEnumerator::FILES, pattern);
318 for (FilePath current = file_iter.Next(); !current.empty();
319 current = file_iter.Next()) {
320 FileEnumerator::FindInfo info;
321 file_iter.GetFindInfo(&info);
322#if defined(OS_WIN)
323 LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh };
324 running_size += li.QuadPart;
325#else
326 running_size += info.stat.st_size;
327#endif
328 }
329 return running_size;
330}
331
estade@chromium.org2c233532008-12-13 08:43:03 +0900332///////////////////////////////////////////////
333// MemoryMappedFile
334
335MemoryMappedFile::~MemoryMappedFile() {
336 CloseHandles();
337}
338
erg@google.com37c078e2011-01-11 09:50:59 +0900339bool MemoryMappedFile::Initialize(const FilePath& file_name) {
340 if (IsValid())
341 return false;
342
343 if (!MapFileToMemory(file_name)) {
344 CloseHandles();
345 return false;
346 }
347
348 return true;
349}
350
estade@chromium.org3ac32062009-11-17 07:55:17 +0900351bool MemoryMappedFile::Initialize(base::PlatformFile file) {
352 if (IsValid())
353 return false;
354
355 file_ = file;
356
357 if (!MapFileToMemoryInternal()) {
358 CloseHandles();
359 return false;
360 }
361
362 return true;
363}
364
tommi@chromium.org39209952011-02-24 06:42:48 +0900365bool MemoryMappedFile::IsValid() const {
erg@google.com37c078e2011-01-11 09:50:59 +0900366 return data_ != NULL;
estade@chromium.org2c233532008-12-13 08:43:03 +0900367}
368
estade@chromium.org3ac32062009-11-17 07:55:17 +0900369bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) {
dumi@chromium.org50f197d2010-09-01 04:30:27 +0900370 file_ = base::CreatePlatformFile(
371 file_name, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
372 NULL, NULL);
estade@chromium.org3ac32062009-11-17 07:55:17 +0900373
374 if (file_ == base::kInvalidPlatformFileValue) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +0900375 DLOG(ERROR) << "Couldn't open " << file_name.value();
estade@chromium.org3ac32062009-11-17 07:55:17 +0900376 return false;
377 }
378
379 return MapFileToMemoryInternal();
380}
381
yuzo@chromium.org2da0f822009-06-09 14:57:38 +0900382///////////////////////////////////////////////
383// FileEnumerator
384//
385// Note: the main logic is in file_util_<platform>.cc
386
387bool FileEnumerator::ShouldSkip(const FilePath& path) {
388 FilePath::StringType basename = path.BaseName().value();
389 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_));
390}
391
initial.commit3f4a7322008-07-27 06:49:38 +0900392} // namespace