From 722ba3e1af4421f556ac9da49973a047fc1a83bc Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Mon, 12 Aug 2024 00:42:21 +0200 Subject: [PATCH] Now actually fix race condition --- main.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index e116098..5259d3e 100644 --- a/main.go +++ b/main.go @@ -18,8 +18,7 @@ func init() { func main() { flag.Parse() var wg sync.WaitGroup - var renamedFiles map[string]struct{} = make(map[string]struct{}) - var mapMutex sync.RWMutex + var renamedFiles sync.Map extensionRegex := regexp.MustCompile(`\.[a-zA-Z0-9]+$`) wg.Wait() @@ -68,11 +67,8 @@ func main() { log.Printf("File %s already exists", newFile) return } - log.Printf("%#v", newFile) - mapMutex.RLock() - _, ok := renamedFiles[newFile] - mapMutex.RUnlock() + _, ok := renamedFiles.Load(newFile) if ok { log.Printf("File %s already renamed", newFile) return @@ -82,10 +78,8 @@ func main() { log.Printf("File %s already has the correct name", file) return } + renamedFiles.Store(newFile, struct{}{}) log.Printf("%s -> %s", file, newFile) - mapMutex.Lock() - renamedFiles[newFile] = struct{}{} - mapMutex.Unlock() err = os.Rename(file, newFile) if err != nil { log.Printf("Error renaming file %s to %s: %s", file, newFile, err)