Merge pull request #2245 from mikiher/watcher-fixes

Fix incorrect subpath checks in server/watcher.js
This commit is contained in:
advplyr 2023-10-23 17:28:44 -05:00 committed by GitHub
commit 0ee6336b02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -6,7 +6,7 @@ const LibraryScanner = require('./scanner/LibraryScanner')
const Task = require('./objects/Task')
const TaskManager = require('./managers/TaskManager')
const { filePathToPOSIX } = require('./utils/fileUtils')
const { filePathToPOSIX, isSameOrSubPath } = require('./utils/fileUtils')
/**
* @typedef PendingFileUpdate
@ -183,7 +183,7 @@ class FolderWatcher extends EventEmitter {
}
// Get file folder
const folder = libwatcher.folders.find(fold => path.startsWith(filePathToPOSIX(fold.fullPath)))
const folder = libwatcher.folders.find(fold => isSameOrSubPath(fold.fullPath, path))
if (!folder) {
Logger.error(`[Watcher] New file folder not found in library "${libwatcher.name}" with path "${path}"`)
return
@ -233,7 +233,7 @@ class FolderWatcher extends EventEmitter {
checkShouldIgnorePath(path) {
return !!this.ignoreDirs.find(dirpath => {
return filePathToPOSIX(path).startsWith(dirpath)
return isSameOrSubPath(dirpath, path)
})
}

View File

@ -19,6 +19,25 @@ const filePathToPOSIX = (path) => {
}
module.exports.filePathToPOSIX = filePathToPOSIX
/**
* Check path is a child of or equal to another path
*
* @param {string} parentPath
* @param {string} childPath
* @returns {boolean}
*/
function isSameOrSubPath(parentPath, childPath) {
parentPath = filePathToPOSIX(parentPath)
childPath = filePathToPOSIX(childPath)
if (parentPath === childPath) return true
const relativePath = Path.relative(parentPath, childPath)
return (
relativePath === '' // Same path (e.g. parentPath = '/a/b/', childPath = '/a/b')
|| !relativePath.startsWith('..') && !Path.isAbsolute(relativePath) // Sub path
)
}
module.exports.isSameOrSubPath = isSameOrSubPath
async function getFileStat(path) {
try {
var stat = await fs.stat(path)