goduf.go
author Mikael Berthe <mikael@lilotux.net>
Sun, 29 Jun 2014 18:00:13 +0200
changeset 19 3389a17fc0d2
parent 16 cc0ee80cf216
child 20 f7ce9d750e83
permissions -rw-r--r--
Remove duplicated code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
 * Copyright (C) 2014 Mikael Berthe <mikael@lilotux.net>
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
 *
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
 * This program is free software; you can redistribute it and/or modify
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
 * it under the terms of the GNU General Public License as published by
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
 * the Free Software Foundation; either version 2 of the License, or (at
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
 * your option) any later version.
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
 *
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful, but
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
 * General Public License for more details.
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
 *
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
 * USA
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
 */
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
// This program (Goduf) is a fast duplicate file finder.
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
// Use goduf --help to get the list of available options.
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
package main
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
import (
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	"crypto/sha1"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	"encoding/hex"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	"errors"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	"flag"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	"fmt"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	"io"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	"log"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	"os"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	"path/filepath"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	"sort"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
const medsumBytes = 128
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
const minSizePartialChecksum = 49152 // Should be > 3*medsumBytes
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
type sumType int
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
const (
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    44
	noChecksum sumType = iota
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    45
	fullChecksum
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	partialChecksum
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
type fileObj struct {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
	//Unique   bool
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
	FilePath string
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
	os.FileInfo
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
	PartialHash []byte
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
	Hash        []byte
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
    55
	needHash    sumType
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
    58
// FileObjList is only exported so that we can have a sort interface on inodes.
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
type FileObjList []*fileObj
16
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
    60
type foListList []FileObjList
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
type dataT struct {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	totalSize   uint64
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
	cmpt        uint
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
    65
	sizeGroups  map[int64]*FileObjList
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	emptyFiles  FileObjList
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
	ignoreCount int
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
var data dataT
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
type myLogT struct {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
	verbosity int
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
var myLog myLogT
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
func (l *myLogT) Printf(level int, format string, args ...interface{}) {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
	if level > l.verbosity {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
		return
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
	if level >= 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
		log.Printf(format, args...)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
		return
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
	// Error message without timestamp
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
	fmt.Fprintf(os.Stderr, format, args...)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
func (l *myLogT) Println(level int, args ...interface{}) {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
	if level > l.verbosity {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
		return
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
	if level >= 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
		log.Println(args...)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
		return
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
	// Error message without timestamp
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
	fmt.Fprintln(os.Stderr, args...)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
func visit(path string, f os.FileInfo, err error) error {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
	if err != nil {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
		if f == nil {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
			return err
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
		if f.IsDir() {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
			myLog.Println(-1, "Warning: cannot process directory:",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
				path)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
			return filepath.SkipDir
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
		myLog.Println(-1, "Ignoring ", path, " - ", err)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
		data.ignoreCount++
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
		return nil
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
	if f.IsDir() {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
		return nil
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
	if mode := f.Mode(); mode&os.ModeType != 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
		if mode&os.ModeSymlink != 0 {
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   123
			myLog.Println(6, "Ignoring symbolic link", path)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   124
		} else {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
			myLog.Println(0, "Ignoring special file", path)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
		data.ignoreCount++
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
		return nil
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
	data.cmpt++
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
	data.totalSize += uint64(f.Size())
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
	fo := &fileObj{FilePath: path, FileInfo: f}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
	if _, ok := data.sizeGroups[f.Size()]; !ok {
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   135
		data.sizeGroups[f.Size()] = new(FileObjList)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
	}
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   137
	*data.sizeGroups[f.Size()] = append(*data.sizeGroups[f.Size()], fo)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
	return nil
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   140
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
func (fo *fileObj) CheckSum() error {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   142
	file, err := os.Open(fo.FilePath)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   143
	if err != nil {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   144
		return err
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   145
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   146
	defer file.Close()
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   147
	hash := sha1.New()
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   148
	if size, err := io.Copy(hash, file); size != fo.Size() || err != nil {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   149
		if err == nil {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   150
			return errors.New("failed to read the whole file: " +
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   151
				fo.FilePath)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   152
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   153
		return err
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   154
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   155
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   156
	fo.Hash = hash.Sum(nil)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   157
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   158
	return nil
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   159
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   160
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   161
func (fo *fileObj) MedSum() error {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   162
	file, err := os.Open(fo.FilePath)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   163
	if err != nil {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   164
		return err
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   165
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   166
	defer file.Close()
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   167
	hash := sha1.New()
19
3389a17fc0d2 Remove duplicated code
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   168
3389a17fc0d2 Remove duplicated code
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   169
	// Read first bytes and last bytes from file
3389a17fc0d2 Remove duplicated code
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   170
	for i := 0; i < 2; i++ {
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   171
		if _, err := io.CopyN(hash, file, medsumBytes); err != nil {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   172
			if err == nil {
19
3389a17fc0d2 Remove duplicated code
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   173
				const errmsg = "failed to read bytes from file: "
3389a17fc0d2 Remove duplicated code
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   174
				return errors.New(errmsg + fo.FilePath)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   175
			}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   176
			return err
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   177
		}
19
3389a17fc0d2 Remove duplicated code
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   178
		if i == 0 { // Seek to end of file
3389a17fc0d2 Remove duplicated code
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
   179
			file.Seek(0-medsumBytes, 2)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   180
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   181
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   182
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   183
	fo.PartialHash = hash.Sum(nil)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   184
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   185
	return nil
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   186
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   187
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   188
func (fo *fileObj) Sum(sType sumType) error {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   189
	if sType == partialChecksum {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   190
		return fo.MedSum()
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   191
	} else if sType == fullChecksum {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   192
		return fo.CheckSum()
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   193
	} else if sType == noChecksum {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   194
		return nil
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   195
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   196
	panic("Internal error: Invalid sType")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   197
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   198
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   199
func (data *dataT) dispCount() { // FIXME rather useless
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   200
	if myLog.verbosity < 4 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   201
		return
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   202
	}
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   203
	var c1, c1b, c2 int
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   204
	var s1 string
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   205
	for _, scListP := range data.sizeGroups {
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   206
		c1 += len(*scListP)
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   207
		c2++
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   208
	}
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   209
	c1b = len(data.emptyFiles)
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   210
	if c1b > 0 {
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   211
		s1 = fmt.Sprintf("+%d", c1b)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   212
	}
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   213
	myLog.Printf(4, "  Current countdown: %d  [%d%s/%d]\n",
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   214
		c1+c1b, c1, s1, c2)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   215
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   216
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   217
// checksum returns the requested checksum as a string.
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   218
// If the checksum has not been pre-computed, it is calculated now.
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   219
func (fo fileObj) checksum(sType sumType) (string, error) {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   220
	var hbytes []byte
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   221
	if sType == partialChecksum {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   222
		hbytes = fo.PartialHash
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   223
	} else if sType == fullChecksum {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   224
		hbytes = fo.Hash
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   225
	} else {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   226
		panic("Internal error: Invalid sType")
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   227
	}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   228
	if hbytes == nil {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   229
		if err := fo.Sum(sType); err != nil {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   230
			return "", err
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   231
		}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   232
		if sType == partialChecksum {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   233
			hbytes = fo.PartialHash
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   234
		} else if sType == fullChecksum {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   235
			hbytes = fo.Hash
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   236
		}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   237
	}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   238
	return hex.EncodeToString(hbytes), nil
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   239
}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   240
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   241
func (fileList FileObjList) computeSheduledChecksums() {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   242
	// Sort the list for better efficiency
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   243
	sort.Sort(ByInode(fileList))
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   244
10
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   245
	//myLog.Printf(6, "  . will compute %d checksums\n", len(fileList))
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   246
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   247
	// Compute checksums
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   248
	for _, fo := range fileList {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   249
		if err := fo.Sum(fo.needHash); err != nil {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   250
			myLog.Println(0, "Error:", err)
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   251
		}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   252
	}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   253
}
16
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   254
func computeSheduledChecksums(fileLists ...foListList) {
10
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   255
	var bigFileList FileObjList
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   256
	// Merge the lists of FileObjList lists and create a unique list
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   257
	// of file objects.
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   258
	for _, foll := range fileLists {
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   259
		for _, fol := range foll {
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   260
			bigFileList = append(bigFileList, fol...)
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   261
		}
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   262
	}
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   263
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   264
	// Sort the list for better efficiency
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   265
	sort.Sort(ByInode(bigFileList))
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   266
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   267
	// Compute checksums
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   268
	for _, fo := range bigFileList {
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   269
		if err := fo.Sum(fo.needHash); err != nil {
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   270
			myLog.Println(0, "Error:", err)
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   271
		}
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   272
	}
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   273
}
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   274
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   275
func (fileList FileObjList) scheduleChecksum(sType sumType) {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   276
	for _, fo := range fileList {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   277
		fo.needHash = sType
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   278
	}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   279
}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   280
16
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   281
func (fileList FileObjList) findDupesChecksums(sType sumType) foListList {
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   282
	var dupeList foListList
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   283
	var scheduleFull foListList
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   284
	hashes := make(map[string]FileObjList)
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   285
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   286
	// Sort the list for better efficiency
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   287
	sort.Sort(ByInode(fileList))
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   288
7
68375cc98f98 Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   289
	// Compute checksums
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   290
	for _, fo := range fileList {
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   291
		hash, err := fo.checksum(sType)
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   292
		if err != nil {
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   293
			myLog.Println(0, "Error:", err)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   294
			continue
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   295
		}
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   296
		hashes[hash] = append(hashes[hash], fo)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   297
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   298
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   299
	// Let's de-dupe now...
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   300
	for _, l := range hashes {
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   301
		if len(l) < 2 {
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   302
			continue
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   303
		}
7
68375cc98f98 Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   304
		if sType == partialChecksum {
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   305
			scheduleFull = append(scheduleFull, l)
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   306
		} else { // full checksums -> we're done
7
68375cc98f98 Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
   307
			dupeList = append(dupeList, l)
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   308
			myLog.Printf(5, "  . found %d new duplicates\n", len(l))
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   309
		}
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   310
	}
9
5b58342459eb Small optimization
Mikael Berthe <mikael@lilotux.net>
parents: 8
diff changeset
   311
	if sType == partialChecksum && len(scheduleFull) > 0 {
10
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   312
		computeSheduledChecksums(scheduleFull)
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   313
		for _, l := range scheduleFull {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   314
			r := l.findDupesChecksums(fullChecksum)
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   315
			dupeList = append(dupeList, r...)
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   316
		}
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   317
	}
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   318
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   319
	return dupeList
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   320
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   321
6
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   322
// findDupes() uses checksums to find file duplicates
16
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   323
func (data *dataT) findDupes(skipPartial bool) foListList {
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   324
	var dupeList foListList
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   325
	var schedulePartial foListList
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   326
	var scheduleFull foListList
6
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   327
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   328
	for size, sgListP := range data.sizeGroups {
6
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   329
		// We skip partial checksums for small files or if requested
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   330
		if size > minSizePartialChecksum && !skipPartial {
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   331
			sgListP.scheduleChecksum(partialChecksum)
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   332
			schedulePartial = append(schedulePartial, *sgListP)
6
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   333
		} else {
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   334
			sgListP.scheduleChecksum(fullChecksum)
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   335
			scheduleFull = append(scheduleFull, *sgListP)
6
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   336
		}
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   337
	}
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   338
10
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   339
	computeSheduledChecksums(schedulePartial, scheduleFull)
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   340
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   341
	for _, l := range schedulePartial {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   342
		r := l.findDupesChecksums(partialChecksum)
6
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   343
		dupeList = append(dupeList, r...)
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   344
	}
8
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   345
	for _, l := range scheduleFull {
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   346
		r := l.findDupesChecksums(fullChecksum)
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   347
		dupeList = append(dupeList, r...)
25ad96511395 Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents: 7
diff changeset
   348
	}
6
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   349
	return dupeList
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   350
}
6740350569d3 Cosmetics - move function
Mikael Berthe <mikael@lilotux.net>
parents: 5
diff changeset
   351
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   352
func (data *dataT) dropEmptyFiles(ignoreEmpty bool) (emptyCount int) {
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   353
	sgListP, ok := data.sizeGroups[0]
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   354
	if ok == false {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   355
		return // no empty files
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   356
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   357
	if !ignoreEmpty {
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   358
		if len(*sgListP) > 1 {
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   359
			data.emptyFiles = *sgListP
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   360
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   361
		delete(data.sizeGroups, 0)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   362
		return
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   363
	}
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   364
	emptyCount = len(*sgListP)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   365
	delete(data.sizeGroups, 0)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   366
	return
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   367
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   368
2
55098d552ae2 s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
   369
// initialCleanup() removes files with unique size as well as hard links
55098d552ae2 s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
   370
func (data *dataT) initialCleanup() (hardLinkCount, uniqueSizeCount int) {
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   371
	for s, sgListP := range data.sizeGroups {
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   372
		if len(*sgListP) < 2 {
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   373
			delete(data.sizeGroups, s)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   374
			uniqueSizeCount++
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   375
			continue
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   376
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   377
10
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   378
		// We can't look for hard links if the O.S. does not support
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   379
		// them...
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   380
		if !OSHasInodes() {
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   381
			continue
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   382
		}
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   383
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   384
		var hardlinksFound bool
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   385
10
1ee01b135e0e Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents: 9
diff changeset
   386
		// Check for hard links
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   387
		// Remove unique dev/inodes
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   388
		// Instead of this loop, another way would be to use the field
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   389
		// "Unique" of the fileObj to mark them to be discarded
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   390
		// and remove them all at the end.
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   391
		for {
15
4e3a67dc70a0 Cosmetics (go fmt and comments...)
Mikael Berthe <mikael@lilotux.net>
parents: 14
diff changeset
   392
			type devinode struct{ dev, ino uint64 }
12
15e3580cfb8d Fix hard link detection
Mikael Berthe <mikael@lilotux.net>
parents: 10
diff changeset
   393
			devinodes := make(map[devinode]bool)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   394
			var hardLinkIndex int
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   395
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   396
			for i, fo := range *sgListP {
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   397
				dev, ino := GetDevIno(fo)
15
4e3a67dc70a0 Cosmetics (go fmt and comments...)
Mikael Berthe <mikael@lilotux.net>
parents: 14
diff changeset
   398
				di := devinode{dev, ino}
12
15e3580cfb8d Fix hard link detection
Mikael Berthe <mikael@lilotux.net>
parents: 10
diff changeset
   399
				if _, hlink := devinodes[di]; hlink {
15e3580cfb8d Fix hard link detection
Mikael Berthe <mikael@lilotux.net>
parents: 10
diff changeset
   400
					hardLinkIndex = i
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   401
					hardLinkCount++
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   402
					hardlinksFound = true
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   403
					break
12
15e3580cfb8d Fix hard link detection
Mikael Berthe <mikael@lilotux.net>
parents: 10
diff changeset
   404
				} else {
15e3580cfb8d Fix hard link detection
Mikael Berthe <mikael@lilotux.net>
parents: 10
diff changeset
   405
					devinodes[di] = true
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   406
				}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   407
			}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   408
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   409
			if hardLinkIndex == 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   410
				break
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   411
			}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   412
			i := hardLinkIndex
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   413
			// Remove hardink
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   414
			copy((*sgListP)[i:], (*sgListP)[i+1:])
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   415
			(*sgListP)[len(*sgListP)-1] = nil
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   416
			*sgListP = (*sgListP)[:len(*sgListP)-1]
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   417
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   418
		// We have found hard links in this size group,
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   419
		// maybe we can remove it
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   420
		if hardlinksFound {
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   421
			if len(*sgListP) < 2 {
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   422
				delete(data.sizeGroups, s)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   423
				uniqueSizeCount++
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   424
				continue
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   425
			}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   426
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   427
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   428
	return
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   429
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   430
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   431
func formatSize(sizeBytes uint64, short bool) string {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   432
	var units = map[int]string{
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   433
		0: "B",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   434
		1: "KiB",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   435
		2: "MiB",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   436
		3: "GiB",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   437
		4: "TiB",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   438
		5: "PiB",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   439
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   440
	humanSize := sizeBytes
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   441
	var n int
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   442
	for n < len(units)-1 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   443
		if humanSize < 10000 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   444
			break
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   445
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   446
		humanSize /= 1024
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   447
		n++
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   448
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   449
	if n < 1 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   450
		return fmt.Sprintf("%d bytes", sizeBytes)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   451
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   452
	if short {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   453
		return fmt.Sprintf("%d %s", humanSize, units[n])
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   454
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   455
	return fmt.Sprintf("%d bytes (%d %s)", sizeBytes, humanSize, units[n])
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   456
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   457
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   458
func main() {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   459
	var verbose bool
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   460
	var summary bool
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   461
	var skipPartial bool
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   462
	var ignoreEmpty bool
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   463
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   464
	flag.BoolVar(&verbose, "verbose", false, "Be verbose (verbosity=1)")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   465
	flag.BoolVar(&verbose, "v", false, "See --verbose")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   466
	flag.BoolVar(&summary, "summary", false, "Do not display the duplicate list")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   467
	flag.BoolVar(&summary, "s", false, "See --summary")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   468
	flag.BoolVar(&skipPartial, "skip-partial", false, "Skip partial checksums")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   469
	flag.IntVar(&myLog.verbosity, "verbosity", 0,
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   470
		"Set verbosity level (1-6)")
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   471
	flag.IntVar(&myLog.verbosity, "vl", 0, "See verbosity")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   472
	timings := flag.Bool("timings", false, "Set detailed log timings")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   473
	flag.BoolVar(&ignoreEmpty, "no-empty", false, "Ignore empty files")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   474
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   475
	flag.Parse()
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   476
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   477
	if myLog.verbosity > 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   478
		verbose = true
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   479
	} else if verbose == true {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   480
		myLog.verbosity = 1
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   481
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   482
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   483
	if len(flag.Args()) == 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   484
		// TODO: more helpful usage statement
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   485
		myLog.Println(-1, "Usage:", os.Args[0],
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   486
			"[options] base_directory")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   487
		os.Exit(0)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   488
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   489
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   490
	if *timings {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   491
		log.SetFlags(log.LstdFlags | log.Lmicroseconds)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   492
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   493
14
ea4286b6c4b1 Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents: 13
diff changeset
   494
	data.sizeGroups = make(map[int64]*FileObjList)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   495
	myLog.Println(1, "* Reading file metadata")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   496
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   497
	for _, root := range flag.Args() {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   498
		if err := filepath.Walk(root, visit); err != nil {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   499
			myLog.Printf(-1, "* Error: could not read file tree:\n")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   500
			myLog.Printf(-1, "> %v\n", err)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   501
			os.Exit(1)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   502
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   503
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   504
	emptyCount := data.dropEmptyFiles(ignoreEmpty)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   505
	if verbose {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   506
		if data.ignoreCount > 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   507
			myLog.Printf(1, "  %d special files were ignored\n",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   508
				data.ignoreCount)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   509
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   510
		myLog.Println(2, "  Initial counter:", data.cmpt, "files")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   511
		myLog.Println(2, "  Total size:", formatSize(data.totalSize,
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   512
			false))
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   513
		if emptyCount > 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   514
			myLog.Printf(1, "  %d empty files were ignored\n",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   515
				emptyCount)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   516
		}
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   517
		data.dispCount() // XXX
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   518
		myLog.Println(3, "* Number of size groups:", len(data.sizeGroups))
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   519
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   520
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   521
	// Remove unique sizes
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   522
	myLog.Println(1, "* Removing files with unique size and hard links...")
2
55098d552ae2 s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
   523
	hardLinkCount, uniqueSizeCount := data.initialCleanup()
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   524
	if verbose {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   525
		myLog.Printf(2, "  Dropped %d files with unique size\n",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   526
			uniqueSizeCount)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   527
		myLog.Printf(2, "  Dropped %d hard links\n", hardLinkCount)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   528
		myLog.Println(3, "* Number of size groups:", len(data.sizeGroups))
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   529
		data.dispCount() // XXX
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   530
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   531
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   532
	// Get list of dupes
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   533
	myLog.Println(1, "* Computing checksums...")
16
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   534
	var result foListList
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   535
	if len(data.emptyFiles) > 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   536
		result = append(result, data.emptyFiles)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   537
	}
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   538
	result = append(result, data.findDupes(skipPartial)...)
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   539
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   540
	myLog.Println(3, "* Number of match groups:", len(result))
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   541
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   542
	// Done!  Dump dupes
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   543
	if len(result) > 0 && !summary {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   544
		myLog.Println(1, "* Dupes:")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   545
	}
16
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   546
	// Sort by increasing size (of the files, not groups)
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   547
	sort.Sort(byGroupFileSize(result))
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   548
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   549
	var dupeSize uint64
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   550
	data.cmpt = 0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   551
	for i, l := range result {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   552
		size := uint64(l[0].Size())
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   553
		// We do not count the size of the 1st item
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   554
		// so we get only duplicate size.
5
887c21c26cc8 Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents: 2
diff changeset
   555
		dupeSize += size * uint64(len(l)-1)
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   556
		if !summary {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   557
			fmt.Printf("\nGroup #%d (%d files * %v):\n", i+1,
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   558
				len(l), formatSize(size, true))
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   559
		}
16
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   560
		sort.Sort(byFilePathName(l))
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   561
		for _, f := range l {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   562
			if !summary {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   563
				fmt.Println(f.FilePath)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   564
			}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   565
			data.cmpt++
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   566
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   567
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   568
	summaryLevel := 1 // Default verbosity for the summary line
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   569
	if summary == false {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   570
		// Add a trailing newline
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   571
		if len(result) > 0 {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   572
			fmt.Println("")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   573
		}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   574
	} else {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   575
		// The summary is requested so we lower the verbosity level
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   576
		summaryLevel = 0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   577
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   578
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   579
	myLog.Println(summaryLevel, "Final count:", data.cmpt,
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   580
		"duplicate files in", len(result), "sets")
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   581
	myLog.Println(summaryLevel, "Redundant data size:",
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   582
		formatSize(dupeSize, false))
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   583
}
16
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   584
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   585
// Implement a sort interface for the list of duplicate groups
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   586
type byGroupFileSize foListList
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   587
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   588
func (a byGroupFileSize) Len() int      { return len(a) }
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   589
func (a byGroupFileSize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   590
func (a byGroupFileSize) Less(i, j int) bool {
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   591
	// Since this is supposed to be used for duplicate lists,
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   592
	// we use the size of the first file of the group.
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   593
	return a[i][0].Size() < a[j][0].Size()
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   594
}
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   595
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   596
// Implement a sort interface for a slice of files
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   597
type byFilePathName FileObjList
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   598
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   599
func (a byFilePathName) Len() int      { return len(a) }
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   600
func (a byFilePathName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   601
func (a byFilePathName) Less(i, j int) bool {
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   602
	return a[i].Name() < a[j].Name()
cc0ee80cf216 Sort results
Mikael Berthe <mikael@lilotux.net>
parents: 15
diff changeset
   603
}