Now actually fix race condition

This commit is contained in:
2024-08-12 00:42:21 +02:00
parent 1d2a7aac17
commit 722ba3e1af

12
main.go
View File

@@ -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)