2017-01-16 20:05:00 -05:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
/*
|
2017-03-27 15:47:39 -04:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2017-01-16 20:05:00 -05:00
|
|
|
*
|
|
|
|
* 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 lock
|
|
|
|
|
|
|
|
import (
|
2017-03-27 15:47:39 -04:00
|
|
|
"fmt"
|
2017-01-16 20:05:00 -05:00
|
|
|
"os"
|
2017-08-12 22:25:43 -04:00
|
|
|
"path/filepath"
|
2017-01-16 20:05:00 -05:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
|
|
|
|
procLockFileEx = modkernel32.NewProc("LockFileEx")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-06-12 20:40:28 -04:00
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
|
|
|
|
lockFileExclusiveLock = 2
|
|
|
|
lockFileFailImmediately = 1
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
|
|
|
|
errLockViolation syscall.Errno = 0x21
|
|
|
|
)
|
|
|
|
|
2017-06-12 20:40:28 -04:00
|
|
|
// lockedOpenFile is an internal function.
|
|
|
|
func lockedOpenFile(path string, flag int, perm os.FileMode, lockType uint32) (*LockedFile, error) {
|
2018-01-31 16:17:24 -05:00
|
|
|
f, err := Open(path, flag, perm)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-12 20:40:28 -04:00
|
|
|
if err = lockFile(syscall.Handle(f.Fd()), lockType); err != nil {
|
2017-01-16 20:05:00 -05:00
|
|
|
f.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-13 06:01:15 -04:00
|
|
|
st, err := os.Stat(path)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if st.IsDir() {
|
|
|
|
f.Close()
|
|
|
|
return nil, &os.PathError{
|
|
|
|
Op: "open",
|
|
|
|
Path: path,
|
|
|
|
Err: syscall.EISDIR,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 15:29:06 -05:00
|
|
|
return &LockedFile{File: f}, nil
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2017-06-12 20:40:28 -04:00
|
|
|
// TryLockedOpenFile - tries a new write lock, functionality
|
|
|
|
// it is similar to LockedOpenFile with with syscall.LOCK_EX
|
|
|
|
// mode but along with syscall.LOCK_NB such that the function
|
|
|
|
// doesn't wait forever but instead returns if it cannot
|
|
|
|
// acquire a write lock.
|
|
|
|
func TryLockedOpenFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
|
2018-09-12 12:29:41 -04:00
|
|
|
var lockType uint32 = lockFileFailImmediately | lockFileExclusiveLock
|
|
|
|
switch flag {
|
|
|
|
case syscall.O_RDONLY:
|
|
|
|
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-lockfileex
|
|
|
|
lockType = lockFileFailImmediately | 0 // Set this to enable shared lock and fail immediately.
|
|
|
|
}
|
|
|
|
return lockedOpenFile(path, flag, perm, lockType)
|
2017-06-12 20:40:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LockedOpenFile - initializes a new lock and protects
|
|
|
|
// the file from concurrent access.
|
|
|
|
func LockedOpenFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
|
2018-09-12 12:29:41 -04:00
|
|
|
var lockType uint32 = lockFileExclusiveLock
|
|
|
|
switch flag {
|
|
|
|
case syscall.O_RDONLY:
|
|
|
|
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-lockfileex
|
|
|
|
lockType = 0 // Set this to enable shared lock.
|
|
|
|
}
|
|
|
|
return lockedOpenFile(path, flag, perm, lockType)
|
2017-06-12 20:40:28 -04:00
|
|
|
}
|
|
|
|
|
2017-08-12 22:25:43 -04:00
|
|
|
// fixLongPath returns the extended-length (\\?\-prefixed) form of
|
|
|
|
// path when needed, in order to avoid the default 260 character file
|
|
|
|
// path limit imposed by Windows. If path is not easily converted to
|
|
|
|
// the extended-length form (for example, if path is a relative path
|
|
|
|
// or contains .. elements), or is short enough, fixLongPath returns
|
|
|
|
// path unmodified.
|
|
|
|
//
|
|
|
|
// See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath
|
|
|
|
func fixLongPath(path string) string {
|
|
|
|
// Do nothing (and don't allocate) if the path is "short".
|
|
|
|
// Empirically (at least on the Windows Server 2013 builder),
|
|
|
|
// the kernel is arbitrarily okay with < 248 bytes. That
|
|
|
|
// matches what the docs above say:
|
|
|
|
// "When using an API to create a directory, the specified
|
|
|
|
// path cannot be so long that you cannot append an 8.3 file
|
|
|
|
// name (that is, the directory name cannot exceed MAX_PATH
|
|
|
|
// minus 12)." Since MAX_PATH is 260, 260 - 12 = 248.
|
|
|
|
//
|
|
|
|
// The MSDN docs appear to say that a normal path that is 248 bytes long
|
|
|
|
// will work; empirically the path must be less then 248 bytes long.
|
|
|
|
if len(path) < 248 {
|
|
|
|
// Don't fix. (This is how Go 1.7 and earlier worked,
|
|
|
|
// not automatically generating the \\?\ form)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
// The extended form begins with \\?\, as in
|
|
|
|
// \\?\c:\windows\foo.txt or \\?\UNC\server\share\foo.txt.
|
|
|
|
// The extended form disables evaluation of . and .. path
|
|
|
|
// elements and disables the interpretation of / as equivalent
|
|
|
|
// to \. The conversion here rewrites / to \ and elides
|
|
|
|
// . elements as well as trailing or duplicate separators. For
|
|
|
|
// simplicity it avoids the conversion entirely for relative
|
|
|
|
// paths or paths containing .. elements. For now,
|
|
|
|
// \\server\share paths are not converted to
|
|
|
|
// \\?\UNC\server\share paths because the rules for doing so
|
|
|
|
// are less well-specified.
|
|
|
|
if len(path) >= 2 && path[:2] == `\\` {
|
|
|
|
// Don't canonicalize UNC paths.
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
// Relative path
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
const prefix = `\\?`
|
|
|
|
|
|
|
|
pathbuf := make([]byte, len(prefix)+len(path)+len(`\`))
|
|
|
|
copy(pathbuf, prefix)
|
|
|
|
n := len(path)
|
|
|
|
r, w := 0, len(prefix)
|
|
|
|
for r < n {
|
|
|
|
switch {
|
|
|
|
case os.IsPathSeparator(path[r]):
|
|
|
|
// empty block
|
|
|
|
r++
|
|
|
|
case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
|
|
|
|
// /./
|
|
|
|
r++
|
|
|
|
case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
|
|
|
|
// /../ is currently unhandled
|
|
|
|
return path
|
|
|
|
default:
|
|
|
|
pathbuf[w] = '\\'
|
|
|
|
w++
|
|
|
|
for ; r < n && !os.IsPathSeparator(path[r]); r++ {
|
|
|
|
pathbuf[w] = path[r]
|
|
|
|
w++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// A drive's root directory needs a trailing \
|
|
|
|
if w == len(`\\?\c:`) {
|
|
|
|
pathbuf[w] = '\\'
|
|
|
|
w++
|
|
|
|
}
|
|
|
|
return string(pathbuf[:w])
|
|
|
|
}
|
|
|
|
|
2018-09-12 12:29:41 -04:00
|
|
|
// Open - perm param is ignored, on windows file perms/NT acls
|
2017-01-16 20:05:00 -05:00
|
|
|
// are not octet combinations. Providing access to NT
|
|
|
|
// acls is out of scope here.
|
2018-01-31 16:17:24 -05:00
|
|
|
func Open(path string, flag int, perm os.FileMode) (*os.File, error) {
|
2017-01-16 20:05:00 -05:00
|
|
|
if path == "" {
|
|
|
|
return nil, syscall.ERROR_FILE_NOT_FOUND
|
|
|
|
}
|
|
|
|
|
2017-08-12 22:25:43 -04:00
|
|
|
pathp, err := syscall.UTF16PtrFromString(fixLongPath(path))
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var access uint32
|
|
|
|
switch flag {
|
|
|
|
case syscall.O_RDONLY:
|
|
|
|
access = syscall.GENERIC_READ
|
|
|
|
case syscall.O_WRONLY:
|
|
|
|
access = syscall.GENERIC_WRITE
|
|
|
|
case syscall.O_RDWR:
|
2017-03-27 15:47:39 -04:00
|
|
|
fallthrough
|
2017-01-16 20:05:00 -05:00
|
|
|
case syscall.O_RDWR | syscall.O_CREAT:
|
2017-03-27 15:47:39 -04:00
|
|
|
fallthrough
|
2017-01-16 20:05:00 -05:00
|
|
|
case syscall.O_WRONLY | syscall.O_CREAT:
|
2017-03-27 15:47:39 -04:00
|
|
|
access = syscall.GENERIC_READ | syscall.GENERIC_WRITE
|
2018-01-31 16:17:24 -05:00
|
|
|
case syscall.O_WRONLY | syscall.O_CREAT | syscall.O_APPEND:
|
|
|
|
access = syscall.FILE_APPEND_DATA
|
2017-03-27 15:47:39 -04:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unsupported flag (%d)", flag)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var createflag uint32
|
|
|
|
switch {
|
|
|
|
case flag&syscall.O_CREAT == syscall.O_CREAT:
|
|
|
|
createflag = syscall.OPEN_ALWAYS
|
|
|
|
default:
|
|
|
|
createflag = syscall.OPEN_EXISTING
|
|
|
|
}
|
|
|
|
|
|
|
|
shareflag := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE | syscall.FILE_SHARE_DELETE)
|
|
|
|
accessAttr := uint32(syscall.FILE_ATTRIBUTE_NORMAL | 0x80000000)
|
|
|
|
|
2017-03-27 15:47:39 -04:00
|
|
|
fd, err := syscall.CreateFile(pathp, access, shareflag, nil, createflag, accessAttr, 0)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.NewFile(uintptr(fd), path), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lockFile(fd syscall.Handle, flags uint32) error {
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
|
|
|
|
if fd == syscall.InvalidHandle {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-12 12:29:41 -04:00
|
|
|
err := lockFileEx(fd, flags, 1, 0, &syscall.Overlapped{})
|
2017-01-16 20:05:00 -05:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
2017-06-12 20:40:28 -04:00
|
|
|
} else if err.Error() == "The process cannot access the file because another process has locked a portion of the file." {
|
|
|
|
return ErrAlreadyLocked
|
2017-01-16 20:05:00 -05:00
|
|
|
} else if err != errLockViolation {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lockFileEx(h syscall.Handle, flags, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
|
|
|
|
var reserved = uint32(0)
|
2017-05-02 05:35:27 -04:00
|
|
|
r1, _, e1 := syscall.Syscall6(procLockFileEx.Addr(), 6, uintptr(h), uintptr(flags),
|
|
|
|
uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))
|
2017-01-16 20:05:00 -05:00
|
|
|
if r1 == 0 {
|
|
|
|
if e1 != 0 {
|
|
|
|
err = error(e1)
|
|
|
|
} else {
|
|
|
|
err = syscall.EINVAL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|