Skip to content

Commit

Permalink
LibCore: Add File::is_directory() helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Feb 9, 2020
1 parent 80b1af2 commit 67ccdbe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Libraries/LibCore/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

namespace Core {
Expand Down Expand Up @@ -83,4 +84,20 @@ bool File::open(IODevice::OpenMode mode)
return true;
}

bool File::is_directory() const
{
struct stat stat;
if (fstat(fd(), &stat) < 0)
return false;
return S_ISDIR(stat.st_mode);
}

bool File::is_directory(const String& filename)
{
struct stat st;
if (stat(filename.characters(), &st) < 0)
return false;
return S_ISDIR(st.st_mode);
}

}
3 changes: 3 additions & 0 deletions Libraries/LibCore/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class File final : public IODevice {
String filename() const { return m_filename; }
void set_filename(const StringView& filename) { m_filename = filename; }

bool is_directory() const;
static bool is_directory(const String& filename);

virtual bool open(IODevice::OpenMode) override;

enum class ShouldCloseFileDescription {
Expand Down

0 comments on commit 67ccdbe

Please sign in to comment.