]> Cypherpunks.ru repositories - goredo.git/blob - cleanup.go
Fix workability under GNU/Linux and other systems because of different syscall
[goredo.git] / cleanup.go
1 /*
2 goredo -- 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         "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 = cwdMustRel(root, fi.Name())
56                         switch what {
57                         case CleanupLog:
58                                 if strings.HasSuffix(fi.Name(), LogSuffix) {
59                                         fmt.Println(pth)
60                                         if err = os.Remove(pth); err != nil {
61                                                 return err
62                                         }
63                                 }
64                         case CleanupTmp:
65                                 if strings.HasPrefix(fi.Name(), TmpPrefix) {
66                                         fmt.Println(pth)
67                                         if err = os.Remove(pth); err != nil {
68                                                 return err
69                                         }
70                                 }
71                         default:
72                                 log.Fatalln("unknown cleanup target")
73                         }
74                 }
75         }
76         return dir.Close()
77 }
78
79 func cleanupWalker(root, what string) error {
80         root, err := filepath.Abs(root)
81         if err != nil {
82                 panic(err)
83         }
84         dir, err := os.Open(root)
85         if err != nil {
86                 return err
87         }
88         defer dir.Close()
89         for {
90                 fis, err := dir.Readdir(1 << 10)
91                 if err != nil {
92                         if err == io.EOF {
93                                 break
94                         }
95                         return err
96                 }
97                 for _, fi := range fis {
98                         pth := path.Join(root, fi.Name())
99                         pthRel := cwdMustRel(root, fi.Name())
100                         if fi.IsDir() {
101                                 if fi.Name() == RedoDir {
102                                         if what == CleanupFull {
103                                                 fmt.Println(pthRel)
104                                                 err = os.RemoveAll(pth)
105                                         } else {
106                                                 err = redoDirClean(pth, what)
107                                         }
108                                 } else {
109                                         err = cleanupWalker(pth, what)
110                                 }
111                                 if err != nil {
112                                         return err
113                                 }
114                                 continue
115                         }
116                         if (what == CleanupTmp || what == CleanupFull) &&
117                                 strings.HasPrefix(fi.Name(), TmpPrefix) {
118                                 fmt.Println(pthRel)
119                                 if err = os.Remove(pth); err != nil {
120                                         return err
121                                 }
122                         }
123                 }
124         }
125         return dir.Close()
126 }