Now actually fix race condition
This commit is contained in:
12
main.go
12
main.go
@@ -18,8 +18,7 @@ func init() {
|
|||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
var renamedFiles map[string]struct{} = make(map[string]struct{})
|
var renamedFiles sync.Map
|
||||||
var mapMutex sync.RWMutex
|
|
||||||
extensionRegex := regexp.MustCompile(`\.[a-zA-Z0-9]+$`)
|
extensionRegex := regexp.MustCompile(`\.[a-zA-Z0-9]+$`)
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
@@ -68,11 +67,8 @@ func main() {
|
|||||||
log.Printf("File %s already exists", newFile)
|
log.Printf("File %s already exists", newFile)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("%#v", newFile)
|
|
||||||
|
|
||||||
mapMutex.RLock()
|
_, ok := renamedFiles.Load(newFile)
|
||||||
_, ok := renamedFiles[newFile]
|
|
||||||
mapMutex.RUnlock()
|
|
||||||
if ok {
|
if ok {
|
||||||
log.Printf("File %s already renamed", newFile)
|
log.Printf("File %s already renamed", newFile)
|
||||||
return
|
return
|
||||||
@@ -82,10 +78,8 @@ func main() {
|
|||||||
log.Printf("File %s already has the correct name", file)
|
log.Printf("File %s already has the correct name", file)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
renamedFiles.Store(newFile, struct{}{})
|
||||||
log.Printf("%s -> %s", file, newFile)
|
log.Printf("%s -> %s", file, newFile)
|
||||||
mapMutex.Lock()
|
|
||||||
renamedFiles[newFile] = struct{}{}
|
|
||||||
mapMutex.Unlock()
|
|
||||||
err = os.Rename(file, newFile)
|
err = os.Rename(file, newFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error renaming file %s to %s: %s", file, newFile, err)
|
log.Printf("Error renaming file %s to %s: %s", file, newFile, err)
|
||||||
|
Reference in New Issue
Block a user