Skip to content

Commit 2e548e5

Browse files
authored
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 ustclug/discussions#435 (comment). * winget-source: use .tmp instead of .bak in syncFile
1 parent d4b9466 commit 2e548e5

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

winget-source/sync-repo.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import async from 'async'
2-
import { rm } from 'fs/promises'
2+
import { rm, rename } from 'fs/promises'
33
import { EX_IOERR, EX_TEMPFAIL, EX_UNAVAILABLE } from './sysexits.js';
44

55
import {
@@ -17,9 +17,12 @@ const { parallelLimit, remote, sqlite3, winston } = setupEnvironment();
1717

1818
winston.info(`start syncing with ${remote}`);
1919

20-
syncFile('source.msix').catch(exitOnError(EX_UNAVAILABLE)).then(async _ => {
20+
const tmpSourceFilename = 'source.msix.tmp';
21+
const sourceFilename = 'source.msix';
22+
23+
syncFile(sourceFilename, false, true).catch(exitOnError(EX_UNAVAILABLE)).then(async _ => {
2124
const temp = await makeTempDirectory('winget-repo-');
22-
const database = await extractDatabaseFromBundle(getLocalPath('source.msix'), temp);
25+
const database = await extractDatabaseFromBundle(getLocalPath(tmpSourceFilename), temp);
2326
const db = new sqlite3.Database(database, sqlite3.OPEN_READONLY, exitOnError(EX_IOERR));
2427

2528
db.all('SELECT * FROM pathparts', (error, rows) => {
@@ -31,6 +34,7 @@ syncFile('source.msix').catch(exitOnError(EX_UNAVAILABLE)).then(async _ => {
3134
async.eachLimit(uris, parallelLimit, download, (error) => {
3235
rm(temp, { recursive: true });
3336
exitOnError(EX_TEMPFAIL)(error);
37+
rename(getLocalPath(tmpSourceFilename), getLocalPath(sourceFilename));
3438
winston.info(`successfully synced with ${remote}`);
3539
});
3640
});

winget-source/utilities.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,12 @@ export function setupEnvironment() {
245245
*
246246
* @param {string} uri URI to sync.
247247
* @param {boolean} update Whether to allow updating an existing file.
248+
* @param {boolean} saveAsTmp Whether to save with ".tmp" suffix
248249
*
249250
* @returns {Promise<boolean>} If the file is new or updated.
250251
*/
251-
export async function syncFile(uri, update = true) {
252-
const localPath = getLocalPath(uri);
252+
export async function syncFile(uri, update = true, saveAsTmp = false) {
253+
const localPath = getLocalPath(saveAsTmp ? uri + ".tmp" : uri);
253254
const remoteURL = getRemoteURL(uri);
254255
await mkdir(path.dirname(localPath), { recursive: true });
255256
if (existsSync(localPath)) {

0 commit comments

Comments
 (0)