From 2e548e5e974dd014115960440cc61c690121fdeb Mon Sep 17 00:00:00 2001 From: taoky Date: Mon, 1 Jul 2024 09:34:43 +0800 Subject: [PATCH] winget-source: rename index file after syncing is done (#113) * winget-source: rename index file after syncing is done Try fixing the issue mentioned in https://github.com/ustclug/discussions/issues/435#issuecomment-2197544426. * winget-source: use .tmp instead of .bak in syncFile --- winget-source/sync-repo.js | 10 +++++++--- winget-source/utilities.js | 5 +++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/winget-source/sync-repo.js b/winget-source/sync-repo.js index 80bfea6..0615ac8 100644 --- a/winget-source/sync-repo.js +++ b/winget-source/sync-repo.js @@ -1,5 +1,5 @@ import async from 'async' -import { rm } from 'fs/promises' +import { rm, rename } from 'fs/promises' import { EX_IOERR, EX_TEMPFAIL, EX_UNAVAILABLE } from './sysexits.js'; import { @@ -17,9 +17,12 @@ const { parallelLimit, remote, sqlite3, winston } = setupEnvironment(); winston.info(`start syncing with ${remote}`); -syncFile('source.msix').catch(exitOnError(EX_UNAVAILABLE)).then(async _ => { +const tmpSourceFilename = 'source.msix.tmp'; +const sourceFilename = 'source.msix'; + +syncFile(sourceFilename, false, true).catch(exitOnError(EX_UNAVAILABLE)).then(async _ => { const temp = await makeTempDirectory('winget-repo-'); - const database = await extractDatabaseFromBundle(getLocalPath('source.msix'), temp); + const database = await extractDatabaseFromBundle(getLocalPath(tmpSourceFilename), temp); const db = new sqlite3.Database(database, sqlite3.OPEN_READONLY, exitOnError(EX_IOERR)); db.all('SELECT * FROM pathparts', (error, rows) => { @@ -31,6 +34,7 @@ syncFile('source.msix').catch(exitOnError(EX_UNAVAILABLE)).then(async _ => { async.eachLimit(uris, parallelLimit, download, (error) => { rm(temp, { recursive: true }); exitOnError(EX_TEMPFAIL)(error); + rename(getLocalPath(tmpSourceFilename), getLocalPath(sourceFilename)); winston.info(`successfully synced with ${remote}`); }); }); diff --git a/winget-source/utilities.js b/winget-source/utilities.js index 4843765..45bf3c3 100644 --- a/winget-source/utilities.js +++ b/winget-source/utilities.js @@ -245,11 +245,12 @@ export function setupEnvironment() { * * @param {string} uri URI to sync. * @param {boolean} update Whether to allow updating an existing file. + * @param {boolean} saveAsTmp Whether to save with ".tmp" suffix * * @returns {Promise} If the file is new or updated. */ -export async function syncFile(uri, update = true) { - const localPath = getLocalPath(uri); +export async function syncFile(uri, update = true, saveAsTmp = false) { + const localPath = getLocalPath(saveAsTmp ? uri + ".tmp" : uri); const remoteURL = getRemoteURL(uri); await mkdir(path.dirname(localPath), { recursive: true }); if (existsSync(localPath)) {