Skip to content

Commit

Permalink
Making the walking function thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmandourah committed Oct 21, 2024
1 parent 303d8de commit 76d8039
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
8 changes: 4 additions & 4 deletions sources/directory/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ func (src *directorySource) loadGamesDirectory(directory string) error {
if err != nil {
return err
}

newGameFiles = src.addDirectoryGame(newGameFiles, extension, fileInfo.Size(), path)
//make it thread safe
src.mutex.Lock()
newGameFiles = src.addDirectoryGame(newGameFiles, extension, fileInfo.Size(), path)
src.mutex.Unlock()

} else if info.IsDir() {
if path != directory {
Expand All @@ -124,13 +126,11 @@ func (src *directorySource) loadGamesDirectory(directory string) error {
if err != nil {
return err
}
src.mutex.Lock()
src.gameFiles = append(src.gameFiles, newGameFiles...)
// Add all files
if len(newGameFiles) > 0 {
src.collection.AddNewGames(newGameFiles)
}
src.mutex.Unlock()
return nil
}

Expand Down
6 changes: 0 additions & 6 deletions sources/directory/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,12 @@ func (src *directorySource) watchDirectory(directory string) {

if event.Op&fsnotify.Create != 0 {
newGames := src.addDirectoryGame(make([]repository.FileDesc, 0), filepath.Ext(event.Name), 0, event.Name)
src.mutex.Lock()
src.gameFiles = append(src.gameFiles, newGames...)
src.collection.AddNewGames(newGames)
src.mutex.Unlock()
} else if event.Op&fsnotify.Remove != 0 {
src.mutex.Lock()
src.removeEntriesFromDirectory(event.Name)
src.mutex.Unlock()
} else if event.Op&fsnotify.Rename == fsnotify.Rename {
src.mutex.Lock()
src.removeEntriesFromDirectory(event.Name)
src.mutex.Unlock()
}

case err, ok := <-src.watcherDirectories.Errors:
Expand Down

0 comments on commit 76d8039

Please sign in to comment.