From c7a17e4e4a1b72e4bc38b8b52cac8558aff4a4b1 Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 22 Oct 2014 09:37:15 +0800 Subject: [PATCH] update file lock --- filelock/file_lock_darwin_amd64.go | 51 ------------------- filelock/file_lock_generic.go | 6 +-- filelock/file_lock_linux_amd64.go | 51 ------------------- filelock/file_lock_openbsd_amd64.go | 51 ------------------- ...file_lock_freebsd.go => file_lock_unix.go} | 29 ++++------- 5 files changed, 10 insertions(+), 178 deletions(-) delete mode 100644 filelock/file_lock_darwin_amd64.go delete mode 100644 filelock/file_lock_linux_amd64.go delete mode 100644 filelock/file_lock_openbsd_amd64.go rename filelock/{file_lock_freebsd.go => file_lock_unix.go} (52%) diff --git a/filelock/file_lock_darwin_amd64.go b/filelock/file_lock_darwin_amd64.go deleted file mode 100644 index efc12fa..0000000 --- a/filelock/file_lock_darwin_amd64.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2012 The LevelDB-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 filelock - -import ( - "io" - "os" - "syscall" - "unsafe" -) - -// lockCloser hides all of an os.File's methods, except for Close. -type lockCloser struct { - f *os.File -} - -func (l lockCloser) Close() error { - return l.f.Close() -} - -func Lock(name string) (io.Closer, error) { - f, err := os.Create(name) - if err != nil { - return nil, err - } - - // This type matches C's "struct flock" defined in /usr/include/sys/fcntl.h. - // TODO: move this into the standard syscall package. - k := struct { - Start uint64 // sizeof(off_t): 8 - Len uint64 // sizeof(off_t): 8 - Pid uint32 // sizeof(pid_t): 4 - Type uint16 // sizeof(short): 2 - Whence uint16 // sizeof(short): 2 - }{ - Type: syscall.F_WRLCK, - Whence: uint16(os.SEEK_SET), - Start: 0, - Len: 0, // 0 means to lock the entire file. - Pid: uint32(os.Getpid()), - } - - _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_SETLK), uintptr(unsafe.Pointer(&k))) - if errno != 0 { - f.Close() - return nil, errno - } - return lockCloser{f}, nil -} diff --git a/filelock/file_lock_generic.go b/filelock/file_lock_generic.go index 2415e45..53c292a 100644 --- a/filelock/file_lock_generic.go +++ b/filelock/file_lock_generic.go @@ -2,11 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build !linux,!amd64 -// +build !darwin,!amd64 -// +build !openbsd,!amd64 -// +build !freebsd -// +build !windows +// +build !darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris,!windows package filelock diff --git a/filelock/file_lock_linux_amd64.go b/filelock/file_lock_linux_amd64.go deleted file mode 100644 index 7fd42ef..0000000 --- a/filelock/file_lock_linux_amd64.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2012 The LevelDB-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 filelock - -import ( - "io" - "os" - "syscall" - "unsafe" -) - -// lockCloser hides all of an os.File's methods, except for Close. -type lockCloser struct { - f *os.File -} - -func (l lockCloser) Close() error { - return l.f.Close() -} - -func Lock(name string) (io.Closer, error) { - f, err := os.Create(name) - if err != nil { - return nil, err - } - - // This type matches C's "struct flock" defined in /usr/include/bits/fcntl.h. - // TODO: move this into the standard syscall package. - k := struct { - Type uint32 - Whence uint32 - Start uint64 - Len uint64 - Pid uint32 - }{ - Type: syscall.F_WRLCK, - Whence: uint32(os.SEEK_SET), - Start: 0, - Len: 0, // 0 means to lock the entire file. - Pid: uint32(os.Getpid()), - } - - _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_SETLK), uintptr(unsafe.Pointer(&k))) - if errno != 0 { - f.Close() - return nil, errno - } - return lockCloser{f}, nil -} diff --git a/filelock/file_lock_openbsd_amd64.go b/filelock/file_lock_openbsd_amd64.go deleted file mode 100644 index 0e89f51..0000000 --- a/filelock/file_lock_openbsd_amd64.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2013 The LevelDB-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 filelock - -import ( - "io" - "os" - "syscall" - "unsafe" -) - -// lockCloser hides all of an os.File's methods, except for Close. -type lockCloser struct { - f *os.File -} - -func (l lockCloser) Close() error { - return l.f.Close() -} - -func Lock(name string) (io.Closer, error) { - f, err := os.Create(name) - if err != nil { - return nil, err - } - - // This type matches C's "struct flock" defined in /usr/include/fcntl.h. - // TODO: move this into the standard syscall package. - k := struct { - Start uint64 - Len uint64 - Pid uint32 - Type uint32 - Whence uint32 - }{ - Type: syscall.F_WRLCK, - Whence: uint32(os.SEEK_SET), - Start: 0, - Len: 0, // 0 means to lock the entire file. - Pid: uint32(os.Getpid()), - } - - _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_SETLK), uintptr(unsafe.Pointer(&k))) - if errno != 0 { - f.Close() - return nil, errno - } - return lockCloser{f}, nil -} diff --git a/filelock/file_lock_freebsd.go b/filelock/file_lock_unix.go similarity index 52% rename from filelock/file_lock_freebsd.go rename to filelock/file_lock_unix.go index 87e7630..2dd0f0b 100644 --- a/filelock/file_lock_freebsd.go +++ b/filelock/file_lock_unix.go @@ -2,13 +2,14 @@ // 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 solaris + package filelock import ( "io" "os" "syscall" - "unsafe" ) // lockCloser hides all of an os.File's methods, except for Close. @@ -25,29 +26,17 @@ func Lock(name string) (io.Closer, error) { if err != nil { return nil, err } - - // This type matches C's "struct flock" defined in /usr/include/fcntl.h. - // TODO: move this into the standard syscall package. - k := struct { - Start int64 /* off_t starting offset */ - Len int64 /* off_t len = 0 means until end of file */ - Pid int32 /* pid_t lock owner */ - Type int16 /* short lock type: read/write, etc. */ - Whence int16 /* short type of l_start */ - Sysid int32 /* int remote system id or zero for local */ - }{ + spec := syscall.Flock_t{ + Type: syscall.F_WRLCK, + Whence: int16(os.SEEK_SET), Start: 0, Len: 0, // 0 means to lock the entire file. Pid: int32(os.Getpid()), - Type: syscall.F_WRLCK, - Whence: int16(os.SEEK_SET), - Sysid: 0, + } + if err := syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &spec); err != nil { + f.Close() + return nil, err } - _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_SETLK), uintptr(unsafe.Pointer(&k))) - if errno != 0 { - f.Close() - return nil, errno - } return lockCloser{f}, nil }