]> Cypherpunks.ru repositories - goredo.git/blob - cleanup.go
Unify dep*Read/Write name
[goredo.git] / cleanup.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2021 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 )
36
37 var DryRun *bool
38
39 func init() {
40         if CmdName() != CmdNameRedoCleanup {
41                 return
42         }
43         DryRun = flag.Bool("n", false, "do no delete files during cleanup, just show them")
44 }
45
46 func redoDirClean(root, what string) error {
47         root, err := filepath.Abs(root)
48         if err != nil {
49                 panic(err)
50         }
51         dir, err := os.Open(root)
52         if err != nil {
53                 return err
54         }
55         for {
56                 fis, err := dir.Readdir(1 << 10)
57                 if err != nil {
58                         if err == io.EOF {
59                                 break
60                         }
61                         return err
62                 }
63                 var pth string
64                 for _, fi := range fis {
65                         pth = cwdMustRel(root, fi.Name())
66                         switch what {
67                         case CleanupLog:
68                                 if strings.HasSuffix(fi.Name(), LogSuffix) ||
69                                         strings.HasSuffix(fi.Name(), LogRecSuffix) {
70                                         fmt.Println(pth)
71                                         if !*DryRun {
72                                                 if err = os.Remove(pth); err != nil {
73                                                         return err
74                                                 }
75                                         }
76                                 }
77                         case CleanupTmp:
78                                 if strings.HasPrefix(fi.Name(), TmpPrefix) {
79                                         fmt.Println(pth)
80                                         if !*DryRun {
81                                                 if err = os.Remove(pth); err != nil {
82                                                         return err
83                                                 }
84                                         }
85                                 }
86                         default:
87                                 log.Fatalln("unknown cleanup target")
88                         }
89                 }
90         }
91         return dir.Close()
92 }
93
94 func cleanupWalker(root, what string) error {
95         root, err := filepath.Abs(root)
96         if err != nil {
97                 panic(err)
98         }
99         dir, err := os.Open(root)
100         if err != nil {
101                 return err
102         }
103         defer dir.Close()
104         for {
105                 fis, err := dir.Readdir(1 << 10)
106                 if err != nil {
107                         if err == io.EOF {
108                                 break
109                         }
110                         return err
111                 }
112                 for _, fi := range fis {
113                         pth := path.Join(root, fi.Name())
114                         pthRel := cwdMustRel(root, fi.Name())
115                         if fi.IsDir() {
116                                 if fi.Name() == RedoDir {
117                                         if what == CleanupFull {
118                                                 fmt.Println(pthRel)
119                                                 if !*DryRun {
120                                                         err = os.RemoveAll(pth)
121                                                 }
122                                         } else {
123                                                 err = redoDirClean(pth, what)
124                                         }
125                                 } else {
126                                         err = cleanupWalker(pth, what)
127                                 }
128                                 if err != nil {
129                                         return err
130                                 }
131                                 continue
132                         }
133                         if (what == CleanupTmp || what == CleanupFull) &&
134                                 strings.HasPrefix(fi.Name(), TmpPrefix) {
135                                 fmt.Println(pthRel)
136                                 if !*DryRun {
137                                         if err = os.Remove(pth); err != nil {
138                                                 return err
139                                         }
140                                 }
141                         }
142                 }
143         }
144         return dir.Close()
145 }