Skip to content

Commit

Permalink
Protect creation of "dir:" with a mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-m committed Jun 10, 2019
1 parent 9c475c3 commit 733c563
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
23 changes: 23 additions & 0 deletions internal/taskfile/task.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package taskfile

import "os"
import "sync"

// Tasks represents a group of tasks
type Tasks map[string]*Task

Expand All @@ -14,10 +17,30 @@ type Task struct {
Generates []string
Status []string
Dir string
mkdirMutex sync.Mutex
Vars Vars
Env Vars
Silent bool
Method string
Prefix string
IgnoreError bool `yaml:"ignore_error"`
}

// Mkdir creates the directory Task.Dir.
// Safe to be called concurrently.
func (t *Task) Mkdir() error {
if t.Dir == "" {
// No "dir:" attribute, so we do nothing.
return nil
}

t.mkdirMutex.Lock()
defer t.mkdirMutex.Unlock()

if _, err := os.Stat(t.Dir); os.IsNotExist(err) {
if err := os.MkdirAll(t.Dir, 0755); err != nil {
return err
}
}
return nil
}
9 changes: 2 additions & 7 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,8 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {

// When using the "dir:" attribute it can happen that the directory doesn't exist.
// If so, we create it.
if t.Dir != "" {
if _, err := os.Stat(t.Dir); os.IsNotExist(err) {
if err := os.MkdirAll(t.Dir, 0755); err != nil {
e.Logger.Errf("task: cannot make directory %q: %v", t.Dir, err)
return err
}
}
if err := t.Mkdir(); err != nil {
e.Logger.Errf("task: cannot make directory %q: %v", t.Dir, err)
}

for i := range t.Cmds {
Expand Down

0 comments on commit 733c563

Please sign in to comment.