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 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 | 511 |
512 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 512 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
513 FilePath* new_temp_path) { | 513 FilePath* new_temp_path) { |
514 FilePath tmpdir; | 514 FilePath tmpdir; |
515 if (!GetTempDir(&tmpdir)) | 515 if (!GetTempDir(&tmpdir)) |
516 return false; | 516 return false; |
517 | 517 |
518 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path); | 518 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path); |
519 } | 519 } |
520 | 520 |
521 bool CreateDirectory(const FilePath& full_path) { | 521 bool CreateDirectoryAndGetError(const FilePath& full_path, |
| 522 base::PlatformFileError* error) { |
522 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). | 523 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). |
523 std::vector<FilePath> subpaths; | 524 std::vector<FilePath> subpaths; |
524 | 525 |
525 // Collect a list of all parent directories. | 526 // Collect a list of all parent directories. |
526 FilePath last_path = full_path; | 527 FilePath last_path = full_path; |
527 subpaths.push_back(full_path); | 528 subpaths.push_back(full_path); |
528 for (FilePath path = full_path.DirName(); | 529 for (FilePath path = full_path.DirName(); |
529 path.value() != last_path.value(); path = path.DirName()) { | 530 path.value() != last_path.value(); path = path.DirName()) { |
530 subpaths.push_back(path); | 531 subpaths.push_back(path); |
531 last_path = path; | 532 last_path = path; |
532 } | 533 } |
533 | 534 |
534 // Iterate through the parents and create the missing ones. | 535 // Iterate through the parents and create the missing ones. |
535 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); | 536 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); |
536 i != subpaths.rend(); ++i) { | 537 i != subpaths.rend(); ++i) { |
537 if (DirectoryExists(*i)) | 538 if (DirectoryExists(*i)) |
538 continue; | 539 continue; |
539 if (mkdir(i->value().c_str(), 0700) == 0) | 540 if (mkdir(i->value().c_str(), 0700) == 0) |
540 continue; | 541 continue; |
541 // Mkdir failed, but it might have failed with EEXIST, or some other error | 542 // Mkdir failed, but it might have failed with EEXIST, or some other error |
542 // due to the the directory appearing out of thin air. This can occur if | 543 // due to the the directory appearing out of thin air. This can occur if |
543 // two processes are trying to create the same file system tree at the same | 544 // two processes are trying to create the same file system tree at the same |
544 // time. Check to see if it exists and make sure it is a directory. | 545 // time. Check to see if it exists and make sure it is a directory. |
545 if (!DirectoryExists(*i)) | 546 int saved_errno = errno; |
| 547 if (!DirectoryExists(*i)) { |
| 548 if (error) |
| 549 *error = base::ErrnoToPlatformFileError(saved_errno); |
546 return false; | 550 return false; |
| 551 } |
547 } | 552 } |
548 return true; | 553 return true; |
549 } | 554 } |
550 | 555 |
551 base::FilePath MakeUniqueDirectory(const base::FilePath& path) { | 556 base::FilePath MakeUniqueDirectory(const base::FilePath& path) { |
552 const int kMaxAttempts = 20; | 557 const int kMaxAttempts = 20; |
553 for (int attempts = 0; attempts < kMaxAttempts; attempts++) { | 558 for (int attempts = 0; attempts < kMaxAttempts; attempts++) { |
554 int uniquifier = | 559 int uniquifier = |
555 GetUniquePathNumber(path, base::FilePath::StringType()); | 560 GetUniquePathNumber(path, base::FilePath::StringType()); |
556 if (uniquifier < 0) | 561 if (uniquifier < 0) |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
926 kFileSystemRoot, path, kRootUid, allowed_group_ids); | 931 kFileSystemRoot, path, kRootUid, allowed_group_ids); |
927 } | 932 } |
928 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 933 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
929 | 934 |
930 int GetMaximumPathComponentLength(const FilePath& path) { | 935 int GetMaximumPathComponentLength(const FilePath& path) { |
931 base::ThreadRestrictions::AssertIOAllowed(); | 936 base::ThreadRestrictions::AssertIOAllowed(); |
932 return pathconf(path.value().c_str(), _PC_NAME_MAX); | 937 return pathconf(path.value().c_str(), _PC_NAME_MAX); |
933 } | 938 } |
934 | 939 |
935 } // namespace file_util | 940 } // namespace file_util |
OLD | NEW |