]> Cypherpunks.ru repositories - goredo.git/blob - cleanup.go
No flags for not appropriate commands
[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                                         fmt.Println(pth)
70                                         if !*DryRun {
71                                                 if err = os.Remove(pth); err != nil {
72                                                         return err
73                                                 }
74                                         }
75                                 }
76                         case CleanupTmp:
77                                 if strings.HasPrefix(fi.Name(), TmpPrefix) {
78                                         fmt.Println(pth)
79                                         if !*DryRun {
80                                                 if err = os.Remove(pth); err != nil {
81                                                         return err
82                                                 }
83                                         }
84                                 }
85                         default:
86                                 log.Fatalln("unknown cleanup target")
87                         }
88                 }
89         }
90         return dir.Close()
91 }
92
93 func cleanupWalker(root, what string) error {
94         root, err := filepath.Abs(root)
95         if err != nil {
96                 panic(err)
97         }
98         dir, err := os.Open(root)
99         if err != nil {
100                 return err
101         }
102         defer dir.Close()
103         for {
104                 fis, err := dir.Readdir(1 << 10)
105                 if err != nil {
106                         if err == io.EOF {
107                                 break
108                         }
109                         return err
110                 }
111                 for _, fi := range fis {
112                         pth := path.Join(root, fi.Name())
113                         pthRel := cwdMustRel(root, fi.Name())
114                         if fi.IsDir() {
115                                 if fi.Name() == RedoDir {
116                                         if what == CleanupFull {
117                                                 fmt.Println(pthRel)
118                                                 if !*DryRun {
119                                                         err = os.RemoveAll(pth)
120                                                 }
121                                         } else {
122                                                 err = redoDirClean(pth, what)
123                                         }
124                                 } else {
125                                         err = cleanupWalker(pth, what)
126                                 }
127                                 if err != nil {
128                                         return err
129                                 }
130                                 continue
131                         }
132                         if (what == CleanupTmp || what == CleanupFull) &&
133                                 strings.HasPrefix(fi.Name(), TmpPrefix) {
134                                 fmt.Println(pthRel)
135                                 if !*DryRun {
136                                         if err = os.Remove(pth); err != nil {
137                                                 return err
138                                         }
139                                 }
140                         }
141                 }
142         }
143         return dir.Close()
144 }