fs: Add sub-temp and mount APIs

This commit is contained in:
Danny Lin 2021-12-16 17:43:17 -08:00
parent 69d6f0e50f
commit 4c69202c7e

View file

@ -9,6 +9,7 @@ const TMP_PREFIX = 'adevtool-'
export interface TempState {
dir: string
mounts: Array<string>
rootTmp?: TempState
}
// https://stackoverflow.com/a/45130990
@ -58,6 +59,20 @@ export async function withTempDir<Return>(callback: (tmp: TempState) => Promise<
}
}
export async function createSubTmp(tmp: TempState, subpath: string) {
await fs.mkdir(`${tmp.dir}/${subpath}`, { recursive: true })
return {
...tmp,
dir: `${tmp.dir}/${subpath}`,
rootTmp: tmp.rootTmp ?? tmp,
} as TempState
}
export async function readFile(path: string) {
return await fs.readFile(path, { encoding: 'utf8' })
}
export async function mount(imgPath: string, mountpoint: string) {
await run(`mount -t ext4 -o ro ${imgPath} ${mountpoint}`)
}