Fix behavior for non-traversable directories (#108)

fix(index): Continue on errors during find

This ensures the gallery is created even when some files are
inaccessible (for example due to invalid names or file permissions).

Requires running the test in Docker as non-root to allow file access test
pull/118/head
Felix Eckhofer 6 years ago committed by Romain
parent bd86ab6535
commit 81a85004c2

@ -1,10 +1,18 @@
# Node.js + build dependencies + runtime dependencies
FROM thumbsupgallery/build:alpine
# Switch to a non-root user
# So we can test edge-cases around file permissions
RUN adduser -D tester
RUN chown -R tester:tester /app
USER tester
# Install and cache dependencies
COPY package.json /app
COPY --chown=tester package.json /app
RUN npm install
# Copy the entire source code
COPY --chown=tester . /app
# Run the tests
COPY . /app
RUN scripts/cibuild

@ -19,14 +19,7 @@ exports.find = function (rootFolder, callback) {
sep: '/'
})
stream.on('data', stats => { entries[stats.path] = stats.mtime.getTime() })
stream.on('error', err => {
if (isEnoentCodeError(err)) {
warn(`File doesn't exist, skipping: ${err.path}`)
if (err.path === rootFolder) callback(null, {})
} else {
callback(err)
}
})
stream.on('error', err => warn(err.message))
stream.on('end', () => callback(null, entries))
}
@ -40,7 +33,3 @@ function canTraverse (folder) {
})
return match.length > 0
}
function isEnoentCodeError (err) {
return err.code === 'ENOENT'
}

@ -154,4 +154,24 @@ describe('Index: glob', function () {
done()
})
})
it('ignores non-traversable directories', (done) => {
mock({
'media/secret': mock.directory({
mode: 0,
items: {
'IMG_0001.jpg': '...'
}
}),
'media/IMG_0002.jpg': '...'
})
glob.find('media', (err, map) => {
if (err) return done(err)
const keys = Object.keys(map).sort()
should(keys).eql([
'IMG_0002.jpg'
])
done()
})
})
})

Loading…
Cancel
Save