dsh 0.2.0-rc.2: in-container update (experimental channel), path-agnostic compose, upstream patches adapted to 0.1.x/0.2.x
This commit is contained in:
commit
e6dcef9bf5
302 files changed
+75178
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { link, mkdir, open, rename, rm } from 'node:fs/promises'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { dirname } from 'node:path'
|
||||
|
||||
async function syncDirectory(path) {
|
||||
const handle = await open(path, 'r')
|
||||
try {
|
||||
await handle.sync()
|
||||
} finally {
|
||||
await handle.close()
|
||||
}
|
||||
}
|
||||
|
||||
async function temporaryFile(path, bytes, mode) {
|
||||
await mkdir(dirname(path), { recursive: true })
|
||||
const temporary = `${path}.${randomUUID()}.tmp`
|
||||
const handle = await open(temporary, 'wx', mode)
|
||||
try {
|
||||
await handle.writeFile(bytes)
|
||||
await handle.sync()
|
||||
} finally {
|
||||
await handle.close()
|
||||
}
|
||||
return temporary
|
||||
}
|
||||
|
||||
export async function durableReplace(path, bytes, mode = 0o600) {
|
||||
const temporary = await temporaryFile(path, bytes, mode)
|
||||
try {
|
||||
await rename(temporary, path)
|
||||
await syncDirectory(dirname(path))
|
||||
} catch (error) {
|
||||
await rm(temporary, { force: true })
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function durableCreate(path, bytes, mode = 0o600) {
|
||||
const temporary = await temporaryFile(path, bytes, mode)
|
||||
try {
|
||||
await link(temporary, path)
|
||||
await syncDirectory(dirname(path))
|
||||
} finally {
|
||||
await rm(temporary, { force: true })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user