]> Cypherpunks.ru repositories - goredo.git/blob - cleanup.go
Print found .do
[goredo.git] / cleanup.go
1 /*
2 goredo -- redo implementation on pure Go
3 Copyright (C) 2020 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         "fmt"
22         "io"
23         "log"
24         "os"
25         "path/filepath"
26         "strings"
27 )
28
29 const (
30         CleanupFull = "full"
31         CleanupLog  = "log"
32         CleanupTmp  = "tmp"
33 )
34
35 func redoDirClean(root, what string) error {
36         root, err := filepath.Abs(root)
37         if err != nil {
38                 panic(err)
39         }
40         dir, err := os.Open(root)
41         if err != nil {
42                 return err
43         }
44         for {
45                 fis, err := dir.Readdir(1 << 10)
46                 if err != nil {
47                         if err == io.EOF {
48                                 break
49                         }
50                         return err
51                 }
52                 var pth string
53                 for _, fi := range fis {
54                         pth = cwdMustRel(root, fi.Name())
55                         switch what {
56                         case CleanupLog:
57                                 if strings.HasSuffix(fi.Name(), LogSuffix) {
58                                         fmt.Println(pth)
59                                         if err = os.Remove(pth); err != nil {
60                                                 return err
61                                         }
62                                 }
63                         case CleanupTmp:
64                                 if strings.HasPrefix(fi.Name(), TmpPrefix) {
65                                         fmt.Println(pth)
66                                         if err = os.Remove(pth); err != nil {
67                                                 return err
68                                         }
69                                 }
70                         default:
71                                 log.Fatalln("unknown cleanup target")
72                         }
73                 }
74         }
75         return dir.Close()
76 }
77
78 func cleanupWalker(root, what string) error {
79         root, err := filepath.Abs(root)
80         if err != nil {
81                 panic(err)
82         }
83         dir, err := os.Open(root)
84         if err != nil {
85                 return err
86         }
87         defer dir.Close()
88         for {
89                 fis, err := dir.Readdir(1 << 10)
90                 if err != nil {
91                         if err == io.EOF {
92                                 break
93                         }
94                         return err
95                 }
96                 var pth string
97                 for _, fi := range fis {
98                         pth = cwdMustRel(root, fi.Name())
99                         if fi.IsDir() {
100                                 if fi.Name() == RedoDir {
101                                         if what == CleanupFull {
102                                                 fmt.Println(pth)
103                                                 err = os.RemoveAll(pth)
104                                         } else {
105                                                 err = redoDirClean(pth, what)
106                                         }
107                                 } else {
108                                         err = cleanupWalker(pth, what)
109                                 }
110                                 if err != nil {
111                                         return err
112                                 }
113                                 continue
114                         }
115                         if (what == CleanupTmp || what == CleanupFull) &&
116                                 strings.HasPrefix(fi.Name(), TmpPrefix) {
117                                 fmt.Println(pth)
118                                 if err = os.Remove(pth); err != nil {
119                                         return err
120                                 }
121                         }
122                 }
123         }
124         return dir.Close()
125 }