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_name = UTF8ToUTF16(base::GenerateGUID() + ".tmp"); |
grt (UTC plus 2)
2017/04/05 11:26:34
ASCIIToUTF16 since you know the GUID is ASCII
chengx
2017/04/05 18:22:34
Done.
| |
344 | 345 FilePath temp_path_name = dir.Append(temp_name); |
345 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { | 346 HANDLE file_handle = |
grt (UTC plus 2)
2017/04/05 11:26:34
either use base::win::ScopedHandle or base::File.
chengx
2017/04/05 18:22:34
Done. Changed to base::File.
| |
347 ::CreateFile(temp_path_name.value().c_str(), GENERIC_READ | GENERIC_WRITE, | |
348 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, | |
grt (UTC plus 2)
2017/04/05 11:26:34
regarding CREATE_ALWAYS: we have no guarantee that
grt (UTC plus 2)
2017/04/05 11:26:34
nit: NULL -> nullptr
chengx
2017/04/05 18:22:34
Done.
I know nullptr is preferred. However, "NULL
chengx
2017/04/05 18:22:34
I agree. Changed CREATE_ALWAYS to CREATE_NEW.
| |
349 FILE_ATTRIBUTE_NORMAL, NULL); | |
350 if (file_handle == INVALID_HANDLE_VALUE) { | |
346 DPLOG(WARNING) << "Failed to get temporary file name in " | 351 DPLOG(WARNING) << "Failed to get temporary file name in " |
347 << UTF16ToUTF8(dir.value()); | 352 << UTF16ToUTF8(dir.value()); |
348 return false; | 353 return false; |
349 } | 354 } |
355 ::CloseHandle(file_handle); | |
350 | 356 |
351 wchar_t long_temp_name[MAX_PATH + 1]; | 357 wchar_t full_path_name[MAX_PATH + 1]; |
352 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); | 358 DWORD full_path_name_len = |
353 if (long_name_len > MAX_PATH || long_name_len == 0) { | 359 GetLongPathName(temp_path_name.value().c_str(), full_path_name, MAX_PATH); |
grt (UTC plus 2)
2017/04/05 11:26:34
could you dig through the blamelist to figure out
chengx
2017/04/05 18:22:34
I had the same doubts here too. So I made some cha
grt (UTC plus 2)
2017/04/06 10:35:02
Yes, GetLongPathName will fail if the user doesn't
| |
354 // GetLongPathName() failed, but we still have a temporary file. | 360 if (full_path_name_len > MAX_PATH || full_path_name_len == 0) { |
355 *temp_file = FilePath(temp_name); | 361 // GetFullPathName() failed, but we still have a temporary file. |
362 *temp_file = temp_path_name; | |
356 return true; | 363 return true; |
357 } | 364 } |
358 | 365 |
359 FilePath::StringType long_temp_name_str; | 366 FilePath::StringType full_path_name_str; |
360 long_temp_name_str.assign(long_temp_name, long_name_len); | 367 full_path_name_str.assign(full_path_name, full_path_name_len); |
361 *temp_file = FilePath(long_temp_name_str); | 368 *temp_file = FilePath(full_path_name_str); |
362 return true; | 369 return true; |
363 } | 370 } |
364 | 371 |
365 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 372 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
366 const FilePath::StringType& prefix, | 373 const FilePath::StringType& prefix, |
367 FilePath* new_dir) { | 374 FilePath* new_dir) { |
368 ThreadRestrictions::AssertIOAllowed(); | 375 ThreadRestrictions::AssertIOAllowed(); |
369 | 376 |
370 FilePath path_to_create; | 377 FilePath path_to_create; |
371 | 378 |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
842 // Like Move, this function is not transactional, so we just | 849 // Like Move, this function is not transactional, so we just |
843 // leave the copied bits behind if deleting from_path fails. | 850 // leave the copied bits behind if deleting from_path fails. |
844 // If to_path exists previously then we have already overwritten | 851 // 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. | 852 // it by now, we don't get better off by deleting the new bits. |
846 } | 853 } |
847 return false; | 854 return false; |
848 } | 855 } |
849 | 856 |
850 } // namespace internal | 857 } // namespace internal |
851 } // namespace base | 858 } // namespace base |
OLD | NEW |