| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <shellapi.h> | 8 #include <shellapi.h> |
| 9 #include <shlobj.h> | 9 #include <shlobj.h> |
| 10 #include <time.h> | 10 #include <time.h> |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 std::wstring path_str(temp_path); | 438 std::wstring path_str(temp_path); |
| 439 TrimTrailingSeparator(&path_str); | 439 TrimTrailingSeparator(&path_str); |
| 440 *path = FilePath(path_str); | 440 *path = FilePath(path_str); |
| 441 return true; | 441 return true; |
| 442 } | 442 } |
| 443 | 443 |
| 444 bool GetShmemTempDir(FilePath* path) { | 444 bool GetShmemTempDir(FilePath* path) { |
| 445 return GetTempDir(path); | 445 return GetTempDir(path); |
| 446 } | 446 } |
| 447 | 447 |
| 448 bool CreateTemporaryFileName(FilePath* path) { | 448 bool CreateTemporaryFile(FilePath* path) { |
| 449 std::wstring temp_path, temp_file; | 449 FilePath temp_file; |
| 450 | 450 |
| 451 if (!GetTempDir(&temp_path)) | 451 if (!GetTempDir(path)) |
| 452 return false; | 452 return false; |
| 453 | 453 |
| 454 if (CreateTemporaryFileNameInDir(temp_path, &temp_file)) { | 454 if (CreateTemporaryFileInDir(*path, &temp_file)) { |
| 455 *path = FilePath(temp_file); | 455 *path = temp_file; |
| 456 return true; | 456 return true; |
| 457 } | 457 } |
| 458 | 458 |
| 459 return false; | 459 return false; |
| 460 } | 460 } |
| 461 | 461 |
| 462 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { | 462 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { |
| 463 return CreateAndOpenTemporaryFile(path); | 463 return CreateAndOpenTemporaryFile(path); |
| 464 } | 464 } |
| 465 | 465 |
| 466 // On POSIX we have semantics to create and open a temporary file | 466 // On POSIX we have semantics to create and open a temporary file |
| 467 // atomically. | 467 // atomically. |
| 468 // TODO(jrg): is there equivalent call to use on Windows instead of | 468 // TODO(jrg): is there equivalent call to use on Windows instead of |
| 469 // going 2-step? | 469 // going 2-step? |
| 470 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { | 470 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
| 471 std::wstring wstring_path; | 471 if (!CreateTemporaryFileInDir(dir, path)) { |
| 472 if (!CreateTemporaryFileNameInDir(dir.value(), &wstring_path)) { | |
| 473 return NULL; | 472 return NULL; |
| 474 } | 473 } |
| 475 *path = FilePath(wstring_path); | |
| 476 // Open file in binary mode, to avoid problems with fwrite. On Windows | 474 // Open file in binary mode, to avoid problems with fwrite. On Windows |
| 477 // it replaces \n's with \r\n's, which may surprise you. | 475 // it replaces \n's with \r\n's, which may surprise you. |
| 478 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx | 476 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx |
| 479 return OpenFile(*path, "wb+"); | 477 return OpenFile(*path, "wb+"); |
| 480 } | 478 } |
| 481 | 479 |
| 482 bool CreateTemporaryFileNameInDir(const std::wstring& dir, | 480 bool CreateTemporaryFileInDir(const FilePath& dir, |
| 483 std::wstring* temp_file) { | 481 FilePath* temp_file) { |
| 484 wchar_t temp_name[MAX_PATH + 1]; | 482 wchar_t temp_name[MAX_PATH + 1]; |
| 485 | 483 |
| 486 if (!GetTempFileName(dir.c_str(), L"", 0, temp_name)) | 484 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) |
| 487 return false; // fail! | 485 return false; // fail! |
| 488 | 486 |
| 489 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); | 487 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); |
| 490 if (path_len > MAX_PATH + 1 || path_len == 0) | 488 if (path_len > MAX_PATH + 1 || path_len == 0) |
| 491 return false; // fail! | 489 return false; // fail! |
| 492 | 490 |
| 493 temp_file->assign(temp_name, path_len); | 491 std::wstring temp_file_str; |
| 492 temp_file_str.assign(temp_name, path_len); |
| 493 *temp_file = FilePath(temp_file_str); |
| 494 return true; | 494 return true; |
| 495 } | 495 } |
| 496 | 496 |
| 497 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 497 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 498 FilePath* new_temp_path) { | 498 FilePath* new_temp_path) { |
| 499 FilePath system_temp_dir; | 499 FilePath system_temp_dir; |
| 500 if (!GetTempDir(&system_temp_dir)) | 500 if (!GetTempDir(&system_temp_dir)) |
| 501 return false; | 501 return false; |
| 502 | 502 |
| 503 FilePath path_to_create; | 503 FilePath path_to_create; |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 ::CloseHandle(file_mapping_); | 809 ::CloseHandle(file_mapping_); |
| 810 if (file_ != INVALID_HANDLE_VALUE) | 810 if (file_ != INVALID_HANDLE_VALUE) |
| 811 ::CloseHandle(file_); | 811 ::CloseHandle(file_); |
| 812 | 812 |
| 813 data_ = NULL; | 813 data_ = NULL; |
| 814 file_mapping_ = file_ = INVALID_HANDLE_VALUE; | 814 file_mapping_ = file_ = INVALID_HANDLE_VALUE; |
| 815 length_ = INVALID_FILE_SIZE; | 815 length_ = INVALID_FILE_SIZE; |
| 816 } | 816 } |
| 817 | 817 |
| 818 } // namespace file_util | 818 } // namespace file_util |
| OLD | NEW |