Skip to content

Commit

Permalink
fix(read): change lstat to stat to correctly evaluate file size (#114)
Browse files Browse the repository at this point in the history
* fix(read): change lstat to stat to support symlinks in the cache

* fix(test/read): lstat -> stat
  • Loading branch information
yorickvP committed Jun 2, 2022
1 parent 78f0b8b commit e3a2928
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/content/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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') {
Expand Down
16 changes: 8 additions & 8 deletions test/content/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}),
Expand Down Expand Up @@ -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 }
},
}),
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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'),
Expand All @@ -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'),
Expand Down

0 comments on commit e3a2928

Please sign in to comment.