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 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 while (success && !directories.empty()) { | 237 while (success && !directories.empty()) { |
238 FilePath dir = FilePath(directories.top()); | 238 FilePath dir = FilePath(directories.top()); |
239 directories.pop(); | 239 directories.pop(); |
240 success = (rmdir(dir.value().c_str()) == 0); | 240 success = (rmdir(dir.value().c_str()) == 0); |
241 } | 241 } |
242 return success; | 242 return success; |
243 } | 243 } |
244 | 244 |
245 bool ReplaceFile(const FilePath& from_path, | 245 bool ReplaceFile(const FilePath& from_path, |
246 const FilePath& to_path, | 246 const FilePath& to_path, |
247 PlatformFileError* error) { | 247 File::Error* error) { |
248 ThreadRestrictions::AssertIOAllowed(); | 248 ThreadRestrictions::AssertIOAllowed(); |
249 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) | 249 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) |
250 return true; | 250 return true; |
251 if (error) | 251 if (error) |
252 *error = ErrnoToPlatformFileError(errno); | 252 *error = File::OSErrorToFileError(errno); |
253 return false; | 253 return false; |
254 } | 254 } |
255 | 255 |
256 bool CopyDirectory(const FilePath& from_path, | 256 bool CopyDirectory(const FilePath& from_path, |
257 const FilePath& to_path, | 257 const FilePath& to_path, |
258 bool recursive) { | 258 bool recursive) { |
259 ThreadRestrictions::AssertIOAllowed(); | 259 ThreadRestrictions::AssertIOAllowed(); |
260 // Some old callers of CopyDirectory want it to support wildcards. | 260 // Some old callers of CopyDirectory want it to support wildcards. |
261 // After some discussion, we decided to fix those callers. | 261 // After some discussion, we decided to fix those callers. |
262 // Break loudly here if anyone tries to do this. | 262 // Break loudly here if anyone tries to do this. |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 582 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
583 FilePath* new_temp_path) { | 583 FilePath* new_temp_path) { |
584 FilePath tmpdir; | 584 FilePath tmpdir; |
585 if (!GetTempDir(&tmpdir)) | 585 if (!GetTempDir(&tmpdir)) |
586 return false; | 586 return false; |
587 | 587 |
588 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path); | 588 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path); |
589 } | 589 } |
590 | 590 |
591 bool CreateDirectoryAndGetError(const FilePath& full_path, | 591 bool CreateDirectoryAndGetError(const FilePath& full_path, |
592 PlatformFileError* error) { | 592 File::Error* error) { |
593 ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). | 593 ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). |
594 std::vector<FilePath> subpaths; | 594 std::vector<FilePath> subpaths; |
595 | 595 |
596 // Collect a list of all parent directories. | 596 // Collect a list of all parent directories. |
597 FilePath last_path = full_path; | 597 FilePath last_path = full_path; |
598 subpaths.push_back(full_path); | 598 subpaths.push_back(full_path); |
599 for (FilePath path = full_path.DirName(); | 599 for (FilePath path = full_path.DirName(); |
600 path.value() != last_path.value(); path = path.DirName()) { | 600 path.value() != last_path.value(); path = path.DirName()) { |
601 subpaths.push_back(path); | 601 subpaths.push_back(path); |
602 last_path = path; | 602 last_path = path; |
603 } | 603 } |
604 | 604 |
605 // Iterate through the parents and create the missing ones. | 605 // Iterate through the parents and create the missing ones. |
606 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); | 606 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); |
607 i != subpaths.rend(); ++i) { | 607 i != subpaths.rend(); ++i) { |
608 if (DirectoryExists(*i)) | 608 if (DirectoryExists(*i)) |
609 continue; | 609 continue; |
610 if (mkdir(i->value().c_str(), 0700) == 0) | 610 if (mkdir(i->value().c_str(), 0700) == 0) |
611 continue; | 611 continue; |
612 // Mkdir failed, but it might have failed with EEXIST, or some other error | 612 // Mkdir failed, but it might have failed with EEXIST, or some other error |
613 // due to the the directory appearing out of thin air. This can occur if | 613 // due to the the directory appearing out of thin air. This can occur if |
614 // two processes are trying to create the same file system tree at the same | 614 // two processes are trying to create the same file system tree at the same |
615 // time. Check to see if it exists and make sure it is a directory. | 615 // time. Check to see if it exists and make sure it is a directory. |
616 int saved_errno = errno; | 616 int saved_errno = errno; |
617 if (!DirectoryExists(*i)) { | 617 if (!DirectoryExists(*i)) { |
618 if (error) | 618 if (error) |
619 *error = ErrnoToPlatformFileError(saved_errno); | 619 *error = File::OSErrorToFileError(saved_errno); |
620 return false; | 620 return false; |
621 } | 621 } |
622 } | 622 } |
623 return true; | 623 return true; |
624 } | 624 } |
625 | 625 |
626 bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) { | 626 bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) { |
627 FilePath real_path_result; | 627 FilePath real_path_result; |
628 if (!RealPath(path, &real_path_result)) | 628 if (!RealPath(path, &real_path_result)) |
629 return false; | 629 return false; |
(...skipping 17 matching lines...) Expand all Loading... |
647 // least be a 'followable' link. | 647 // least be a 'followable' link. |
648 if (CallLstat(file_path.value().c_str(), &st) != 0) | 648 if (CallLstat(file_path.value().c_str(), &st) != 0) |
649 return false; | 649 return false; |
650 | 650 |
651 if (S_ISLNK(st.st_mode)) | 651 if (S_ISLNK(st.st_mode)) |
652 return true; | 652 return true; |
653 else | 653 else |
654 return false; | 654 return false; |
655 } | 655 } |
656 | 656 |
657 bool GetFileInfo(const FilePath& file_path, PlatformFileInfo* results) { | 657 bool GetFileInfo(const FilePath& file_path, File::Info* results) { |
658 stat_wrapper_t file_info; | 658 stat_wrapper_t file_info; |
659 #if defined(OS_ANDROID) | 659 #if defined(OS_ANDROID) |
660 if (file_path.IsContentUri()) { | 660 if (file_path.IsContentUri()) { |
661 int fd = OpenContentUriForRead(file_path); | 661 int fd = OpenContentUriForRead(file_path); |
662 if (fd < 0) | 662 if (fd < 0) |
663 return false; | 663 return false; |
664 file_util::ScopedFD scoped_fd(&fd); | 664 file_util::ScopedFD scoped_fd(&fd); |
665 if (CallFstat(fd, &file_info) != 0) | 665 if (CallFstat(fd, &file_info) != 0) |
666 return false; | 666 return false; |
667 } else { | 667 } else { |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 result = false; | 958 result = false; |
959 if (IGNORE_EINTR(close(outfile)) < 0) | 959 if (IGNORE_EINTR(close(outfile)) < 0) |
960 result = false; | 960 result = false; |
961 | 961 |
962 return result; | 962 return result; |
963 } | 963 } |
964 #endif // !defined(OS_MACOSX) | 964 #endif // !defined(OS_MACOSX) |
965 | 965 |
966 } // namespace internal | 966 } // namespace internal |
967 } // namespace base | 967 } // namespace base |
OLD | NEW |