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