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 #include <windows.h> | 7 #include <windows.h> |
8 #include <psapi.h> | 8 #include <psapi.h> |
9 #include <shellapi.h> | 9 #include <shellapi.h> |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 // TODO(jrg): is there equivalent call to use on Windows instead of | 257 // TODO(jrg): is there equivalent call to use on Windows instead of |
258 // going 2-step? | 258 // going 2-step? |
259 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { | 259 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
260 ThreadRestrictions::AssertIOAllowed(); | 260 ThreadRestrictions::AssertIOAllowed(); |
261 if (!CreateTemporaryFileInDir(dir, path)) { | 261 if (!CreateTemporaryFileInDir(dir, path)) { |
262 return NULL; | 262 return NULL; |
263 } | 263 } |
264 // Open file in binary mode, to avoid problems with fwrite. On Windows | 264 // Open file in binary mode, to avoid problems with fwrite. On Windows |
265 // it replaces \n's with \r\n's, which may surprise you. | 265 // it replaces \n's with \r\n's, which may surprise you. |
266 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx | 266 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx |
267 return OpenFile(*path, "wb+"); | 267 return file_util::OpenFile(*path, "wb+"); |
268 } | 268 } |
269 | 269 |
270 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 270 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
271 ThreadRestrictions::AssertIOAllowed(); | 271 ThreadRestrictions::AssertIOAllowed(); |
272 | 272 |
273 wchar_t temp_name[MAX_PATH + 1]; | 273 wchar_t temp_name[MAX_PATH + 1]; |
274 | 274 |
275 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { | 275 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { |
276 DPLOG(WARNING) << "Failed to get temporary file name in " | 276 DPLOG(WARNING) << "Failed to get temporary file name in " |
277 << UTF16ToUTF8(dir.value()); | 277 << UTF16ToUTF8(dir.value()); |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 | 523 |
524 results->is_directory = | 524 results->is_directory = |
525 (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | 525 (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
526 results->last_modified = Time::FromFileTime(attr.ftLastWriteTime); | 526 results->last_modified = Time::FromFileTime(attr.ftLastWriteTime); |
527 results->last_accessed = Time::FromFileTime(attr.ftLastAccessTime); | 527 results->last_accessed = Time::FromFileTime(attr.ftLastAccessTime); |
528 results->creation_time = Time::FromFileTime(attr.ftCreationTime); | 528 results->creation_time = Time::FromFileTime(attr.ftCreationTime); |
529 | 529 |
530 return true; | 530 return true; |
531 } | 531 } |
532 | 532 |
| 533 } // namespace base |
| 534 |
| 535 // ----------------------------------------------------------------------------- |
| 536 |
| 537 namespace file_util { |
| 538 |
| 539 using base::DirectoryExists; |
| 540 using base::FilePath; |
| 541 using base::kFileShareAll; |
| 542 |
533 FILE* OpenFile(const FilePath& filename, const char* mode) { | 543 FILE* OpenFile(const FilePath& filename, const char* mode) { |
534 ThreadRestrictions::AssertIOAllowed(); | 544 base::ThreadRestrictions::AssertIOAllowed(); |
535 std::wstring w_mode = ASCIIToWide(std::string(mode)); | 545 std::wstring w_mode = ASCIIToWide(std::string(mode)); |
536 return _wfsopen(filename.value().c_str(), w_mode.c_str(), _SH_DENYNO); | 546 return _wfsopen(filename.value().c_str(), w_mode.c_str(), _SH_DENYNO); |
537 } | 547 } |
538 | 548 |
| 549 FILE* OpenFile(const std::string& filename, const char* mode) { |
| 550 base::ThreadRestrictions::AssertIOAllowed(); |
| 551 return _fsopen(filename.c_str(), mode, _SH_DENYNO); |
| 552 } |
| 553 |
539 int ReadFile(const FilePath& filename, char* data, int size) { | 554 int ReadFile(const FilePath& filename, char* data, int size) { |
540 ThreadRestrictions::AssertIOAllowed(); | 555 base::ThreadRestrictions::AssertIOAllowed(); |
541 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), | 556 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
542 GENERIC_READ, | 557 GENERIC_READ, |
543 FILE_SHARE_READ | FILE_SHARE_WRITE, | 558 FILE_SHARE_READ | FILE_SHARE_WRITE, |
544 NULL, | 559 NULL, |
545 OPEN_EXISTING, | 560 OPEN_EXISTING, |
546 FILE_FLAG_SEQUENTIAL_SCAN, | 561 FILE_FLAG_SEQUENTIAL_SCAN, |
547 NULL)); | 562 NULL)); |
548 if (!file) | 563 if (!file) |
549 return -1; | 564 return -1; |
550 | 565 |
551 DWORD read; | 566 DWORD read; |
552 if (::ReadFile(file, data, size, &read, NULL) && | 567 if (::ReadFile(file, data, size, &read, NULL) && |
553 static_cast<int>(read) == size) | 568 static_cast<int>(read) == size) |
554 return read; | 569 return read; |
555 return -1; | 570 return -1; |
556 } | 571 } |
557 | 572 |
558 } // namespace base | |
559 | |
560 // ----------------------------------------------------------------------------- | |
561 | |
562 namespace file_util { | |
563 | |
564 using base::DirectoryExists; | |
565 using base::FilePath; | |
566 using base::kFileShareAll; | |
567 | |
568 FILE* OpenFile(const std::string& filename, const char* mode) { | |
569 base::ThreadRestrictions::AssertIOAllowed(); | |
570 return _fsopen(filename.c_str(), mode, _SH_DENYNO); | |
571 } | |
572 | |
573 int WriteFile(const FilePath& filename, const char* data, int size) { | 573 int WriteFile(const FilePath& filename, const char* data, int size) { |
574 base::ThreadRestrictions::AssertIOAllowed(); | 574 base::ThreadRestrictions::AssertIOAllowed(); |
575 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), | 575 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
576 GENERIC_WRITE, | 576 GENERIC_WRITE, |
577 0, | 577 0, |
578 NULL, | 578 NULL, |
579 CREATE_ALWAYS, | 579 CREATE_ALWAYS, |
580 0, | 580 0, |
581 NULL)); | 581 NULL)); |
582 if (!file) { | 582 if (!file) { |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 // Like Move, this function is not transactional, so we just | 744 // Like Move, this function is not transactional, so we just |
745 // leave the copied bits behind if deleting from_path fails. | 745 // leave the copied bits behind if deleting from_path fails. |
746 // If to_path exists previously then we have already overwritten | 746 // If to_path exists previously then we have already overwritten |
747 // it by now, we don't get better off by deleting the new bits. | 747 // it by now, we don't get better off by deleting the new bits. |
748 } | 748 } |
749 return false; | 749 return false; |
750 } | 750 } |
751 | 751 |
752 } // namespace internal | 752 } // namespace internal |
753 } // namespace base | 753 } // namespace base |
OLD | NEW |