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 <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <libgen.h> | 10 #include <libgen.h> |
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 | 506 |
507 FilePath rv; | 507 FilePath rv; |
508 if (GetTempDir(&rv)) | 508 if (GetTempDir(&rv)) |
509 return rv; | 509 return rv; |
510 | 510 |
511 // Last resort. | 511 // Last resort. |
512 return FilePath("/tmp"); | 512 return FilePath("/tmp"); |
513 } | 513 } |
514 #endif // !defined(OS_MACOSX) | 514 #endif // !defined(OS_MACOSX) |
515 | 515 |
516 } // namespace base | |
517 | |
518 // ----------------------------------------------------------------------------- | |
519 | |
520 namespace file_util { | |
521 | |
522 using base::stat_wrapper_t; | |
523 using base::CallStat; | |
524 using base::CallLstat; | |
525 using base::CreateAndOpenFdForTemporaryFile; | |
526 using base::DirectoryExists; | |
527 using base::FileEnumerator; | |
528 using base::FilePath; | |
529 using base::MakeAbsoluteFilePath; | |
530 using base::RealPath; | |
531 using base::VerifySpecificPathControlledByUser; | |
532 | |
533 bool CreateTemporaryFile(FilePath* path) { | 516 bool CreateTemporaryFile(FilePath* path) { |
534 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). | 517 ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
535 FilePath directory; | 518 FilePath directory; |
536 if (!GetTempDir(&directory)) | 519 if (!GetTempDir(&directory)) |
537 return false; | 520 return false; |
538 int fd = CreateAndOpenFdForTemporaryFile(directory, path); | 521 int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
539 if (fd < 0) | 522 if (fd < 0) |
540 return false; | 523 return false; |
541 ignore_result(HANDLE_EINTR(close(fd))); | 524 ignore_result(HANDLE_EINTR(close(fd))); |
542 return true; | 525 return true; |
543 } | 526 } |
544 | 527 |
(...skipping 10 matching lines...) Expand all Loading... |
555 if (fd < 0) | 538 if (fd < 0) |
556 return NULL; | 539 return NULL; |
557 | 540 |
558 FILE* file = fdopen(fd, "a+"); | 541 FILE* file = fdopen(fd, "a+"); |
559 if (!file) | 542 if (!file) |
560 ignore_result(HANDLE_EINTR(close(fd))); | 543 ignore_result(HANDLE_EINTR(close(fd))); |
561 return file; | 544 return file; |
562 } | 545 } |
563 | 546 |
564 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 547 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
565 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). | 548 ThreadRestrictions::AssertIOAllowed(); // For call to close(). |
566 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); | 549 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); |
567 return ((fd >= 0) && !HANDLE_EINTR(close(fd))); | 550 return ((fd >= 0) && !HANDLE_EINTR(close(fd))); |
568 } | 551 } |
569 | 552 |
570 static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, | 553 static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, |
571 const FilePath::StringType& name_tmpl, | 554 const FilePath::StringType& name_tmpl, |
572 FilePath* new_dir) { | 555 FilePath* new_dir) { |
573 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp(). | 556 ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp(). |
574 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) | 557 DCHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) |
575 << "Directory name template must contain \"XXXXXX\"."; | 558 << "Directory name template must contain \"XXXXXX\"."; |
576 | 559 |
577 FilePath sub_dir = base_dir.Append(name_tmpl); | 560 FilePath sub_dir = base_dir.Append(name_tmpl); |
578 std::string sub_dir_string = sub_dir.value(); | 561 std::string sub_dir_string = sub_dir.value(); |
579 | 562 |
580 // this should be OK since mkdtemp just replaces characters in place | 563 // this should be OK since mkdtemp just replaces characters in place |
581 char* buffer = const_cast<char*>(sub_dir_string.c_str()); | 564 char* buffer = const_cast<char*>(sub_dir_string.c_str()); |
582 char* dtemp = mkdtemp(buffer); | 565 char* dtemp = mkdtemp(buffer); |
583 if (!dtemp) { | 566 if (!dtemp) { |
(...skipping 11 matching lines...) Expand all Loading... |
595 mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX")); | 578 mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX")); |
596 return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir); | 579 return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir); |
597 } | 580 } |
598 | 581 |
599 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 582 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
600 FilePath* new_temp_path) { | 583 FilePath* new_temp_path) { |
601 FilePath tmpdir; | 584 FilePath tmpdir; |
602 if (!GetTempDir(&tmpdir)) | 585 if (!GetTempDir(&tmpdir)) |
603 return false; | 586 return false; |
604 | 587 |
605 return CreateTemporaryDirInDirImpl(tmpdir, base::TempFileName(), | 588 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path); |
606 new_temp_path); | |
607 } | 589 } |
608 | 590 |
| 591 |
| 592 } // namespace base |
| 593 |
| 594 // ----------------------------------------------------------------------------- |
| 595 |
| 596 namespace file_util { |
| 597 |
| 598 using base::stat_wrapper_t; |
| 599 using base::CallStat; |
| 600 using base::CallLstat; |
| 601 using base::CreateAndOpenFdForTemporaryFile; |
| 602 using base::DirectoryExists; |
| 603 using base::FileEnumerator; |
| 604 using base::FilePath; |
| 605 using base::MakeAbsoluteFilePath; |
| 606 using base::RealPath; |
| 607 using base::VerifySpecificPathControlledByUser; |
| 608 |
609 bool CreateDirectoryAndGetError(const FilePath& full_path, | 609 bool CreateDirectoryAndGetError(const FilePath& full_path, |
610 base::PlatformFileError* error) { | 610 base::PlatformFileError* error) { |
611 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). | 611 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). |
612 std::vector<FilePath> subpaths; | 612 std::vector<FilePath> subpaths; |
613 | 613 |
614 // Collect a list of all parent directories. | 614 // Collect a list of all parent directories. |
615 FilePath last_path = full_path; | 615 FilePath last_path = full_path; |
616 subpaths.push_back(full_path); | 616 subpaths.push_back(full_path); |
617 for (FilePath path = full_path.DirName(); | 617 for (FilePath path = full_path.DirName(); |
618 path.value() != last_path.value(); path = path.DirName()) { | 618 path.value() != last_path.value(); path = path.DirName()) { |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
971 result = false; | 971 result = false; |
972 if (HANDLE_EINTR(close(outfile)) < 0) | 972 if (HANDLE_EINTR(close(outfile)) < 0) |
973 result = false; | 973 result = false; |
974 | 974 |
975 return result; | 975 return result; |
976 } | 976 } |
977 #endif // !defined(OS_MACOSX) | 977 #endif // !defined(OS_MACOSX) |
978 | 978 |
979 } // namespace internal | 979 } // namespace internal |
980 } // namespace base | 980 } // namespace base |
OLD | NEW |