From e3a2928e053e19fb6e8e73946ffe3d212e402ba7 Mon Sep 17 00:00:00 2001 From: Yorick Date: Thu, 2 Jun 2022 15:57:29 +0200 Subject: [PATCH] fix(read): change lstat to stat to correctly evaluate file size (#114) * fix(read): change lstat to stat to support symlinks in the cache * fix(test/read): lstat -> stat --- lib/content/read.js | 10 +++++----- test/content/read.js | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/content/read.js b/lib/content/read.js index f5128fe1..8367ccb2 100644 --- a/lib/content/read.js +++ b/lib/content/read.js @@ -13,7 +13,7 @@ async function read (cache, integrity, opts = {}) { const { size } = opts const { stat, cpath, sri } = await withContentSri(cache, integrity, async (cpath, sri) => { // get size - const stat = await fs.lstat(cpath) + const stat = await fs.stat(cpath) return { stat, cpath, sri } }) if (typeof size === 'number' && stat.size !== size) { @@ -73,8 +73,8 @@ function readStream (cache, integrity, opts = {}) { // Set all this up to run on the stream and then just return the stream Promise.resolve().then(async () => { const { stat, cpath, sri } = await withContentSri(cache, integrity, async (cpath, sri) => { - // just lstat to ensure it exists - const stat = await fs.lstat(cpath) + // just stat to ensure it exists + const stat = await fs.stat(cpath) return { stat, cpath, sri } }) if (typeof size === 'number' && size !== stat.size) { @@ -111,7 +111,7 @@ async function hasContent (cache, integrity) { try { return await withContentSri(cache, integrity, async (cpath, sri) => { - const stat = await fs.lstat(cpath) + const stat = await fs.stat(cpath) return { size: stat.size, sri, stat } }) } catch (err) { @@ -139,7 +139,7 @@ function hasContentSync (cache, integrity) { return withContentSriSync(cache, integrity, (cpath, sri) => { try { - const stat = fs.lstatSync(cpath) + const stat = fs.statSync(cpath) return { size: stat.size, sri, stat } } catch (err) { if (err.code === 'ENOENT') { diff --git a/test/content/read.js b/test/content/read.js index 79431fe7..aad4e8f9 100644 --- a/test/content/read.js +++ b/test/content/read.js @@ -17,12 +17,12 @@ permissionError.code = 'EPERM' // helpers const getRead = (t, opts) => t.mock('../../lib/content/read', opts) -const getReadLstatFailure = (t, err) => getRead(t, { +const getReadStatFailure = (t, err) => getRead(t, { '@npmcli/fs': Object.assign({}, require('@npmcli/fs'), { - async lstat (path) { + async stat (path) { throw err }, - lstatSync () { + statSync () { throw err }, }), @@ -261,7 +261,7 @@ t.test('read: opening large files', function (t) { const CACHE = t.testdir() const mockedRead = getRead(t, { '@npmcli/fs': Object.assign({}, require('@npmcli/fs'), { - async lstat (path) { + async stat (path) { return { size: Number.MAX_SAFE_INTEGER } }, }), @@ -370,7 +370,7 @@ t.test('hasContent: tests content existence', (t) => { t.test('hasContent: permission error', (t) => { const CACHE = t.testdir() // setup a syntetic permission error - const mockedRead = getReadLstatFailure(t, permissionError) + const mockedRead = getReadStatFailure(t, permissionError) t.plan(1) t.rejects( @@ -382,7 +382,7 @@ t.test('hasContent: permission error', (t) => { t.test('hasContent: generic error', (t) => { const CACHE = t.testdir() - const mockedRead = getReadLstatFailure(t, genericError) + const mockedRead = getReadStatFailure(t, genericError) t.plan(1) t.resolves( @@ -426,7 +426,7 @@ t.test('hasContent.sync: checks content existence synchronously', (t) => { t.test('hasContent.sync: permission error', (t) => { const CACHE = t.testdir() - const mockedRead = getReadLstatFailure(t, permissionError) + const mockedRead = getReadStatFailure(t, permissionError) t.throws( () => mockedRead.hasContent.sync(CACHE, 'sha1-deadbeef sha1-13371337'), @@ -438,7 +438,7 @@ t.test('hasContent.sync: permission error', (t) => { t.test('hasContent.sync: generic error', (t) => { const CACHE = t.testdir() - const mockedRead = getReadLstatFailure(t, genericError) + const mockedRead = getReadStatFailure(t, genericError) t.notOk( mockedRead.hasContent.sync(CACHE, 'sha1-deadbeef sha1-13371337'),