-
Notifications
You must be signed in to change notification settings - Fork 767
Expand file tree
/
Copy pathlock.go
More file actions
127 lines (104 loc) · 3.67 KB
/
lock.go
File metadata and controls
127 lines (104 loc) · 3.67 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Portions from internal go
//
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// https://cs.opensource.google/go/go/+/refs/tags/go1.24.3:LICENSE
// https://cs.opensource.google/go/go/+/refs/tags/go1.24.3:src/cmd/go/internal/lockedfile/internal/filelock/filelock.go
package filesystem
import (
"errors"
"os"
"runtime"
)
// Lock places an advisory write lock on the file, blocking until it can be locked.
//
// If Lock returns nil, no other process will be able to place a read or write lock on the file until
// this process exits, closes f, or calls Unlock on it.
func Lock(path string) (file *os.File, err error) {
return commonlock(path, writeLock)
}
// ReadOnlyLock places an advisory read lock on the file, blocking until it can be locked.
//
// If ReadOnlyLock returns nil, no other process will be able to place a write lock on
// the file until this process exits, closes f, or calls Unlock on it.
func ReadOnlyLock(path string) (file *os.File, err error) {
return commonlock(path, readLock)
}
func commonlock(path string, mode lockType) (file *os.File, err error) {
defer func() {
if err != nil {
err = errors.Join(ErrLockFail, err)
if file != nil {
err = errors.Join(err, file.Close())
}
}
}()
if runtime.GOOS == "windows" {
// LockFileEx does not work on directories, so check what we have first.
// If that is a dir, swap out the path for a sidecar file instead (not inside the directory).
// Note that this cannot be done in platform specific implementation without moving all the fd Open and Close
// logic over there, which is undesirable.
if sl, err := os.Stat(path); err == nil && sl.IsDir() {
path = path + ".nerdctl.lock"
}
}
file, err = os.Open(path)
if errors.Is(err, os.ErrNotExist) {
file, err = os.OpenFile(path, os.O_RDONLY|os.O_CREATE, privateFilePermission)
}
if err != nil {
return nil, err
}
if err = platformSpecificLock(file, mode); err != nil {
return nil, errors.Join(err, file.Close())
}
return file, nil
}
// Unlock removes an advisory lock placed on f by this process.
func Unlock(lock *os.File) error {
if lock == nil {
return ErrLockIsNil
}
if err := errors.Join(platformSpecificUnlock(lock), lock.Close()); err != nil {
return errors.Join(ErrUnlockFail, err)
}
return nil
}
// WithLock executes the provided function after placing a write lock on `path`.
// The lock is released once the function has been run, regardless of outcome.
func WithLock(path string, function func() error) (err error) {
file, err := Lock(path)
if err != nil {
return err
}
defer func() {
err = errors.Join(Unlock(file), err)
}()
return function()
}
// WithReadOnlyLock executes the provided function after placing a read lock on `path`.
// The lock is released once the function has been run, regardless of outcome.
func WithReadOnlyLock(path string, function func() error) (err error) {
file, err := ReadOnlyLock(path)
if err != nil {
return err
}
defer func() {
err = errors.Join(Unlock(file), err)
}()
return function()
}