Skip to content

Commit 2ffc6ed

Browse files
committed
Enhance Path
1 parent 27d9f6b commit 2ffc6ed

2 files changed

Lines changed: 27 additions & 10 deletions

File tree

lib/node.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ enum class FileType : mode_t {
5858
};
5959

6060
inline FileType GetFileType(const mode_t mode) {
61-
return FileType(mode & S_IFMT);
61+
// Consider an unknown file type as a regular file.
62+
mode_t const ft = mode & S_IFMT;
63+
return ft ? FileType(ft) : FileType::File;
6264
}
6365

6466
inline void SetFileType(mode_t* const mode, const FileType type) {

lib/path.cc

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323

2424
#include "log.h"
2525

26+
namespace {
27+
28+
// Converts a string to ASCII lower case.
29+
std::string ToLower(std::string_view const s) {
30+
std::string r(s);
31+
for (char& c : r) {
32+
if ('A' <= c && c <= 'Z') {
33+
c += 'a' - 'A';
34+
}
35+
}
36+
37+
return r;
38+
}
39+
40+
} // namespace
41+
2642
bool Path::redact = false;
2743

2844
std::ostream& operator<<(std::ostream& out, const Path path) {
@@ -67,7 +83,7 @@ Path Path::WithoutTrailingSeparator() const {
6783
Path::size_type Path::FinalExtensionPosition() const {
6884
const size_type last_dot = find_last_of("/. ");
6985
if (last_dot == npos || at(last_dot) != '.' || last_dot == 0 ||
70-
last_dot == size() - 1 || size() - last_dot > 6) {
86+
last_dot == size() - 1 || size() - last_dot > 8) {
7187
return size();
7288
}
7389

@@ -87,17 +103,16 @@ Path::size_type Path::ExtensionPosition() const {
87103

88104
// Extract extension without dot and in ASCII lowercase.
89105
assert(at(last_dot) == '.');
90-
std::string ext(substr(last_dot + 1));
91-
for (char& c : ext) {
92-
if ('A' <= c && c <= 'Z') {
93-
c += 'a' - 'A';
94-
}
95-
}
106+
const std::string ext = ToLower(substr(last_dot + 1));
96107

97108
// Is it a special extension?
98109
static const std::unordered_set<std::string_view> special_exts = {
99-
"z", "gz", "bz", "bz2", "xz", "zst", "lz", "lzma"};
100-
if (special_exts.count(ext)) {
110+
"asc", "b64", "base64", "br", "brotli", "bz", "bz2",
111+
"bzip2", "gpg", "grz", "grzip", "gz", "gzip", "lrz",
112+
"lrzip", "lz", "lzip", "lz4", "lzma", "lzo", "lzop",
113+
"pgp", "uu", "xz", "z", "zst", "zstd"};
114+
115+
if (special_exts.contains(ext)) {
101116
return Path(substr(0, last_dot)).FinalExtensionPosition();
102117
}
103118

0 commit comments

Comments
 (0)