]> Cypherpunks.ru repositories - goredo.git/blob - cleanup.go
Download link for 2.6.2 release
[goredo.git] / cleanup.go
1 // goredo -- djb's redo implementation on pure Go
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package main
17
18 import (
19         "flag"
20         "fmt"
21         "io"
22         "log"
23         "os"
24         "path"
25         "strings"
26 )
27
28 const (
29         CleanupFull = "full"
30         CleanupLog  = "log"
31         CleanupTmp  = "tmp"
32         CleanupLock = "lock"
33 )
34
35 var DryRun *bool
36
37 func init() {
38         if CmdName() != CmdNameRedoCleanup {
39                 return
40         }
41         DryRun = flag.Bool("n", false, "do no delete files during cleanup, just show them")
42 }
43
44 func redoDirClean(root, what string) error {
45         root = mustAbs(root)
46         dir, err := os.Open(root)
47         if err != nil {
48                 return ErrLine(err)
49         }
50         defer dir.Close()
51         for {
52                 entries, err := dir.ReadDir(1 << 10)
53                 if err != nil {
54                         if err == io.EOF {
55                                 break
56                         }
57                         return ErrLine(err)
58                 }
59                 var pth string
60                 for _, entry := range entries {
61                         pth = cwdMustRel(root, entry.Name())
62                         switch what {
63                         case CleanupLog:
64                                 if strings.HasSuffix(entry.Name(), LogSuffix) ||
65                                         strings.HasSuffix(entry.Name(), LogRecSuffix) {
66                                         fmt.Println(pth)
67                                         if !*DryRun {
68                                                 if err = os.Remove(pth); err != nil {
69                                                         return ErrLine(err)
70                                                 }
71                                         }
72                                 }
73                         case CleanupLock:
74                                 if strings.HasSuffix(entry.Name(), LockSuffix) {
75                                         fmt.Println(pth)
76                                         if !*DryRun {
77                                                 if err = os.Remove(pth); err != nil {
78                                                         return ErrLine(err)
79                                                 }
80                                         }
81                                 }
82                         case CleanupTmp:
83                                 if strings.HasPrefix(entry.Name(), TmpPrefix) {
84                                         fmt.Println(pth)
85                                         if !*DryRun {
86                                                 if err = os.Remove(pth); err != nil {
87                                                         return ErrLine(err)
88                                                 }
89                                         }
90                                 }
91                         default:
92                                 log.Fatal("unknown cleanup target")
93                         }
94                 }
95         }
96         return nil
97 }
98
99 func cleanupWalker(root, what string) error {
100         root = mustAbs(root)
101         dir, err := os.Open(root)
102         if err != nil {
103                 return ErrLine(err)
104         }
105         defer dir.Close()
106         for {
107                 entries, err := dir.ReadDir(1 << 10)
108                 if err != nil {
109                         if err == io.EOF {
110                                 break
111                         }
112                         return ErrLine(err)
113                 }
114                 for _, entry := range entries {
115                         pth := path.Join(root, entry.Name())
116                         pthRel := cwdMustRel(root, entry.Name())
117                         if entry.IsDir() {
118                                 if entry.Name() == RedoDir {
119                                         if what == CleanupFull {
120                                                 fmt.Println(pthRel)
121                                                 if !*DryRun {
122                                                         err = ErrLine(os.RemoveAll(pth))
123                                                 }
124                                         } else {
125                                                 err = redoDirClean(pth, what)
126                                         }
127                                 } else if (what == CleanupTmp || what == CleanupFull) &&
128                                         strings.HasPrefix(entry.Name(), TmpPrefix) {
129                                         fmt.Println(pthRel)
130                                         if !*DryRun {
131                                                 err = ErrLine(os.RemoveAll(pth))
132                                         }
133                                 } else {
134                                         err = cleanupWalker(pth, what)
135                                 }
136                                 if err != nil {
137                                         return err
138                                 }
139                                 continue
140                         }
141                         if (what == CleanupTmp || what == CleanupFull) &&
142                                 strings.HasPrefix(entry.Name(), TmpPrefix) {
143                                 fmt.Println(pthRel)
144                                 if !*DryRun {
145                                         if err = os.Remove(pth); err != nil {
146                                                 return ErrLine(err)
147                                         }
148                                 }
149                         }
150                 }
151         }
152         return nil
153 }