Skip to content

Commit

Permalink
clean: fix attempt to remove files multiple times
Browse files Browse the repository at this point in the history
Before this patch, `tt clean` could list files multiple times since it
is possible that memtx_dir, wal_dir, vinyl_dir and log_dir are the same
directory (and the first three are the same by default). It also had
tried to removed them several times, which had resulted in `[ERR]`
message in logs. This patch fixes the issue.

Closes #735
  • Loading branch information
DifferentialOrange committed May 3, 2024
1 parent f7bfc21 commit e28ea2f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- `tt clean` no longer tries to clean files multiple times.

## [2.2.1] - 2024-04-03

### Added
Expand Down
16 changes: 8 additions & 8 deletions cli/cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,44 @@ func NewCleanCmd() *cobra.Command {
return cleanCmd
}

func collectFiles(list []string, dirname string) ([]string, error) {
func collectFiles(files map[string]bool, dirname string) (map[string]bool, error) {
err := filepath.Walk(dirname,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() {
list = append(list, path)
files[path] = true
}
return nil
})
if err != nil {
return nil, err
}

return list, nil
return files, nil
}

func clean(run *running.InstanceCtx) error {
removeList := []string{}
removeFiles := map[string]bool{}
confirm := false
var err error

for _, dir := range [...]string{run.LogDir, run.WalDir, run.VinylDir, run.MemtxDir} {
removeList, err = collectFiles(removeList, dir)
removeFiles, err = collectFiles(removeFiles, dir)
if err != nil {
return err
}
}

if len(removeList) == 0 {
if len(removeFiles) == 0 {
log.Infof("Already cleaned.\n")
return nil
}

log.Infof("List of files to delete:\n")
for _, file := range removeList {
for file, _ := range removeFiles {
log.Infof("%s", file)
}

Expand All @@ -92,7 +92,7 @@ func clean(run *running.InstanceCtx) error {
}

if confirm || forceRemove {
for _, file := range removeList {
for file, _ := range removeFiles {
err = os.Remove(file)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion test/integration/running/test_running.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def test_logrotate(tt_cmd, tmpdir_with_cfg):


def assert_file_cleaned(filepath, cmd_out):
assert re.search(r"• " + str(filepath), cmd_out)
assert len(re.findall(r"• " + str(filepath), cmd_out)) == 1 # https://github.com/tarantool/tt/issues/735
assert os.path.exists(filepath) is False


Expand Down Expand Up @@ -229,6 +229,7 @@ def test_clean(tt_cmd, tmpdir_with_cfg):
# Check that clean is working.
clean_rc, clean_out = run_command_and_get_output(clean_cmd, cwd=tmpdir)
assert clean_rc == 0
assert re.search(r"\[ERR\]", clean_out) is None

assert_file_cleaned(os.path.join(log_dir, log_file), clean_out)
assert_file_cleaned(os.path.join(lib_dir, initial_snap), clean_out)
Expand Down

0 comments on commit e28ea2f

Please sign in to comment.