| 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/file_util.h" | 5 #include "base/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> |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 if (line1 != line2) | 122 if (line1 != line2) |
| 123 return false; | 123 return false; |
| 124 } while (!file1.eof() || !file2.eof()); | 124 } while (!file1.eof() || !file2.eof()); |
| 125 | 125 |
| 126 return true; | 126 return true; |
| 127 } | 127 } |
| 128 | 128 |
| 129 bool ReadFileToString(const FilePath& path, std::string* contents) { | 129 bool ReadFileToString(const FilePath& path, std::string* contents) { |
| 130 if (path.ReferencesParent()) | 130 if (path.ReferencesParent()) |
| 131 return false; | 131 return false; |
| 132 FILE* file = file_util::OpenFile(path, "rb"); | 132 FILE* file = OpenFile(path, "rb"); |
| 133 if (!file) { | 133 if (!file) { |
| 134 return false; | 134 return false; |
| 135 } | 135 } |
| 136 | 136 |
| 137 char buf[1 << 16]; | 137 char buf[1 << 16]; |
| 138 size_t len; | 138 size_t len; |
| 139 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { | 139 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
| 140 if (contents) | 140 if (contents) |
| 141 contents->append(buf, len); | 141 contents->append(buf, len); |
| 142 } | 142 } |
| 143 file_util::CloseFile(file); | 143 CloseFile(file); |
| 144 | 144 |
| 145 return true; | 145 return true; |
| 146 } | 146 } |
| 147 | 147 |
| 148 bool IsDirectoryEmpty(const FilePath& dir_path) { | 148 bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 149 FileEnumerator files(dir_path, false, | 149 FileEnumerator files(dir_path, false, |
| 150 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); | 150 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
| 151 if (files.Next().empty()) | 151 if (files.Next().empty()) |
| 152 return true; | 152 return true; |
| 153 return false; | 153 return false; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 const PlatformFile file = CreatePlatformFile(path, flags, NULL, NULL); | 187 const PlatformFile file = CreatePlatformFile(path, flags, NULL, NULL); |
| 188 if (file != kInvalidPlatformFileValue) { | 188 if (file != kInvalidPlatformFileValue) { |
| 189 bool result = TouchPlatformFile(file, last_accessed, last_modified); | 189 bool result = TouchPlatformFile(file, last_accessed, last_modified); |
| 190 ClosePlatformFile(file); | 190 ClosePlatformFile(file); |
| 191 return result; | 191 return result; |
| 192 } | 192 } |
| 193 | 193 |
| 194 return false; | 194 return false; |
| 195 } | 195 } |
| 196 | 196 |
| 197 } // namespace base | |
| 198 | |
| 199 // ----------------------------------------------------------------------------- | |
| 200 | |
| 201 namespace file_util { | |
| 202 | |
| 203 using base::FileEnumerator; | |
| 204 using base::FilePath; | |
| 205 using base::kMaxUniqueFiles; | |
| 206 | |
| 207 bool CloseFile(FILE* file) { | 197 bool CloseFile(FILE* file) { |
| 208 if (file == NULL) | 198 if (file == NULL) |
| 209 return true; | 199 return true; |
| 210 return fclose(file) == 0; | 200 return fclose(file) == 0; |
| 211 } | 201 } |
| 212 | 202 |
| 213 bool TruncateFile(FILE* file) { | 203 bool TruncateFile(FILE* file) { |
| 214 if (file == NULL) | 204 if (file == NULL) |
| 215 return false; | 205 return false; |
| 216 long current_offset = ftell(file); | 206 long current_offset = ftell(file); |
| 217 if (current_offset == -1) | 207 if (current_offset == -1) |
| 218 return false; | 208 return false; |
| 219 #if defined(OS_WIN) | 209 #if defined(OS_WIN) |
| 220 int fd = _fileno(file); | 210 int fd = _fileno(file); |
| 221 if (_chsize(fd, current_offset) != 0) | 211 if (_chsize(fd, current_offset) != 0) |
| 222 return false; | 212 return false; |
| 223 #else | 213 #else |
| 224 int fd = fileno(file); | 214 int fd = fileno(file); |
| 225 if (ftruncate(fd, current_offset) != 0) | 215 if (ftruncate(fd, current_offset) != 0) |
| 226 return false; | 216 return false; |
| 227 #endif | 217 #endif |
| 228 return true; | 218 return true; |
| 229 } | 219 } |
| 230 | 220 |
| 221 } // namespace base |
| 222 |
| 223 // ----------------------------------------------------------------------------- |
| 224 |
| 225 namespace file_util { |
| 226 |
| 227 using base::FilePath; |
| 228 using base::kMaxUniqueFiles; |
| 229 |
| 231 int GetUniquePathNumber( | 230 int GetUniquePathNumber( |
| 232 const FilePath& path, | 231 const FilePath& path, |
| 233 const FilePath::StringType& suffix) { | 232 const FilePath::StringType& suffix) { |
| 234 bool have_suffix = !suffix.empty(); | 233 bool have_suffix = !suffix.empty(); |
| 235 if (!PathExists(path) && | 234 if (!PathExists(path) && |
| 236 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { | 235 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { |
| 237 return 0; | 236 return 0; |
| 238 } | 237 } |
| 239 | 238 |
| 240 FilePath new_path; | 239 FilePath new_path; |
| 241 for (int count = 1; count <= kMaxUniqueFiles; ++count) { | 240 for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
| 242 new_path = | 241 new_path = |
| 243 path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", count)); | 242 path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", count)); |
| 244 if (!PathExists(new_path) && | 243 if (!PathExists(new_path) && |
| 245 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { | 244 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 246 return count; | 245 return count; |
| 247 } | 246 } |
| 248 } | 247 } |
| 249 | 248 |
| 250 return -1; | 249 return -1; |
| 251 } | 250 } |
| 252 | 251 |
| 253 } // namespace file_util | 252 } // namespace file_util |
| OLD | NEW |