Chromium Code Reviews| 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 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <io.h> | 8 #include <io.h> |
| 9 #include <psapi.h> | 9 #include <psapi.h> |
| 10 #include <shellapi.h> | 10 #include <shellapi.h> |
| 11 #include <shlobj.h> | 11 #include <shlobj.h> |
| 12 #include <stddef.h> | 12 #include <stddef.h> |
| 13 #include <stdint.h> | 13 #include <stdint.h> |
| 14 #include <time.h> | 14 #include <time.h> |
| 15 #include <winsock2.h> | 15 #include <winsock2.h> |
| 16 | 16 |
| 17 #include <algorithm> | 17 #include <algorithm> |
| 18 #include <limits> | 18 #include <limits> |
| 19 #include <string> | 19 #include <string> |
| 20 | 20 |
| 21 #include "base/files/file_enumerator.h" | 21 #include "base/files/file_enumerator.h" |
| 22 #include "base/files/file_path.h" | 22 #include "base/files/file_path.h" |
| 23 #include "base/guid.h" | |
| 23 #include "base/logging.h" | 24 #include "base/logging.h" |
| 24 #include "base/macros.h" | 25 #include "base/macros.h" |
| 25 #include "base/metrics/histogram.h" | 26 #include "base/metrics/histogram.h" |
| 26 #include "base/process/process_handle.h" | 27 #include "base/process/process_handle.h" |
| 27 #include "base/rand_util.h" | 28 #include "base/rand_util.h" |
| 28 #include "base/strings/string_number_conversions.h" | 29 #include "base/strings/string_number_conversions.h" |
| 29 #include "base/strings/string_util.h" | 30 #include "base/strings/string_util.h" |
| 30 #include "base/strings/utf_string_conversions.h" | 31 #include "base/strings/utf_string_conversions.h" |
| 31 #include "base/threading/thread_restrictions.h" | 32 #include "base/threading/thread_restrictions.h" |
| 32 #include "base/time/time.h" | 33 #include "base/time/time.h" |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 } | 334 } |
| 334 // Open file in binary mode, to avoid problems with fwrite. On Windows | 335 // Open file in binary mode, to avoid problems with fwrite. On Windows |
| 335 // it replaces \n's with \r\n's, which may surprise you. | 336 // it replaces \n's with \r\n's, which may surprise you. |
| 336 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx | 337 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx |
| 337 return OpenFile(*path, "wb+"); | 338 return OpenFile(*path, "wb+"); |
| 338 } | 339 } |
| 339 | 340 |
| 340 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 341 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
| 341 ThreadRestrictions::AssertIOAllowed(); | 342 ThreadRestrictions::AssertIOAllowed(); |
| 342 | 343 |
| 343 wchar_t temp_name[MAX_PATH + 1]; | 344 FilePath::StringType temp_file_name = |
|
grt (UTC plus 2)
2017/04/06 10:35:02
please leave a comment explaining why GetTempFileN
chengx
2017/04/06 17:46:01
Comments added.
| |
| 344 | 345 ASCIIToUTF16(base::GenerateGUID() + ".tmp"); |
| 345 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { | 346 FilePath temp_name = dir.Append(temp_file_name); |
| 347 File file(::CreateFile(temp_name.value().c_str(), | |
|
grt (UTC plus 2)
2017/04/06 10:35:02
can you not use the (const FilePath& path, uint32_
chengx
2017/04/06 17:46:01
Done. Changed to the (const FilePath& path, uint32
| |
| 348 GENERIC_READ | GENERIC_WRITE, | |
| 349 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, | |
| 350 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr)); | |
| 351 if (!file.IsValid()) { | |
| 346 DPLOG(WARNING) << "Failed to get temporary file name in " | 352 DPLOG(WARNING) << "Failed to get temporary file name in " |
| 347 << UTF16ToUTF8(dir.value()); | 353 << UTF16ToUTF8(dir.value()); |
| 348 return false; | 354 return false; |
| 349 } | 355 } |
| 356 file.Close(); | |
| 350 | 357 |
| 351 wchar_t long_temp_name[MAX_PATH + 1]; | 358 wchar_t long_temp_name[MAX_PATH + 1]; |
| 352 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); | 359 DWORD long_name_len = |
| 360 GetLongPathName(temp_name.value().c_str(), long_temp_name, MAX_PATH); | |
| 353 if (long_name_len > MAX_PATH || long_name_len == 0) { | 361 if (long_name_len > MAX_PATH || long_name_len == 0) { |
| 354 // GetLongPathName() failed, but we still have a temporary file. | 362 // GetLongPathName() failed, but we still have a temporary file. |
| 355 *temp_file = FilePath(temp_name); | 363 *temp_file = temp_name; |
| 356 return true; | 364 return true; |
| 357 } | 365 } |
| 358 | 366 |
| 359 FilePath::StringType long_temp_name_str; | 367 FilePath::StringType long_temp_name_str; |
| 360 long_temp_name_str.assign(long_temp_name, long_name_len); | 368 long_temp_name_str.assign(long_temp_name, long_name_len); |
| 361 *temp_file = FilePath(long_temp_name_str); | 369 *temp_file = FilePath(long_temp_name_str); |
| 362 return true; | 370 return true; |
| 363 } | 371 } |
| 364 | 372 |
| 365 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 373 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
| (...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 842 // Like Move, this function is not transactional, so we just | 850 // Like Move, this function is not transactional, so we just |
| 843 // leave the copied bits behind if deleting from_path fails. | 851 // leave the copied bits behind if deleting from_path fails. |
| 844 // If to_path exists previously then we have already overwritten | 852 // If to_path exists previously then we have already overwritten |
| 845 // it by now, we don't get better off by deleting the new bits. | 853 // it by now, we don't get better off by deleting the new bits. |
| 846 } | 854 } |
| 847 return false; | 855 return false; |
| 848 } | 856 } |
| 849 | 857 |
| 850 } // namespace internal | 858 } // namespace internal |
| 851 } // namespace base | 859 } // namespace base |
| OLD | NEW |