From 4c69202c7e35334be117e9045ad7adbc8916787a Mon Sep 17 00:00:00 2001 From: Danny Lin Date: Thu, 16 Dec 2021 17:43:17 -0800 Subject: [PATCH] fs: Add sub-temp and mount APIs --- src/util/fs.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/util/fs.ts b/src/util/fs.ts index 99527eb..bc3e1fe 100644 --- a/src/util/fs.ts +++ b/src/util/fs.ts @@ -9,6 +9,7 @@ const TMP_PREFIX = 'adevtool-' export interface TempState { dir: string mounts: Array + rootTmp?: TempState } // https://stackoverflow.com/a/45130990 @@ -58,6 +59,20 @@ export async function withTempDir(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}`) +}