use locking lib at go4.org
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
Jeromy committed
Jun 21, 2016 at 14:34 UTC
1fc8ffb7695cff57dae32615d31da3a0d6f3cf8d
14 files changed
+9
-932
Godeps/_workspace/src/github.com/camlistore/lock/.gitignore
deleted
-1
@@ -1 +0,0 @@
1
-*~
Godeps/_workspace/src/github.com/camlistore/lock/COPYING
deleted
-202
@@ -1,202 +0,0 @@
1
-
2
- Apache License
3
- Version 2.0, January 2004
4
- http://www.apache.org/licenses/
5
-
6
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
-
8
- 1. Definitions.
9
-
10
- "License" shall mean the terms and conditions for use, reproduction,
11
- and distribution as defined by Sections 1 through 9 of this document.
12
-
13
- "Licensor" shall mean the copyright owner or entity authorized by
14
- the copyright owner that is granting the License.
15
-
16
- "Legal Entity" shall mean the union of the acting entity and all
17
- other entities that control, are controlled by, or are under common
18
- control with that entity. For the purposes of this definition,
19
- "control" means (i) the power, direct or indirect, to cause the
20
- direction or management of such entity, whether by contract or
21
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
- outstanding shares, or (iii) beneficial ownership of such entity.
23
-
24
- "You" (or "Your") shall mean an individual or Legal Entity
25
- exercising permissions granted by this License.
26
-
27
- "Source" form shall mean the preferred form for making modifications,
28
- including but not limited to software source code, documentation
29
- source, and configuration files.
30
-
31
- "Object" form shall mean any form resulting from mechanical
32
- transformation or translation of a Source form, including but
33
- not limited to compiled object code, generated documentation,
34
- and conversions to other media types.
35
-
36
- "Work" shall mean the work of authorship, whether in Source or
37
- Object form, made available under the License, as indicated by a
38
- copyright notice that is included in or attached to the work
39
- (an example is provided in the Appendix below).
40
-
41
- "Derivative Works" shall mean any work, whether in Source or Object
42
- form, that is based on (or derived from) the Work and for which the
43
- editorial revisions, annotations, elaborations, or other modifications
44
- represent, as a whole, an original work of authorship. For the purposes
45
- of this License, Derivative Works shall not include works that remain
46
- separable from, or merely link (or bind by name) to the interfaces of,
47
- the Work and Derivative Works thereof.
48
-
49
- "Contribution" shall mean any work of authorship, including
50
- the original version of the Work and any modifications or additions
51
- to that Work or Derivative Works thereof, that is intentionally
52
- submitted to Licensor for inclusion in the Work by the copyright owner
53
- or by an individual or Legal Entity authorized to submit on behalf of
54
- the copyright owner. For the purposes of this definition, "submitted"
55
- means any form of electronic, verbal, or written communication sent
56
- to the Licensor or its representatives, including but not limited to
57
- communication on electronic mailing lists, source code control systems,
58
- and issue tracking systems that are managed by, or on behalf of, the
59
- Licensor for the purpose of discussing and improving the Work, but
60
- excluding communication that is conspicuously marked or otherwise
61
- designated in writing by the copyright owner as "Not a Contribution."
62
-
63
- "Contributor" shall mean Licensor and any individual or Legal Entity
64
- on behalf of whom a Contribution has been received by Licensor and
65
- subsequently incorporated within the Work.
66
-
67
- 2. Grant of Copyright License. Subject to the terms and conditions of
68
- this License, each Contributor hereby grants to You a perpetual,
69
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
- copyright license to reproduce, prepare Derivative Works of,
71
- publicly display, publicly perform, sublicense, and distribute the
72
- Work and such Derivative Works in Source or Object form.
73
-
74
- 3. Grant of Patent License. Subject to the terms and conditions of
75
- this License, each Contributor hereby grants to You a perpetual,
76
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
- (except as stated in this section) patent license to make, have made,
78
- use, offer to sell, sell, import, and otherwise transfer the Work,
79
- where such license applies only to those patent claims licensable
80
- by such Contributor that are necessarily infringed by their
81
- Contribution(s) alone or by combination of their Contribution(s)
82
- with the Work to which such Contribution(s) was submitted. If You
83
- institute patent litigation against any entity (including a
84
- cross-claim or counterclaim in a lawsuit) alleging that the Work
85
- or a Contribution incorporated within the Work constitutes direct
86
- or contributory patent infringement, then any patent licenses
87
- granted to You under this License for that Work shall terminate
88
- as of the date such litigation is filed.
89
-
90
- 4. Redistribution. You may reproduce and distribute copies of the
91
- Work or Derivative Works thereof in any medium, with or without
92
- modifications, and in Source or Object form, provided that You
93
- meet the following conditions:
94
-
95
- (a) You must give any other recipients of the Work or
96
- Derivative Works a copy of this License; and
97
-
98
- (b) You must cause any modified files to carry prominent notices
99
- stating that You changed the files; and
100
-
101
- (c) You must retain, in the Source form of any Derivative Works
102
- that You distribute, all copyright, patent, trademark, and
103
- attribution notices from the Source form of the Work,
104
- excluding those notices that do not pertain to any part of
105
- the Derivative Works; and
106
-
107
- (d) If the Work includes a "NOTICE" text file as part of its
108
- distribution, then any Derivative Works that You distribute must
109
- include a readable copy of the attribution notices contained
110
- within such NOTICE file, excluding those notices that do not
111
- pertain to any part of the Derivative Works, in at least one
112
- of the following places: within a NOTICE text file distributed
113
- as part of the Derivative Works; within the Source form or
114
- documentation, if provided along with the Derivative Works; or,
115
- within a display generated by the Derivative Works, if and
116
- wherever such third-party notices normally appear. The contents
117
- of the NOTICE file are for informational purposes only and
118
- do not modify the License. You may add Your own attribution
119
- notices within Derivative Works that You distribute, alongside
120
- or as an addendum to the NOTICE text from the Work, provided
121
- that such additional attribution notices cannot be construed
122
- as modifying the License.
123
-
124
- You may add Your own copyright statement to Your modifications and
125
- may provide additional or different license terms and conditions
126
- for use, reproduction, or distribution of Your modifications, or
127
- for any such Derivative Works as a whole, provided Your use,
128
- reproduction, and distribution of the Work otherwise complies with
129
- the conditions stated in this License.
130
-
131
- 5. Submission of Contributions. Unless You explicitly state otherwise,
132
- any Contribution intentionally submitted for inclusion in the Work
133
- by You to the Licensor shall be under the terms and conditions of
134
- this License, without any additional terms or conditions.
135
- Notwithstanding the above, nothing herein shall supersede or modify
136
- the terms of any separate license agreement you may have executed
137
- with Licensor regarding such Contributions.
138
-
139
- 6. Trademarks. This License does not grant permission to use the trade
140
- names, trademarks, service marks, or product names of the Licensor,
141
- except as required for reasonable and customary use in describing the
142
- origin of the Work and reproducing the content of the NOTICE file.
143
-
144
- 7. Disclaimer of Warranty. Unless required by applicable law or
145
- agreed to in writing, Licensor provides the Work (and each
146
- Contributor provides its Contributions) on an "AS IS" BASIS,
147
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
- implied, including, without limitation, any warranties or conditions
149
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
- PARTICULAR PURPOSE. You are solely responsible for determining the
151
- appropriateness of using or redistributing the Work and assume any
152
- risks associated with Your exercise of permissions under this License.
153
-
154
- 8. Limitation of Liability. In no event and under no legal theory,
155
- whether in tort (including negligence), contract, or otherwise,
156
- unless required by applicable law (such as deliberate and grossly
157
- negligent acts) or agreed to in writing, shall any Contributor be
158
- liable to You for damages, including any direct, indirect, special,
159
- incidental, or consequential damages of any character arising as a
160
- result of this License or out of the use or inability to use the
161
- Work (including but not limited to damages for loss of goodwill,
162
- work stoppage, computer failure or malfunction, or any and all
163
- other commercial damages or losses), even if such Contributor
164
- has been advised of the possibility of such damages.
165
-
166
- 9. Accepting Warranty or Additional Liability. While redistributing
167
- the Work or Derivative Works thereof, You may choose to offer,
168
- and charge a fee for, acceptance of support, warranty, indemnity,
169
- or other liability obligations and/or rights consistent with this
170
- License. However, in accepting such obligations, You may act only
171
- on Your own behalf and on Your sole responsibility, not on behalf
172
- of any other Contributor, and only if You agree to indemnify,
173
- defend, and hold each Contributor harmless for any liability
174
- incurred by, or claims asserted against, such Contributor by reason
175
- of your accepting any such warranty or additional liability.
176
-
177
- END OF TERMS AND CONDITIONS
178
-
179
- APPENDIX: How to apply the Apache License to your work.
180
-
181
- To apply the Apache License to your work, attach the following
182
- boilerplate notice, with the fields enclosed by brackets "[]"
183
- replaced with your own identifying information. (Don't include
184
- the brackets!) The text should be enclosed in the appropriate
185
- comment syntax for the file format. We also recommend that a
186
- file or class name and description of purpose be included on the
187
- same "printed page" as the copyright notice for easier
188
- identification within third-party archives.
189
-
190
- Copyright [yyyy] [name of copyright owner]
191
-
192
- Licensed under the Apache License, Version 2.0 (the "License");
193
- you may not use this file except in compliance with the License.
194
- You may obtain a copy of the License at
195
-
196
- http://www.apache.org/licenses/LICENSE-2.0
197
-
198
- Unless required by applicable law or agreed to in writing, software
199
- distributed under the License is distributed on an "AS IS" BASIS,
200
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
- See the License for the specific language governing permissions and
202
- limitations under the License.
Godeps/_workspace/src/github.com/camlistore/lock/README.txt
deleted
-3
@@ -1,3 +0,0 @@
1
-File locking library.
2
-
3
-See http://godoc.org/github.com/camlistore/lock
Godeps/_workspace/src/github.com/camlistore/lock/lock.go
deleted
-158
@@ -1,158 +0,0 @@
1
-/*
2
-Copyright 2013 The Go Authors
3
-
4
-Licensed under the Apache License, Version 2.0 (the "License");
5
-you may not use this file except in compliance with the License.
6
-You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
-Unless required by applicable law or agreed to in writing, software
11
-distributed under the License is distributed on an "AS IS" BASIS,
12
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
-See the License for the specific language governing permissions and
14
-limitations under the License.
15
-*/
16
-
17
-package lock
18
-
19
-import (
20
- "encoding/json"
21
- "fmt"
22
- "io"
23
- "os"
24
- "path/filepath"
25
- "sync"
26
-)
27
-
28
-// Lock locks the given file, creating the file if necessary. If the
29
-// file already exists, it must have zero size or an error is returned.
30
-// The lock is an exclusive lock (a write lock), but locked files
31
-// should neither be read from nor written to. Such files should have
32
-// zero size and only exist to co-ordinate ownership across processes.
33
-//
34
-// A nil Closer is returned if an error occurred. Otherwise, close that
35
-// Closer to release the lock.
36
-//
37
-// On Linux, FreeBSD and OSX, a lock has the same semantics as fcntl(2)'s
38
-// advisory locks. In particular, closing any other file descriptor for the
39
-// same file will release the lock prematurely.
40
-//
41
-// Attempting to lock a file that is already locked by the current process
42
-// has undefined behavior.
43
-//
44
-// On other operating systems, lock will fallback to using the presence and
45
-// content of a file named name + '.lock' to implement locking behavior.
46
-func Lock(name string) (io.Closer, error) {
47
- return lockFn(name)
48
-}
49
-
50
-var lockFn = lockPortable
51
-
52
-// Portable version not using fcntl. Doesn't handle crashes as gracefully,
53
-// since it can leave stale lock files.
54
-// TODO: write pid of owner to lock file and on race see if pid is
55
-// still alive?
56
-func lockPortable(name string) (io.Closer, error) {
57
- absName, err := filepath.Abs(name)
58
- if err != nil {
59
- return nil, fmt.Errorf("can't Lock file %q: can't find abs path: %v", name, err)
60
- }
61
- fi, err := os.Stat(absName)
62
- if err == nil && fi.Size() > 0 {
63
- if isStaleLock(absName) {
64
- os.Remove(absName)
65
- } else {
66
- return nil, fmt.Errorf("can't Lock file %q: has non-zero size", name)
67
- }
68
- }
69
- f, err := os.OpenFile(absName, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_EXCL, 0666)
70
- if err != nil {
71
- return nil, fmt.Errorf("failed to create lock file %s %v", absName, err)
72
- }
73
- if err := json.NewEncoder(f).Encode(&pidLockMeta{OwnerPID: os.Getpid()}); err != nil {
74
- return nil, err
75
- }
76
- return &lockCloser{f: f, abs: absName}, nil
77
-}
78
-
79
-type pidLockMeta struct {
80
- OwnerPID int
81
-}
82
-
83
-func isStaleLock(path string) bool {
84
- f, err := os.Open(path)
85
- if err != nil {
86
- return false
87
- }
88
- defer f.Close()
89
- var meta pidLockMeta
90
- if json.NewDecoder(f).Decode(&meta) != nil {
91
- return false
92
- }
93
- if meta.OwnerPID == 0 {
94
- return false
95
- }
96
- p, err := os.FindProcess(meta.OwnerPID)
97
- if err != nil {
98
- // e.g. on Windows
99
- return true
100
- }
101
- // On unix, os.FindProcess always is true, so we have to send
102
- // it a signal to see if it's alive.
103
- if signalZero != nil {
104
- if p.Signal(signalZero) != nil {
105
- return true
106
- }
107
- }
108
- return false
109
-}
110
-
111
-var signalZero os.Signal // nil or set by lock_sigzero.go
112
-
113
-type lockCloser struct {
114
- f *os.File
115
- abs string
116
- once sync.Once
117
- err error
118
-}
119
-
120
-func (lc *lockCloser) Close() error {
121
- lc.once.Do(lc.close)
122
- return lc.err
123
-}
124
-
125
-func (lc *lockCloser) close() {
126
- if err := lc.f.Close(); err != nil {
127
- lc.err = err
128
- }
129
- if err := os.Remove(lc.abs); err != nil {
130
- lc.err = err
131
- }
132
-}
133
-
134
-var (
135
- lockmu sync.Mutex
136
- locked = map[string]bool{} // abs path -> true
137
-)
138
-
139
-// unlocker is used by the darwin and linux implementations with fcntl
140
-// advisory locks.
141
-type unlocker struct {
142
- f *os.File
143
- abs string
144
-}
145
-
146
-func (u *unlocker) Close() error {
147
- lockmu.Lock()
148
- // Remove is not necessary but it's nice for us to clean up.
149
- // If we do do this, though, it needs to be before the
150
- // u.f.Close below.
151
- os.Remove(u.abs)
152
- if err := u.f.Close(); err != nil {
153
- return err
154
- }
155
- delete(locked, u.abs)
156
- lockmu.Unlock()
157
- return nil
158
-}
Godeps/_workspace/src/github.com/camlistore/lock/lock_appengine.go
deleted
-32
@@ -1,32 +0,0 @@
1
-// +build appengine
2
-
3
-/*
4
-Copyright 2013 The Go Authors
5
-
6
-Licensed under the Apache License, Version 2.0 (the "License");
7
-you may not use this file except in compliance with the License.
8
-You may obtain a copy of the License at
9
-
10
- http://www.apache.org/licenses/LICENSE-2.0
11
-
12
-Unless required by applicable law or agreed to in writing, software
13
-distributed under the License is distributed on an "AS IS" BASIS,
14
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
-See the License for the specific language governing permissions and
16
-limitations under the License.
17
-*/
18
-
19
-package lock
20
-
21
-import (
22
- "errors"
23
- "io"
24
-)
25
-
26
-func init() {
27
- lockFn = lockAppEngine
28
-}
29
-
30
-func lockAppEngine(name string) (io.Closer, error) {
31
- return nil, errors.New("Lock not available on App Engine")
32
-}
Godeps/_workspace/src/github.com/camlistore/lock/lock_darwin_amd64.go
deleted
-80
@@ -1,80 +0,0 @@
1
-// +build darwin,amd64
2
-// +build !appengine
3
-
4
-/*
5
-Copyright 2013 The Go Authors
6
-
7
-Licensed under the Apache License, Version 2.0 (the "License");
8
-you may not use this file except in compliance with the License.
9
-You may obtain a copy of the License at
10
-
11
- http://www.apache.org/licenses/LICENSE-2.0
12
-
13
-Unless required by applicable law or agreed to in writing, software
14
-distributed under the License is distributed on an "AS IS" BASIS,
15
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
-See the License for the specific language governing permissions and
17
-limitations under the License.
18
-*/
19
-
20
-package lock
21
-
22
-import (
23
- "fmt"
24
- "io"
25
- "os"
26
- "path/filepath"
27
- "syscall"
28
- "unsafe"
29
-)
30
-
31
-func init() {
32
- lockFn = lockFcntl
33
-}
34
-
35
-func lockFcntl(name string) (io.Closer, error) {
36
- abs, err := filepath.Abs(name)
37
- if err != nil {
38
- return nil, err
39
- }
40
- lockmu.Lock()
41
- if locked[abs] {
42
- lockmu.Unlock()
43
- return nil, fmt.Errorf("file %q already locked", abs)
44
- }
45
- locked[abs] = true
46
- lockmu.Unlock()
47
-
48
- fi, err := os.Stat(name)
49
- if err == nil && fi.Size() > 0 {
50
- return nil, fmt.Errorf("can't Lock file %q: has non-zero size", name)
51
- }
52
-
53
- f, err := os.Create(name)
54
- if err != nil {
55
- return nil, fmt.Errorf("Lock Create of %s (abs: %s) failed: %v", name, abs, err)
56
- }
57
-
58
- // This type matches C's "struct flock" defined in /usr/include/sys/fcntl.h.
59
- // TODO: move this into the standard syscall package.
60
- k := struct {
61
- Start uint64 // sizeof(off_t): 8
62
- Len uint64 // sizeof(off_t): 8
63
- Pid uint32 // sizeof(pid_t): 4
64
- Type uint16 // sizeof(short): 2
65
- Whence uint16 // sizeof(short): 2
66
- }{
67
- Type: syscall.F_WRLCK,
68
- Whence: uint16(os.SEEK_SET),
69
- Start: 0,
70
- Len: 0, // 0 means to lock the entire file.
71
- Pid: uint32(os.Getpid()),
72
- }
73
-
74
- _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_SETLK), uintptr(unsafe.Pointer(&k)))
75
- if errno != 0 {
76
- f.Close()
77
- return nil, errno
78
- }
79
- return &unlocker{f, abs}, nil
80
-}
Godeps/_workspace/src/github.com/camlistore/lock/lock_freebsd.go
deleted
-79
@@ -1,79 +0,0 @@
1
-/*
2
-Copyright 2013 The Go Authors
3
-
4
-Licensed under the Apache License, Version 2.0 (the "License");
5
-you may not use this file except in compliance with the License.
6
-You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
-Unless required by applicable law or agreed to in writing, software
11
-distributed under the License is distributed on an "AS IS" BASIS,
12
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
-See the License for the specific language governing permissions and
14
-limitations under the License.
15
-*/
16
-
17
-package lock
18
-
19
-import (
20
- "fmt"
21
- "io"
22
- "os"
23
- "path/filepath"
24
- "syscall"
25
- "unsafe"
26
-)
27
-
28
-func init() {
29
- lockFn = lockFcntl
30
-}
31
-
32
-func lockFcntl(name string) (io.Closer, error) {
33
- abs, err := filepath.Abs(name)
34
- if err != nil {
35
- return nil, err
36
- }
37
- lockmu.Lock()
38
- if locked[abs] {
39
- lockmu.Unlock()
40
- return nil, fmt.Errorf("file %q already locked", abs)
41
- }
42
- locked[abs] = true
43
- lockmu.Unlock()
44
-
45
- fi, err := os.Stat(name)
46
- if err == nil && fi.Size() > 0 {
47
- return nil, fmt.Errorf("can't Lock file %q: has non-zero size", name)
48
- }
49
-
50
- f, err := os.Create(name)
51
- if err != nil {
52
- return nil, err
53
- }
54
-
55
- // This type matches C's "struct flock" defined in /usr/include/fcntl.h.
56
- // TODO: move this into the standard syscall package.
57
- k := struct {
58
- Start int64 /* off_t starting offset */
59
- Len int64 /* off_t len = 0 means until end of file */
60
- Pid int32 /* pid_t lock owner */
61
- Type int16 /* short lock type: read/write, etc. */
62
- Whence int16 /* short type of l_start */
63
- Sysid int32 /* int remote system id or zero for local */
64
- }{
65
- Start: 0,
66
- Len: 0, // 0 means to lock the entire file.
67
- Pid: int32(os.Getpid()),
68
- Type: syscall.F_WRLCK,
69
- Whence: int16(os.SEEK_SET),
70
- Sysid: 0,
71
- }
72
-
73
- _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_SETLK), uintptr(unsafe.Pointer(&k)))
74
- if errno != 0 {
75
- f.Close()
76
- return nil, errno
77
- }
78
- return &unlocker{f, abs}, nil
79
-}
Godeps/_workspace/src/github.com/camlistore/lock/lock_linux_amd64.go
deleted
-80
@@ -1,80 +0,0 @@
1
-// +build linux,amd64
2
-// +build !appengine
3
-
4
-/*
5
-Copyright 2013 The Go Authors
6
-
7
-Licensed under the Apache License, Version 2.0 (the "License");
8
-you may not use this file except in compliance with the License.
9
-You may obtain a copy of the License at
10
-
11
- http://www.apache.org/licenses/LICENSE-2.0
12
-
13
-Unless required by applicable law or agreed to in writing, software
14
-distributed under the License is distributed on an "AS IS" BASIS,
15
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
-See the License for the specific language governing permissions and
17
-limitations under the License.
18
-*/
19
-
20
-package lock
21
-
22
-import (
23
- "fmt"
24
- "io"
25
- "os"
26
- "path/filepath"
27
- "syscall"
28
- "unsafe"
29
-)
30
-
31
-func init() {
32
- lockFn = lockFcntl
33
-}
34
-
35
-func lockFcntl(name string) (io.Closer, error) {
36
- abs, err := filepath.Abs(name)
37
- if err != nil {
38
- return nil, err
39
- }
40
- lockmu.Lock()
41
- if locked[abs] {
42
- lockmu.Unlock()
43
- return nil, fmt.Errorf("file %q already locked", abs)
44
- }
45
- locked[abs] = true
46
- lockmu.Unlock()
47
-
48
- fi, err := os.Stat(name)
49
- if err == nil && fi.Size() > 0 {
50
- return nil, fmt.Errorf("can't Lock file %q: has non-zero size", name)
51
- }
52
-
53
- f, err := os.Create(name)
54
- if err != nil {
55
- return nil, err
56
- }
57
-
58
- // This type matches C's "struct flock" defined in /usr/include/bits/fcntl.h.
59
- // TODO: move this into the standard syscall package.
60
- k := struct {
61
- Type uint32
62
- Whence uint32
63
- Start uint64
64
- Len uint64
65
- Pid uint32
66
- }{
67
- Type: syscall.F_WRLCK,
68
- Whence: uint32(os.SEEK_SET),
69
- Start: 0,
70
- Len: 0, // 0 means to lock the entire file.
71
- Pid: uint32(os.Getpid()),
72
- }
73
-
74
- _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_SETLK), uintptr(unsafe.Pointer(&k)))
75
- if errno != 0 {
76
- f.Close()
77
- return nil, errno
78
- }
79
- return &unlocker{f, abs}, nil
80
-}
Godeps/_workspace/src/github.com/camlistore/lock/lock_linux_arm.go
deleted
-81
@@ -1,81 +0,0 @@
1
-// +build linux,arm
2
-// +build !appengine
3
-
4
-/*
5
-Copyright 2013 The Go Authors
6
-
7
-Licensed under the Apache License, Version 2.0 (the "License");
8
-you may not use this file except in compliance with the License.
9
-You may obtain a copy of the License at
10
-
11
- http://www.apache.org/licenses/LICENSE-2.0
12
-
13
-Unless required by applicable law or agreed to in writing, software
14
-distributed under the License is distributed on an "AS IS" BASIS,
15
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
-See the License for the specific language governing permissions and
17
-limitations under the License.
18
-*/
19
-
20
-package lock
21
-
22
-import (
23
- "fmt"
24
- "io"
25
- "os"
26
- "path/filepath"
27
- "syscall"
28
- "unsafe"
29
-)
30
-
31
-func init() {
32
- lockFn = lockFcntl
33
-}
34
-
35
-func lockFcntl(name string) (io.Closer, error) {
36
- abs, err := filepath.Abs(name)
37
- if err != nil {
38
- return nil, err
39
- }
40
- lockmu.Lock()
41
- if locked[abs] {
42
- lockmu.Unlock()
43
- return nil, fmt.Errorf("file %q already locked", abs)
44
- }
45
- locked[abs] = true
46
- lockmu.Unlock()
47
-
48
- fi, err := os.Stat(name)
49
- if err == nil && fi.Size() > 0 {
50
- return nil, fmt.Errorf("can't Lock file %q: has non-zero size", name)
51
- }
52
-
53
- f, err := os.Create(name)
54
- if err != nil {
55
- return nil, err
56
- }
57
-
58
- // This type matches C's "struct flock" defined in /usr/include/bits/fcntl.h.
59
- // TODO: move this into the standard syscall package.
60
- k := struct {
61
- Type uint16
62
- Whence uint16
63
- Start uint32
64
- Len uint32
65
- Pid uint32
66
- }{
67
- Type: syscall.F_WRLCK,
68
- Whence: uint16(os.SEEK_SET),
69
- Start: 0,
70
- Len: 0, // 0 means to lock the entire file.
71
- Pid: uint32(os.Getpid()),
72
- }
73
-
74
- const F_SETLK = 6 // actual value. syscall package is wrong: golang.org/issue/7059
75
- _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(F_SETLK), uintptr(unsafe.Pointer(&k)))
76
- if errno != 0 {
77
- f.Close()
78
- return nil, errno
79
- }
80
- return &unlocker{f, abs}, nil
81
-}
Godeps/_workspace/src/github.com/camlistore/lock/lock_plan9.go
deleted
-55
@@ -1,55 +0,0 @@
1
-/*
2
-Copyright 2013 The Go Authors
3
-
4
-Licensed under the Apache License, Version 2.0 (the "License");
5
-you may not use this file except in compliance with the License.
6
-You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
-Unless required by applicable law or agreed to in writing, software
11
-distributed under the License is distributed on an "AS IS" BASIS,
12
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
-See the License for the specific language governing permissions and
14
-limitations under the License.
15
-*/
16
-
17
-package lock
18
-
19
-import (
20
- "fmt"
21
- "io"
22
- "os"
23
- "path/filepath"
24
-)
25
-
26
-func init() {
27
- lockFn = lockPlan9
28
-}
29
-
30
-func lockPlan9(name string) (io.Closer, error) {
31
- var f *os.File
32
- abs, err := filepath.Abs(name)
33
- if err != nil {
34
- return nil, err
35
- }
36
- lockmu.Lock()
37
- if locked[abs] {
38
- lockmu.Unlock()
39
- return nil, fmt.Errorf("file %q already locked", abs)
40
- }
41
- locked[abs] = true
42
- lockmu.Unlock()
43
-
44
- fi, err := os.Stat(name)
45
- if err == nil && fi.Size() > 0 {
46
- return nil, fmt.Errorf("can't Lock file %q: has non-zero size", name)
47
- }
48
-
49
- f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE, os.ModeExclusive|0644)
50
- if err != nil {
51
- return nil, fmt.Errorf("Lock Create of %s (abs: %s) failed: %v", name, abs, err)
52
- }
53
-
54
- return &unlocker{f, abs}, nil
55
-}
Godeps/_workspace/src/github.com/camlistore/lock/lock_sigzero.go
deleted
-26
@@ -1,26 +0,0 @@
1
-// +build !appengine
2
-// +build linux darwin freebsd openbsd netbsd dragonfly
3
-
4
-/*
5
-Copyright 2013 The Go Authors
6
-
7
-Licensed under the Apache License, Version 2.0 (the "License");
8
-you may not use this file except in compliance with the License.
9
-You may obtain a copy of the License at
10
-
11
- http://www.apache.org/licenses/LICENSE-2.0
12
-
13
-Unless required by applicable law or agreed to in writing, software
14
-distributed under the License is distributed on an "AS IS" BASIS,
15
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
-See the License for the specific language governing permissions and
17
-limitations under the License.
18
-*/
19
-
20
-package lock
21
-
22
-import "syscall"
23
-
24
-func init() {
25
- signalZero = syscall.Signal(0)
26
-}
Godeps/_workspace/src/github.com/camlistore/lock/lock_test.go
deleted
-131
@@ -1,131 +0,0 @@
1
-/*
2
-Copyright 2013 The Go Authors
3
-
4
-Licensed under the Apache License, Version 2.0 (the "License");
5
-you may not use this file except in compliance with the License.
6
-You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
-Unless required by applicable law or agreed to in writing, software
11
-distributed under the License is distributed on an "AS IS" BASIS,
12
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
-See the License for the specific language governing permissions and
14
-limitations under the License.
15
-*/
16
-
17
-package lock
18
-
19
-import (
20
- "fmt"
21
- "io/ioutil"
22
- "log"
23
- "os"
24
- "os/exec"
25
- "path/filepath"
26
- "strconv"
27
- "testing"
28
-)
29
-
30
-func TestLock(t *testing.T) {
31
- testLock(t, false)
32
-}
33
-
34
-func TestLockPortable(t *testing.T) {
35
- testLock(t, true)
36
-}
37
-
38
-func TestLockInChild(t *testing.T) {
39
- f := os.Getenv("TEST_LOCK_FILE")
40
- if f == "" {
41
- // not child
42
- return
43
- }
44
- lock := Lock
45
- if v, _ := strconv.ParseBool(os.Getenv("TEST_LOCK_PORTABLE")); v {
46
- lock = lockPortable
47
- }
48
-
49
- lk, err := lock(f)
50
- if err != nil {
51
- log.Fatalf("Lock failed: %v", err)
52
- }
53
-
54
- if v, _ := strconv.ParseBool(os.Getenv("TEST_LOCK_CRASH")); v {
55
- // Simulate a crash, or at least not unlocking the
56
- // lock. We still exit 0 just to simplify the parent
57
- // process exec code.
58
- os.Exit(0)
59
- }
60
- lk.Close()
61
-}
62
-
63
-func testLock(t *testing.T, portable bool) {
64
- lock := Lock
65
- if portable {
66
- lock = lockPortable
67
- }
68
-
69
- td, err := ioutil.TempDir("", "")
70
- if err != nil {
71
- t.Fatal(err)
72
- }
73
- defer os.RemoveAll(td)
74
-
75
- path := filepath.Join(td, "foo.lock")
76
-
77
- childLock := func(crash bool) error {
78
- cmd := exec.Command(os.Args[0], "-test.run=LockInChild$")
79
- cmd.Env = []string{"TEST_LOCK_FILE=" + path}
80
- if portable {
81
- cmd.Env = append(cmd.Env, "TEST_LOCK_PORTABLE=1")
82
- }
83
- if crash {
84
- cmd.Env = append(cmd.Env, "TEST_LOCK_CRASH=1")
85
- }
86
- out, err := cmd.CombinedOutput()
87
- t.Logf("Child output: %q (err %v)", out, err)
88
- if err != nil {
89
- return fmt.Errorf("Child Process lock of %s failed: %v %s", path, err, out)
90
- }
91
- return nil
92
- }
93
-
94
- t.Logf("Locking in crashing child...")
95
- if err := childLock(true); err != nil {
96
- t.Fatalf("first lock in child process: %v", err)
97
- }
98
-
99
- t.Logf("Locking+unlocking in child...")
100
- if err := childLock(false); err != nil {
101
- t.Fatalf("lock in child process after crashing child: %v", err)
102
- }
103
-
104
- t.Logf("Locking in parent...")
105
- lk1, err := lock(path)
106
- if err != nil {
107
- t.Fatal(err)
108
- }
109
-
110
- t.Logf("Again in parent...")
111
- _, err = lock(path)
112
- if err == nil {
113
- t.Fatal("expected second lock to fail")
114
- }
115
-
116
- t.Logf("Locking in child...")
117
- if childLock(false) == nil {
118
- t.Fatalf("expected lock in child process to fail")
119
- }
120
-
121
- t.Logf("Unlocking lock in parent")
122
- if err := lk1.Close(); err != nil {
123
- t.Fatal(err)
124
- }
125
-
126
- lk3, err := lock(path)
127
- if err != nil {
128
- t.Fatal(err)
129
- }
130
- lk3.Close()
131
-}
package.json
+6
@@ -159,6 +159,12 @@
159
"hash": "QmVYxfoJQiZijTgPNHCHgHELvQpbsJNTg6Crmc3dQkj3yy",
160
"name": "golang-lru",
161
"version": "0.0.0"
162
+ },
163
+ {
164
+ "author": "whyrusleeping",
165
+ "hash": "QmWi28zbQG6B1xfaaWx5cYoLn3kBFU6pQ6GWQNRV5P6dNe",
166
+ "name": "lock",
167
+ "version": "0.0.0"
168
}
169
],
170
"gxVersion": "0.4.0",
repo/fsrepo/lock/lock.go
+3
-4
@@ -8,7 +8,7 @@ import (
8
"strings"
9
"syscall"
10
11
- lock "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
11
+ lock "gx/ipfs/QmWi28zbQG6B1xfaaWx5cYoLn3kBFU6pQ6GWQNRV5P6dNe/lock"
12
logging "gx/ipfs/QmYtB7Qge8cJpXc4irsEp8zRqfnZMBeB7aTrMEkPk67DRv/go-log"
13
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
14
)
@@ -25,8 +25,7 @@ func errPerm(path string) error {
25
}
26
27
func Lock(confdir string) (io.Closer, error) {
28
- c, err := lock.Lock(path.Join(confdir, LockFile))
29
- return c, err
28
+ return lock.Lock(path.Join(confdir, LockFile))
29
}
30
31
func Locked(confdir string) (bool, error) {
@@ -41,7 +40,7 @@ func Locked(confdir string) (bool, error) {
40
log.Debugf("Someone else has the lock: %s", path.Join(confdir, LockFile))
41
return true, nil
42
}
44
- if strings.Contains(err.Error(), "can't Lock file") {
43
+ if strings.Contains(err.Error(), "resource temporarily unavailable") {
44
log.Debugf("Can't lock file: %s.\n reason: %s", path.Join(confdir, LockFile), err.Error())
45
return true, nil
46
}