github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/vfs/file_lock_windows.go (about)

     1  // Copyright 2013 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  //go:build windows
     6  // +build windows
     7  
     8  package vfs
     9  
    10  import (
    11  	"io"
    12  	"syscall"
    13  )
    14  
    15  // lockCloser hides all of an syscall.Handle's methods, except for Close.
    16  type lockCloser struct {
    17  	fd syscall.Handle
    18  }
    19  
    20  func (l lockCloser) Close() error {
    21  	return syscall.Close(l.fd)
    22  }
    23  
    24  // Lock locks the given file. On Windows, Locking will fail if the file is
    25  // already open by the current process.
    26  func (defaultFS) Lock(name string) (io.Closer, error) {
    27  	p, err := syscall.UTF16PtrFromString(name)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	fd, err := syscall.CreateFile(p,
    32  		syscall.GENERIC_READ|syscall.GENERIC_WRITE,
    33  		0, nil, syscall.CREATE_ALWAYS,
    34  		syscall.FILE_ATTRIBUTE_NORMAL,
    35  		0,
    36  	)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	return lockCloser{fd: fd}, nil
    41  }