Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed FilesystemPath::isFile return value. #491

Merged
merged 2 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# dev

* Fix: FilesystemPath::isFile ([#490] (https://github.com/avast-tl/retdec/issues/490)).
* New Feature: Added presentation of imported types and TypeRef hashes for .NET binaries ([#363](https://github.com/avast-tl/retdec/issues/363), [#364](https://github.com/avast-tl/retdec/issues/364), [#428](https://github.com/avast-tl/retdec/issues/428)).
* New Feature: Added computation and presentation of icon hashes for exact and also similarity matching in PE files ([#339](https://github.com/avast-tl/retdec/issues/339)).
* Enhancement: Added support for build and run on FreeBSD and potentially on other BSD OSes ([#476](https://github.com/avast-tl/retdec/pull/476)).
Expand Down
12 changes: 10 additions & 2 deletions src/utils/filesystem_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ class FilesystemPathImplWindows : public FilesystemPathImpl

virtual bool isFile() override
{
return !isDirectory();
WIN32_FIND_DATA ffd;
Copy link
Member

Choose a reason for hiding this comment

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

Instead of FindFirstFile(), wouldn't it be easier and more straightforward to use just GetFileAttributes()?

virtual bool isFile() override
{
	auto attrs = GetFileAttributes(_path.c_str());
	return (attrs != INVALID_FILE_ATTRIBUTES &&
		(attrs & FILE_ATTRIBUTE_DIRECTORY) == 0);
}

I am not a Windows user, but GetFileAttributes() kind of more resembles the POSIX stat() function.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @s3rvac that GetFileAttributes makes more sense here than FindFirstFile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have implemented it as suggested and did a forced update. At first I thought that the above would incorrectly return true on INVALID_FILE_ATTRIBUTES since it is defined as -1. However, I see that the logical && and == causes it to indeed function properly.

if (FindFirstFile(_path.c_str(), &ffd) == reinterpret_cast<HANDLE>(-1))
s3rvac marked this conversation as resolved.
Show resolved Hide resolved
return false;

return !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
}

virtual bool isDirectory() override
Expand Down Expand Up @@ -231,7 +235,11 @@ class FilesystemPathImplUnix : public FilesystemPathImpl

virtual bool isFile() override
{
return !isDirectory();
struct stat st;
if (stat(_path.c_str(), &st) != 0)
return false;

return S_ISREG(st.st_mode);
}

virtual bool isDirectory() override
Expand Down