bugfix: #503 Fix possible race condition in onfileindir* (#576)

This commit is contained in:
James Read 2025-04-26 21:42:12 +01:00 committed by GitHub
parent 633e513697
commit 0a7f3f3226
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 10 deletions

View File

@ -2,26 +2,32 @@ package onfileindir
import ( import (
"fmt" "fmt"
"os"
"path/filepath"
"github.com/OliveTin/OliveTin/internal/acl" "github.com/OliveTin/OliveTin/internal/acl"
"github.com/OliveTin/OliveTin/internal/config" "github.com/OliveTin/OliveTin/internal/config"
"github.com/OliveTin/OliveTin/internal/executor" "github.com/OliveTin/OliveTin/internal/executor"
"github.com/OliveTin/OliveTin/internal/filehelper" "github.com/OliveTin/OliveTin/internal/filehelper"
"os"
"path/filepath"
) )
func WatchFilesInDirectory(cfg *config.Config, ex *executor.Executor) { func WatchFilesInDirectory(cfg *config.Config, ex *executor.Executor) {
for _, action := range cfg.Actions { for _, action := range cfg.Actions {
for _, dirname := range action.ExecOnFileChangedInDir { for _, dirname := range action.ExecOnFileChangedInDir {
go filehelper.WatchDirectoryWrite(dirname, func(filename string) { // Pass values into anonymous function because of this issue
scheduleExec(action, cfg, ex, filename) // https://github.com/OliveTin/OliveTin/issues/503
})
}
for _, dirname := range action.ExecOnFileCreatedInDir { go func(act *config.Action, dir string) {
go filehelper.WatchDirectoryCreate(dirname, func(filename string) { filehelper.WatchDirectoryWrite(dir, func(filename string) {
scheduleExec(action, cfg, ex, filename) scheduleExec(act, cfg, ex, filename)
}) })
}(action, dirname)
go func(act *config.Action, dir string) {
filehelper.WatchDirectoryCreate(dir, func(filename string) {
scheduleExec(act, cfg, ex, filename)
})
}(action, dirname)
} }
} }
} }