Skip to content

Commit

Permalink
cli/delete-cache: Switch to fastwalk
Browse files Browse the repository at this point in the history
  • Loading branch information
joebonrichie committed Mar 20, 2024
1 parent 85b1dfc commit 0cb403f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
46 changes: 32 additions & 14 deletions cli/delete_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package cli

import (
"fmt"
"io/fs"
"log/slog"
"math"
"os"

"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/MichaelTJones/walk"
"github.com/charlievieth/fastwalk"

"github.com/getsolus/solbuild/builder"
"github.com/getsolus/solbuild/builder/source"
Expand Down Expand Up @@ -142,7 +143,7 @@ func DeleteCacheRun(r *cmd.Root, s *cmd.Sub) {
}

func deleteDir(path string) (int64, error) {
var size int64
var totalSize int64

// Return nothing if dir doesn't exist
_, err := os.Stat(path)
Expand All @@ -151,21 +152,30 @@ func deleteDir(path string) (int64, error) {
return 0, nil
}

walkConf := fastwalk.Config{
Follow: false,
}

/* Parallelized file walk */
walk.Walk(path, func(path string, info os.FileInfo, err error) error {
fastwalk.Walk(&walkConf, path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if !info.IsDir() {
size += info.Size()
}

/* Remove if file */
if info.Mode().IsRegular() {
if err = os.Remove(path); err != nil {
if !d.IsDir() {
file, err := d.Info()
if err != nil {
return err
}

totalSize += file.Size()

/* Remove if file */
if d.Type().IsRegular() {
if err = os.Remove(path); err != nil {
return err
}
}
}

return err
Expand All @@ -178,7 +188,7 @@ func deleteDir(path string) (int64, error) {
slog.Warn("Could not remove directory", "reason", err)
}

return size, err
return totalSize, err
}

// getDirSize returns the disk usage of a directory.
Expand All @@ -192,14 +202,22 @@ func getDirSize(path string) (int64, error) {
return 0, nil
}

walkConf := fastwalk.Config{
Follow: false,
}

// Walk the dir, get size, add to totalSize
err = walk.Walk(path, func(_ string, info os.FileInfo, err error) error {
err = fastwalk.Walk(&walkConf, path, func(_ string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if !info.IsDir() {
totalSize += info.Size()
if !d.IsDir() {
file, err := d.Info()
if err != nil {
return err
}
totalSize += file.Size()
}

return err
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/BurntSushi/toml v1.3.2
github.com/DataDrake/cli-ng/v2 v2.0.2
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/charlievieth/fastwalk v1.0.2
github.com/cheggaaa/pb/v3 v3.1.5
github.com/coreos/go-systemd/v22 v22.5.0
github.com/getsolus/libosdev v0.0.0-20181023041421-9ab0f4b463fd
Expand All @@ -18,7 +19,6 @@ require (

require (
dario.cat/mergo v1.0.0 // indirect
github.com/MichaelTJones/walk v0.0.0-20161122175330-4748e29d5718 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
Expand All @@ -43,6 +43,7 @@ require (
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/DataDrake/cli-ng/v2 v2.0.2 h1:7+25l25VmlERCE95glW6QKBUF13vxqAM2jasFiN02xQ=
github.com/DataDrake/cli-ng/v2 v2.0.2/go.mod h1:bU9YaNNWWVq0eIdDsU3TCe9+7Jb398iBBoqee5EiKWQ=
github.com/MichaelTJones/walk v0.0.0-20161122175330-4748e29d5718 h1:FSsoaa1q4jAaeiAUxf9H0PgFP7eA/UL6c3PdJH+nMN4=
github.com/MichaelTJones/walk v0.0.0-20161122175330-4748e29d5718/go.mod h1:VVwKsx9Dc8rNG55BWqogoJzGubjKnRoXdUvpGbWqeCc=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
Expand All @@ -20,6 +18,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkY
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4=
github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4=
github.com/charlievieth/fastwalk v1.0.2 h1:KYWo7xszmoldOGrwdNIeznSzhj9mhgk+6DwHunG99bc=
github.com/charlievieth/fastwalk v1.0.2/go.mod h1:JSfglY/gmL/rqsUS1NCsJTocB5n6sSl9ApAqif4CUbs=
github.com/cheggaaa/pb/v3 v3.1.5 h1:QuuUzeM2WsAqG2gMqtzaWithDJv0i+i6UlnwSCI4QLk=
github.com/cheggaaa/pb/v3 v3.1.5/go.mod h1:CrxkeghYTXi1lQBEI7jSn+3svI3cuc19haAj6jM60XI=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
Expand Down Expand Up @@ -128,8 +128,8 @@ golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down

0 comments on commit 0cb403f

Please sign in to comment.