import struct


def is_sparseext4(filepath):
    """
    From ext4_utils/sparse_format.h:
    typedef struct sparse_header {
      __le32    magic;      /* 0xed26ff3a */
      __le16    major_version;  /* (0x1) - reject images with higher major versions */
      __le16    minor_version;  /* (0x0) - allow images with higer minor versions */
      __le16    file_hdr_sz;    /* 28 bytes for first revision of the file format */
      __le16    chunk_hdr_sz;   /* 12 bytes for first revision of the file format */
      __le32    blk_sz;     /* block size in bytes, must be a multiple of 4 (4096) */
      __le32    total_blks; /* total blocks in the non-sparse output image */
      __le32    total_chunks;   /* total chunks in the sparse input image */
      __le32    image_checksum; /* CRC32 checksum of the original data, counting "don't care" */
                    /* as 0. Standard 802.3 polynomial, use a Public Domain */
                    /* table implementation */
    } sparse_header_t;

    #define SPARSE_HEADER_MAGIC 0xed26ff3a
    """
    try:
        headerbytes = open(filepath, "br").read(4)
        (magic,) = struct.unpack("I", headerbytes)
        return magic == 0xed26ff3a
    except struct.error:
        return False