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