]> Cypherpunks.ru repositories - goredo.git/blob - cleanup.go
Fix some file descriptors closing
[goredo.git] / cleanup.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2022 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         defer dir.Close()
56         for {
57                 fis, err := dir.Readdir(1 << 10)
58                 if err != nil {
59                         if err == io.EOF {
60                                 break
61                         }
62                         return err
63                 }
64                 var pth string
65                 for _, fi := range fis {
66                         pth = cwdMustRel(root, fi.Name())
67                         switch what {
68                         case CleanupLog:
69                                 if strings.HasSuffix(fi.Name(), LogSuffix) ||
70                                         strings.HasSuffix(fi.Name(), LogRecSuffix) {
71                                         fmt.Println(pth)
72                                         if !*DryRun {
73                                                 if err = os.Remove(pth); err != nil {
74                                                         return err
75                                                 }
76                                         }
77                                 }
78                         case CleanupTmp:
79                                 if strings.HasPrefix(fi.Name(), TmpPrefix) {
80                                         fmt.Println(pth)
81                                         if !*DryRun {
82                                                 if err = os.Remove(pth); err != nil {
83                                                         return err
84                                                 }
85                                         }
86                                 }
87                         default:
88                                 log.Fatalln("unknown cleanup target")
89                         }
90                 }
91         }
92         return nil
93 }
94
95 func cleanupWalker(root, what string) error {
96         root, err := filepath.Abs(root)
97         if err != nil {
98                 panic(err)
99         }
100         dir, err := os.Open(root)
101         if err != nil {
102                 return err
103         }
104         defer dir.Close()
105         for {
106                 fis, err := dir.Readdir(1 << 10)
107                 if err != nil {
108                         if err == io.EOF {
109                                 break
110                         }
111                         return err
112                 }
113                 for _, fi := range fis {
114                         pth := path.Join(root, fi.Name())
115                         pthRel := cwdMustRel(root, fi.Name())
116                         if fi.IsDir() {
117                                 if fi.Name() == RedoDir {
118                                         if what == CleanupFull {
119                                                 fmt.Println(pthRel)
120                                                 if !*DryRun {
121                                                         err = os.RemoveAll(pth)
122                                                 }
123                                         } else {
124                                                 err = redoDirClean(pth, what)
125                                         }
126                                 } else if (what == CleanupTmp || what == CleanupFull) &&
127                                         strings.HasPrefix(fi.Name(), TmpPrefix) {
128                                         fmt.Println(pthRel)
129                                         if !*DryRun {
130                                                 err = os.RemoveAll(pth)
131                                         }
132                                 } else {
133                                         err = cleanupWalker(pth, what)
134                                 }
135                                 if err != nil {
136                                         return err
137                                 }
138                                 continue
139                         }
140                         if (what == CleanupTmp || what == CleanupFull) &&
141                                 strings.HasPrefix(fi.Name(), TmpPrefix) {
142                                 fmt.Println(pthRel)
143                                 if !*DryRun {
144                                         if err = os.Remove(pth); err != nil {
145                                                 return err
146                                         }
147                                 }
148                         }
149                 }
150         }
151         return nil
152 }