Merge pull request #16440 from overleaf/jpa-em-remove-extra-stat

[clsi] remove unnecessary stat call for checking each output file

GitOrigin-RevId: ba7fe435264596368808552a3da3e36d731eda09
This commit is contained in:
Jakob Ackermann 2024-01-11 08:13:02 +00:00 committed by Copybot
parent 94594701e3
commit a0f8a1b806

View file

@ -266,33 +266,25 @@ module.exports = OutputCacheManager = {
const newFile = _.clone(file) const newFile = _.clone(file)
const src = Path.join(compileDir, file.path) const src = Path.join(compileDir, file.path)
const dst = Path.join(cacheDir, file.path) const dst = Path.join(cacheDir, file.path)
OutputCacheManager._checkFileIsSafe(src, function (err, isSafe) { OutputCacheManager._checkIfShouldCopy(
if (err) { src,
return cb(err) function (err, shouldCopy) {
} if (err) {
if (!isSafe) { return cb(err)
return cb() }
} if (!shouldCopy) {
OutputCacheManager._checkIfShouldCopy( return cb()
src, }
function (err, shouldCopy) { OutputCacheManager._copyFile(src, dst, dirCache, err => {
if (err) { if (err) {
return cb(err) return cb(err)
} }
if (!shouldCopy) { newFile.build = buildId // attach a build id if we cached the file
return cb() results.push(newFile)
} cb()
OutputCacheManager._copyFile(src, dst, dirCache, err => { })
if (err) { }
return cb(err) )
}
newFile.build = buildId // attach a build id if we cached the file
results.push(newFile)
cb()
})
}
)
})
}, },
function (err) { function (err) {
if (err) { if (err) {
@ -497,26 +489,18 @@ module.exports = OutputCacheManager = {
function (file, cb) { function (file, cb) {
const src = Path.join(compileDir, file.path) const src = Path.join(compileDir, file.path)
const dst = Path.join(archiveDir, file.path) const dst = Path.join(archiveDir, file.path)
OutputCacheManager._checkFileIsSafe(src, function (err, isSafe) { OutputCacheManager._checkIfShouldArchive(
if (err) { src,
return cb(err) function (err, shouldArchive) {
} if (err) {
if (!isSafe) { return cb(err)
return cb()
}
OutputCacheManager._checkIfShouldArchive(
src,
function (err, shouldArchive) {
if (err) {
return cb(err)
}
if (!shouldArchive) {
return cb()
}
OutputCacheManager._copyFile(src, dst, dirCache, cb)
} }
) if (!shouldArchive) {
}) return cb()
}
OutputCacheManager._copyFile(src, dst, dirCache, cb)
}
)
}, },
callback callback
) )
@ -617,33 +601,6 @@ module.exports = OutputCacheManager = {
return path?.match(/^\.|\/\./) != null return path?.match(/^\.|\/\./) != null
}, },
_checkFileIsSafe(src, callback) {
// check if we have a valid file to copy into the cache
fs.stat(src, function (err, stats) {
if (err?.code === 'ENOENT') {
logger.warn(
{ err, file: src },
'file has disappeared before copying to build cache'
)
return callback(err, false)
} else if (err) {
// some other problem reading the file
logger.error({ err, file: src }, 'stat error for file in cache')
return callback(err, false)
} else if (!stats.isFile()) {
// other filetype - reject it
logger.warn(
{ src, stat: stats },
'nonfile output - refusing to copy to cache'
)
return callback(null, false)
} else {
// it's a plain file, ok to copy
return callback(null, true)
}
})
},
_ensureParentExists(dst, dirCache, callback) { _ensureParentExists(dst, dirCache, callback) {
let parent = Path.dirname(dst) let parent = Path.dirname(dst)
if (dirCache.has(parent)) { if (dirCache.has(parent)) {