Skip to content

Commit

Permalink
fix: do not clean temporary files of the shell which is running. (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-bar authored May 23, 2024
1 parent 2d15fc8 commit 52ff51c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/PuerkitoBio/goquery v1.9.1
github.com/bodgit/sevenzip v1.5.1
github.com/lithammer/fuzzysearch v1.1.8
github.com/mitchellh/go-ps v1.0.0
github.com/pterm/pterm v0.12.79
github.com/schollz/progressbar/v3 v3.14.2
github.com/ulikunitz/xz v0.5.12
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ=
github.com/pierrec/lz4/v4 v4.1.21/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
28 changes: 19 additions & 9 deletions internal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"strconv"
"strings"

"github.com/mitchellh/go-ps"
"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
"github.com/version-fox/vfox/internal/config"
Expand Down Expand Up @@ -583,26 +584,35 @@ func (m *Manager) Available() (RegistryIndex, error) {
func (m *Manager) CleanTmp() {
// once per day
cleanFlagPath := filepath.Join(m.PathMeta.TempPath, cleanupFlagFilename)
if !util.FileExists(cleanFlagPath) {
_ = os.WriteFile(cleanFlagPath, []byte(strconv.FormatInt(util.GetBeginOfToday(), 10)), 0777)
} else {
if str, err := os.ReadFile(cleanFlagPath); err == nil {
if i, err := strconv.ParseInt(string(str), 10, 64); err == nil && !util.IsBeforeToday(i) {
return
if str, err := os.ReadFile(cleanFlagPath); err == nil {
if i, err := strconv.ParseInt(string(str), 10, 64); err == nil && !util.IsBeforeToday(i) {
return
}
}
_ = os.WriteFile(cleanFlagPath, []byte(strconv.FormatInt(util.GetBeginOfToday(), 10)), os.ModePerm)

procExists := make(map[string]struct{})
if procList, err := ps.Processes(); err == nil {
for _, v := range procList {
if v != nil {
procExists[strconv.Itoa(v.Pid())] = struct{}{}
}
}
}

dir, err := os.ReadDir(m.PathMeta.TempPath)
if err == nil {
for _, file := range dir {
if !file.IsDir() {
continue
}
names := strings.SplitN(file.Name(), "-", 2)
if len(names) != 2 {
timestamp, pid, ok := strings.Cut(file.Name(), "-")
if !ok {
continue
}
if _, ok = procExists[pid]; ok {
continue
}
timestamp := names[0]
i, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
continue
Expand Down

0 comments on commit 52ff51c

Please sign in to comment.