host-local: Update host-local IPAM to support Windows
This commit is contained in:
parent
fbced0cccb
commit
47668f6d64
4
Godeps/Godeps.json
generated
4
Godeps/Godeps.json
generated
@ -6,6 +6,10 @@
|
|||||||
"./..."
|
"./..."
|
||||||
],
|
],
|
||||||
"Deps": [
|
"Deps": [
|
||||||
|
{
|
||||||
|
"ImportPath": "github.com/alexflint/go-filemutex",
|
||||||
|
"Rev": "72bdc8eae2aef913234599b837f5dda445ca9bd9"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/containernetworking/cni/libcni",
|
"ImportPath": "github.com/containernetworking/cni/libcni",
|
||||||
"Comment": "v0.6.0-rc1",
|
"Comment": "v0.6.0-rc1",
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containernetworking/plugins/plugins/ipam/host-local/backend"
|
"github.com/containernetworking/plugins/plugins/ipam/host-local/backend"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
const lastIPFilePrefix = "last_reserved_ip."
|
const lastIPFilePrefix = "last_reserved_ip."
|
||||||
@ -55,7 +56,8 @@ func New(network, dataDir string) (*Store, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Reserve(id string, ip net.IP, rangeID string) (bool, error) {
|
func (s *Store) Reserve(id string, ip net.IP, rangeID string) (bool, error) {
|
||||||
fname := filepath.Join(s.dataDir, ip.String())
|
fname := GetEscapedPath(s.dataDir, ip.String())
|
||||||
|
|
||||||
f, err := os.OpenFile(fname, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0644)
|
f, err := os.OpenFile(fname, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0644)
|
||||||
if os.IsExist(err) {
|
if os.IsExist(err) {
|
||||||
return false, nil
|
return false, nil
|
||||||
@ -73,7 +75,7 @@ func (s *Store) Reserve(id string, ip net.IP, rangeID string) (bool, error) {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
// store the reserved ip in lastIPFile
|
// store the reserved ip in lastIPFile
|
||||||
ipfile := filepath.Join(s.dataDir, lastIPFilePrefix+rangeID)
|
ipfile := GetEscapedPath(s.dataDir, lastIPFilePrefix+rangeID)
|
||||||
err = ioutil.WriteFile(ipfile, []byte(ip.String()), 0644)
|
err = ioutil.WriteFile(ipfile, []byte(ip.String()), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@ -83,7 +85,7 @@ func (s *Store) Reserve(id string, ip net.IP, rangeID string) (bool, error) {
|
|||||||
|
|
||||||
// LastReservedIP returns the last reserved IP if exists
|
// LastReservedIP returns the last reserved IP if exists
|
||||||
func (s *Store) LastReservedIP(rangeID string) (net.IP, error) {
|
func (s *Store) LastReservedIP(rangeID string) (net.IP, error) {
|
||||||
ipfile := filepath.Join(s.dataDir, lastIPFilePrefix+rangeID)
|
ipfile := GetEscapedPath(s.dataDir, lastIPFilePrefix+rangeID)
|
||||||
data, err := ioutil.ReadFile(ipfile)
|
data, err := ioutil.ReadFile(ipfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -92,7 +94,7 @@ func (s *Store) LastReservedIP(rangeID string) (net.IP, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Release(ip net.IP) error {
|
func (s *Store) Release(ip net.IP) error {
|
||||||
return os.Remove(filepath.Join(s.dataDir, ip.String()))
|
return os.Remove(GetEscapedPath(s.dataDir, ip.String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// N.B. This function eats errors to be tolerant and
|
// N.B. This function eats errors to be tolerant and
|
||||||
@ -115,3 +117,10 @@ func (s *Store) ReleaseByID(id string) error {
|
|||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetEscapedPath(dataDir string, fname string) string {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
fname = strings.Replace(fname, ":", "_", -1)
|
||||||
|
}
|
||||||
|
return filepath.Join(dataDir, fname)
|
||||||
|
}
|
||||||
|
27
plugins/ipam/host-local/backend/disk/disk_suite_test.go
Normal file
27
plugins/ipam/host-local/backend/disk/disk_suite_test.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// Copyright 2016 CNI 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.
|
||||||
|
|
||||||
|
package disk
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLock(t *testing.T) {
|
||||||
|
RegisterFailHandler(Fail)
|
||||||
|
RunSpecs(t, "Disk Suite")
|
||||||
|
}
|
@ -1,4 +1,3 @@
|
|||||||
// +build !windows
|
|
||||||
// Copyright 2015 CNI authors
|
// Copyright 2015 CNI authors
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -16,18 +15,28 @@
|
|||||||
package disk
|
package disk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/alexflint/go-filemutex"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FileLock wraps os.File to be used as a lock using flock
|
// FileLock wraps os.File to be used as a lock using flock
|
||||||
type FileLock struct {
|
type FileLock struct {
|
||||||
f *os.File
|
f *filemutex.FileMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFileLock opens file/dir at path and returns unlocked FileLock object
|
// NewFileLock opens file/dir at path and returns unlocked FileLock object
|
||||||
func NewFileLock(path string) (*FileLock, error) {
|
func NewFileLock(lockPath string) (*FileLock, error) {
|
||||||
f, err := os.Open(path)
|
fi, err := os.Stat(lockPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.IsDir() {
|
||||||
|
lockPath = path.Join(lockPath, "lock")
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := filemutex.New(lockPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -35,17 +44,16 @@ func NewFileLock(path string) (*FileLock, error) {
|
|||||||
return &FileLock{f}, nil
|
return &FileLock{f}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes underlying file
|
|
||||||
func (l *FileLock) Close() error {
|
func (l *FileLock) Close() error {
|
||||||
return l.f.Close()
|
return l.f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock acquires an exclusive lock
|
// Lock acquires an exclusive lock
|
||||||
func (l *FileLock) Lock() error {
|
func (l *FileLock) Lock() error {
|
||||||
return syscall.Flock(int(l.f.Fd()), syscall.LOCK_EX)
|
return l.f.Lock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock releases the lock
|
// Unlock releases the lock
|
||||||
func (l *FileLock) Unlock() error {
|
func (l *FileLock) Unlock() error {
|
||||||
return syscall.Flock(int(l.f.Fd()), syscall.LOCK_UN)
|
return l.f.Unlock()
|
||||||
}
|
}
|
||||||
|
63
plugins/ipam/host-local/backend/disk/lock_test.go
Normal file
63
plugins/ipam/host-local/backend/disk/lock_test.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
// Copyright 2016 CNI 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.
|
||||||
|
|
||||||
|
package disk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = Describe("Lock Operations", func() {
|
||||||
|
It("locks a file path", func() {
|
||||||
|
dir, err := ioutil.TempDir("", "")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
// create a dummy file to lock
|
||||||
|
path := filepath.Join(dir, "x")
|
||||||
|
f, err := os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0666)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
err = f.Close()
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
// now use it to lock
|
||||||
|
m, err := NewFileLock(path)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
err = m.Lock()
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
err = m.Unlock()
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("locks a folder path", func() {
|
||||||
|
dir, err := ioutil.TempDir("", "")
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
// use the folder to lock
|
||||||
|
m, err := NewFileLock(dir)
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
err = m.Lock()
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
err = m.Unlock()
|
||||||
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
})
|
||||||
|
})
|
@ -28,6 +28,7 @@ import (
|
|||||||
"github.com/containernetworking/cni/pkg/types/current"
|
"github.com/containernetworking/cni/pkg/types/current"
|
||||||
"github.com/containernetworking/plugins/pkg/testutils"
|
"github.com/containernetworking/plugins/pkg/testutils"
|
||||||
|
|
||||||
|
"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/disk"
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
@ -37,7 +38,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
const ifname string = "eth0"
|
const ifname string = "eth0"
|
||||||
const nspath string = "/some/where"
|
const nspath string = "/some/where"
|
||||||
|
|
||||||
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
tmpDir, err := getTmpDir()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
@ -112,7 +113,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(string(contents)).To(Equal("dummy"))
|
Expect(string(contents)).To(Equal("dummy"))
|
||||||
|
|
||||||
ipFilePath2 := filepath.Join(tmpDir, "mynet", "2001:db8:1::2")
|
ipFilePath2 := filepath.Join(tmpDir, disk.GetEscapedPath("mynet", "2001:db8:1::2"))
|
||||||
contents, err = ioutil.ReadFile(ipFilePath2)
|
contents, err = ioutil.ReadFile(ipFilePath2)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(string(contents)).To(Equal("dummy"))
|
Expect(string(contents)).To(Equal("dummy"))
|
||||||
@ -142,7 +143,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
const ifname string = "eth0"
|
const ifname string = "eth0"
|
||||||
const nspath string = "/some/where"
|
const nspath string = "/some/where"
|
||||||
|
|
||||||
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
tmpDir, err := getTmpDir()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
@ -176,7 +177,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
const ifname string = "eth0"
|
const ifname string = "eth0"
|
||||||
const nspath string = "/some/where"
|
const nspath string = "/some/where"
|
||||||
|
|
||||||
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
tmpDir, err := getTmpDir()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
@ -245,7 +246,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
const ifname string = "eth0"
|
const ifname string = "eth0"
|
||||||
const nspath string = "/some/where"
|
const nspath string = "/some/where"
|
||||||
|
|
||||||
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
tmpDir, err := getTmpDir()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
@ -296,7 +297,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
const ifname string = "eth0"
|
const ifname string = "eth0"
|
||||||
const nspath string = "/some/where"
|
const nspath string = "/some/where"
|
||||||
|
|
||||||
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
tmpDir, err := getTmpDir()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
@ -331,7 +332,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
const ifname string = "eth0"
|
const ifname string = "eth0"
|
||||||
const nspath string = "/some/where"
|
const nspath string = "/some/where"
|
||||||
|
|
||||||
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
tmpDir, err := getTmpDir()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
@ -376,7 +377,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
const ifname string = "eth0"
|
const ifname string = "eth0"
|
||||||
const nspath string = "/some/where"
|
const nspath string = "/some/where"
|
||||||
|
|
||||||
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
tmpDir, err := getTmpDir()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
@ -426,7 +427,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
const ifname string = "eth0"
|
const ifname string = "eth0"
|
||||||
const nspath string = "/some/where"
|
const nspath string = "/some/where"
|
||||||
|
|
||||||
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
tmpDir, err := getTmpDir()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
@ -476,7 +477,7 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
const ifname string = "eth0"
|
const ifname string = "eth0"
|
||||||
const nspath string = "/some/where"
|
const nspath string = "/some/where"
|
||||||
|
|
||||||
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
tmpDir, err := getTmpDir()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
defer os.RemoveAll(tmpDir)
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
@ -517,6 +518,15 @@ var _ = Describe("host-local Operations", func() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
func getTmpDir() (string, error) {
|
||||||
|
tmpDir, err := ioutil.TempDir("", "host_local_artifacts")
|
||||||
|
if err == nil {
|
||||||
|
tmpDir = filepath.ToSlash(tmpDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tmpDir, err
|
||||||
|
}
|
||||||
|
|
||||||
func mustCIDR(s string) net.IPNet {
|
func mustCIDR(s string) net.IPNet {
|
||||||
ip, n, err := net.ParseCIDR(s)
|
ip, n, err := net.ParseCIDR(s)
|
||||||
n.IP = ip
|
n.IP = ip
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
plugins/host-device
|
plugins/host-device
|
||||||
plugins/ipam/dhcp
|
plugins/ipam/dhcp
|
||||||
plugins/ipam/host-local
|
|
||||||
plugins/main/bridge
|
plugins/main/bridge
|
||||||
plugins/main/ipvlan
|
plugins/main/ipvlan
|
||||||
plugins/main/loopback
|
plugins/main/loopback
|
||||||
|
21
vendor/github.com/alexflint/go-filemutex/LICENSE
generated
vendored
Normal file
21
vendor/github.com/alexflint/go-filemutex/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
The MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2010-2017 Alex Flint.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
31
vendor/github.com/alexflint/go-filemutex/README.md
generated
vendored
Normal file
31
vendor/github.com/alexflint/go-filemutex/README.md
generated
vendored
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
# FileMutex
|
||||||
|
|
||||||
|
FileMutex is similar to `sync.RWMutex`, but also synchronizes across processes.
|
||||||
|
On Linux, OSX, and other POSIX systems it uses the flock system call. On windows
|
||||||
|
it uses the LockFileEx and UnlockFileEx system calls.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"github.com/alexflint/go-filemutex"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m, err := filemutex.New("/tmp/foo.lock")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Directory did not exist or file could not created")
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Lock() // Will block until lock can be acquired
|
||||||
|
|
||||||
|
// Code here is protected by the mutex
|
||||||
|
|
||||||
|
m.Unlock()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Installation
|
||||||
|
|
||||||
|
go get github.com/alexflint/go-filemutex
|
||||||
|
|
||||||
|
Forked from https://github.com/golang/build/tree/master/cmd/builder/filemutex_*.go
|
67
vendor/github.com/alexflint/go-filemutex/filemutex_flock.go
generated
vendored
Normal file
67
vendor/github.com/alexflint/go-filemutex/filemutex_flock.go
generated
vendored
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
// Copyright 2013 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.
|
||||||
|
|
||||||
|
// +build darwin dragonfly freebsd linux netbsd openbsd
|
||||||
|
|
||||||
|
package filemutex
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
mkdirPerm = 0750
|
||||||
|
)
|
||||||
|
|
||||||
|
// FileMutex is similar to sync.RWMutex, but also synchronizes across processes.
|
||||||
|
// This implementation is based on flock syscall.
|
||||||
|
type FileMutex struct {
|
||||||
|
fd int
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(filename string) (*FileMutex, error) {
|
||||||
|
fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY, mkdirPerm)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &FileMutex{fd: fd}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FileMutex) Lock() error {
|
||||||
|
if err := syscall.Flock(m.fd, syscall.LOCK_EX); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FileMutex) Unlock() error {
|
||||||
|
if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FileMutex) RLock() error {
|
||||||
|
if err := syscall.Flock(m.fd, syscall.LOCK_SH); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FileMutex) RUnlock() error {
|
||||||
|
if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close does an Unlock() combined with closing and unlinking the associated
|
||||||
|
// lock file. You should create a New() FileMutex for every Lock() attempt if
|
||||||
|
// using Close().
|
||||||
|
func (m *FileMutex) Close() error {
|
||||||
|
if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return syscall.Close(m.fd)
|
||||||
|
}
|
102
vendor/github.com/alexflint/go-filemutex/filemutex_windows.go
generated
vendored
Normal file
102
vendor/github.com/alexflint/go-filemutex/filemutex_windows.go
generated
vendored
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
// Copyright 2013 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.
|
||||||
|
|
||||||
|
package filemutex
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||||
|
procLockFileEx = modkernel32.NewProc("LockFileEx")
|
||||||
|
procUnlockFileEx = modkernel32.NewProc("UnlockFileEx")
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
lockfileExclusiveLock = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
func lockFileEx(h syscall.Handle, flags, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
|
||||||
|
r1, _, e1 := syscall.Syscall6(procLockFileEx.Addr(), 6, uintptr(h), uintptr(flags), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))
|
||||||
|
if r1 == 0 {
|
||||||
|
if e1 != 0 {
|
||||||
|
err = error(e1)
|
||||||
|
} else {
|
||||||
|
err = syscall.EINVAL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func unlockFileEx(h syscall.Handle, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
|
||||||
|
r1, _, e1 := syscall.Syscall6(procUnlockFileEx.Addr(), 5, uintptr(h), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)), 0)
|
||||||
|
if r1 == 0 {
|
||||||
|
if e1 != 0 {
|
||||||
|
err = error(e1)
|
||||||
|
} else {
|
||||||
|
err = syscall.EINVAL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// FileMutex is similar to sync.RWMutex, but also synchronizes across processes.
|
||||||
|
// This implementation is based on flock syscall.
|
||||||
|
type FileMutex struct {
|
||||||
|
fd syscall.Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(filename string) (*FileMutex, error) {
|
||||||
|
fd, err := syscall.CreateFile(&(syscall.StringToUTF16(filename)[0]), syscall.GENERIC_READ|syscall.GENERIC_WRITE,
|
||||||
|
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE, nil, syscall.OPEN_ALWAYS, syscall.FILE_ATTRIBUTE_NORMAL, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &FileMutex{fd: fd}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FileMutex) Lock() error {
|
||||||
|
var ol syscall.Overlapped
|
||||||
|
if err := lockFileEx(m.fd, lockfileExclusiveLock, 0, 1, 0, &ol); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FileMutex) Unlock() error {
|
||||||
|
var ol syscall.Overlapped
|
||||||
|
if err := unlockFileEx(m.fd, 0, 1, 0, &ol); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FileMutex) RLock() error {
|
||||||
|
var ol syscall.Overlapped
|
||||||
|
if err := lockFileEx(m.fd, 0, 0, 1, 0, &ol); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FileMutex) RUnlock() error {
|
||||||
|
var ol syscall.Overlapped
|
||||||
|
if err := unlockFileEx(m.fd, 0, 1, 0, &ol); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close does an Unlock() combined with closing and unlinking the associated
|
||||||
|
// lock file. You should create a New() FileMutex for every Lock() attempt if
|
||||||
|
// using Close().
|
||||||
|
func (m *FileMutex) Close() error {
|
||||||
|
var ol syscall.Overlapped
|
||||||
|
if err := unlockFileEx(m.fd, 0, 1, 0, &ol); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return syscall.Close(m.fd)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user