| 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 = OpenFile(path, "rb"); | 132 FILE* file = file_util::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 CloseFile(file); | 143 file_util::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 |
| 197 bool CloseFile(FILE* file) { | 207 bool CloseFile(FILE* file) { |
| 198 if (file == NULL) | 208 if (file == NULL) |
| 199 return true; | 209 return true; |
| 200 return fclose(file) == 0; | 210 return fclose(file) == 0; |
| 201 } | 211 } |
| 202 | 212 |
| 203 bool TruncateFile(FILE* file) { | 213 bool TruncateFile(FILE* file) { |
| 204 if (file == NULL) | 214 if (file == NULL) |
| 205 return false; | 215 return false; |
| 206 long current_offset = ftell(file); | 216 long current_offset = ftell(file); |
| 207 if (current_offset == -1) | 217 if (current_offset == -1) |
| 208 return false; | 218 return false; |
| 209 #if defined(OS_WIN) | 219 #if defined(OS_WIN) |
| 210 int fd = _fileno(file); | 220 int fd = _fileno(file); |
| 211 if (_chsize(fd, current_offset) != 0) | 221 if (_chsize(fd, current_offset) != 0) |
| 212 return false; | 222 return false; |
| 213 #else | 223 #else |
| 214 int fd = fileno(file); | 224 int fd = fileno(file); |
| 215 if (ftruncate(fd, current_offset) != 0) | 225 if (ftruncate(fd, current_offset) != 0) |
| 216 return false; | 226 return false; |
| 217 #endif | 227 #endif |
| 218 return true; | 228 return true; |
| 219 } | 229 } |
| 220 | 230 |
| 221 } // namespace base | |
| 222 | |
| 223 // ----------------------------------------------------------------------------- | |
| 224 | |
| 225 namespace file_util { | |
| 226 | |
| 227 using base::FilePath; | |
| 228 using base::kMaxUniqueFiles; | |
| 229 | |
| 230 int GetUniquePathNumber( | 231 int GetUniquePathNumber( |
| 231 const FilePath& path, | 232 const FilePath& path, |
| 232 const FilePath::StringType& suffix) { | 233 const FilePath::StringType& suffix) { |
| 233 bool have_suffix = !suffix.empty(); | 234 bool have_suffix = !suffix.empty(); |
| 234 if (!PathExists(path) && | 235 if (!PathExists(path) && |
| 235 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { | 236 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { |
| 236 return 0; | 237 return 0; |
| 237 } | 238 } |
| 238 | 239 |
| 239 FilePath new_path; | 240 FilePath new_path; |
| 240 for (int count = 1; count <= kMaxUniqueFiles; ++count) { | 241 for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
| 241 new_path = | 242 new_path = |
| 242 path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", count)); | 243 path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", count)); |
| 243 if (!PathExists(new_path) && | 244 if (!PathExists(new_path) && |
| 244 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { | 245 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 245 return count; | 246 return count; |
| 246 } | 247 } |
| 247 } | 248 } |
| 248 | 249 |
| 249 return -1; | 250 return -1; |
| 250 } | 251 } |
| 251 | 252 |
| 252 } // namespace file_util | 253 } // namespace file_util |
| OLD | NEW |