From c6eea8085b0bfae31de1d1ef17e2de822419cf27 Mon Sep 17 00:00:00 2001 From: Danny Lin Date: Thu, 16 Dec 2021 17:46:21 -0800 Subject: [PATCH] util: Add function to check for Android sparse image magic --- src/util/sparse.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/util/sparse.ts diff --git a/src/util/sparse.ts b/src/util/sparse.ts new file mode 100644 index 0000000..05bd66d --- /dev/null +++ b/src/util/sparse.ts @@ -0,0 +1,15 @@ +import { promises as fs } from 'fs' + +const FILE_MAGIC = 0xed26ff3a + +export async function isSparseImage(path: string) { + let file = await fs.open(path, 'r') + try { + let buf = new Uint32Array(1) + await file.read(buf, 0, 4) + + return buf[0] == FILE_MAGIC + } finally { + await file.close() + } +}