]> Cypherpunks.ru repositories - goredo.git/blobdiff - cleanup.go
Use more efficient ReadDir instead of Readdir
[goredo.git] / cleanup.go
index 8c757aeebcf4c58d2b1e96ba5e93fa10e0c4b271..d0b508d44201a7645717f77192b653cd5f4f30ef 100644 (file)
@@ -1,6 +1,6 @@
 /*
 goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
 
 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 {
@@ -122,6 +133,12 @@ func cleanupWalker(root, what string) error {
                                        } else {
                                                err = redoDirClean(pth, what)
                                        }
+                               } else if (what == CleanupTmp || what == CleanupFull) &&
+                                       strings.HasPrefix(entry.Name(), TmpPrefix) {
+                                       fmt.Println(pthRel)
+                                       if !*DryRun {
+                                               err = os.RemoveAll(pth)
+                                       }
                                } else {
                                        err = cleanupWalker(pth, what)
                                }
@@ -131,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 {
@@ -141,5 +158,5 @@ func cleanupWalker(root, what string) error {
                        }
                }
        }
-       return dir.Close()
+       return nil
 }