-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
99 lines (80 loc) · 2.19 KB
/
lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package gitdb
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func (g *gitdb) Lock(m Model) error {
if !m.IsLockable() {
return errors.New("Model is not lockable")
}
var lockFilesWritten []string
fullPath := g.lockDir(m)
if _, err := os.Stat(fullPath); err != nil {
err := os.MkdirAll(fullPath, 0755)
if err != nil {
return err
}
}
lockFiles := m.GetLockFileNames()
for _, file := range lockFiles {
lockFile := filepath.Join(fullPath, file)
g.events <- newWriteBeforeEvent("...", lockFile)
//when locking a model, lockfile should not exist
if _, err := os.Stat(lockFile); err == nil {
if derr := g.deleteLockFiles(lockFilesWritten); derr != nil {
//log.PutError(derr.Error())
}
return errors.New("Lock file already exist: " + lockFile)
}
err := ioutil.WriteFile(lockFile, []byte(""), 0644)
if err != nil {
if derr := g.deleteLockFiles(lockFilesWritten); derr != nil {
//log.PutError(derr.Error())
}
return errors.New("Failed to write lock " + lockFile + ": " + err.Error())
}
lockFilesWritten = append(lockFilesWritten, lockFile)
}
commitMsg := "Created Lock Files for: " + ID(m)
g.events <- newWriteEvent(commitMsg, fullPath, g.autoCommit)
return nil
}
func (g *gitdb) Unlock(m Model) error {
if !m.IsLockable() {
return errors.New("Model is not lockable")
}
fullPath := g.lockDir(m)
lockFiles := m.GetLockFileNames()
for _, file := range lockFiles {
lockFile := filepath.Join(fullPath, file)
if _, err := os.Stat(lockFile); err == nil {
//log.PutInfo("Removing " + lockFile)
err := os.Remove(lockFile)
if err != nil {
return errors.New("Could not delete lock file: " + lockFile)
}
}
}
commitMsg := "Removing Lock Files for: " + ID(m)
g.events <- newWriteEvent(commitMsg, fullPath, g.autoCommit)
return nil
}
func (g *gitdb) deleteLockFiles(files []string) error {
var err error
var failedDeletes []string
if len(files) > 0 {
for _, file := range files {
err = os.Remove(file)
if err != nil {
failedDeletes = append(failedDeletes, file)
}
}
}
if len(failedDeletes) > 0 {
return errors.New("Could not delete lock files: " + strings.Join(failedDeletes, ","))
}
return err
}