1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #include "base/files/file_util.h"
6*635a8641SAndroid Build Coastguard Worker
7*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
8*635a8641SAndroid Build Coastguard Worker #include <io.h>
9*635a8641SAndroid Build Coastguard Worker #endif
10*635a8641SAndroid Build Coastguard Worker #include <stdio.h>
11*635a8641SAndroid Build Coastguard Worker
12*635a8641SAndroid Build Coastguard Worker #include <fstream>
13*635a8641SAndroid Build Coastguard Worker #include <limits>
14*635a8641SAndroid Build Coastguard Worker
15*635a8641SAndroid Build Coastguard Worker #include "base/files/file_enumerator.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/files/file_path.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_piece.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_util.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/strings/stringprintf.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/strings/utf_string_conversions.h"
22*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
23*635a8641SAndroid Build Coastguard Worker
24*635a8641SAndroid Build Coastguard Worker namespace base {
25*635a8641SAndroid Build Coastguard Worker
26*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL_NONSFI)
27*635a8641SAndroid Build Coastguard Worker namespace {
28*635a8641SAndroid Build Coastguard Worker
29*635a8641SAndroid Build Coastguard Worker // The maximum number of 'uniquified' files we will try to create.
30*635a8641SAndroid Build Coastguard Worker // This is used when the filename we're trying to download is already in use,
31*635a8641SAndroid Build Coastguard Worker // so we create a new unique filename by appending " (nnn)" before the
32*635a8641SAndroid Build Coastguard Worker // extension, where 1 <= nnn <= kMaxUniqueFiles.
33*635a8641SAndroid Build Coastguard Worker // Also used by code that cleans up said files.
34*635a8641SAndroid Build Coastguard Worker static const int kMaxUniqueFiles = 100;
35*635a8641SAndroid Build Coastguard Worker
36*635a8641SAndroid Build Coastguard Worker } // namespace
37*635a8641SAndroid Build Coastguard Worker
ComputeDirectorySize(const FilePath & root_path)38*635a8641SAndroid Build Coastguard Worker int64_t ComputeDirectorySize(const FilePath& root_path) {
39*635a8641SAndroid Build Coastguard Worker int64_t running_size = 0;
40*635a8641SAndroid Build Coastguard Worker FileEnumerator file_iter(root_path, true, FileEnumerator::FILES);
41*635a8641SAndroid Build Coastguard Worker while (!file_iter.Next().empty())
42*635a8641SAndroid Build Coastguard Worker running_size += file_iter.GetInfo().GetSize();
43*635a8641SAndroid Build Coastguard Worker return running_size;
44*635a8641SAndroid Build Coastguard Worker }
45*635a8641SAndroid Build Coastguard Worker
Move(const FilePath & from_path,const FilePath & to_path)46*635a8641SAndroid Build Coastguard Worker bool Move(const FilePath& from_path, const FilePath& to_path) {
47*635a8641SAndroid Build Coastguard Worker if (from_path.ReferencesParent() || to_path.ReferencesParent())
48*635a8641SAndroid Build Coastguard Worker return false;
49*635a8641SAndroid Build Coastguard Worker return internal::MoveUnsafe(from_path, to_path);
50*635a8641SAndroid Build Coastguard Worker }
51*635a8641SAndroid Build Coastguard Worker
ContentsEqual(const FilePath & filename1,const FilePath & filename2)52*635a8641SAndroid Build Coastguard Worker bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) {
53*635a8641SAndroid Build Coastguard Worker // We open the file in binary format even if they are text files because
54*635a8641SAndroid Build Coastguard Worker // we are just comparing that bytes are exactly same in both files and not
55*635a8641SAndroid Build Coastguard Worker // doing anything smart with text formatting.
56*635a8641SAndroid Build Coastguard Worker std::ifstream file1(filename1.value().c_str(),
57*635a8641SAndroid Build Coastguard Worker std::ios::in | std::ios::binary);
58*635a8641SAndroid Build Coastguard Worker std::ifstream file2(filename2.value().c_str(),
59*635a8641SAndroid Build Coastguard Worker std::ios::in | std::ios::binary);
60*635a8641SAndroid Build Coastguard Worker
61*635a8641SAndroid Build Coastguard Worker // Even if both files aren't openable (and thus, in some sense, "equal"),
62*635a8641SAndroid Build Coastguard Worker // any unusable file yields a result of "false".
63*635a8641SAndroid Build Coastguard Worker if (!file1.is_open() || !file2.is_open())
64*635a8641SAndroid Build Coastguard Worker return false;
65*635a8641SAndroid Build Coastguard Worker
66*635a8641SAndroid Build Coastguard Worker const int BUFFER_SIZE = 2056;
67*635a8641SAndroid Build Coastguard Worker char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE];
68*635a8641SAndroid Build Coastguard Worker do {
69*635a8641SAndroid Build Coastguard Worker file1.read(buffer1, BUFFER_SIZE);
70*635a8641SAndroid Build Coastguard Worker file2.read(buffer2, BUFFER_SIZE);
71*635a8641SAndroid Build Coastguard Worker
72*635a8641SAndroid Build Coastguard Worker if ((file1.eof() != file2.eof()) ||
73*635a8641SAndroid Build Coastguard Worker (file1.gcount() != file2.gcount()) ||
74*635a8641SAndroid Build Coastguard Worker (memcmp(buffer1, buffer2, static_cast<size_t>(file1.gcount())))) {
75*635a8641SAndroid Build Coastguard Worker file1.close();
76*635a8641SAndroid Build Coastguard Worker file2.close();
77*635a8641SAndroid Build Coastguard Worker return false;
78*635a8641SAndroid Build Coastguard Worker }
79*635a8641SAndroid Build Coastguard Worker } while (!file1.eof() || !file2.eof());
80*635a8641SAndroid Build Coastguard Worker
81*635a8641SAndroid Build Coastguard Worker file1.close();
82*635a8641SAndroid Build Coastguard Worker file2.close();
83*635a8641SAndroid Build Coastguard Worker return true;
84*635a8641SAndroid Build Coastguard Worker }
85*635a8641SAndroid Build Coastguard Worker
TextContentsEqual(const FilePath & filename1,const FilePath & filename2)86*635a8641SAndroid Build Coastguard Worker bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) {
87*635a8641SAndroid Build Coastguard Worker std::ifstream file1(filename1.value().c_str(), std::ios::in);
88*635a8641SAndroid Build Coastguard Worker std::ifstream file2(filename2.value().c_str(), std::ios::in);
89*635a8641SAndroid Build Coastguard Worker
90*635a8641SAndroid Build Coastguard Worker // Even if both files aren't openable (and thus, in some sense, "equal"),
91*635a8641SAndroid Build Coastguard Worker // any unusable file yields a result of "false".
92*635a8641SAndroid Build Coastguard Worker if (!file1.is_open() || !file2.is_open())
93*635a8641SAndroid Build Coastguard Worker return false;
94*635a8641SAndroid Build Coastguard Worker
95*635a8641SAndroid Build Coastguard Worker do {
96*635a8641SAndroid Build Coastguard Worker std::string line1, line2;
97*635a8641SAndroid Build Coastguard Worker getline(file1, line1);
98*635a8641SAndroid Build Coastguard Worker getline(file2, line2);
99*635a8641SAndroid Build Coastguard Worker
100*635a8641SAndroid Build Coastguard Worker // Check for mismatched EOF states, or any error state.
101*635a8641SAndroid Build Coastguard Worker if ((file1.eof() != file2.eof()) ||
102*635a8641SAndroid Build Coastguard Worker file1.bad() || file2.bad()) {
103*635a8641SAndroid Build Coastguard Worker return false;
104*635a8641SAndroid Build Coastguard Worker }
105*635a8641SAndroid Build Coastguard Worker
106*635a8641SAndroid Build Coastguard Worker // Trim all '\r' and '\n' characters from the end of the line.
107*635a8641SAndroid Build Coastguard Worker std::string::size_type end1 = line1.find_last_not_of("\r\n");
108*635a8641SAndroid Build Coastguard Worker if (end1 == std::string::npos)
109*635a8641SAndroid Build Coastguard Worker line1.clear();
110*635a8641SAndroid Build Coastguard Worker else if (end1 + 1 < line1.length())
111*635a8641SAndroid Build Coastguard Worker line1.erase(end1 + 1);
112*635a8641SAndroid Build Coastguard Worker
113*635a8641SAndroid Build Coastguard Worker std::string::size_type end2 = line2.find_last_not_of("\r\n");
114*635a8641SAndroid Build Coastguard Worker if (end2 == std::string::npos)
115*635a8641SAndroid Build Coastguard Worker line2.clear();
116*635a8641SAndroid Build Coastguard Worker else if (end2 + 1 < line2.length())
117*635a8641SAndroid Build Coastguard Worker line2.erase(end2 + 1);
118*635a8641SAndroid Build Coastguard Worker
119*635a8641SAndroid Build Coastguard Worker if (line1 != line2)
120*635a8641SAndroid Build Coastguard Worker return false;
121*635a8641SAndroid Build Coastguard Worker } while (!file1.eof() || !file2.eof());
122*635a8641SAndroid Build Coastguard Worker
123*635a8641SAndroid Build Coastguard Worker return true;
124*635a8641SAndroid Build Coastguard Worker }
125*635a8641SAndroid Build Coastguard Worker #endif // !defined(OS_NACL_NONSFI)
126*635a8641SAndroid Build Coastguard Worker
ReadFileToStringWithMaxSize(const FilePath & path,std::string * contents,size_t max_size)127*635a8641SAndroid Build Coastguard Worker bool ReadFileToStringWithMaxSize(const FilePath& path,
128*635a8641SAndroid Build Coastguard Worker std::string* contents,
129*635a8641SAndroid Build Coastguard Worker size_t max_size) {
130*635a8641SAndroid Build Coastguard Worker if (contents)
131*635a8641SAndroid Build Coastguard Worker contents->clear();
132*635a8641SAndroid Build Coastguard Worker if (path.ReferencesParent())
133*635a8641SAndroid Build Coastguard Worker return false;
134*635a8641SAndroid Build Coastguard Worker FILE* file = OpenFile(path, "rb");
135*635a8641SAndroid Build Coastguard Worker if (!file) {
136*635a8641SAndroid Build Coastguard Worker return false;
137*635a8641SAndroid Build Coastguard Worker }
138*635a8641SAndroid Build Coastguard Worker
139*635a8641SAndroid Build Coastguard Worker // Many files supplied in |path| have incorrect size (proc files etc).
140*635a8641SAndroid Build Coastguard Worker // Hence, the file is read sequentially as opposed to a one-shot read, using
141*635a8641SAndroid Build Coastguard Worker // file size as a hint for chunk size if available.
142*635a8641SAndroid Build Coastguard Worker constexpr int64_t kDefaultChunkSize = 1 << 16;
143*635a8641SAndroid Build Coastguard Worker int64_t chunk_size;
144*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL_NONSFI)
145*635a8641SAndroid Build Coastguard Worker if (!GetFileSize(path, &chunk_size) || chunk_size <= 0)
146*635a8641SAndroid Build Coastguard Worker chunk_size = kDefaultChunkSize - 1;
147*635a8641SAndroid Build Coastguard Worker // We need to attempt to read at EOF for feof flag to be set so here we
148*635a8641SAndroid Build Coastguard Worker // use |chunk_size| + 1.
149*635a8641SAndroid Build Coastguard Worker chunk_size = std::min<uint64_t>(chunk_size, max_size) + 1;
150*635a8641SAndroid Build Coastguard Worker #else
151*635a8641SAndroid Build Coastguard Worker chunk_size = kDefaultChunkSize;
152*635a8641SAndroid Build Coastguard Worker #endif // !defined(OS_NACL_NONSFI)
153*635a8641SAndroid Build Coastguard Worker size_t bytes_read_this_pass;
154*635a8641SAndroid Build Coastguard Worker size_t bytes_read_so_far = 0;
155*635a8641SAndroid Build Coastguard Worker bool read_status = true;
156*635a8641SAndroid Build Coastguard Worker std::string local_contents;
157*635a8641SAndroid Build Coastguard Worker local_contents.resize(chunk_size);
158*635a8641SAndroid Build Coastguard Worker
159*635a8641SAndroid Build Coastguard Worker while ((bytes_read_this_pass = fread(&local_contents[bytes_read_so_far], 1,
160*635a8641SAndroid Build Coastguard Worker chunk_size, file)) > 0) {
161*635a8641SAndroid Build Coastguard Worker if ((max_size - bytes_read_so_far) < bytes_read_this_pass) {
162*635a8641SAndroid Build Coastguard Worker // Read more than max_size bytes, bail out.
163*635a8641SAndroid Build Coastguard Worker bytes_read_so_far = max_size;
164*635a8641SAndroid Build Coastguard Worker read_status = false;
165*635a8641SAndroid Build Coastguard Worker break;
166*635a8641SAndroid Build Coastguard Worker }
167*635a8641SAndroid Build Coastguard Worker // In case EOF was not reached, iterate again but revert to the default
168*635a8641SAndroid Build Coastguard Worker // chunk size.
169*635a8641SAndroid Build Coastguard Worker if (bytes_read_so_far == 0)
170*635a8641SAndroid Build Coastguard Worker chunk_size = kDefaultChunkSize;
171*635a8641SAndroid Build Coastguard Worker
172*635a8641SAndroid Build Coastguard Worker bytes_read_so_far += bytes_read_this_pass;
173*635a8641SAndroid Build Coastguard Worker // Last fread syscall (after EOF) can be avoided via feof, which is just a
174*635a8641SAndroid Build Coastguard Worker // flag check.
175*635a8641SAndroid Build Coastguard Worker if (feof(file))
176*635a8641SAndroid Build Coastguard Worker break;
177*635a8641SAndroid Build Coastguard Worker local_contents.resize(bytes_read_so_far + chunk_size);
178*635a8641SAndroid Build Coastguard Worker }
179*635a8641SAndroid Build Coastguard Worker read_status = read_status && !ferror(file);
180*635a8641SAndroid Build Coastguard Worker CloseFile(file);
181*635a8641SAndroid Build Coastguard Worker if (contents) {
182*635a8641SAndroid Build Coastguard Worker contents->swap(local_contents);
183*635a8641SAndroid Build Coastguard Worker contents->resize(bytes_read_so_far);
184*635a8641SAndroid Build Coastguard Worker }
185*635a8641SAndroid Build Coastguard Worker
186*635a8641SAndroid Build Coastguard Worker return read_status;
187*635a8641SAndroid Build Coastguard Worker }
188*635a8641SAndroid Build Coastguard Worker
ReadFileToString(const FilePath & path,std::string * contents)189*635a8641SAndroid Build Coastguard Worker bool ReadFileToString(const FilePath& path, std::string* contents) {
190*635a8641SAndroid Build Coastguard Worker return ReadFileToStringWithMaxSize(path, contents,
191*635a8641SAndroid Build Coastguard Worker std::numeric_limits<size_t>::max());
192*635a8641SAndroid Build Coastguard Worker }
193*635a8641SAndroid Build Coastguard Worker
194*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL_NONSFI)
IsDirectoryEmpty(const FilePath & dir_path)195*635a8641SAndroid Build Coastguard Worker bool IsDirectoryEmpty(const FilePath& dir_path) {
196*635a8641SAndroid Build Coastguard Worker FileEnumerator files(dir_path, false,
197*635a8641SAndroid Build Coastguard Worker FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
198*635a8641SAndroid Build Coastguard Worker if (files.Next().empty())
199*635a8641SAndroid Build Coastguard Worker return true;
200*635a8641SAndroid Build Coastguard Worker return false;
201*635a8641SAndroid Build Coastguard Worker }
202*635a8641SAndroid Build Coastguard Worker
CreateAndOpenTemporaryFile(FilePath * path)203*635a8641SAndroid Build Coastguard Worker FILE* CreateAndOpenTemporaryFile(FilePath* path) {
204*635a8641SAndroid Build Coastguard Worker FilePath directory;
205*635a8641SAndroid Build Coastguard Worker if (!GetTempDir(&directory))
206*635a8641SAndroid Build Coastguard Worker return nullptr;
207*635a8641SAndroid Build Coastguard Worker
208*635a8641SAndroid Build Coastguard Worker return CreateAndOpenTemporaryFileInDir(directory, path);
209*635a8641SAndroid Build Coastguard Worker }
210*635a8641SAndroid Build Coastguard Worker
CreateDirectory(const FilePath & full_path)211*635a8641SAndroid Build Coastguard Worker bool CreateDirectory(const FilePath& full_path) {
212*635a8641SAndroid Build Coastguard Worker return CreateDirectoryAndGetError(full_path, nullptr);
213*635a8641SAndroid Build Coastguard Worker }
214*635a8641SAndroid Build Coastguard Worker
GetFileSize(const FilePath & file_path,int64_t * file_size)215*635a8641SAndroid Build Coastguard Worker bool GetFileSize(const FilePath& file_path, int64_t* file_size) {
216*635a8641SAndroid Build Coastguard Worker File::Info info;
217*635a8641SAndroid Build Coastguard Worker if (!GetFileInfo(file_path, &info))
218*635a8641SAndroid Build Coastguard Worker return false;
219*635a8641SAndroid Build Coastguard Worker *file_size = info.size;
220*635a8641SAndroid Build Coastguard Worker return true;
221*635a8641SAndroid Build Coastguard Worker }
222*635a8641SAndroid Build Coastguard Worker
TouchFile(const FilePath & path,const Time & last_accessed,const Time & last_modified)223*635a8641SAndroid Build Coastguard Worker bool TouchFile(const FilePath& path,
224*635a8641SAndroid Build Coastguard Worker const Time& last_accessed,
225*635a8641SAndroid Build Coastguard Worker const Time& last_modified) {
226*635a8641SAndroid Build Coastguard Worker int flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES;
227*635a8641SAndroid Build Coastguard Worker
228*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
229*635a8641SAndroid Build Coastguard Worker // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory.
230*635a8641SAndroid Build Coastguard Worker if (DirectoryExists(path))
231*635a8641SAndroid Build Coastguard Worker flags |= File::FLAG_BACKUP_SEMANTICS;
232*635a8641SAndroid Build Coastguard Worker #endif // OS_WIN
233*635a8641SAndroid Build Coastguard Worker
234*635a8641SAndroid Build Coastguard Worker File file(path, flags);
235*635a8641SAndroid Build Coastguard Worker if (!file.IsValid())
236*635a8641SAndroid Build Coastguard Worker return false;
237*635a8641SAndroid Build Coastguard Worker
238*635a8641SAndroid Build Coastguard Worker return file.SetTimes(last_accessed, last_modified);
239*635a8641SAndroid Build Coastguard Worker }
240*635a8641SAndroid Build Coastguard Worker #endif // !defined(OS_NACL_NONSFI)
241*635a8641SAndroid Build Coastguard Worker
CloseFile(FILE * file)242*635a8641SAndroid Build Coastguard Worker bool CloseFile(FILE* file) {
243*635a8641SAndroid Build Coastguard Worker if (file == nullptr)
244*635a8641SAndroid Build Coastguard Worker return true;
245*635a8641SAndroid Build Coastguard Worker return fclose(file) == 0;
246*635a8641SAndroid Build Coastguard Worker }
247*635a8641SAndroid Build Coastguard Worker
248*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL_NONSFI)
TruncateFile(FILE * file)249*635a8641SAndroid Build Coastguard Worker bool TruncateFile(FILE* file) {
250*635a8641SAndroid Build Coastguard Worker if (file == nullptr)
251*635a8641SAndroid Build Coastguard Worker return false;
252*635a8641SAndroid Build Coastguard Worker long current_offset = ftell(file);
253*635a8641SAndroid Build Coastguard Worker if (current_offset == -1)
254*635a8641SAndroid Build Coastguard Worker return false;
255*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
256*635a8641SAndroid Build Coastguard Worker int fd = _fileno(file);
257*635a8641SAndroid Build Coastguard Worker if (_chsize(fd, current_offset) != 0)
258*635a8641SAndroid Build Coastguard Worker return false;
259*635a8641SAndroid Build Coastguard Worker #else
260*635a8641SAndroid Build Coastguard Worker int fd = fileno(file);
261*635a8641SAndroid Build Coastguard Worker if (ftruncate(fd, current_offset) != 0)
262*635a8641SAndroid Build Coastguard Worker return false;
263*635a8641SAndroid Build Coastguard Worker #endif
264*635a8641SAndroid Build Coastguard Worker return true;
265*635a8641SAndroid Build Coastguard Worker }
266*635a8641SAndroid Build Coastguard Worker
GetUniquePathNumber(const FilePath & path,const FilePath::StringType & suffix)267*635a8641SAndroid Build Coastguard Worker int GetUniquePathNumber(const FilePath& path,
268*635a8641SAndroid Build Coastguard Worker const FilePath::StringType& suffix) {
269*635a8641SAndroid Build Coastguard Worker bool have_suffix = !suffix.empty();
270*635a8641SAndroid Build Coastguard Worker if (!PathExists(path) &&
271*635a8641SAndroid Build Coastguard Worker (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) {
272*635a8641SAndroid Build Coastguard Worker return 0;
273*635a8641SAndroid Build Coastguard Worker }
274*635a8641SAndroid Build Coastguard Worker
275*635a8641SAndroid Build Coastguard Worker FilePath new_path;
276*635a8641SAndroid Build Coastguard Worker for (int count = 1; count <= kMaxUniqueFiles; ++count) {
277*635a8641SAndroid Build Coastguard Worker new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count));
278*635a8641SAndroid Build Coastguard Worker if (!PathExists(new_path) &&
279*635a8641SAndroid Build Coastguard Worker (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
280*635a8641SAndroid Build Coastguard Worker return count;
281*635a8641SAndroid Build Coastguard Worker }
282*635a8641SAndroid Build Coastguard Worker }
283*635a8641SAndroid Build Coastguard Worker
284*635a8641SAndroid Build Coastguard Worker return -1;
285*635a8641SAndroid Build Coastguard Worker }
286*635a8641SAndroid Build Coastguard Worker #endif // !defined(OS_NACL_NONSFI)
287*635a8641SAndroid Build Coastguard Worker
288*635a8641SAndroid Build Coastguard Worker } // namespace base
289