| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <io.h> | 8 #include <io.h> |
| 9 #endif | 9 #endif |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| 11 | 11 |
| 12 #include <fstream> | 12 #include <fstream> |
| 13 #include <limits> | 13 #include <limits> |
| 14 | 14 |
| 15 #include "base/files/file_enumerator.h" | 15 #include "base/files/file_enumerator.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/strings/string_piece.h" | 18 #include "base/strings/string_piece.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 | 22 |
| 23 namespace base { | 23 namespace base { |
| 24 | 24 |
| 25 #if !defined(OS_NACL_NONSFI) |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 // The maximum number of 'uniquified' files we will try to create. | 28 // 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 // 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 // so we create a new unique filename by appending " (nnn)" before the |
| 30 // extension, where 1 <= nnn <= kMaxUniqueFiles. | 31 // extension, where 1 <= nnn <= kMaxUniqueFiles. |
| 31 // Also used by code that cleans up said files. | 32 // Also used by code that cleans up said files. |
| 32 static const int kMaxUniqueFiles = 100; | 33 static const int kMaxUniqueFiles = 100; |
| 33 | 34 |
| 34 } // namespace | 35 } // namespace |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 line2.clear(); | 114 line2.clear(); |
| 114 else if (end2 + 1 < line2.length()) | 115 else if (end2 + 1 < line2.length()) |
| 115 line2.erase(end2 + 1); | 116 line2.erase(end2 + 1); |
| 116 | 117 |
| 117 if (line1 != line2) | 118 if (line1 != line2) |
| 118 return false; | 119 return false; |
| 119 } while (!file1.eof() || !file2.eof()); | 120 } while (!file1.eof() || !file2.eof()); |
| 120 | 121 |
| 121 return true; | 122 return true; |
| 122 } | 123 } |
| 124 #endif // !defined(OS_NACL_NONSFI) |
| 123 | 125 |
| 124 bool ReadFileToString(const FilePath& path, | 126 bool ReadFileToString(const FilePath& path, |
| 125 std::string* contents, | 127 std::string* contents, |
| 126 size_t max_size) { | 128 size_t max_size) { |
| 127 if (contents) | 129 if (contents) |
| 128 contents->clear(); | 130 contents->clear(); |
| 129 if (path.ReferencesParent()) | 131 if (path.ReferencesParent()) |
| 130 return false; | 132 return false; |
| 131 FILE* file = OpenFile(path, "rb"); | 133 FILE* file = OpenFile(path, "rb"); |
| 132 if (!file) { | 134 if (!file) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 155 read_status = read_status && !ferror(file); | 157 read_status = read_status && !ferror(file); |
| 156 CloseFile(file); | 158 CloseFile(file); |
| 157 | 159 |
| 158 return read_status; | 160 return read_status; |
| 159 } | 161 } |
| 160 | 162 |
| 161 bool ReadFileToString(const FilePath& path, std::string* contents) { | 163 bool ReadFileToString(const FilePath& path, std::string* contents) { |
| 162 return ReadFileToString(path, contents, std::numeric_limits<size_t>::max()); | 164 return ReadFileToString(path, contents, std::numeric_limits<size_t>::max()); |
| 163 } | 165 } |
| 164 | 166 |
| 167 #if !defined(OS_NACL_NONSFI) |
| 165 bool IsDirectoryEmpty(const FilePath& dir_path) { | 168 bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 166 FileEnumerator files(dir_path, false, | 169 FileEnumerator files(dir_path, false, |
| 167 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); | 170 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
| 168 if (files.Next().empty()) | 171 if (files.Next().empty()) |
| 169 return true; | 172 return true; |
| 170 return false; | 173 return false; |
| 171 } | 174 } |
| 172 | 175 |
| 173 FILE* CreateAndOpenTemporaryFile(FilePath* path) { | 176 FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
| 174 FilePath directory; | 177 FilePath directory; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 200 if (DirectoryExists(path)) | 203 if (DirectoryExists(path)) |
| 201 flags |= File::FLAG_BACKUP_SEMANTICS; | 204 flags |= File::FLAG_BACKUP_SEMANTICS; |
| 202 #endif // OS_WIN | 205 #endif // OS_WIN |
| 203 | 206 |
| 204 File file(path, flags); | 207 File file(path, flags); |
| 205 if (!file.IsValid()) | 208 if (!file.IsValid()) |
| 206 return false; | 209 return false; |
| 207 | 210 |
| 208 return file.SetTimes(last_accessed, last_modified); | 211 return file.SetTimes(last_accessed, last_modified); |
| 209 } | 212 } |
| 213 #endif // !defined(OS_NACL_NONSFI) |
| 210 | 214 |
| 211 bool CloseFile(FILE* file) { | 215 bool CloseFile(FILE* file) { |
| 212 if (file == NULL) | 216 if (file == NULL) |
| 213 return true; | 217 return true; |
| 214 return fclose(file) == 0; | 218 return fclose(file) == 0; |
| 215 } | 219 } |
| 216 | 220 |
| 221 #if !defined(OS_NACL_NONSFI) |
| 217 bool TruncateFile(FILE* file) { | 222 bool TruncateFile(FILE* file) { |
| 218 if (file == NULL) | 223 if (file == NULL) |
| 219 return false; | 224 return false; |
| 220 long current_offset = ftell(file); | 225 long current_offset = ftell(file); |
| 221 if (current_offset == -1) | 226 if (current_offset == -1) |
| 222 return false; | 227 return false; |
| 223 #if defined(OS_WIN) | 228 #if defined(OS_WIN) |
| 224 int fd = _fileno(file); | 229 int fd = _fileno(file); |
| 225 if (_chsize(fd, current_offset) != 0) | 230 if (_chsize(fd, current_offset) != 0) |
| 226 return false; | 231 return false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 244 for (int count = 1; count <= kMaxUniqueFiles; ++count) { | 249 for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
| 245 new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); | 250 new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); |
| 246 if (!PathExists(new_path) && | 251 if (!PathExists(new_path) && |
| 247 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { | 252 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 248 return count; | 253 return count; |
| 249 } | 254 } |
| 250 } | 255 } |
| 251 | 256 |
| 252 return -1; | 257 return -1; |
| 253 } | 258 } |
| 259 #endif // !defined(OS_NACL_NONSFI) |
| 254 | 260 |
| 255 } // namespace base | 261 } // namespace base |
| OLD | NEW |