Skip to content

Commit

Permalink
Added path checks to new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
TheQwertiest committed Feb 7, 2021
1 parent 384fa70 commit 718c070
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
43 changes: 39 additions & 4 deletions foo_spider_monkey_panel/js_objects/utils.cpp
Expand Up @@ -260,7 +260,15 @@ void JsUtils::EditTextFile( const std::wstring& path )

bool JsUtils::FileExists( const std::wstring& path )
{
return std::filesystem::exists( path );
namespace fs = std::filesystem;
try
{
return fs::exists( path );
}
catch ( const fs::filesystem_error& e )
{
throw qwr::QwrException( e );
}
}

JS::Value JsUtils::FileTest( const std::wstring& path, const std::wstring& mode )
Expand Down Expand Up @@ -422,7 +430,16 @@ JSObject* JsUtils::GetAlbumArtV2WithOpt( size_t optArgCount, JsFbMetadbHandle* h

uint64_t JsUtils::GetFileSize( const std::wstring& path )
{
return std::filesystem::file_size( path );
namespace fs = std::filesystem;
try
{
qwr::QwrException::ExpectTrue( fs::exists( path ), L"Path does not point to a file: {}", path );
return fs::file_size( path );
}
catch ( const fs::filesystem_error& e )
{
throw qwr::QwrException( e );
}
}

std::u8string JsUtils::GetPackagePath( const std::u8string& packageId )
Expand Down Expand Up @@ -545,12 +562,30 @@ std::u8string JsUtils::InputBoxWithOpt( size_t optArgCount, uint32_t hWnd, const

bool JsUtils::IsDirectory( const std::wstring& path )
{
return std::filesystem::is_directory( path );
namespace fs = std::filesystem;
try
{
qwr::QwrException::ExpectTrue( fs::exists( path ), L"Path does not point to a valid location: {}", path );
return fs::is_directory( path );
}
catch ( const fs::filesystem_error& e )
{
throw qwr::QwrException( e );
}
}

bool JsUtils::IsFile( const std::wstring& path )
{
return std::filesystem::is_regular_file( path );
namespace fs = std::filesystem;
try
{
qwr::QwrException::ExpectTrue( fs::exists( path ), L"Path does not point to a valid location: {}", path );
return fs::is_regular_file( path );
}
catch ( const fs::filesystem_error& e )
{
throw qwr::QwrException( e );
}
}

bool JsUtils::IsKeyPressed( uint32_t vkey )
Expand Down
1 change: 1 addition & 0 deletions foo_spider_monkey_panel/js_objects/utils.h
Expand Up @@ -38,6 +38,7 @@ class JsUtils
uint32_t DetectCharset( const std::wstring& path );
void EditTextFile( const std::wstring& path );
bool FileExists( const std::wstring& path );
// TODO v2: remove
JS::Value FileTest( const std::wstring& path, const std::wstring& mode );
std::u8string FormatDuration( double p );
std::u8string FormatFileSize( uint64_t p );
Expand Down

2 comments on commit 718c070

@marc2k3
Copy link
Contributor

@marc2k3 marc2k3 commented on 718c070 Feb 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IsFile/IsDirectory checks are wrong, If you feed them garbage, they're supposed to return false, not throw.

is_regular_file and is_directory are already tolerant enough not to need extra checks or try/catch. It's just the file_size that was an issue.

@TheQwertiest
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, should fixed now

Please sign in to comment.