| 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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 if (_chsize(fd, current_offset) != 0) | 229 if (_chsize(fd, current_offset) != 0) |
| 230 return false; | 230 return false; |
| 231 #else | 231 #else |
| 232 int fd = fileno(file); | 232 int fd = fileno(file); |
| 233 if (ftruncate(fd, current_offset) != 0) | 233 if (ftruncate(fd, current_offset) != 0) |
| 234 return false; | 234 return false; |
| 235 #endif | 235 #endif |
| 236 return true; | 236 return true; |
| 237 } | 237 } |
| 238 | 238 |
| 239 } // namespace base | 239 int GetUniquePathNumber(const FilePath& path, |
| 240 | 240 const FilePath::StringType& suffix) { |
| 241 // ----------------------------------------------------------------------------- | |
| 242 | |
| 243 namespace file_util { | |
| 244 | |
| 245 using base::FilePath; | |
| 246 using base::kMaxUniqueFiles; | |
| 247 | |
| 248 int GetUniquePathNumber( | |
| 249 const FilePath& path, | |
| 250 const FilePath::StringType& suffix) { | |
| 251 bool have_suffix = !suffix.empty(); | 241 bool have_suffix = !suffix.empty(); |
| 252 if (!PathExists(path) && | 242 if (!PathExists(path) && |
| 253 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { | 243 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { |
| 254 return 0; | 244 return 0; |
| 255 } | 245 } |
| 256 | 246 |
| 257 FilePath new_path; | 247 FilePath new_path; |
| 258 for (int count = 1; count <= kMaxUniqueFiles; ++count) { | 248 for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
| 259 new_path = | 249 new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); |
| 260 path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", count)); | |
| 261 if (!PathExists(new_path) && | 250 if (!PathExists(new_path) && |
| 262 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { | 251 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 263 return count; | 252 return count; |
| 264 } | 253 } |
| 265 } | 254 } |
| 266 | 255 |
| 267 return -1; | 256 return -1; |
| 268 } | 257 } |
| 269 | 258 |
| 270 } // namespace file_util | 259 } // namespace base |
| OLD | NEW |