1 // goredo -- djb's redo implementation on pure Go
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
38 if CmdName() != CmdNameRedoCleanup {
41 DryRun = flag.Bool("n", false, "do no delete files during cleanup, just show them")
44 func redoDirClean(root, what string) error {
46 dir, err := os.Open(root)
52 entries, err := dir.ReadDir(1 << 10)
60 for _, entry := range entries {
61 pth = cwdMustRel(root, entry.Name())
64 if strings.HasSuffix(entry.Name(), LogSuffix) ||
65 strings.HasSuffix(entry.Name(), LogRecSuffix) {
68 if err = os.Remove(pth); err != nil {
74 if strings.HasSuffix(entry.Name(), LockSuffix) {
77 if err = os.Remove(pth); err != nil {
83 if strings.HasPrefix(entry.Name(), TmpPrefix) {
86 if err = os.Remove(pth); err != nil {
92 log.Fatal("unknown cleanup target")
99 func cleanupWalker(root, what string) error {
101 dir, err := os.Open(root)
107 entries, err := dir.ReadDir(1 << 10)
114 for _, entry := range entries {
115 pth := path.Join(root, entry.Name())
116 pthRel := cwdMustRel(root, entry.Name())
118 if entry.Name() == RedoDir {
119 if what == CleanupFull {
122 err = ErrLine(os.RemoveAll(pth))
125 err = redoDirClean(pth, what)
127 } else if (what == CleanupTmp || what == CleanupFull) &&
128 strings.HasPrefix(entry.Name(), TmpPrefix) {
131 err = ErrLine(os.RemoveAll(pth))
134 err = cleanupWalker(pth, what)
141 if (what == CleanupTmp || what == CleanupFull) &&
142 strings.HasPrefix(entry.Name(), TmpPrefix) {
145 if err = os.Remove(pth); err != nil {