Skip to content

Commit

Permalink
Merge pull request #25 from mainak55512/enhancement
Browse files Browse the repository at this point in the history
Enhancement
  • Loading branch information
mainak55512 authored Sep 10, 2024
2 parents ffa0cfd + 2c6d9f8 commit 77dffa9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 20 deletions.
23 changes: 11 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func main() {
// Limiting os threads to available cpu
runtime.GOMAXPROCS(runtime.NumCPU())

// Limited goroutines to 1000
max_goroutines := 1000
// Limited goroutines to 20
max_goroutines := 20

// this channel will limit the goroutine number
guard := make(chan struct{}, max_goroutines)
Expand Down Expand Up @@ -103,24 +103,23 @@ func main() {
fmt.Sprint(total_comments),
fmt.Sprint(total_code),
})
table.Render()

pwd, e := os.Getwd()
fmt.Printf(
"\nGit initialized:\t%t\nTotal sub-directories:\t%5d\n",
is_git_initialized,
folder_count,
)
fmt.Println("Target directory: ", path.Join(pwd, folder_name))
fmt.Println()

table.Render()

if e != nil {
fmt.Println(e)
os.Exit(1)
}

fmt.Println("Target directory: ", path.Join(pwd, folder_name))

// total subdirectories are folder_count-1,
// as present working directory is not a subdirectory
fmt.Printf(
"Total sub-directories:\t%5d\nGit initialized:\t%t\n",
folder_count-1,
is_git_initialized,
)
} else {

// will be set to true if atleast one file
Expand Down
52 changes: 44 additions & 8 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,28 @@ func GetFiles(
if file_directory_name != "" {
folder_location = path.Join(folder_location, file_directory_name)
}
err := filepath.Walk(folder_location, func(
wgDir := &sync.WaitGroup{}
muDir := &sync.RWMutex{}

// Limited goroutines to 10000
// buffer is set to high to avoid deadlock
max_goroutines_dir := 10000

// this channel will limit the goroutine number
guardDir := make(chan struct{}, max_goroutines_dir)

guardDir <- struct{}{}
wgDir.Add(1)

err := walkDirConcur(folder_location, folder_count, &files, is_git_initialized, wgDir, muDir, guardDir)
wgDir.Wait()
return files, err
}

func walkDirConcur(folder_location string, folder_count *int32, files *[]file_info, is_git_initialized *bool, wgDir *sync.WaitGroup, muDir *sync.RWMutex, guardDir chan struct{}) error {
defer wgDir.Done()

visitFolder := func(
_path string,
f os.FileInfo,
err error,
Expand All @@ -292,15 +313,26 @@ func GetFiles(
return err
}
// if it is a folder, then increase the folder count
if f.IsDir() {
if f.IsDir() && _path != folder_location {

// if folder name is '.git', then
// set is_git_initialized to true
if _path == path.Join(folder_location, ".git") && *is_git_initialized == false {
*is_git_initialized = true
if _path == path.Join(folder_location, ".git") {
muDir.Lock()
if *is_git_initialized == false {
*is_git_initialized = true
}
muDir.Unlock()
}
muDir.Lock()
*folder_count++
} else {
muDir.Unlock()
guardDir <- struct{}{}
wgDir.Add(1)
go walkDirConcur(_path, folder_count, files, is_git_initialized, wgDir, muDir, guardDir)
return filepath.SkipDir
}
if f.Mode().IsRegular() {
ext := strings.Join(
strings.Split(f.Name(), ".")[1:],
".",
Expand All @@ -309,13 +341,17 @@ func GetFiles(
ext = f.Name()
}
if _, exists := lookup_map[ext]; exists {
files = append(files, file_info{
muDir.Lock()
*files = append(*files, file_info{
path: _path,
ext: ext,
})
muDir.Unlock()
}
}
return nil
})
return files, err
}
err := filepath.Walk(folder_location, visitFolder)
<-guardDir
return err
}

0 comments on commit 77dffa9

Please sign in to comment.