]> Cypherpunks.ru repositories - goredo.git/blobdiff - cleanup.go
Download link for 2.6.2 release
[goredo.git] / cleanup.go
index 09a72203ad2be8bcb29162128fcfe8fc9be78dc3..efc2e97e9f5956b4fe16b0c3465141a7349479b3 100644 (file)
@@ -1,19 +1,17 @@
-/*
-goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2022 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
-the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
+// goredo -- djb's redo implementation on pure Go
+// Copyright (C) 2020-2024 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
+// the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 package main
 
@@ -24,7 +22,6 @@ import (
        "log"
        "os"
        "path"
-       "path/filepath"
        "strings"
 )
 
@@ -45,57 +42,54 @@ func init() {
 }
 
 func redoDirClean(root, what string) error {
-       root, err := filepath.Abs(root)
-       if err != nil {
-               panic(err)
-       }
+       root = mustAbs(root)
        dir, err := os.Open(root)
        if err != nil {
-               return err
+               return ErrLine(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
                        }
-                       return err
+                       return ErrLine(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
+                                                       return ErrLine(err)
                                                }
                                        }
                                }
                        case CleanupLock:
-                               if strings.HasSuffix(fi.Name(), LockSuffix) {
+                               if strings.HasSuffix(entry.Name(), LockSuffix) {
                                        fmt.Println(pth)
                                        if !*DryRun {
                                                if err = os.Remove(pth); err != nil {
-                                                       return err
+                                                       return ErrLine(err)
                                                }
                                        }
                                }
                        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 {
-                                                       return err
+                                                       return ErrLine(err)
                                                }
                                        }
                                }
                        default:
-                               log.Fatalln("unknown cleanup target")
+                               log.Fatal("unknown cleanup target")
                        }
                }
        }
@@ -103,41 +97,38 @@ func redoDirClean(root, what string) error {
 }
 
 func cleanupWalker(root, what string) error {
-       root, err := filepath.Abs(root)
-       if err != nil {
-               panic(err)
-       }
+       root = mustAbs(root)
        dir, err := os.Open(root)
        if err != nil {
-               return err
+               return ErrLine(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
                        }
-                       return err
+                       return ErrLine(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 {
-                                                       err = os.RemoveAll(pth)
+                                                       err = ErrLine(os.RemoveAll(pth))
                                                }
                                        } else {
                                                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)
+                                               err = ErrLine(os.RemoveAll(pth))
                                        }
                                } else {
                                        err = cleanupWalker(pth, what)
@@ -148,11 +139,11 @@ 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 {
-                                               return err
+                                               return ErrLine(err)
                                        }
                                }
                        }