Skip to content

Commit

Permalink
Merge pull request #112 from AppImage/add_static_appimage_getType
Browse files Browse the repository at this point in the history
Add static appimage get type
  • Loading branch information
TheAssassin authored May 9, 2019
2 parents 0705fea + 7e083d7 commit 352e8a9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
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

0 comments on commit 352e8a9

Please sign in to comment.