2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2022 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 {
47 root, err := filepath.Abs(root)
51 dir, err := os.Open(root)
57 fis, err := dir.Readdir(1 << 10)
65 for _, fi := range fis {
66 pth = cwdMustRel(root, fi.Name())
69 if strings.HasSuffix(fi.Name(), LogSuffix) ||
70 strings.HasSuffix(fi.Name(), LogRecSuffix) {
73 if err = os.Remove(pth); err != nil {
79 if strings.HasPrefix(fi.Name(), TmpPrefix) {
82 if err = os.Remove(pth); err != nil {
88 log.Fatalln("unknown cleanup target")
95 func cleanupWalker(root, what string) error {
96 root, err := filepath.Abs(root)
100 dir, err := os.Open(root)
106 fis, err := dir.Readdir(1 << 10)
113 for _, fi := range fis {
114 pth := path.Join(root, fi.Name())
115 pthRel := cwdMustRel(root, fi.Name())
117 if fi.Name() == RedoDir {
118 if what == CleanupFull {
121 err = os.RemoveAll(pth)
124 err = redoDirClean(pth, what)
126 } else if (what == CleanupTmp || what == CleanupFull) &&
127 strings.HasPrefix(fi.Name(), TmpPrefix) {
130 err = os.RemoveAll(pth)
133 err = cleanupWalker(pth, what)
140 if (what == CleanupTmp || what == CleanupFull) &&
141 strings.HasPrefix(fi.Name(), TmpPrefix) {
144 if err = os.Remove(pth); err != nil {