Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect subpath checks in server/watcher.js #2245

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions server/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -233,7 +233,7 @@ class FolderWatcher extends EventEmitter {

checkShouldIgnorePath(path) {
return !!this.ignoreDirs.find(dirpath => {
return filePathToPOSIX(path).startsWith(dirpath)
return isSameOrSubPath(dirpath, path)
})
}

Expand Down
19 changes: 19 additions & 0 deletions server/utils/fileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down