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