Created
October 4, 2011 20:19
-
-
Save joncooper/1262674 to your computer and use it in GitHub Desktop.
Explorations in Go: A dupe checker in Go and Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ➜ dupe git:(master) ✗ ./dupe --help | |
| Usage of ./dupe: | |
| -verbose=false: Print the list of duplicate files. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| VERBOSE = ARGV.delete('--verbose') | |
| ROOT_DIR = ARGV[0] ||= "." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func Walk(root string, v Visitor, errors chan<- os Error) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Visitor interface { | |
| VisitDir(path string, f *os.FileInfo) bool | |
| VisitFile(path string, f *os.FileInfo) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def md5_of_file(fullpath) | |
| Digest::MD5.hexdigest(File.read(fullpath)) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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