From 3289b6a88746c264aa4304cf4869ca345f90cc16 Mon Sep 17 00:00:00 2001 From: Ariel Don Date: Sun, 11 Jul 2021 15:38:55 -0500 Subject: [PATCH] LibCore: Implement File::is_link() It was already possible to check if a path was a directory or a device. Now, it is possible to check if a path is a link in a similar manner. --- Userland/Libraries/LibCore/File.cpp | 16 ++++++++++++++++ Userland/Libraries/LibCore/File.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index 0f30a995cb..8cdb47130a 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -130,6 +130,22 @@ bool File::is_directory(const String& filename) return S_ISDIR(st.st_mode); } +bool File::is_link() const +{ + struct stat stat; + if (fstat(fd(), &stat) < 0) + return false; + return S_ISLNK(stat.st_mode); +} + +bool File::is_link(const String& filename) +{ + struct stat st; + if (lstat(filename.characters(), &st) < 0) + return false; + return S_ISLNK(st.st_mode); +} + bool File::exists(const String& filename) { struct stat st; diff --git a/Userland/Libraries/LibCore/File.h b/Userland/Libraries/LibCore/File.h index cb81920d1d..86738aba94 100644 --- a/Userland/Libraries/LibCore/File.h +++ b/Userland/Libraries/LibCore/File.h @@ -30,6 +30,9 @@ public: bool is_device() const; static bool is_device(const String& filename); + bool is_link() const; + static bool is_link(const String& filename); + static bool exists(const String& filename); static bool ensure_parent_directories(const String& path);