Commit b7ce4523 authored by Max Kellermann's avatar Max Kellermann

fs/Traits: add IsSpecialFilename()

Merge some duplicate code in a central library.
parent 5faf7605
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "storage/StorageInterface.hxx" #include "storage/StorageInterface.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "fs/FileInfo.hxx" #include "fs/FileInfo.hxx"
#include "fs/Traits.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <string> #include <string>
...@@ -146,8 +147,7 @@ WatchDirectory::GetUriFS() const noexcept ...@@ -146,8 +147,7 @@ WatchDirectory::GetUriFS() const noexcept
/* we don't look at "." / ".." nor files with newlines in their name */ /* we don't look at "." / ".." nor files with newlines in their name */
static bool skip_path(const char *path) static bool skip_path(const char *path)
{ {
return (path[0] == '.' && path[1] == 0) || return PathTraitsFS::IsSpecialFilename(path) ||
(path[0] == '.' && path[1] == '.' && path[2] == 0) ||
strchr(path, '\n') != nullptr; strchr(path, '\n') != nullptr;
} }
......
...@@ -237,7 +237,7 @@ try { ...@@ -237,7 +237,7 @@ try {
LogError(std::current_exception()); LogError(std::current_exception());
} }
/* we don't look at "." / ".." nor files with newlines in their name */ /* we don't look at files with newlines in their name */
gcc_pure gcc_pure
static bool static bool
skip_path(const char *name_utf8) noexcept skip_path(const char *name_utf8) noexcept
......
...@@ -109,6 +109,12 @@ struct PathTraitsFS { ...@@ -109,6 +109,12 @@ struct PathTraitsFS {
} }
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
static bool IsSpecialFilename(const_pointer_type name) noexcept {
return (name[0] == '.' && name[1] == 0) ||
(name[0] == '.' && name[1] == '.' && name[2] == 0);
}
gcc_pure gcc_nonnull_all
static size_t GetLength(const_pointer_type p) noexcept { static size_t GetLength(const_pointer_type p) noexcept {
return StringLength(p); return StringLength(p);
} }
...@@ -217,6 +223,12 @@ struct PathTraitsUTF8 { ...@@ -217,6 +223,12 @@ struct PathTraitsUTF8 {
} }
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
static bool IsSpecialFilename(const_pointer_type name) noexcept {
return (name[0] == '.' && name[1] == 0) ||
(name[0] == '.' && name[1] == '.' && name[2] == 0);
}
gcc_pure gcc_nonnull_all
static size_t GetLength(const_pointer_type p) noexcept { static size_t GetLength(const_pointer_type p) noexcept {
return StringLength(p); return StringLength(p);
} }
......
...@@ -144,21 +144,12 @@ LocalStorage::OpenDirectory(const char *uri_utf8) ...@@ -144,21 +144,12 @@ LocalStorage::OpenDirectory(const char *uri_utf8)
return std::make_unique<LocalDirectoryReader>(MapFSOrThrow(uri_utf8)); return std::make_unique<LocalDirectoryReader>(MapFSOrThrow(uri_utf8));
} }
gcc_pure
static bool
SkipNameFS(PathTraitsFS::const_pointer_type name_fs) noexcept
{
return name_fs[0] == '.' &&
(name_fs[1] == 0 ||
(name_fs[1] == '.' && name_fs[2] == 0));
}
const char * const char *
LocalDirectoryReader::Read() noexcept LocalDirectoryReader::Read() noexcept
{ {
while (reader.ReadEntry()) { while (reader.ReadEntry()) {
const Path name_fs = reader.GetEntry(); const Path name_fs = reader.GetEntry();
if (SkipNameFS(name_fs.c_str())) if (PathTraitsFS::IsSpecialFilename(name_fs.c_str()))
continue; continue;
try { try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment