X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=cleanup.go;h=d0b508d44201a7645717f77192b653cd5f4f30ef;hb=4631e48a3b576942454e5534cd269ff3f5c1a56d;hp=e00e3ff299842929f37ea8a5129abe82d36b68ea;hpb=0d0e69d21056fe2c199beb7e7ce82572ae688825;p=goredo.git diff --git a/cleanup.go b/cleanup.go index e00e3ff..d0b508d 100644 --- a/cleanup.go +++ b/cleanup.go @@ -1,6 +1,6 @@ /* goredo -- djb's redo implementation on pure Go -Copyright (C) 2020-2022 Sergey Matveev +Copyright (C) 2020-2023 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -32,6 +32,7 @@ const ( CleanupFull = "full" CleanupLog = "log" CleanupTmp = "tmp" + CleanupLock = "lock" ) var DryRun *bool @@ -52,8 +53,9 @@ func redoDirClean(root, what string) error { if err != nil { return err } + defer dir.Close() for { - fis, err := dir.Readdir(1 << 10) + entries, err := dir.ReadDir(1 << 10) if err != nil { if err == io.EOF { break @@ -61,12 +63,21 @@ func redoDirClean(root, what string) error { return err } var pth string - for _, fi := range fis { - pth = cwdMustRel(root, fi.Name()) + for _, entry := range entries { + pth = cwdMustRel(root, entry.Name()) switch what { case CleanupLog: - if strings.HasSuffix(fi.Name(), LogSuffix) || - strings.HasSuffix(fi.Name(), LogRecSuffix) { + if strings.HasSuffix(entry.Name(), LogSuffix) || + strings.HasSuffix(entry.Name(), LogRecSuffix) { + fmt.Println(pth) + if !*DryRun { + if err = os.Remove(pth); err != nil { + return err + } + } + } + case CleanupLock: + if strings.HasSuffix(entry.Name(), LockSuffix) { fmt.Println(pth) if !*DryRun { if err = os.Remove(pth); err != nil { @@ -75,7 +86,7 @@ func redoDirClean(root, what string) error { } } case CleanupTmp: - if strings.HasPrefix(fi.Name(), TmpPrefix) { + if strings.HasPrefix(entry.Name(), TmpPrefix) { fmt.Println(pth) if !*DryRun { if err = os.Remove(pth); err != nil { @@ -88,7 +99,7 @@ func redoDirClean(root, what string) error { } } } - return dir.Close() + return nil } func cleanupWalker(root, what string) error { @@ -102,18 +113,18 @@ func cleanupWalker(root, what string) error { } defer dir.Close() for { - fis, err := dir.Readdir(1 << 10) + entries, err := dir.ReadDir(1 << 10) if err != nil { if err == io.EOF { break } return err } - for _, fi := range fis { - pth := path.Join(root, fi.Name()) - pthRel := cwdMustRel(root, fi.Name()) - if fi.IsDir() { - if fi.Name() == RedoDir { + for _, entry := range entries { + pth := path.Join(root, entry.Name()) + pthRel := cwdMustRel(root, entry.Name()) + if entry.IsDir() { + if entry.Name() == RedoDir { if what == CleanupFull { fmt.Println(pthRel) if !*DryRun { @@ -123,7 +134,7 @@ func cleanupWalker(root, what string) error { err = redoDirClean(pth, what) } } else if (what == CleanupTmp || what == CleanupFull) && - strings.HasPrefix(fi.Name(), TmpPrefix) { + strings.HasPrefix(entry.Name(), TmpPrefix) { fmt.Println(pthRel) if !*DryRun { err = os.RemoveAll(pth) @@ -137,7 +148,7 @@ func cleanupWalker(root, what string) error { continue } if (what == CleanupTmp || what == CleanupFull) && - strings.HasPrefix(fi.Name(), TmpPrefix) { + strings.HasPrefix(entry.Name(), TmpPrefix) { fmt.Println(pthRel) if !*DryRun { if err = os.Remove(pth); err != nil { @@ -147,5 +158,5 @@ func cleanupWalker(root, what string) error { } } } - return dir.Close() + return nil }