From d546d8442f1aee1a7617ecaca4e145b3ae06d8e4 Mon Sep 17 00:00:00 2001 From: zvon Date: Sat, 18 Jan 2020 21:31:09 +0100 Subject: [PATCH] Filesystem: added some checks --- unix/filesystem.cpp | 7 +++++-- windows/filesystem.cpp | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/unix/filesystem.cpp b/unix/filesystem.cpp index f0019ae..ffaabfd 100644 --- a/unix/filesystem.cpp +++ b/unix/filesystem.cpp @@ -1,4 +1,5 @@ #include "../filesystem.hpp" +#include #include FSLib::Directory::Directory( const string &path_ ) : dir_path( path_ ) {} @@ -19,7 +20,8 @@ FSLib::Directory::Iterator::Iterator( const Directory &d_, : d( opendir( d_.path() ) ), current_entry( current_entry_ ) {} FSLib::Directory::Iterator::~Iterator() { - closedir( d ); + if( d ) + closedir( d ); } bool FSLib::exists( const string &path ) { @@ -44,7 +46,8 @@ string FSLib::canonical( const string &path ) { bool FSLib::isDirectory( const string &path ) { struct stat path_stat; - stat( path.c_str(), &path_stat ); + if( stat( path.c_str(), &path_stat ) != 0 ) + return false; return S_ISDIR( path_stat.st_mode ); } diff --git a/windows/filesystem.cpp b/windows/filesystem.cpp index 1c217c1..9d6e392 100644 --- a/windows/filesystem.cpp +++ b/windows/filesystem.cpp @@ -1,5 +1,6 @@ #include "../filesystem.hpp" #include +#include #include FSLib::Directory::Directory( const string &path_ ) : dir_path( path_ ) { @@ -63,7 +64,8 @@ string FSLib::canonical( const string &path ) { bool FSLib::isDirectory( const string &path ) { struct _stat path_stat; - _wstat( path.c_str(), &path_stat ); + if( _wstat( path.c_str(), &path_stat ) != 0 ) + return false; return path_stat.st_mode & _S_IFDIR; }