2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
41 if CmdName() != CmdNameRedoCleanup {
44 DryRun = flag.Bool("n", false, "do no delete files during cleanup, just show them")
47 func redoDirClean(root, what string) error {
48 root, err := filepath.Abs(root)
52 dir, err := os.Open(root)
58 fis, err := dir.Readdir(1 << 10)
66 for _, fi := range fis {
67 pth = cwdMustRel(root, fi.Name())
70 if strings.HasSuffix(fi.Name(), LogSuffix) ||
71 strings.HasSuffix(fi.Name(), LogRecSuffix) {
74 if err = os.Remove(pth); err != nil {
80 if strings.HasSuffix(fi.Name(), LockSuffix) {
83 if err = os.Remove(pth); err != nil {
89 if strings.HasPrefix(fi.Name(), TmpPrefix) {
92 if err = os.Remove(pth); err != nil {
98 log.Fatalln("unknown cleanup target")
105 func cleanupWalker(root, what string) error {
106 root, err := filepath.Abs(root)
110 dir, err := os.Open(root)
116 fis, err := dir.Readdir(1 << 10)
123 for _, fi := range fis {
124 pth := path.Join(root, fi.Name())
125 pthRel := cwdMustRel(root, fi.Name())
127 if fi.Name() == RedoDir {
128 if what == CleanupFull {
131 err = os.RemoveAll(pth)
134 err = redoDirClean(pth, what)
136 } else if (what == CleanupTmp || what == CleanupFull) &&
137 strings.HasPrefix(fi.Name(), TmpPrefix) {
140 err = os.RemoveAll(pth)
143 err = cleanupWalker(pth, what)
150 if (what == CleanupTmp || what == CleanupFull) &&
151 strings.HasPrefix(fi.Name(), TmpPrefix) {
154 if err = os.Remove(pth); err != nil {