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

Add static appimage get type #112

Merged
merged 3 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions include/appimage/core/AppImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ namespace appimage {
*/
AppImageFormat getFormat() const;

/**
* Inspect the magic bytes of the file to guess the AppImage <FORMAT>
* @return AppImage <FORMAT>
*/
static AppImageFormat getFormat(const std::string& path);

/**
* Calculate the offset in the AppImage file where is located the payload file system.
*
Expand Down
12 changes: 9 additions & 3 deletions src/libappimage/core/AppImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ namespace appimage {
return d->format;
}

AppImageFormat AppImage::getFormat(const std::string& path) {
return Private::getFormat(path);
}

AppImage::Private::Private(const std::string& path) : path(path) {
format = getFormat(path);

Expand All @@ -47,15 +51,17 @@ namespace appimage {
AppImageFormat AppImage::Private::getFormat(const std::string& path) {
utils::MagicBytesChecker magicBytesChecker(path);

if (!magicBytesChecker.hasElfSignature())
return AppImageFormat::INVALID;

if (magicBytesChecker.hasAppImageType1Signature())
return AppImageFormat::TYPE_1;

if (magicBytesChecker.hasAppImageType2Signature())
return AppImageFormat::TYPE_2;

if (magicBytesChecker.hasIso9660Signature() && magicBytesChecker.hasElfSignature()) {
std::cerr << "WARNING: " << path << " seems to be a Type 1 AppImage without magic bytes."
<< std::endl;
if (magicBytesChecker.hasIso9660Signature()) {
std::cerr << "WARNING: " << path << " seems to be a Type 1 AppImage without magic bytes." << std::endl;
return AppImageFormat::TYPE_1;
}

Expand Down
5 changes: 2 additions & 3 deletions src/libappimage/libappimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ extern "C" {
int appimage_get_type(const char* path, bool) {
typedef std::underlying_type<AppImageFormat>::type utype;
CATCH_ALL(
AppImage appImage(path);
return static_cast<utype>(appImage.getFormat());
const auto format = AppImage::getFormat(path);
return static_cast<utype>(format);
);

return static_cast<utype>(AppImageFormat::INVALID);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/libappimage/TestLibappimage++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ TEST_F(AppImageTests, getFormat) {
"/non_existend_file").getFormat(), core::AppImageError);
}

TEST_F(AppImageTests, getFormatStatic) {
ASSERT_EQ(core::AppImage::getFormat(TEST_DATA_DIR
"/AppImageExtract_6-x86_64.AppImage"), core::AppImageFormat::TYPE_1);
ASSERT_EQ(core::AppImage::getFormat(TEST_DATA_DIR
"/AppImageExtract_6_no_magic_bytes-x86_64.AppImage"), core::AppImageFormat::TYPE_1);
ASSERT_EQ(core::AppImage::getFormat(TEST_DATA_DIR
"/Echo-x86_64.AppImage"), core::AppImageFormat::TYPE_2);
ASSERT_EQ(core::AppImage::getFormat(TEST_DATA_DIR
"/appimaged-i686.AppImage"), core::AppImageFormat::TYPE_2);
ASSERT_EQ(core::AppImage::getFormat(TEST_DATA_DIR
"/elffile"), core::AppImageFormat::INVALID);
ASSERT_EQ(core::AppImage::getFormat(TEST_DATA_DIR
"/minimal.iso"), core::AppImageFormat::INVALID);
ASSERT_EQ(core::AppImage::getFormat(TEST_DATA_DIR
"/Cura.desktop"), core::AppImageFormat::INVALID);
ASSERT_EQ(core::AppImage::getFormat(TEST_DATA_DIR
"/non_existend_file"), core::AppImageFormat::INVALID);
}

TEST_F(AppImageTests, getPayloadOffset) {
ASSERT_EQ(core::AppImage(TEST_DATA_DIR
"/AppImageExtract_6-x86_64.AppImage").getPayloadOffset(), 28040);
Expand Down