( directory: DirectoryBinding, kind: MemoryBackupKind, bytes: Uint8Array, )
| 844 | } |
| 845 | |
| 846 | async function writeBackupBytes( |
| 847 | directory: DirectoryBinding, |
| 848 | kind: MemoryBackupKind, |
| 849 | bytes: Uint8Array, |
| 850 | ): Promise<void> { |
| 851 | const destination = backupPath(directory.path, kind); |
| 852 | await assertRegularOrMissing(destination, BACKUP_FILES[kind]); |
| 853 | const temporaryPath = `${destination}.${randomUUID()}.tmp`; |
| 854 | let handle: Awaited<ReturnType<typeof open>> | undefined; |
| 855 | try { |
| 856 | handle = await open(temporaryPath, 'wx', 0o600); |
| 857 | await handle.writeFile(bytes); |
| 858 | await handle.sync(); |
| 859 | await handle.close(); |
| 860 | handle = undefined; |
| 861 | await assertDirectoryBinding(directory); |
| 862 | await rename(temporaryPath, destination); |
| 863 | await chmod(destination, 0o600); |
| 864 | await syncDirectory(directory.path); |
| 865 | await assertDirectoryBinding(directory); |
| 866 | } finally { |
| 867 | if (handle) await handle.close(); |
| 868 | await rm(temporaryPath, { force: true }); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | async function rotateRestoreHistory(directory: DirectoryBinding): Promise<void> { |
| 873 | for (let index = RESTORE_HISTORY_LIMIT - 1; index >= 1; index -= 1) { |
no test coverage detected