diff --git a/src/populateCache.ts b/src/populateCache.ts index de1d8fb..86c7316 100644 --- a/src/populateCache.ts +++ b/src/populateCache.ts @@ -4,7 +4,7 @@ import fs from 'fs/promises'; import git from 'isomorphic-git'; import http from 'isomorphic-git/http/node'; -import { isWriteable, pointsToLFS } from './util'; +import { isVacantAndWriteable, isWriteable, pointsToLFS } from './util'; import downloadBlobFromPointer from './download'; import { readPointer } from "./pointers"; @@ -55,8 +55,9 @@ export default async function populateCache(workDir: string, ref: string = 'HEAD if (pointsToLFS(buff)) { const pointer = readPointer({ dir: workDir, content: buff }); - // Don’t even start the download if LFS cache path is not accessible. - if (await isWriteable(pointer.objectPath) === false) + // Don’t even start the download if LFS cache path is not accessible, + // or if it already exists + if (await isVacantAndWriteable(pointer.objectPath) === false) return; const content = await downloadBlobFromPointer( diff --git a/src/util.ts b/src/util.ts index b7977d4..cb73787 100644 --- a/src/util.ts +++ b/src/util.ts @@ -20,12 +20,27 @@ export async function isWriteable(filepath: string): Promise { await fs.access(filepath, fsConstants.W_OK); return true; } catch (e) { - if ((e as { code: string }).code !== 'ENOENT') { - return false; - } else { + if ((e as { code: string }).code === 'ENOENT') { + return true; + } + return false; + } +} + + +/** + * Returns true if given path is available for writing + * and not occupied. + */ +export async function isVacantAndWriteable(filepath: string): Promise { + try { + await fs.access(filepath, fsConstants.W_OK); + } catch (e) { + if ((e as { code: string }).code === 'ENOENT') { return true; } } + return false; }