|
28 | 28 | #include <wtf/FileSystem.h> |
29 | 29 |
|
30 | 30 | #include <wtf/CryptographicallyRandomNumber.h> |
31 | | -#include <wtf/FileMetadata.h> |
32 | 31 | #include <wtf/HexNumber.h> |
33 | 32 | #include <wtf/Scope.h> |
34 | 33 | #include <wtf/StdFilesystem.h> |
@@ -139,20 +138,6 @@ static WallTime toWallTime(std::filesystem::file_time_type fileTime) |
139 | 138 | return WallTime::fromRawSeconds(toTimeT(fileTime)); |
140 | 139 | } |
141 | 140 |
|
142 | | -static std::filesystem::path resolveSymlinks(std::filesystem::path path, std::error_code& ec) |
143 | | -{ |
144 | | -#ifdef MAXSYMLINKS |
145 | | - static constexpr unsigned maxSymlinkDepth = MAXSYMLINKS; |
146 | | -#else |
147 | | - static constexpr unsigned maxSymlinkDepth = 40; |
148 | | -#endif |
149 | | - |
150 | | - unsigned currentDepth = 0; |
151 | | - while (++currentDepth <= maxSymlinkDepth && !ec && std::filesystem::is_symlink(path, ec)) |
152 | | - path = std::filesystem::read_symlink(path, ec); |
153 | | - return path; |
154 | | -} |
155 | | - |
156 | 141 | String encodeForFileName(const String& inputString) |
157 | 142 | { |
158 | 143 | unsigned length = inputString.length(); |
@@ -606,18 +591,6 @@ Optional<uint64_t> fileSize(const String& path) |
606 | 591 | return size; |
607 | 592 | } |
608 | 593 |
|
609 | | -bool isDirectory(const String& path) |
610 | | -{ |
611 | | - std::error_code ec; |
612 | | - return std::filesystem::symlink_status(toStdFileSystemPath(path), ec).type() == std::filesystem::file_type::directory; |
613 | | -} |
614 | | - |
615 | | -bool isDirectoryFollowingSymlinks(const String& path) |
616 | | -{ |
617 | | - std::error_code ec; |
618 | | - return std::filesystem::status(toStdFileSystemPath(path), ec).type() == std::filesystem::file_type::directory; |
619 | | -} |
620 | | - |
621 | 594 | bool makeAllDirectories(const String& path) |
622 | 595 | { |
623 | 596 | std::error_code ec; |
@@ -692,56 +665,44 @@ bool updateFileModificationTime(const String& path) |
692 | 665 | return !ec; |
693 | 666 | } |
694 | 667 |
|
695 | | -enum class ShouldFollowSymbolicLinks { No, Yes }; |
696 | | -static Optional<FileMetadata> fileMetadataPotentiallyFollowingSymlinks(const String& path, ShouldFollowSymbolicLinks shouldFollowSymbolicLinks) |
| 668 | +bool isHiddenFile(const String& path) |
697 | 669 | { |
698 | | - if (path.isEmpty()) |
699 | | - return WTF::nullopt; |
700 | | - |
701 | | - auto fsPath = toStdFileSystemPath(path); |
702 | | - |
703 | | - std::error_code ec; |
704 | | - if (shouldFollowSymbolicLinks == ShouldFollowSymbolicLinks::Yes && std::filesystem::is_symlink(fsPath, ec)) { |
705 | | - fsPath = resolveSymlinks(fsPath, ec); |
706 | | - if (ec) |
707 | | - return WTF::nullopt; |
708 | | - } |
709 | | - |
710 | | - ec = { }; |
711 | | - std::filesystem::directory_entry entry(fsPath, ec); |
712 | | - if (ec) |
713 | | - return WTF::nullopt; |
714 | | - |
715 | | - auto modificationTime = toWallTime(entry.last_write_time(ec)); |
716 | | - |
717 | | - // Note that the result of attempting to determine the size of a directory is implementation-defined. |
718 | | - auto fileSize = entry.file_size(ec); |
719 | | - if (ec) |
720 | | - fileSize = 0; |
721 | | - |
722 | 670 | #if OS(UNIX) |
| 671 | + auto fsPath = toStdFileSystemPath(path); |
723 | 672 | std::filesystem::path::string_type filename = fsPath.filename(); |
724 | | - bool isHidden = !filename.empty() && filename[0] == '.'; |
| 673 | + return !filename.empty() && filename[0] == '.'; |
725 | 674 | #else |
726 | | - bool isHidden = false; |
| 675 | + UNUSED_PARAM(path); |
| 676 | + return false; |
727 | 677 | #endif |
728 | | - FileMetadata::Type type = FileMetadata::Type::File; |
729 | | - if (entry.is_symlink(ec)) |
730 | | - type = FileMetadata::Type::SymbolicLink; |
731 | | - else if (entry.is_directory(ec)) |
732 | | - type = FileMetadata::Type::Directory; |
| 678 | +} |
733 | 679 |
|
734 | | - return FileMetadata { modificationTime, static_cast<long long>(fileSize), isHidden, type }; |
| 680 | +enum class ShouldFollowSymbolicLinks { No, Yes }; |
| 681 | +static Optional<FileType> fileTypePotentiallyFollowingSymLinks(const String& path, ShouldFollowSymbolicLinks shouldFollowSymbolicLinks) |
| 682 | +{ |
| 683 | + std::error_code ec; |
| 684 | + auto status = shouldFollowSymbolicLinks == ShouldFollowSymbolicLinks::Yes ? std::filesystem::status(toStdFileSystemPath(path), ec) : std::filesystem::symlink_status(toStdFileSystemPath(path), ec); |
| 685 | + if (ec) |
| 686 | + return WTF::nullopt; |
| 687 | + switch (status.type()) { |
| 688 | + case std::filesystem::file_type::directory: |
| 689 | + return FileType::Directory; |
| 690 | + case std::filesystem::file_type::symlink: |
| 691 | + return FileType::SymbolicLink; |
| 692 | + default: |
| 693 | + break; |
| 694 | + } |
| 695 | + return FileType::Regular; |
735 | 696 | } |
736 | 697 |
|
737 | | -Optional<FileMetadata> fileMetadata(const String& path) |
| 698 | +Optional<FileType> fileType(const String& path) |
738 | 699 | { |
739 | | - return fileMetadataPotentiallyFollowingSymlinks(path, ShouldFollowSymbolicLinks::No); |
| 700 | + return fileTypePotentiallyFollowingSymLinks(path, ShouldFollowSymbolicLinks::No); |
740 | 701 | } |
741 | 702 |
|
742 | | -Optional<FileMetadata> fileMetadataFollowingSymlinks(const String& path) |
| 703 | +Optional<FileType> fileTypeFollowingSymlinks(const String& path) |
743 | 704 | { |
744 | | - return fileMetadataPotentiallyFollowingSymlinks(path, ShouldFollowSymbolicLinks::Yes); |
| 705 | + return fileTypePotentiallyFollowingSymLinks(path, ShouldFollowSymbolicLinks::Yes); |
745 | 706 | } |
746 | 707 |
|
747 | 708 | String pathFileName(const String& path) |
|
0 commit comments