output.go
author Mikael Berthe <mikael@lilotux.net>
Wed, 23 Feb 2022 22:56:53 +0100
changeset 45 ea6a9ba7a3c8
parent 42 3fa13770e970
permissions -rw-r--r--
Display existing hard links in result sets This is a breaking change in the plain text output, but somehow the list displayed in case of existing hard links was arbitrary, since the all the hardlinked filenames were not displayed. Here's a sample JSON result with this patch: { "file_size": 9216, "paths": [ "test_tree/f09-1_5.raw", "test_tree/f09-4_5.raw" ], "links": { "test_tree/f09-1_5.raw": [ "test_tree/f09-2_5.raw", "test_tree/f09-3_5.raw" ], "test_tree/f09-4_5.raw": [ "test_tree/f09-5_5.raw" ] } } Here the 5 files have the same contents, but there are two hardlink groups: "test_tree/f09-1_5.raw" "test_tree/f09-2_5.raw" "test_tree/f09-3_5.raw" are hard-linked, and "test_tree/f09-4_5.raw" "test_tree/f09-5_5.raw" are hard-linked. Here's the same set displayed With the regular text output: Group #5 (2 files * 9216 bytes): test_tree/f09-1_5.raw test_tree/f09-2_5.raw test_tree/f09-3_5.raw test_tree/f09-4_5.raw test_tree/f09-5_5.raw (The link file names are indented using 1 space character.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
45
ea6a9ba7a3c8 Display existing hard links in result sets
Mikael Berthe <mikael@lilotux.net>
parents: 42
diff changeset
     2
 * Copyright (C) 2014-2022 Mikael Berthe <mikael@lilotux.net>
36
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
 *
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
 * This program is free software; you can redistribute it and/or modify
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
 * it under the terms of the GNU General Public License as published by
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
 * the Free Software Foundation; either version 2 of the License, or (at
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
 * your option) any later version.
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
 *
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful, but
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
 * General Public License for more details.
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
 *
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
 * USA
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
 */
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
package main
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
import (
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	"encoding/json"
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
	"fmt"
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
)
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
39
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    27
// formatSize returns the size in a string with a human-readable format.
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    28
func formatSize(sizeBytes uint64, short bool) string {
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    29
	var units = map[int]string{
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    30
		0: "B",
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    31
		1: "KiB",
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    32
		2: "MiB",
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    33
		3: "GiB",
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    34
		4: "TiB",
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    35
		5: "PiB",
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    36
	}
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    37
	humanSize := sizeBytes
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    38
	var n int
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    39
	for n < len(units)-1 {
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    40
		if humanSize < 10000 {
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    41
			break
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    42
		}
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    43
		humanSize /= 1024
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    44
		n++
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    45
	}
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    46
	if n < 1 {
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    47
		return fmt.Sprintf("%d bytes", sizeBytes)
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    48
	}
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    49
	if short {
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    50
		return fmt.Sprintf("%d %s", humanSize, units[n])
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    51
	}
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    52
	return fmt.Sprintf("%d bytes (%d %s)", sizeBytes, humanSize, units[n])
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    53
}
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    54
24ca1bf4a0bf Move formatSize() to output.go
Mikael Berthe <mikael@lilotux.net>
parents: 36
diff changeset
    55
// displayResults formats results to plaintext or JSON and sends them to stdout
36
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
func displayResults(results Results, jsonOutput bool, summaryOnly bool) {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
	if jsonOutput {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
		displayResultsJSON(results)
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
		return
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
	if !summaryOnly {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
		for i, g := range results.Groups {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
			fmt.Printf("\nGroup #%d (%d files * %v):\n", i+1,
42
3fa13770e970 Rename JSON size fields
Mikael Berthe <mikael@lilotux.net>
parents: 39
diff changeset
    65
				len(g.Paths), formatSize(g.FileSize, true))
36
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
			for _, f := range g.Paths {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
				fmt.Println(f)
45
ea6a9ba7a3c8 Display existing hard links in result sets
Mikael Berthe <mikael@lilotux.net>
parents: 42
diff changeset
    68
				if g.Links != nil { // Display linked files
ea6a9ba7a3c8 Display existing hard links in result sets
Mikael Berthe <mikael@lilotux.net>
parents: 42
diff changeset
    69
					for _, lf := range g.Links[f] {
ea6a9ba7a3c8 Display existing hard links in result sets
Mikael Berthe <mikael@lilotux.net>
parents: 42
diff changeset
    70
						fmt.Printf(" %s\n", lf)
ea6a9ba7a3c8 Display existing hard links in result sets
Mikael Berthe <mikael@lilotux.net>
parents: 42
diff changeset
    71
					}
ea6a9ba7a3c8 Display existing hard links in result sets
Mikael Berthe <mikael@lilotux.net>
parents: 42
diff changeset
    72
				}
36
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
			}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
		}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
	// We're done if we do not display statistics
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
	if myLog.verbosity < 1 && !summaryOnly {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
		return
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
	// Add a trailing newline
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
	if len(results.Groups) > 0 && myLog.verbosity > 0 {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
		fmt.Println()
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
	myLog.Println(0, "Final count:", results.Duplicates,
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
		"duplicate files in", len(results.Groups), "sets")
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
	myLog.Println(0, "Redundant data size:",
42
3fa13770e970 Rename JSON size fields
Mikael Berthe <mikael@lilotux.net>
parents: 39
diff changeset
    89
		formatSize(results.RedundantDataSizeBytes, false))
36
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
func displayResultsJSON(results Results) {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
	b, err := json.Marshal(results)
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
	if err != nil {
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
		panic(err)
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
	}
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
	fmt.Println(string(b))
e918b7e63748 Add JSON output
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
}