Skip to content

Commit e3a2928

Browse files
authored
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
1 parent 78f0b8b commit e3a2928

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

lib/content/read.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function read (cache, integrity, opts = {}) {
1313
const { size } = opts
1414
const { stat, cpath, sri } = await withContentSri(cache, integrity, async (cpath, sri) => {
1515
// get size
16-
const stat = await fs.lstat(cpath)
16+
const stat = await fs.stat(cpath)
1717
return { stat, cpath, sri }
1818
})
1919
if (typeof size === 'number' && stat.size !== size) {
@@ -73,8 +73,8 @@ function readStream (cache, integrity, opts = {}) {
7373
// Set all this up to run on the stream and then just return the stream
7474
Promise.resolve().then(async () => {
7575
const { stat, cpath, sri } = await withContentSri(cache, integrity, async (cpath, sri) => {
76-
// just lstat to ensure it exists
77-
const stat = await fs.lstat(cpath)
76+
// just stat to ensure it exists
77+
const stat = await fs.stat(cpath)
7878
return { stat, cpath, sri }
7979
})
8080
if (typeof size === 'number' && size !== stat.size) {
@@ -111,7 +111,7 @@ async function hasContent (cache, integrity) {
111111

112112
try {
113113
return await withContentSri(cache, integrity, async (cpath, sri) => {
114-
const stat = await fs.lstat(cpath)
114+
const stat = await fs.stat(cpath)
115115
return { size: stat.size, sri, stat }
116116
})
117117
} catch (err) {
@@ -139,7 +139,7 @@ function hasContentSync (cache, integrity) {
139139

140140
return withContentSriSync(cache, integrity, (cpath, sri) => {
141141
try {
142-
const stat = fs.lstatSync(cpath)
142+
const stat = fs.statSync(cpath)
143143
return { size: stat.size, sri, stat }
144144
} catch (err) {
145145
if (err.code === 'ENOENT') {

test/content/read.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ permissionError.code = 'EPERM'
1717

1818
// helpers
1919
const getRead = (t, opts) => t.mock('../../lib/content/read', opts)
20-
const getReadLstatFailure = (t, err) => getRead(t, {
20+
const getReadStatFailure = (t, err) => getRead(t, {
2121
'@npmcli/fs': Object.assign({}, require('@npmcli/fs'), {
22-
async lstat (path) {
22+
async stat (path) {
2323
throw err
2424
},
25-
lstatSync () {
25+
statSync () {
2626
throw err
2727
},
2828
}),
@@ -261,7 +261,7 @@ t.test('read: opening large files', function (t) {
261261
const CACHE = t.testdir()
262262
const mockedRead = getRead(t, {
263263
'@npmcli/fs': Object.assign({}, require('@npmcli/fs'), {
264-
async lstat (path) {
264+
async stat (path) {
265265
return { size: Number.MAX_SAFE_INTEGER }
266266
},
267267
}),
@@ -370,7 +370,7 @@ t.test('hasContent: tests content existence', (t) => {
370370
t.test('hasContent: permission error', (t) => {
371371
const CACHE = t.testdir()
372372
// setup a syntetic permission error
373-
const mockedRead = getReadLstatFailure(t, permissionError)
373+
const mockedRead = getReadStatFailure(t, permissionError)
374374

375375
t.plan(1)
376376
t.rejects(
@@ -382,7 +382,7 @@ t.test('hasContent: permission error', (t) => {
382382

383383
t.test('hasContent: generic error', (t) => {
384384
const CACHE = t.testdir()
385-
const mockedRead = getReadLstatFailure(t, genericError)
385+
const mockedRead = getReadStatFailure(t, genericError)
386386

387387
t.plan(1)
388388
t.resolves(
@@ -426,7 +426,7 @@ t.test('hasContent.sync: checks content existence synchronously', (t) => {
426426

427427
t.test('hasContent.sync: permission error', (t) => {
428428
const CACHE = t.testdir()
429-
const mockedRead = getReadLstatFailure(t, permissionError)
429+
const mockedRead = getReadStatFailure(t, permissionError)
430430

431431
t.throws(
432432
() => mockedRead.hasContent.sync(CACHE, 'sha1-deadbeef sha1-13371337'),
@@ -438,7 +438,7 @@ t.test('hasContent.sync: permission error', (t) => {
438438

439439
t.test('hasContent.sync: generic error', (t) => {
440440
const CACHE = t.testdir()
441-
const mockedRead = getReadLstatFailure(t, genericError)
441+
const mockedRead = getReadStatFailure(t, genericError)
442442

443443
t.notOk(
444444
mockedRead.hasContent.sync(CACHE, 'sha1-deadbeef sha1-13371337'),

0 commit comments

Comments
 (0)