Skip to content

Commit

Permalink
winget-source: rename index file after syncing is done (#113)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
taoky authored Jul 1, 2024
1 parent d4b9466 commit 2e548e5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 7 additions & 3 deletions winget-source/sync-repo.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) => {
Expand All @@ -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}`);
});
});
Expand Down
5 changes: 3 additions & 2 deletions winget-source/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>} 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)) {
Expand Down

0 comments on commit 2e548e5

Please sign in to comment.