Skip to content

Commit

Permalink
Split goduf.go into several files
Browse files Browse the repository at this point in the history
  • Loading branch information
McKael committed Oct 13, 2018
1 parent bd902ec commit 083bc94
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 55 deletions.
58 changes: 3 additions & 55 deletions goduf.go
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014-2017 Mikael Berthe <mikael@lilotux.net>
* Copyright (C) 2014-2018 Mikael Berthe <mikael@lilotux.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -33,7 +33,6 @@ import (
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -73,37 +72,9 @@ type dataT struct {

var data dataT

type myLogT struct {
verbosity int
}

// Implement my own logger
var myLog myLogT

func (l *myLogT) Printf(level int, format string, args ...interface{}) {
if level > l.verbosity {
return
}
if level >= 0 {
log.Printf(format, args...)
return
}
// Error message without timestamp
fmt.Fprintf(os.Stderr, format, args...)
}

func (l *myLogT) Println(level int, args ...interface{}) {
if level > l.verbosity {
return
}
if level >= 0 {
log.Println(args...)
return
}
// Error message without timestamp
fmt.Fprintln(os.Stderr, args...)
}

// visit is called for every file and directory.
// We check the file object is correct (regular, readable...) and add
// it to the data.sizeGroups hash.
Expand Down Expand Up @@ -487,7 +458,7 @@ func main() {

// Assertion on constant values
if minSizePartialChecksum <= 2*medsumBytes {
log.Fatal("Internal error: assert minSizePartialChecksum > 2*medsumBytes")
myLog.Fatal("Internal error: assert minSizePartialChecksum > 2*medsumBytes")
}

// Command line parameters parsingg
Expand Down Expand Up @@ -520,7 +491,7 @@ func main() {

// Change log format for benchmarking
if *timings {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
myLog.SetBenchFlags()
}

data.sizeGroups = make(map[int64]*FileObjList)
Expand Down Expand Up @@ -621,26 +592,3 @@ func main() {
myLog.Println(summaryLevel, "Redundant data size:",
formatSize(dupeSize, false))
}

// Implement a sort interface for the list of duplicate groups
type byGroupFileSize foListList

func (a byGroupFileSize) Len() int { return len(a) }
func (a byGroupFileSize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byGroupFileSize) Less(i, j int) bool {
// Since this is supposed to be used for duplicate lists,
// we use the size of the first file of the group.
if a[i][0].Size() == a[j][0].Size() {
return a[i][0].FilePath < a[j][0].FilePath
}
return a[i][0].Size() < a[j][0].Size()
}

// Implement a sort interface for a slice of files
type byFilePathName FileObjList

func (a byFilePathName) Len() int { return len(a) }
func (a byFilePathName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byFilePathName) Less(i, j int) bool {
return a[i].FilePath < a[j].FilePath
}
62 changes: 62 additions & 0 deletions logging.go
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2014-2018 Mikael Berthe <mikael@lilotux.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/

package main

import (
"fmt"
"log"
"os"
)

type myLogT struct {
verbosity int
}

func (l *myLogT) Printf(level int, format string, args ...interface{}) {
if level > l.verbosity {
return
}
if level >= 0 {
log.Printf(format, args...)
return
}
// Error message without timestamp
fmt.Fprintf(os.Stderr, format, args...)
}

func (l *myLogT) Println(level int, args ...interface{}) {
if level > l.verbosity {
return
}
if level >= 0 {
log.Println(args...)
return
}
// Error message without timestamp
fmt.Fprintln(os.Stderr, args...)
}

func (l *myLogT) Fatal(args ...interface{}) {
log.Fatal(args...)
}

func (l *myLogT) SetBenchFlags() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
}
43 changes: 43 additions & 0 deletions sort.go
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2014-2018 Mikael Berthe <mikael@lilotux.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/

package main

// Implement a sort interface for the list of duplicate groups
type byGroupFileSize foListList

func (a byGroupFileSize) Len() int { return len(a) }
func (a byGroupFileSize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byGroupFileSize) Less(i, j int) bool {
// Since this is supposed to be used for duplicate lists,
// we use the size of the first file of the group.
if a[i][0].Size() == a[j][0].Size() {
return a[i][0].FilePath < a[j][0].FilePath
}
return a[i][0].Size() < a[j][0].Size()
}

// Implement a sort interface for a slice of files
type byFilePathName FileObjList

func (a byFilePathName) Len() int { return len(a) }
func (a byFilePathName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byFilePathName) Less(i, j int) bool {
return a[i].FilePath < a[j].FilePath
}

0 comments on commit 083bc94

Please sign in to comment.