Skip to content

Commit

Permalink
trim seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
David Moreau committed Sep 10, 2022
1 parent e499ef6 commit 659d3d0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
21 changes: 5 additions & 16 deletions core/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"fmt"
"io"
"os"
"sort"
)

// Group of duplicated Nodes sharing the same Hash (and Size)
type Group struct {
// Nodes having the same size and hash
Nodes []*Node
Hash [sha1.Size]byte
Size int64
// Hash of a single Node
Hash [sha1.Size]byte
// Size of a single Node
Size int64
}

type ByPath []*Node
Expand All @@ -25,19 +27,6 @@ func (a ByPath) Less(i, j int) bool {
}
func (a ByPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

func (g Group) String(current *Tree) string {
s := fmt.Sprintf("%d nodes (save %s)\n", len(g.Nodes), g.WastedSize())
sort.Sort(ByPath(g.Nodes))
for _, n := range g.Nodes {
if n.tree == current {
s = s + fmt.Sprintf("\tC %s [%s]\n", n, n.tree.RelPath(n))
} else {
s = s + fmt.Sprintf("\td %s\n", n)
}
}
return s
}

func (g Group) WastedSize() ByteSize {
return ByteSize(g.Size * int64(len(g.Nodes)-1))
}
Expand Down
4 changes: 4 additions & 0 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type Node struct {
tree *Tree
}

func (n *Node) Tree() *Tree {
return n.tree
}

type NodeP struct {
Node *Node
Path string
Expand Down
5 changes: 5 additions & 0 deletions core/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Tree struct {

func NewTree(info *Info) *Tree {
return &Tree{
info: info,
nodes: make(map[int]*Node),
children: make(map[int][]int),
}
Expand Down Expand Up @@ -57,6 +58,10 @@ func (t *Tree) RelPath(n *Node) (path string) {
return
}

func (t *Tree) AbsPath(n *Node) (path string) {
return filepath.Join(t.info.RootPath, t.RelPath(n))
}

// TODO check it is connected
func (t *Tree) IsOK() (bool, error) {
if len(t.nodes) != len(t.children) {
Expand Down
35 changes: 28 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"
"path/filepath"
"sort"
"time"

"github.com/dav-m85/hashsnap/core"
Expand All @@ -18,6 +19,7 @@ import (
)

var wd, spath string
var delete bool

// st, err := state.LookupFrom(opt.WD)
// State *state.StateFile
Expand Down Expand Up @@ -73,6 +75,7 @@ func main() {
createCmd.BoolVar(&verbose, "verbose", false, "displays hashing speed")
infoCmd.BoolVar(&verbose, "verbose", false, "enumerates all files (high-mem)")
trimCmd.BoolVar(&verbose, "verbose", false, "list all groups")
trimCmd.BoolVar(&delete, "delete", false, "really deletes stuff")

cm := subcommands[os.Args[1]]
if cm == nil {
Expand Down Expand Up @@ -148,7 +151,7 @@ main:
withs = append(withs, wpath)
}

err = trim(withs...)
err = trim(delete, withs...)

// case "convert":
// err = cmd.Convert()
Expand Down Expand Up @@ -215,7 +218,7 @@ func create(spy io.Writer) error {
defer cleanup()

skipper := func(n fs.FileInfo) bool {
return !n.Mode().IsRegular() || n.Size() == 0 || n.Name() == core.STATE_NAME
return !n.Mode().IsDir() && (!n.Mode().IsRegular() || n.Size() == 0 || n.Name() == core.STATE_NAME)
}

// ⛲️ Source by exploring all nodes
Expand Down Expand Up @@ -296,7 +299,6 @@ func info() error {
}
}

// Write some report on stdout
fmt.Fprintf(output, "Totalling %s and %d files\n", core.ByteSize(size), count)
return nil
}
Expand Down Expand Up @@ -325,8 +327,6 @@ func readTree(path string) (*core.Tree, error) {
return nil, err
}

// Write some report on stdout
// fmt.Fprintf(output, "Totalling %s and %d files\n", core.ByteSize(size), count)
return t, nil
}

Expand All @@ -347,7 +347,7 @@ func readInfo(path string) (*core.Info, error) {
return i, nil
}

func trim(withs ...string) error {
func trim(delete bool, withs ...string) error {
matches := make(core.HashGroup)

cur, err := readTree(spath)
Expand Down Expand Up @@ -376,7 +376,28 @@ func trim(withs ...string) error {
continue
}
if verbose {
fmt.Fprintln(output, g.String(cur))

s := fmt.Sprintf("%d nodes (save %s)\n", len(g.Nodes), g.WastedSize())
sort.Sort(core.ByPath(g.Nodes))
for _, n := range g.Nodes {
if n.Tree() == cur {
s = s + fmt.Sprintf(color.Red+"\t- %s [%s]\n"+color.Reset, n, n.Tree().RelPath(n))
} else {
s = s + fmt.Sprintf(color.Green+"\t+ %s\n"+color.Reset, n)
}
}

fmt.Fprintln(output, s)
}
if delete {
for _, n := range g.Nodes {
if n.Tree() == cur {
err := os.Remove(n.Tree().AbsPath(n))
if err != nil {
return err
}
}
}
}
count++
waste = waste + int64(g.WastedSize())
Expand Down

0 comments on commit 659d3d0

Please sign in to comment.