daily pastebin goal
22%
SHARE
TWEET

Untitled

a guest Jun 3rd, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "crypto/sha512"
  5.     "fmt"
  6.     "io"
  7.     "log"
  8.     "os"
  9.     "path/filepath"
  10.     "sync"
  11. )
  12.  
  13. type DubCandid struct {
  14.     os.FileInfo
  15.     Path      string
  16.     Hash      string
  17.     IsDeleted bool
  18. }
  19.  
  20. func main() {
  21.     var wg sync.WaitGroup
  22.  
  23.     path := os.Args[1]
  24.     files := readRDir(path)
  25.     fmt.Printf("Searhing %s for dubs in %d files\n", path, len(files))
  26.     i := 0
  27.     wg.Add(1)
  28.     go (func() {
  29.         for _, file := range files {
  30.             wg.Add(1)
  31.             go func() {
  32.                 h := sha512.New()
  33.                 f, _ := os.Open(file.Path)
  34.                 if _, err := io.Copy(h, f); err != nil {
  35.                     log.Fatal(err)
  36.                 }
  37.                 file.Hash = fmt.Sprintf("%x", h.Sum(nil))
  38.                 fmt.Printf("%s : %s\n", file.Path, file.Hash)
  39.                 i++
  40.                 wg.Done()
  41.             }()
  42.         }
  43.         wg.Done()
  44.     })()
  45.  
  46.     wg.Wait()
  47.     fmt.Println(i)
  48.  
  49. }
  50.  
  51. func readRDir(path string) (res []DubCandid) {
  52.     filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
  53.         if err != nil {
  54.             return err
  55.         }
  56.         if info.IsDir() == false {
  57.             res = append(res, DubCandid{
  58.                 FileInfo: info,
  59.                 Path:     path,
  60.             })
  61.         }
  62.         return nil
  63.     })
  64.     return res
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top