github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/lockedfile/internal/filelock/filelock_windows.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build windows
     6  
     7  package filelock
     8  
     9  import (
    10  	"github.com/gagliardetto/golang-go/not-internal/syscall/windows"
    11  	"os"
    12  	"syscall"
    13  )
    14  
    15  type lockType uint32
    16  
    17  const (
    18  	readLock  lockType = 0
    19  	writeLock lockType = windows.LOCKFILE_EXCLUSIVE_LOCK
    20  )
    21  
    22  const (
    23  	reserved = 0
    24  	allBytes = ^uint32(0)
    25  )
    26  
    27  func lock(f File, lt lockType) error {
    28  	// Per https://golang.org/issue/19098, “Programs currently expect the Fd
    29  	// method to return a handle that uses ordinary synchronous I/O.”
    30  	// However, LockFileEx still requires an OVERLAPPED structure,
    31  	// which contains the file offset of the beginning of the lock range.
    32  	// We want to lock the entire file, so we leave the offset as zero.
    33  	ol := new(syscall.Overlapped)
    34  
    35  	err := windows.LockFileEx(syscall.Handle(f.Fd()), uint32(lt), reserved, allBytes, allBytes, ol)
    36  	if err != nil {
    37  		return &os.PathError{
    38  			Op:   lt.String(),
    39  			Path: f.Name(),
    40  			Err:  err,
    41  		}
    42  	}
    43  	return nil
    44  }
    45  
    46  func unlock(f File) error {
    47  	ol := new(syscall.Overlapped)
    48  	err := windows.UnlockFileEx(syscall.Handle(f.Fd()), reserved, allBytes, allBytes, ol)
    49  	if err != nil {
    50  		return &os.PathError{
    51  			Op:   "Unlock",
    52  			Path: f.Name(),
    53  			Err:  err,
    54  		}
    55  	}
    56  	return nil
    57  }
    58  
    59  func isNotSupported(err error) bool {
    60  	switch err {
    61  	case windows.ERROR_NOT_SUPPORTED, windows.ERROR_CALL_NOT_IMPLEMENTED, ErrNotSupported:
    62  		return true
    63  	default:
    64  		return false
    65  	}
    66  }