Skip to content

Instantly share code, notes, and snippets.

@joncooper
Created October 4, 2011 20:19
Show Gist options
  • Select an option

  • Save joncooper/1262674 to your computer and use it in GitHub Desktop.

Select an option

Save joncooper/1262674 to your computer and use it in GitHub Desktop.
Explorations in Go: A dupe checker in Go and Ruby
➜ dupe git:(master) ✗ ./dupe --help
Usage of ./dupe:
-verbose=false: Print the list of duplicate files.
var verbose *bool = flag.Bool("verbose", false, "Print the list of duplicate files.")
var rootDir string = "."
func ParseArgs() {
flag.Parse()
if (len(flag.Args()) > 0) {
rootDir = flag.Arg(0)
}
}
VERBOSE = ARGV.delete('--verbose')
ROOT_DIR = ARGV[0] ||= "."
var fullPathsByFilename map[string][]string
type DupeChecker struct{}
func (dc DupeChecker) VisitDir(fullpath string, f *os.FileInfo) bool {
return true
}
func (dc DupeChecker) VisitFile(fullpath string, f *os.FileInfo) {
filename := path.Base(fullpath)
fullPathsByFilename[filename] = append(fullPathsByFilename[filename], fullpath)
}
func FindDupes(root string) {
fullPathsByFilename = make(map[string][]string)
filepath.Walk(root, DupeChecker{}, nil)
}
func Walk(root string, v Visitor, errors chan<- os Error)
type Visitor interface {
VisitDir(path string, f *os.FileInfo) bool
VisitFile(path string, f *os.FileInfo)
}
@full_paths_by_filename = {}
def visit_file(fullpath)
return if File.directory? fullpath
return if File.symlink? fullpath
basename = File.basename(fullpath)
(@full_paths_by_filename[basename] ||= []) << fullpath
end
Find.find(ROOT_DIR) do |fullpath|
visit_file(fullpath)
end
func MD5OfFile(fullpath string) []byte {
if contents, err := ioutil.ReadFile(fullpath); err == nil {
var md5sum hash.Hash = md5.New()
md5sum.Write(contents)
return md5sum.Sum()
}
return nil
}
def md5_of_file(fullpath)
Digest::MD5.hexdigest(File.read(fullpath))
end
fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
return errno;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment