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/>.
40 if CmdName() != CmdNameRedoCleanup {
43 DryRun = flag.Bool("n", false, "do no delete files during cleanup, just show them")
46 func redoDirClean(root, what string) error {
48 dir, err := os.Open(root)
54 entries, err := dir.ReadDir(1 << 10)
62 for _, entry := range entries {
63 pth = cwdMustRel(root, entry.Name())
66 if strings.HasSuffix(entry.Name(), LogSuffix) ||
67 strings.HasSuffix(entry.Name(), LogRecSuffix) {
70 if err = os.Remove(pth); err != nil {
76 if strings.HasSuffix(entry.Name(), LockSuffix) {
79 if err = os.Remove(pth); err != nil {
85 if strings.HasPrefix(entry.Name(), TmpPrefix) {
88 if err = os.Remove(pth); err != nil {
94 log.Fatal("unknown cleanup target")
101 func cleanupWalker(root, what string) error {
103 dir, err := os.Open(root)
109 entries, err := dir.ReadDir(1 << 10)
116 for _, entry := range entries {
117 pth := path.Join(root, entry.Name())
118 pthRel := cwdMustRel(root, entry.Name())
120 if entry.Name() == RedoDir {
121 if what == CleanupFull {
124 err = ErrLine(os.RemoveAll(pth))
127 err = redoDirClean(pth, what)
129 } else if (what == CleanupTmp || what == CleanupFull) &&
130 strings.HasPrefix(entry.Name(), TmpPrefix) {
133 err = ErrLine(os.RemoveAll(pth))
136 err = cleanupWalker(pth, what)
143 if (what == CleanupTmp || what == CleanupFull) &&
144 strings.HasPrefix(entry.Name(), TmpPrefix) {
147 if err = os.Remove(pth); err != nil {