-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add getLabel() to retrieve filesystem label
Change-type: minor Signed-off-by: Ken Bannister <[email protected]>
- Loading branch information
Showing
4 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { deepEqual } from 'assert'; | ||
import { FileDisk, withOpenFile } from 'file-disk'; | ||
import * as Fs from 'fs'; | ||
import * as Path from 'path'; | ||
import * as tmp from 'tmp'; | ||
import * as partitioninfo from 'partitioninfo'; | ||
|
||
import * as imagefs from '../lib'; | ||
|
||
const RASPBERRYPI = Path.join(__dirname, 'images', 'raspberrypi.img'); | ||
const MBR_FAT32 = Path.join(__dirname, 'images', 'mbr-fat32.img'); | ||
|
||
async function tmpFile(): Promise<{ path: string; cleanup: () => void }> { | ||
return await new Promise((resolve, reject) => { | ||
tmp.file( | ||
{ discardDescriptor: true }, | ||
(error: Error | null, path: string, _fd: number, cleanup: () => void) => { | ||
if (error != null) { | ||
reject(error); | ||
} else { | ||
resolve({ path, cleanup }); | ||
} | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
async function withFileCopy<T>( | ||
filePath: string, | ||
fn: (tmpFilePath: string) => Promise<T>, | ||
): Promise<T> { | ||
const { path, cleanup } = await tmpFile(); | ||
await Fs.promises.copyFile(filePath, path); | ||
try { | ||
return await fn(path); | ||
} finally { | ||
cleanup(); | ||
} | ||
} | ||
|
||
function testWithFileCopy( | ||
title: string, | ||
file: string, | ||
fn: (fileCopy: string) => Promise<void>, | ||
) { | ||
it(title, async () => { | ||
await withFileCopy(file, async (fileCopy: string) => { | ||
await fn(fileCopy); | ||
}); | ||
}); | ||
} | ||
|
||
function testFileDisk( | ||
title: string, | ||
image: { image: string; partition: number }, | ||
fn: ( | ||
disk: FileDisk, | ||
partition: partitioninfo.GPTPartition | partitioninfo.MBRPartition, | ||
) => Promise<void>, | ||
) { | ||
testWithFileCopy( | ||
`${title} (filedisk)`, | ||
image.image, | ||
async (fileCopy: string) => { | ||
await withOpenFile(fileCopy, 'r+', async (handle) => { | ||
const disk = new FileDisk(handle); | ||
const partition = await partitioninfo.get(disk, image.partition); | ||
await fn(disk, partition); | ||
}); | ||
}, | ||
); | ||
} | ||
|
||
testFileDisk( | ||
'should find label in MBR with FAT16 (0xB) partition', | ||
{ | ||
image: RASPBERRYPI, | ||
partition: 1, | ||
}, | ||
async ( | ||
disk: FileDisk, | ||
partition: partitioninfo.GPTPartition | partitioninfo.MBRPartition, | ||
) => { | ||
const label = await imagefs.getLabel(disk, partition); | ||
deepEqual(label, 'RESIN-BOOT'); | ||
}, | ||
); | ||
|
||
testFileDisk( | ||
'should find label in MBR with FAT32 (0xC) partition', | ||
{ | ||
image: MBR_FAT32, | ||
partition: 1, | ||
}, | ||
async ( | ||
disk: FileDisk, | ||
partition: partitioninfo.GPTPartition | partitioninfo.MBRPartition, | ||
) => { | ||
const label = await imagefs.getLabel(disk, partition); | ||
deepEqual(label, 'resin-boot'); | ||
}, | ||
); | ||
|
||
testFileDisk( | ||
'should find label in MBR with ext4 (0x83) partition', | ||
{ | ||
image: MBR_FAT32, | ||
partition: 6, | ||
}, | ||
async ( | ||
disk: FileDisk, | ||
partition: partitioninfo.GPTPartition | partitioninfo.MBRPartition, | ||
) => { | ||
const label = await imagefs.getLabel(disk, partition); | ||
deepEqual(label, 'resin-data'); | ||
}, | ||
); |
Binary file not shown.