~hrodude/dui

05106cb273761e06b55191cea48b0fcb9f1e22f1 — Georg Kaemmert 2 months ago 8712836
status: working beta; new -s flag
2 files changed, 63 insertions(+), 33 deletions(-)

M main.go
M tree/tree.go
M main.go => main.go +52 -28
@@ 3,21 3,63 @@ package main
import (
	"dui/tree"
	"flag"
	"fmt"
	"log"
)

func listUpdates(idx1, idx2 *tree.Node) map[string]*tree.Node {
	updates := make(map[string]*tree.Node)
	for node := range idx1.Iterate() {
		updates[node.PrintPath()] = node
func PrintDUIFiles(idx1, idx2 *tree.Node) {
	dui := idx1.GetChanges(idx2)

	for node := range dui.Inserts.Iterate() {
		if node.IsFile {
			fmt.Printf("insert\t%s\t%d\n",node.GetPathToNode(), node.Size)
		}
	}
	for node := range dui.Updates.Iterate() {
		if node.IsFile {
			fmt.Printf("update\t%s\t%d\n", node.GetPathToNode(), node.Size)
		}
	}
	for node := range dui.Deletes.Iterate() {
		if node.IsFile {
			fmt.Printf("delete\t%s\t%d\n", node.GetPathToNode(), node.Size)
		}
	}
}

func PrintDUICompact(idx1, idx2 *tree.Node) {
	dui := idx1.GetChanges(idx2)

	var sumIns, sumUpd, sumDel int64
	var countIns, countUpd, countDel uint
	for node := range dui.Inserts.Iterate() {
		if node.IsFile {
			sumIns = sumIns + node.Size
			countIns = countIns + 1
		}
	}
	for node := range dui.Updates.Iterate() {
		if node.IsFile {
			sumUpd = sumUpd + node.Size
			countUpd = countUpd + 1
		}
	}
	for node := range dui.Deletes.Iterate() {
		if node.IsFile {
			sumDel = sumDel + node.Size
			countDel = countDel + 1
		}
	}
	return updates
	fmt.Printf("insert\t%d\t%d\n", countIns, sumIns)
	fmt.Printf("insert\t%d\t%d\n", countUpd, sumUpd)
	fmt.Printf("insert\t%d\t%d\n", countDel, sumDel)
}

func main() {
	fileFlag := flag.String("f", "", "index file to create; defaults to the last element of the indexed directory (base).idx; if used with -c both files are compared")
	compareFileFlag := flag.String("c", "", "index file to compare; -f must also be used")
	folderFlag := flag.String("d", "", " directory to index")
	sFlag := flag.Bool("s", false, "summarize output, dont print single files")
	flag.Parse()

	if len(*fileFlag) > 0 && len(*compareFileFlag) == 0 {


@@ 40,28 82,10 @@ func main() {
		if err != nil {
			log.Fatal(err)
		}
		//idx1.PrintPaths()

		//log.Printf("TreeEquals: %t", idx1.TreeEquals(idx2))
		//log.Printf("TreeContains: %t", idx1.TreeContains(idx1))
		//log.Println(idx2.TreeEquals(idx1))

		//idx2.PrintFiles()
		//path1 := idx1.Children[0].Children[0].Children[0].Children[0].Children[0].ChainFromRoot(false)
		//path2 := idx2.Children[0].Children[0].Children[0].Children[2].Children[0].ChainFromRoot(false)
		//path1.MergeNode(path2)
		//path1.PrintFiles()

		dui := idx1.GetChanges(idx2)
		log.Println("Inserts")
		dui.Inserts.PrintFiles()
		log.Println("Updates")
		dui.Updates.PrintFiles()
		log.Println("Deletes")
		dui.Deletes.PrintFiles()

		//for k, v := range listUpdates(idx1, idx2) {
		//	log.Printf("%s, %p\n", k,v)
		//}
		if *sFlag {
			PrintDUICompact(idx1, idx2)
		} else {
			PrintDUIFiles(idx1, idx2)
		}
	}
}

M tree/tree.go => tree/tree.go +11 -5
@@ 16,7 16,6 @@ type Node struct {
	Size     int64
	Parent   *Node
	Children []*Node
	visited  bool
}

type DUITrees struct {


@@ 166,10 165,10 @@ func (node *Node) MergeNode(mergeNode *Node) {
prints all files/leaves Node.IsFile == true
*/
func (node *Node) PrintFiles() {
	printIndex(node, "")
	printFiles(node, "")
}

func printIndex(node *Node, path string) {
func printFiles(node *Node, path string) {
	//      log.Printf("printIndex(%#v, %s)", node, path)
	var newPath string
	if node.Name != "" {


@@ 179,15 178,19 @@ func printIndex(node *Node, path string) {
		log.Println(newPath)
	} else {
		for _, child := range node.Children {
			printIndex(child, newPath)
			printFiles(child, newPath)
		}
	}
}

func (node *Node) PrintPathToNode() {
	log.Println(node.GetPathToNode())
}

/*
returns path for one node
*/
func (node *Node) PrintPath() string {
func (node *Node) GetPathToNode() string {
	var path string
	currentNode := node
	for currentNode.Parent != nil {


@@ 374,5 377,8 @@ func (node *Node) GetChanges(newNode *Node) *DUITrees {

		workingQueue = nextQueue
	}
	dui.Inserts.SetParents()
	dui.Updates.SetParents()
	dui.Deletes.SetParents()
	return dui
}